-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayerStatus.py
444 lines (390 loc) · 18.4 KB
/
playerStatus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# Created by LeviSnoot
# https://github.com/LeviSnoot/FNFest-Status
import os
import time
import psutil
import json
import requests
import sys
import re
# Path to the status file
status_file_path = os.path.join(os.path.dirname(__file__), 'status.json')
# Initial status values
initial_status = {
"current_song": None,
"current_artist": None,
"current_instrument": None,
"current_intensity": None,
"current_difficulty": None,
"current_album_art": None,
"icon_bass": None,
"icon_guitar": None,
"icon_vocals": None,
"song_state": False,
"is_game_running": False,
"is_battle_stage": False,
"in_backstage": False,
"round_number": 1,
"in_song_results": False,
"duration": None,
"playback_start_time": None
}
# Check if the status file exists, if not create it with initial values
if not os.path.exists(status_file_path):
with open(status_file_path, 'w') as f:
json.dump(initial_status, f)
print(f"Created initial status file at {status_file_path}")
sys.stdout.flush()
def write_status_to_file(status):
with open(status_file_path, 'w') as f:
json.dump(status, f)
def read_status_from_file():
with open(status_file_path, 'r') as f:
return json.load(f)
# Path to the log file
log_file_path = os.path.expandvars(r'%localappdata%\FortniteGame\Saved\Logs\FortniteGame.log')
# Validate and sanitize the log file path
if not os.path.isfile(log_file_path):
raise ValueError(f"Invalid log file path: {log_file_path}")
# URL to fetch the API data from
base_api_url = 'https://levisnoot.github.io/FNFest-Content-API/api/track_api/'
album_art_base_url = 'https://levisnoot.github.io/fnfest-artwork-store/album_art/'
# Mapping of known discrepancies between log song IDs and API song IDs
song_id_mapping = {
'astronautintheocean': 'astronoutintheocean',
'theedgeofglory': 'edgeofglory'
}
# Initial state
in_backstage = False
matchmaking_started = False
playing_song = False
current_song = None
current_instrument = None
current_intensity = None
current_difficulty = None
is_battle_stage = False
in_lobby = False
in_sleep_mode = False
game_running_state = False
round_counter = 1
in_song_results = False
def reset_state():
global in_backstage, matchmaking_started, playing_song, current_song, current_instrument, current_intensity, current_difficulty, is_battle_stage, in_lobby, in_sleep_mode, game_running_state, in_song_results, round_counter
in_backstage = False
matchmaking_started = False
playing_song = False
is_battle_stage = False
in_lobby = False
in_sleep_mode = False
game_running_state = False
in_song_results = False
round_counter = 1
print("State has been reset.")
sys.stdout.flush()
# Update the status file to reflect the reset state
status = read_status_from_file()
status.update({
"song_state": False,
"is_game_running": False,
"is_battle_stage": False,
"in_backstage": False,
"in_song_results": False,
"round_number": round_counter,
"duration": None,
"playback_start_time": None
})
write_status_to_file(status)
print(f"Updated status: {json.dumps(status, indent=4)}")
sys.stdout.flush()
def update_state(new_state):
global in_backstage, round_counter, playing_song
if in_backstage != new_state:
in_backstage = new_state
status = read_status_from_file()
status["in_backstage"] = in_backstage
if in_backstage:
round_counter = 1
status["round_number"] = round_counter
print(f"Player is now in the backstage area.")
if playing_song:
end_song_state()
status["song_state"] = False # Ensure song_state is set to False
else:
print(f"Player is no longer in the backstage area.")
write_status_to_file(status)
sys.stdout.flush()
def update_song_state(song, instrument, intensity, difficulty, artist, album_art, icon_bass, icon_guitar, icon_vocals, duration, playback_start_time):
global playing_song, current_song, current_instrument, current_intensity, current_difficulty
playing_song = True
current_song = song
current_instrument = instrument
current_intensity = intensity
current_difficulty = difficulty
# Write to the status file with all properties
status = read_status_from_file()
status.update({
"current_song": song,
"current_artist": artist,
"current_instrument": instrument,
"current_intensity": intensity,
"current_difficulty": difficulty,
"current_album_art": album_art,
"icon_bass": icon_bass,
"icon_guitar": icon_guitar,
"icon_vocals": icon_vocals,
"round_number": round_counter,
"song_state": True,
"duration": duration,
"playback_start_time": playback_start_time
})
write_status_to_file(status)
print(f"Updated status: {json.dumps(status, indent=4)}")
sys.stdout.flush()
print(f"Playing song: {current_song} by {artist}, Instrument: {instrument}, Intensity: {intensity}, Difficulty: {difficulty}")
sys.stdout.flush()
print(f"Album Art URL: {album_art}")
sys.stdout.flush()
def update_playback_start_time(playback_start_time):
# Update only the playback_start_time in the status file
status = read_status_from_file()
status["playback_start_time"] = playback_start_time
write_status_to_file(status)
print(f"Updated playback start time: {playback_start_time}")
sys.stdout.flush()
def end_song_state():
global playing_song, round_counter
playing_song = False
round_counter += 1
status = read_status_from_file()
status["song_state"] = False
status["round_number"] = round_counter
write_status_to_file(status)
print(f"Updated status: {json.dumps(status, indent=4)}")
sys.stdout.flush()
print(f"Finished playing song: {current_song}")
sys.stdout.flush()
def is_game_running():
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'] == 'FortniteClient-Win64-Shipping.exe':
return True
return False
def get_song_info(song_id):
# Correct the song ID if it exists in the mapping
corrected_song_id = song_id_mapping.get(song_id, song_id)
url = f"{base_api_url}{corrected_song_id}.json"
response = requests.get(url)
if response.status_code == 200:
song_info = response.json()
print(f"Fetched song info: {json.dumps(song_info, indent=4)}")
track_info = song_info.get('track', {})
if 'albumArtFilename' in track_info:
track_info['albumArtFilename'] = f"{album_art_base_url}{track_info['albumArtFilename']}"
return track_info
else:
print(f"Song ID '{corrected_song_id}' not found in API.")
sys.stdout.flush()
return None
def format_instrument_name(instrument):
instrument_mapping = {
'bass': 'Bass',
'drum': 'Drums',
'guitar': 'Lead',
'vocals': 'Vocals',
'plasticguitar': 'Pro Lead',
'plasticbass': 'Pro Bass'
}
return instrument_mapping.get(instrument, instrument.capitalize())
def format_difficulty_name(difficulty):
difficulty_mapping = {
'DifficultyEasy': 'Easy',
'DifficultyMedium': 'Medium',
'DifficultyHard': 'Hard',
'DifficultyExpert': 'Expert'
}
return difficulty_mapping.get(difficulty, difficulty)
def monitor_log_file():
global matchmaking_started, is_battle_stage, game_running, current_song, current_artist, current_album_art, in_lobby, in_sleep_mode, game_running_state, in_song_results
game_running = False
# Regex to match playback start log lines
playback_start_regex = re.compile(r'LogElectraPlayer: \[.*\] Playback started at play position (\d+\.\d+)')
while True:
if is_game_running():
if not game_running:
print("Fortnite has started.")
sys.stdout.flush()
game_running = True
status = read_status_from_file()
status["is_game_running"] = True
write_status_to_file(status)
with open(log_file_path, 'r', encoding='utf-8-sig') as log_file:
log_file.seek(0, os.SEEK_END) # Move to the end of the file
while is_game_running():
line = log_file.readline()
if not line:
time.sleep(0.1) # Sleep briefly to avoid busy-waiting
continue
# Skip checks if in lobby
if in_lobby:
if 'LogAthenaMatchmakingWidget: UFortAthenaMatchmakingWidgetLegacy::RequestMatchmakingStart' in line:
matchmaking_started = True
in_lobby = False
print("Matchmaking started.")
sys.stdout.flush()
continue
# Check for Matchmaking start
if 'LogAthenaMatchmakingWidget: UFortAthenaMatchmakingWidgetLegacy::RequestMatchmakingStart' in line:
matchmaking_started = True
print("Matchmaking started.")
sys.stdout.flush()
# Check for Battle Stage mode
if 'Playlist_PilgrimBattleStage' in line:
is_battle_stage = True
status = read_status_from_file()
status["is_battle_stage"] = True
write_status_to_file(status)
print("Battle Stage mode detected.")
sys.stdout.flush()
# Check for Main Stage mode
if 'Playlist_PilgrimQuickplay' in line:
is_battle_stage = False
status = read_status_from_file()
status["is_battle_stage"] = False
write_status_to_file(status)
print("Main Stage mode detected.")
sys.stdout.flush()
# Check for entering Pregame state (backstage)
if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Entering Pilgrim Quickplay state EPilgrimQuickplayState::Pregame' in line:
update_state(True)
# Check for leaving Pregame state (no longer backstage)
if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Leaving Pilgrim Quickplay state EPilgrimQuickplayState::Pregame' in line:
update_state(False)
# Check for loading into a game
if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Entering Pilgrim Quickplay state EPilgrimQuickplayState::Loading' in line:
print("Player is loading into a game.")
sys.stdout.flush()
# Check for Return to Main Menu
if 'LogOnlineGame: FortPC::ReturnToMainMenu' in line:
if playing_song:
end_song_state() # Ensure the song state is ended
reset_state()
in_lobby = True
print("Player returned to main menu.")
sys.stdout.flush()
# Check for Song Start
if 'LogPilgrimMusicBattle: Client -1 received song to play:' in line:
print("Song gameplay started.")
sys.stdout.flush()
# Extract song ID from the log line and convert to lowercase
song_id = line.split('received song to play: ')[1].split(' - ')[0].strip().lower()
song_info = get_song_info(song_id)
if song_info:
print(f"Song info keys: {list(song_info.keys())}")
current_song = song_info['trackTitle']
current_artist = song_info['artistName']
current_album_art = song_info['albumArtFilename']
icon_bass = song_info.get('iconBass')
icon_guitar = song_info.get('iconGuitar')
icon_vocals = song_info.get('iconVocals')
duration = song_info.get('duration')
else:
current_song = None
# Check for "Playback reached end at play position" to determine whether player finished the song
if 'Playback reached end at play position' in line:
print("Song finished playing.")
end_song_state()
# Check for Song End
#if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Leaving Pilgrim Quickplay state EPilgrimQuickplayState::SongGameplay' in line:
# end_song_state()
# Check for entering Song Results state (post-game screen)
if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Entering Pilgrim Quickplay state EPilgrimQuickplayState::SongResults' in line:
in_song_results = True
status = read_status_from_file()
status["in_song_results"] = True
write_status_to_file(status)
print("Entering Song Results state.")
sys.stdout.flush()
# Check for leaving Song Results state (post-game screen)
if 'LogPilgrimQuickplayStateMachine: Display: (Client -1)Leaving Pilgrim Quickplay state EPilgrimQuickplayState::SongResults' in line:
in_song_results = False
status = read_status_from_file()
status["in_song_results"] = False
write_status_to_file(status)
print("Leaving Song Results state.")
sys.stdout.flush()
# Check for Instrument and Difficulty
if 'LogPilgrimGemBreakListener: UPilgrimGemBreakListener::Init:' in line:
parts = line.split('using track ')[1].split(' and Difficulty ')
instrument = parts[0].split('::')[-1].replace('Track', '').lower()
difficulty = parts[1].split('::')[-1].replace('Difficulty', '').strip() # Strip whitespace
# Map the instrument to the correct key in the intensities dictionary
intensity_mapping = {
'bass': 'bass',
'drum': 'drums',
'guitar': 'guitar',
'vocals': 'vocals',
'plasticguitar': 'proGuitar',
'plasticbass': 'proBass'
}
instrument_key = intensity_mapping.get(instrument, instrument) # Use the mapped key or the instrument name directly
# Fetch song info if not already fetched
if not current_song:
song_info = get_song_info(current_song)
if song_info and instrument_key in song_info['intensities']:
intensity_value = song_info['intensities'][instrument_key]
formatted_instrument = format_instrument_name(instrument)
formatted_difficulty = format_difficulty_name(difficulty)
update_song_state(current_song, formatted_instrument, intensity_value, formatted_difficulty, current_artist, current_album_art, icon_bass, icon_guitar, icon_vocals, duration, None)
else:
print(f"Instrument key '{instrument_key}' not found in song info.")
sys.stdout.flush()
# Check for Matchmaking cancellation
if 'LogMatchmakingServiceClient: HandleError - Type: \'Canceled\'' in line:
matchmaking_started = False
print("Matchmaking canceled.")
sys.stdout.flush()
# Check for entering Sleep Mode
if 'LogFortPlaytimeManager: UFortPlaytimeManager::SetPlaytimeState - Changing Playtime State from Unblocked to Sleep' in line:
in_sleep_mode = True
print("Player entered Sleep Mode.")
sys.stdout.flush()
# Check for leaving Sleep Mode
if 'LogFortPlaytimeManager: UFortPlaytimeManager::SetPlaytimeState - Changing Playtime State from Sleep to Unblocked' in line:
in_sleep_mode = False
print("Player left Sleep Mode.")
sys.stdout.flush()
# Check for Playback Start
match = playback_start_regex.search(line)
if match and playing_song:
playback_start_time = time.time()
playback_position = match.group(1)
print(f"Playback started at position: {playback_position}")
sys.stdout.flush()
update_playback_start_time(playback_start_time)
print("Fortnite has stopped.")
sys.stdout.flush()
reset_state()
status = read_status_from_file()
status["is_game_running"] = False
write_status_to_file(status)
game_running = False
else:
if game_running:
print("Fortnite has stopped.")
sys.stdout.flush()
reset_state()
status = read_status_from_file()
status["is_game_running"] = False
write_status_to_file(status)
game_running = False
print("Waiting for Fortnite to start...")
sys.stdout.flush()
time.sleep(5)
if __name__ == "__main__":
try:
monitor_log_file()
except KeyboardInterrupt:
if playing_song:
end_song_state() # Ensure the song state is ended
reset_state()
print("Exiting...")
sys.stdout.flush()