Added regex function to format songname

This commit is contained in:
Jamon 2024-08-12 09:09:39 +12:00
parent f15c03b7cd
commit 3d7bd274c2
2 changed files with 34 additions and 4 deletions

View File

@ -950,6 +950,7 @@ def on_request_music_sync():
if request.sid in game_state['players'] and music_player.is_playing:
emit('music_sync', {
'song': music_player.get_current_song(),
'songName': music_player.get_formatted_current_song(),
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
@ -1080,6 +1081,7 @@ def on_join(data):
if music_player.is_playing:
emit('music_sync', {
'song': music_player.get_current_song(),
'songName': music_player.get_formatted_current_song(),
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
@ -1479,6 +1481,7 @@ def game_loop():
socketio.emit('music_control', {
'action': 'change',
'song': f"/{music_player.get_current_song()}",
'songName': music_player.get_formatted_current_song(),
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
@ -1550,6 +1553,7 @@ def start_music():
socketio.emit('music_control', {
'action': 'start',
'song': music_player.get_current_song(),
'songName': music_player.get_formatted_current_song(),
'startTime': music_player.start_time,
'serverTime': time.time(),
'songDuration': music_player.get_current_song_duration()
@ -1584,6 +1588,27 @@ class AudioStreamer:
# Initialize the audio streamer
def format_song_name(filename):
# Remove the .mp3 extension
name = os.path.splitext(filename)[0]
# Add space before and after "-"
name = name.replace("-", " - ")
# Split the name into parts (artist and title)
parts = name.split(" - ")
# Function to add spaces before capital letters, except the first one
def add_spaces(text):
return re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', text)
# Apply the spacing to each part
formatted_parts = [add_spaces(part) for part in parts]
# Join the parts back together
return " - ".join(formatted_parts)
class MusicPlayer:
def __init__(self, playlist):
self.playlist = playlist
@ -1616,6 +1641,10 @@ class MusicPlayer:
def get_current_song(self):
# Return just the filename, not the full path
return os.path.basename(self.playlist[self.current_song_index])
def get_formatted_current_song(self):
return format_song_name(self.get_current_song())
def get_current_song_duration(self):
return self.song_durations[self.get_current_song()]
@ -1635,7 +1664,7 @@ class MusicPlayer:
new_song = self.get_current_song()
# Send Discord alert for automatic song change
send_discord_alert(f"🎵 Now playing: {new_song}")
send_discord_alert(f"🎵 Now playing: {self.get_formatted_current_song}")
return True # Indicates that the song has changed
return False
@ -1650,11 +1679,12 @@ class MusicPlayer:
socketio.emit('music_control', {
'action': 'change',
'song': new_song,
'songName': format_song_name(new_song),
'startTime': self.start_time,
'serverTime': time.time(),
'songDuration': self.get_current_song_duration()
}, room=main_room)
send_discord_alert(f"🎵 Now playing: {new_song}")
send_discord_alert(f"🎵 Now playing: {format_song_name(new_song)}")
return new_song

View File

@ -2247,7 +2247,7 @@ function startGame() {
const songPosition = (Date.now() / 1000 - data.startTime - serverClientTimeDiff) % currentSongDuration;
setupAudio(data.song, songPosition);
currentSongName = data.song.split('/').pop();
currentSongName = data.songName;
updateSongDurationBar();
}
@ -2549,7 +2549,7 @@ socket.on('bullet_impact', (data) => {
const songPosition = 0; // Start from the beginning for 'change' action
setupAudio(data.song, songPosition);
currentSongName = data.song.split('/').pop();
currentSongName = data.songName;
updateSongDurationBar();
break;
case 'stop':