Added regex function to format songname
This commit is contained in:
parent
f15c03b7cd
commit
3d7bd274c2
@ -950,6 +950,7 @@ def on_request_music_sync():
|
|||||||
if request.sid in game_state['players'] and music_player.is_playing:
|
if request.sid in game_state['players'] and music_player.is_playing:
|
||||||
emit('music_sync', {
|
emit('music_sync', {
|
||||||
'song': music_player.get_current_song(),
|
'song': music_player.get_current_song(),
|
||||||
|
'songName': music_player.get_formatted_current_song(),
|
||||||
'startTime': music_player.start_time,
|
'startTime': music_player.start_time,
|
||||||
'serverTime': time.time(),
|
'serverTime': time.time(),
|
||||||
'songDuration': music_player.get_current_song_duration()
|
'songDuration': music_player.get_current_song_duration()
|
||||||
@ -1080,6 +1081,7 @@ def on_join(data):
|
|||||||
if music_player.is_playing:
|
if music_player.is_playing:
|
||||||
emit('music_sync', {
|
emit('music_sync', {
|
||||||
'song': music_player.get_current_song(),
|
'song': music_player.get_current_song(),
|
||||||
|
'songName': music_player.get_formatted_current_song(),
|
||||||
'startTime': music_player.start_time,
|
'startTime': music_player.start_time,
|
||||||
'serverTime': time.time(),
|
'serverTime': time.time(),
|
||||||
'songDuration': music_player.get_current_song_duration()
|
'songDuration': music_player.get_current_song_duration()
|
||||||
@ -1479,6 +1481,7 @@ def game_loop():
|
|||||||
socketio.emit('music_control', {
|
socketio.emit('music_control', {
|
||||||
'action': 'change',
|
'action': 'change',
|
||||||
'song': f"/{music_player.get_current_song()}",
|
'song': f"/{music_player.get_current_song()}",
|
||||||
|
'songName': music_player.get_formatted_current_song(),
|
||||||
'startTime': music_player.start_time,
|
'startTime': music_player.start_time,
|
||||||
'serverTime': time.time(),
|
'serverTime': time.time(),
|
||||||
'songDuration': music_player.get_current_song_duration()
|
'songDuration': music_player.get_current_song_duration()
|
||||||
@ -1550,6 +1553,7 @@ def start_music():
|
|||||||
socketio.emit('music_control', {
|
socketio.emit('music_control', {
|
||||||
'action': 'start',
|
'action': 'start',
|
||||||
'song': music_player.get_current_song(),
|
'song': music_player.get_current_song(),
|
||||||
|
'songName': music_player.get_formatted_current_song(),
|
||||||
'startTime': music_player.start_time,
|
'startTime': music_player.start_time,
|
||||||
'serverTime': time.time(),
|
'serverTime': time.time(),
|
||||||
'songDuration': music_player.get_current_song_duration()
|
'songDuration': music_player.get_current_song_duration()
|
||||||
@ -1584,6 +1588,27 @@ class AudioStreamer:
|
|||||||
|
|
||||||
# Initialize the audio streamer
|
# 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:
|
class MusicPlayer:
|
||||||
def __init__(self, playlist):
|
def __init__(self, playlist):
|
||||||
self.playlist = playlist
|
self.playlist = playlist
|
||||||
@ -1616,6 +1641,10 @@ class MusicPlayer:
|
|||||||
def get_current_song(self):
|
def get_current_song(self):
|
||||||
# Return just the filename, not the full path
|
# Return just the filename, not the full path
|
||||||
return os.path.basename(self.playlist[self.current_song_index])
|
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):
|
def get_current_song_duration(self):
|
||||||
return self.song_durations[self.get_current_song()]
|
return self.song_durations[self.get_current_song()]
|
||||||
@ -1635,7 +1664,7 @@ class MusicPlayer:
|
|||||||
new_song = self.get_current_song()
|
new_song = self.get_current_song()
|
||||||
|
|
||||||
# Send Discord alert for automatic song change
|
# 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 True # Indicates that the song has changed
|
||||||
return False
|
return False
|
||||||
@ -1650,11 +1679,12 @@ class MusicPlayer:
|
|||||||
socketio.emit('music_control', {
|
socketio.emit('music_control', {
|
||||||
'action': 'change',
|
'action': 'change',
|
||||||
'song': new_song,
|
'song': new_song,
|
||||||
|
'songName': format_song_name(new_song),
|
||||||
'startTime': self.start_time,
|
'startTime': self.start_time,
|
||||||
'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}")
|
send_discord_alert(f"🎵 Now playing: {format_song_name(new_song)}")
|
||||||
|
|
||||||
return new_song
|
return new_song
|
||||||
|
|
||||||
|
|||||||
@ -2247,7 +2247,7 @@ function startGame() {
|
|||||||
const songPosition = (Date.now() / 1000 - data.startTime - serverClientTimeDiff) % currentSongDuration;
|
const songPosition = (Date.now() / 1000 - data.startTime - serverClientTimeDiff) % currentSongDuration;
|
||||||
setupAudio(data.song, songPosition);
|
setupAudio(data.song, songPosition);
|
||||||
|
|
||||||
currentSongName = data.song.split('/').pop();
|
currentSongName = data.songName;
|
||||||
updateSongDurationBar();
|
updateSongDurationBar();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2549,7 +2549,7 @@ socket.on('bullet_impact', (data) => {
|
|||||||
const songPosition = 0; // Start from the beginning for 'change' action
|
const songPosition = 0; // Start from the beginning for 'change' action
|
||||||
setupAudio(data.song, songPosition);
|
setupAudio(data.song, songPosition);
|
||||||
|
|
||||||
currentSongName = data.song.split('/').pop();
|
currentSongName = data.songName;
|
||||||
updateSongDurationBar();
|
updateSongDurationBar();
|
||||||
break;
|
break;
|
||||||
case 'stop':
|
case 'stop':
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user