Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
BoQsc committed Aug 2, 2024
1 parent 26e80a0 commit 9243469
Show file tree
Hide file tree
Showing 37 changed files with 1,373 additions and 55 deletions.
15 changes: 12 additions & 3 deletions webserver/Webserver-testing/Webserver/player_status.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"online_count": 0,
"players": {},
"server_status": "Shutdown"
"online_count": 2,
"players": {
"Hr3born[LV]": {
"status": "Online,Spawned",
"pltfm_id": "Steam_76561197995381320"
},
"boqsc": {
"status": "Online,Joining",
"pltfm_id": "Steam_76561198072601792"
}
},
"server_status": "Online"
}
98 changes: 49 additions & 49 deletions webserver/Webserver-testing/Webserver/player_status_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

def get_latest_log_file():
script_dir = os.path.dirname(os.path.abspath(__file__))
log_dir = os.path.abspath(os.path.join(script_dir, '..','7DaysToDie_Data'))
log_dir = os.path.abspath(os.path.join(script_dir, '..', '7DaysToDie_Data'))
log_files = [f for f in os.listdir(log_dir) if f.startswith("output_log__") and f.endswith(".txt")]

if not log_files:
raise FileNotFoundError(f"No log files found in the directory: {log_dir}")
latest_file = os.path.join(log_dir, max(log_files, key=lambda x: os.path.getmtime(os.path.join(log_dir, x))))

# Sort log files based on the timestamp in the filename
sorted_log_files = sorted(log_files, key=lambda x: x.split('__')[1], reverse=True)
latest_file = os.path.join(log_dir, sorted_log_files[0])

print(f"Debug: Latest log file found: {latest_file}")
return latest_file

Expand All @@ -20,49 +25,31 @@ def parse_log_line(line):
pltfm_id_match = re.search(r"PltfmId='(\w+_\d+)'", line)
pltfm_id = pltfm_id_match.group(1) if pltfm_id_match else None

login_match = re.search(r"PlayerLogin: (\w+)/V", line)
if login_match:
player = login_match.group(1)
print(f"Debug: Player '{player}' is logging in")
return player, "Online,Joining", pltfm_id

spawn_match = re.search(r"PlayerSpawnedInWorld.*PlayerName='(\w+)'", line)
if spawn_match:
player = spawn_match.group(1)
print(f"Debug: Player '{player}' has spawned in the world")
return player, "Online,Spawned", pltfm_id

join_match = re.search(r"GMSG: Player '(\w+)' joined the game", line)
if join_match:
player = join_match.group(1)
print(f"Debug: Player '{player}' has fully joined the game")
return player, "Online,Playing", pltfm_id

leave_match = re.search(r"GMSG: Player '(\w+)' left the game", line)
if leave_match:
player = leave_match.group(1)
print(f"Debug: Player '{player}' left the game")
return player, "Offline", pltfm_id

disconnect_match = re.search(r"Player disconnected: EntityID=.*PlayerName='(\w+)'", line)
if disconnect_match:
player = disconnect_match.group(1)
print(f"Debug: Player '{player}' disconnected")
return player, "Offline", pltfm_id

auth_match = re.search(r"\[Auth\].*PlayerName='(\w+)'", line)
if auth_match:
player = auth_match.group(1)
print(f"Debug: Player '{player}' authentication info found")
return player, None, pltfm_id

shutdown_match = re.search(r"Shutdown game from", line)
if shutdown_match:
# Patterns for player detection
patterns = [
(r"PlayerLogin: (.+?)/V", "Online,Joining"),
(r"PlayerSpawnedInWorld.*PlayerName='(.+?)'", "Online,Spawned"),
(r"GMSG: Player '(.+?)' joined the game", "Online,Playing"),
(r"GMSG: Player '(.+?)' left the game", "Offline"),
(r"Player disconnected: EntityID=.*PlayerName='(.+?)'", "Offline"),
(r"\[Auth\].*PlayerName='(.+?)'", None),
(r"INF.*PlayerName='(.+?)'", None), # New pattern to catch more player mentions
]

for pattern, status in patterns:
match = re.search(pattern, line)
if match:
player = match.group(1)
print(f"Debug: Player '{player}' detected with status: {status}")
return player, status, pltfm_id

if "Shutdown game from" in line:
print("Debug: Server is shutting down")
return "SERVER", "Shutdown", None

return None, None, pltfm_id


def generate_json(player_status, force_create=False):
online_players = {player: info for player, info in player_status.items()
if info["status"].startswith("Online") and player != "SERVER"}
Expand All @@ -79,15 +66,13 @@ def generate_json(player_status, force_create=False):

if force_create or not os.path.exists(json_file):
print(f"Debug: Creating new player_status.json file")
with open(json_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
print(f"Debug: New JSON file created: {json_file}")
else:
with open(json_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
print(f"Debug: JSON file updated: {json_file}")

print(f"Debug: Current player status: {player_status}")
print(f"Debug: Updating player_status.json file")

with open(json_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)

print(f"Debug: Current player status: {dict(player_status)}")
print(f"Debug: Online count: {online_count}")
print(f"Debug: JSON data: {json.dumps(data, indent=2)}")

Expand Down Expand Up @@ -119,6 +104,12 @@ def initialize_player_status(log_file):
print(f"Debug: Initial player status: {dict(player_status)}")
return player_status

def print_full_status(player_status):
print("\nFull Player Status Report:")
for player, info in player_status.items():
print(f" {player}: {info['status']} (PltfmId: {info['pltfm_id']})")
print()

def main():
try:
current_log_file = get_latest_log_file()
Expand All @@ -136,8 +127,14 @@ def main():

print("Debug: Starting to monitor log file in real-time...")
last_check_time = time.time()
line_count = 0

for line in follow(current_log_file):
line_count += 1
if line_count % 1000 == 0:
print(f"Debug: Processed {line_count} lines")
print_full_status(player_status)

# Check for new log file every 60 seconds
if time.time() - last_check_time > 60:
try:
Expand All @@ -157,6 +154,9 @@ def main():
player, status, pltfm_id = parse_log_line(line)
if player:
update_needed = False
if player not in player_status:
print(f"Debug: New player '{player}' added to tracking")
update_needed = True
if status:
old_status = player_status[player]["status"]
player_status[player]["status"] = status
Expand Down Expand Up @@ -191,4 +191,4 @@ def main():
if __name__ == "__main__":
print("Debug: Script started.")
main()
print("Debug: Script ended.")
print("Debug: Script ended.")
6 changes: 3 additions & 3 deletions webserver/Webserver-testing/Webserver/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ def get_self_paths_json(handler_class):
if __name__ == '__main__':
list_all_self_paths()

#from threading import Thread
#import player_status_monitor
#Thread(target=player_status_monitor.main, daemon=True).start()
from threading import Thread
import player_status_monitor
Thread(target=player_status_monitor.main, daemon=True).start()

#from threading import Thread
#import Webserver_UPNP_Portforwarding
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Webserver_FindNextHordeNightTime_inside_logs_folder.py

import os
import glob
from datetime import datetime
import re

def find_last_bloodmoon_setday():
# Get the directory of the current script
script_dir = os.path.dirname(os.path.abspath(__file__))

# Directory containing the log files relative to the script's directory
log_directory = os.path.join(script_dir, '../7DaysToDie_Data')

# Temporary | Optional | Remove this later
# Override log directory with default 7DTD Path on Windows.
#log_directory = "C:/Program Files (x86)/Steam/steamapps/common/7 Days To Die/7DaysToDie_Data"

# Define the pattern to match files
file_pattern = 'output_log__*.txt'

# Get a list of all files matching the pattern
log_files = glob.glob(os.path.join(log_directory, file_pattern))

# Function to extract timestamp from filename
def extract_timestamp(filename):
timestamp_str = filename.split('__')[1]
return datetime.strptime(filename.split('__')[1]+ "__" +filename.split('__')[2], '%Y-%m-%d__%H-%M-%S.txt')

# Function to extract day number from line
def extract_day_number(line):
match = re.search(r'day (\d+)', line)
if match:
return int(match.group(1))
return None

# Sort the files by timestamp (newest first)
log_files.sort(key=lambda x: extract_timestamp(x), reverse=True)

if log_files:
latest_file = log_files[0]
print("Latest file:", latest_file)

# Open the latest log file
with open(latest_file, 'r') as file:
# Read the lines of the file in reverse order
lines = reversed(file.readlines())
# Search for the last occurrence of the line containing the information
for line in lines:
if "BloodMoon SetDay" in line:
day_number = extract_day_number(line)
if day_number:
return day_number
else:
print("Day number not found in the line.")
break
else:
print("No log files found.")
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Webserver_FindNextHordeNightTime_inside_logs_folder
Webserver_FindNextHordeNightTime_inside_logs_folder.find_last_bloodmoon_setday()
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import os
import glob
from datetime import datetime
import re

def get_latest_game_server_log_file_name():
script_dir = os.path.dirname(os.path.abspath(__file__))
log_directory = os.path.join(script_dir, '../7DaysToDie_Data')

# Temporary | Optional | Remove this later
# Override log directory with default 7DTD Path on Windows.
#log_directory = "C:/Program Files (x86)/Steam/steamapps/common/7 Days To Die/7DaysToDie_Data"

file_pattern = 'output_log__*.txt'
log_files = glob.glob(os.path.join(log_directory, file_pattern))

# Function to extract timestamp from filename
def extract_timestamp(filename):
timestamp_str = filename.split('__')[1]
return datetime.strptime(filename.split('__')[1]+ "__" +filename.split('__')[2], '%Y-%m-%d__%H-%M-%S.txt')
log_files.sort(key=lambda x: extract_timestamp(x), reverse=True)
if log_files:
latest_file = log_files[0]
print("Latest file:", latest_file)
return latest_file


Loading

0 comments on commit 9243469

Please sign in to comment.