Compare commits

...

2 Commits

Author SHA1 Message Date
659c2dbaaa Fix for discord alerts on song changes 2024-08-11 17:43:00 +12:00
5cf6f8766e Small refactoring for backend 2024-08-11 17:31:51 +12:00

View File

@ -1448,8 +1448,6 @@ def game_loop():
game_state['enemies'].extend(generate_enemies(1)) game_state['enemies'].extend(generate_enemies(1))
break break
# Update player shooting
# Update player shooting
for player_id, player in game_state['players'].items(): for player_id, player in game_state['players'].items():
if not player.is_paused: if not player.is_paused:
update_synth_coins(game_state['players']) update_synth_coins(game_state['players'])
@ -1473,7 +1471,7 @@ def game_loop():
if game_state['is_playing']: if game_state['is_playing']:
song_duration = len(AudioSegment.from_mp3(game_state['playlist'][game_state['current_song_index']])) song_duration = len(AudioSegment.from_mp3(game_state['playlist'][game_state['current_song_index']]))
if current_time - game_state['music_start_time'] > song_duration / 1000: if current_time - game_state['music_start_time'] > song_duration / 1000:
change_song() music_player.next_song()
if music_player.is_playing: if music_player.is_playing:
if music_player.update(): if music_player.update():
@ -1493,6 +1491,7 @@ def game_loop():
socketio.emit('game_update', serialized_state, room=main_room) socketio.emit('game_update', serialized_state, room=main_room)
socketio.sleep(0.033) # Update approximately 30 times per second socketio.sleep(0.033) # Update approximately 30 times per second
except Exception as e: except Exception as e:
logging.error(f"Error in game loop: {str(e)}") logging.error(f"Error in game loop: {str(e)}")
break break
@ -1560,17 +1559,6 @@ def stop_music():
music_player.stop() music_player.stop()
socketio.emit('music_stop', room=main_room) socketio.emit('music_stop', room=main_room)
def change_song():
new_song = music_player.next_song()
socketio.emit('music_control', {
'action': 'change',
'song': new_song,
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
}, room=main_room)
class AudioStreamer: class AudioStreamer:
def __init__(self, music_player): def __init__(self, music_player):
self.music_player = music_player self.music_player = music_player
@ -1619,7 +1607,6 @@ class MusicPlayer:
self.is_playing = True self.is_playing = True
self.start_time = time.time() self.start_time = time.time()
self.load_current_song() self.load_current_song()
send_discord_alert(f"🎵 Now playing: {self.get_current_song()}")
def stop(self): def stop(self):
@ -1645,6 +1632,11 @@ class MusicPlayer:
self.current_song_index = (self.current_song_index + 1) % len(self.playlist) self.current_song_index = (self.current_song_index + 1) % len(self.playlist)
self.start_time = current_time self.start_time = current_time
self.load_current_song() self.load_current_song()
new_song = self.get_current_song()
# Send Discord alert for automatic song change
send_discord_alert(f"🎵 Now playing: {new_song}")
return True # Indicates that the song has changed return True # Indicates that the song has changed
return False return False
@ -1653,7 +1645,6 @@ class MusicPlayer:
self.start_time = time.time() self.start_time = time.time()
self.load_current_song() self.load_current_song()
new_song = self.get_current_song() new_song = self.get_current_song()
send_discord_alert(f"🎵 Now playing: {new_song}")
# Emit updated music sync information # Emit updated music sync information
socketio.emit('music_control', { socketio.emit('music_control', {
@ -1663,7 +1654,8 @@ class MusicPlayer:
'serverTime': time.time(), 'serverTime': time.time(),
'songDuration': self.get_current_song_duration() 'songDuration': self.get_current_song_duration()
}, room=main_room) }, room=main_room)
send_discord_alert(f"🎵 Now playing: {new_song}")
return new_song return new_song
@ -1672,7 +1664,6 @@ class MusicPlayer:
self.current_song_index = (self.current_song_index + 1) % len(self.playlist) self.current_song_index = (self.current_song_index + 1) % len(self.playlist)
self.start_time = time.time() self.start_time = time.time()
self.load_current_song() self.load_current_song()
send_discord_alert(f"🎵 Now playing: {self.get_current_song()}")
def reload_playlist(self): def reload_playlist(self):
new_playlist = load_music_playlist() new_playlist = load_music_playlist()
@ -1858,13 +1849,6 @@ class AdminConsole(cmd.Cmd):
"""Stop the music""" """Stop the music"""
stop_music() stop_music()
def do_change_song(self, arg):
"""Change the current song"""
if not arg:
print("Error: Song filename is required")
else:
change_song(arg)
def do_quit(self, arg): def do_quit(self, arg):
"""Exit the admin console""" """Exit the admin console"""
print("Exiting admin console") print("Exiting admin console")
@ -1872,15 +1856,7 @@ class AdminConsole(cmd.Cmd):
def do_next_song(self, arg): def do_next_song(self, arg):
"""Change to the next song in the playlist""" """Change to the next song in the playlist"""
new_song = music_player.next_song() music_player.next_song()
print(f"Changed to next song: {new_song}")
socketio.emit('music_control', {
'action': 'change',
'song': new_song,
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
}, room=main_room)
def do_send_alert(self, arg): def do_send_alert(self, arg):
"""Send an alert to all players: send_alert <message>""" """Send an alert to all players: send_alert <message>"""
@ -1896,19 +1872,7 @@ class AdminConsole(cmd.Cmd):
"""Reload the music playlist""" """Reload the music playlist"""
song_count = music_player.reload_playlist() song_count = music_player.reload_playlist()
print(f"Playlist reloaded. {song_count} songs available.") print(f"Playlist reloaded. {song_count} songs available.")
music_player.next_song()
# Optionally, you can change to the next song after reloading
new_song = music_player.next_song()
print(f"Changed to next song: {new_song}")
socketio.emit('music_control', {
'action': 'change',
'song': new_song,
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
}, room=main_room)
def admin_cli(): def admin_cli():
try: try: