Add offline mode: play from a music folder instead of the server
Settings has a Music folder row. A folder with subfolders is played as soundtracks: Next picks a random subfolder and a random song in it, the subfolder is the answer's soundtrack and the file name without its type the song. The inspiration list and Search show the subfolders. A folder with songs and no subfolders is a prepared quiz, played once in order (natural sort) or at random, showing only the song title. It has no winning score: after the last song Next becomes Show winner, and the players with the most points win, ties included. While a folder is used nothing but the characters is asked from the server: Sync, QR, Statistics, the update check and play counting are off. Characters fall back to the cached pictures when the server can't be reached. mp3, ogg and wav play; other files and broken ones are skipped. The old local playback code, the hidden Open button and the hidden Local toggle are removed. Next now waits for the song to start before preparing the next one.
This commit is contained in:
1 parent
b120440404
commit
222d231b1b
42 files changed
+8245
-474
No files matched your search
@@ -24,24 +24,6 @@ grow_vertical = 2
|
||||
mouse_filter = 2
|
||||
script = ExtResource("quiz_script")
|
||||
|
||||
[node name="Open" type="Button" parent="." unique_id=165052329]
|
||||
visible = false
|
||||
layout_mode = 0
|
||||
offset_left = 1296.0
|
||||
offset_top = 66.0
|
||||
offset_right = 1346.0
|
||||
offset_bottom = 97.0
|
||||
text = "Open"
|
||||
|
||||
[node name="FileDialog" type="FileDialog" parent="." unique_id=485771559]
|
||||
title = "Open a Directory"
|
||||
initial_position = 2
|
||||
size = Vector2i(616, 400)
|
||||
ok_button_text = "Select Current Folder"
|
||||
file_mode = 2
|
||||
access = 2
|
||||
show_hidden_files = true
|
||||
|
||||
[node name="MusicListPanel" parent="." unique_id=2011292375 instance=ExtResource("scene_MusicListPanel")]
|
||||
layout_mode = 0
|
||||
offset_left = 64.0
|
||||
@@ -131,5 +113,3 @@ offset_bottom = 984.0
|
||||
|
||||
[node name="SongLoader" type="Node" parent="." unique_id=1520867349]
|
||||
script = ExtResource("song_loader_script")
|
||||
|
||||
[connection signal="dir_selected" from="FileDialog" to="." method="_on_file_dialog_dir_selected"]
|
||||
@@ -105,6 +105,7 @@ action_mode = 0
|
||||
text = "Hide ticks in the playbar"
|
||||
|
||||
[node name="WinningScoreRow" type="HBoxContainer" parent="CardPanel/VBox"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Title" type="Label" parent="CardPanel/VBox/WinningScoreRow"]
|
||||
|
||||
@@ -48,9 +48,13 @@ func _process(delta: float) -> void:
|
||||
scroll_bar.value = 0
|
||||
|
||||
|
||||
## Fetches every soundtrack from the server and shows them.
|
||||
func load_from_server() -> void:
|
||||
GameLog.debug("load_from_server")
|
||||
## Shows every soundtrack: the music folder's soundtracks when one is picked, otherwise
|
||||
## the server's.
|
||||
func load_soundtracks() -> void:
|
||||
GameLog.debug("load_soundtracks")
|
||||
if Settings.is_offline():
|
||||
show_soundtracks(Settings.music_folder.soundtrack_names())
|
||||
return
|
||||
var populate_list: Callable = func populate_list(array: Variant) -> void:
|
||||
if typeof(array) == TYPE_ARRAY:
|
||||
show_soundtracks(array)
|
||||
|
||||
+38
-46
@@ -18,7 +18,6 @@ signal search_pressed
|
||||
## Emitted with a line for the game log, e.g. when a player gets a point.
|
||||
signal event_logged(text: String)
|
||||
|
||||
var songs: Array = []
|
||||
var current_player_row: PlayerRow
|
||||
|
||||
## True while statistics are off because a player won, until points are reset.
|
||||
@@ -27,8 +26,6 @@ var stats_paused_by_win: bool = false
|
||||
## Dims the window behind an open popup and keeps clicks from reaching the buttons under it.
|
||||
var popup_backdrop: PopupBackdrop
|
||||
|
||||
@onready var open_button: Button = $Open
|
||||
@onready var file_dialog: FileDialog = $FileDialog
|
||||
@onready var inspiration_list: InspirationList = $InspirationList
|
||||
@onready var now_playing: NowPlaying = $NowPlaying
|
||||
@onready var players_panel: PlayersPanel = $PlayersPanel
|
||||
@@ -47,6 +44,7 @@ func _ready() -> void:
|
||||
popup_backdrop = PopupBackdrop.add_to(self, _popups())
|
||||
song_controls.next_pressed.connect(play_next_song)
|
||||
song_controls.show_answer_pressed.connect(show_answer_pressed)
|
||||
song_controls.show_winner_pressed.connect(show_final_winners)
|
||||
toolbar.sound_test_pressed.connect(music_player.get_sound_test_song)
|
||||
toolbar.search_pressed.connect(search_pressed.emit)
|
||||
quiz_settings_button.pressed.connect(quiz_settings_card.toggle)
|
||||
@@ -63,9 +61,7 @@ func _ready() -> void:
|
||||
music_player.play_next_song.connect(play_next_song)
|
||||
music_player.update_song_list.connect(update_song_list)
|
||||
|
||||
open_button.pressed.connect(open)
|
||||
|
||||
inspiration_list.load_from_server()
|
||||
inspiration_list.load_soundtracks()
|
||||
song_loader.load_song_list()
|
||||
character_select_popup.load_characters()
|
||||
|
||||
@@ -106,7 +102,7 @@ func on_sync_finished() -> void:
|
||||
## Reloads the soundtracks, playlist and characters from the server.
|
||||
func refresh_from_server() -> void:
|
||||
GameLog.debug("refresh_from_server")
|
||||
inspiration_list.load_from_server()
|
||||
inspiration_list.load_soundtracks()
|
||||
reset_playlist()
|
||||
character_select_popup.load_characters()
|
||||
|
||||
@@ -127,13 +123,23 @@ func show_answer_pressed() -> void:
|
||||
|
||||
func play_next_song() -> void:
|
||||
GameLog.debug("play_next_song ", Playlist.latest_played_song)
|
||||
# Every song of a prepared quiz has played, e.g. when songs play on by themselves
|
||||
if Settings.is_prepared_quiz() && Playlist.first_song_played \
|
||||
&& Playlist.get_preloaded_songs() == 0:
|
||||
song_controls.show_winner_button()
|
||||
return
|
||||
if Playlist.first_song_played:
|
||||
Playlist.set_latest_played_song()
|
||||
Playlist.first_song_played = true
|
||||
music_player.play_song_object(Playlist.latest_played_song)
|
||||
if !(Playlist.get_preloaded_songs() > 0):
|
||||
song_controls.set_next_enabled(false)
|
||||
# The song counts as played once it starts, which can take a moment if another song
|
||||
# is stopping, so the next song is picked after that
|
||||
await music_player.play_song_object(Playlist.latest_played_song)
|
||||
song_loader.fetch_song_if_idle()
|
||||
if !(Playlist.get_preloaded_songs() > 0):
|
||||
if Settings.is_prepared_quiz():
|
||||
song_controls.show_winner_button()
|
||||
else:
|
||||
song_controls.set_next_enabled(false)
|
||||
|
||||
|
||||
# The quiz settings card is left out: it does not dim the quiz
|
||||
@@ -159,6 +165,10 @@ func song_object_clicked(song_no: int) -> void:
|
||||
GameLog.debug("Clicked Song Object: ", Playlist.song_object_array[song_no].to_string())
|
||||
if !Settings.character_select_open:
|
||||
if Playlist.get_song(song_no) == null:
|
||||
if Settings.is_offline():
|
||||
GameLog.warning("The song's file is gone: ",
|
||||
Playlist.song_object_array[song_no].song_path)
|
||||
return
|
||||
GameLog.debug("has no song, need to download")
|
||||
var song_downloaded: Callable = func song_downloaded(body: PackedByteArray) -> void:
|
||||
Playlist.add_song_to_song_object(song_no, body)
|
||||
@@ -174,7 +184,10 @@ func reset_playlist() -> void:
|
||||
var playlist_reset_done: Callable = func playlist_reset_done() -> void:
|
||||
Playlist.reset_playlist()
|
||||
song_loader.load_song_list()
|
||||
|
||||
if Settings.is_offline():
|
||||
Settings.music_folder.restart()
|
||||
playlist_reset_done.call()
|
||||
return
|
||||
MusicServerClient.reset_playlist(playlist_reset_done)
|
||||
|
||||
|
||||
@@ -224,6 +237,20 @@ func _on_point_taken(player_object: PlayerObject) -> void:
|
||||
update_song_list()
|
||||
|
||||
|
||||
## Shows the players with the most points as the winners, at the end of a prepared quiz.
|
||||
func show_final_winners() -> void:
|
||||
GameLog.debug("show_final_winners")
|
||||
var top_score: int = 0
|
||||
for player: PlayerObject in Settings.player_array:
|
||||
top_score = maxi(top_score, player.player_score)
|
||||
var winners: Array[PlayerObject] = []
|
||||
for player: PlayerObject in Settings.player_array:
|
||||
if player.player_score == top_score:
|
||||
winners.append(player)
|
||||
winner_popup.show_winners(winners)
|
||||
music_player.play_song(preload("res://sounds/winning.mp3"))
|
||||
|
||||
|
||||
func _on_player_won(player_object: PlayerObject) -> void:
|
||||
GameLog.debug("_on_player_won")
|
||||
winner_popup.show_winner(player_object)
|
||||
@@ -249,38 +276,3 @@ func _on_change_character_requested(new_player: PlayerRow, player_name: String)
|
||||
func _on_new_character_selected(character_texture: ImageTexture) -> void:
|
||||
GameLog.debug("_on_new_character_selected")
|
||||
current_player_row.new_character_selected(character_texture)
|
||||
|
||||
|
||||
###Local
|
||||
|
||||
var local_path: String = '/Users/sebastian/ResilioSync/Sorterat_test/Metal Gear Solid 4 - Guns of the Patriots/2-16 Metal Gear Saga.mp3'
|
||||
|
||||
|
||||
func open() -> void:
|
||||
file_dialog.popup()
|
||||
|
||||
|
||||
func _on_file_dialog_dir_selected(path: String) -> void:
|
||||
GameLog.debug(path)
|
||||
dir_contents(path)
|
||||
|
||||
|
||||
func dir_contents(path: String) -> void:
|
||||
var dir: DirAccess = DirAccess.open(path)
|
||||
if dir:
|
||||
dir.list_dir_begin()
|
||||
var file_name: String = dir.get_next()
|
||||
songs.clear()
|
||||
var soundtracks: Array = []
|
||||
while file_name != "":
|
||||
if dir.current_is_dir():
|
||||
#GameLog.debug("Found directory: " + file_name)
|
||||
soundtracks.append(file_name)
|
||||
else:
|
||||
#GameLog.debug("Found file: " + file_name)
|
||||
if file_name.ends_with(".mp3"):
|
||||
songs.append(path + "/" + file_name)
|
||||
file_name = dir.get_next()
|
||||
inspiration_list.show_soundtracks(soundtracks)
|
||||
else:
|
||||
GameLog.warning("An error occurred when trying to access the path.")
|
||||
@@ -28,6 +28,7 @@ const PADDING: Vector2 = Vector2(16, 12)
|
||||
@onready var increase_hide_for_seconds_button: Button = %IncreaseHideForSecondsButton
|
||||
@onready var hide_length_button: CheckButton = %HideLengthCheckButton
|
||||
@onready var hide_ticks_button: CheckButton = %HideTicksCheckButton
|
||||
@onready var winning_score_row: HBoxContainer = %WinningScoreRow
|
||||
@onready var score_label: Label = %ScoreLabel
|
||||
@onready var lower_winning_score_button: Button = %LowerButton
|
||||
@onready var increase_winning_score_button: Button = %IncreaseButton
|
||||
@@ -65,6 +66,8 @@ func show_settings() -> void:
|
||||
hide_for_seconds_row.visible = Settings.hide_beginning
|
||||
hide_for_seconds_label.text = "%d" % Settings.hide_for_seconds
|
||||
score_label.text = str(Settings.winning_score)
|
||||
# A prepared quiz is won by having the most points when every song has played
|
||||
winning_score_row.visible = !Settings.is_prepared_quiz()
|
||||
inspiration_speed_label.text = str(Settings.inspiration_list_speed)
|
||||
_fit_to_rows()
|
||||
|
||||
|
||||
+29
-1
@@ -4,6 +4,15 @@ extends Control
|
||||
|
||||
signal show_answer_pressed
|
||||
signal next_pressed
|
||||
## Emitted when Next is pressed at the end of a prepared quiz, where it reads
|
||||
## "Show winner".
|
||||
signal show_winner_pressed
|
||||
|
||||
const NEXT_TEXT: String = "Randomize new track"
|
||||
const SHOW_WINNER_TEXT: String = "Show winner"
|
||||
|
||||
## True while Next reads "Show winner", after the last song of a prepared quiz.
|
||||
var shows_winner: bool = false
|
||||
|
||||
@onready var show_answer_button: Button = $ShowAnswerButton
|
||||
@onready var next_button: Button = $NextButton
|
||||
@@ -11,17 +20,36 @@ signal next_pressed
|
||||
|
||||
func _ready() -> void:
|
||||
show_answer_button.pressed.connect(show_answer_pressed.emit)
|
||||
next_button.pressed.connect(next_pressed.emit)
|
||||
next_button.pressed.connect(_on_next_button_pressed)
|
||||
|
||||
|
||||
## Enables Next while there is a downloaded song to play next.
|
||||
func set_next_enabled(enabled: bool) -> void:
|
||||
_set_shows_winner(false)
|
||||
next_button.disabled = !enabled
|
||||
|
||||
|
||||
## Turns Next into "Show winner", for when the last song of a prepared quiz has played.
|
||||
func show_winner_button() -> void:
|
||||
_set_shows_winner(true)
|
||||
next_button.disabled = false
|
||||
|
||||
|
||||
## Disables both buttons, e.g. while the server syncs the song list. Unlocking enables
|
||||
## Show answer again; Next comes back when the next song has been downloaded.
|
||||
func set_locked(locked: bool) -> void:
|
||||
show_answer_button.disabled = locked
|
||||
if locked:
|
||||
next_button.disabled = true
|
||||
|
||||
|
||||
func _set_shows_winner(on: bool) -> void:
|
||||
shows_winner = on
|
||||
next_button.text = SHOW_WINNER_TEXT if on else NEXT_TEXT
|
||||
|
||||
|
||||
func _on_next_button_pressed() -> void:
|
||||
if shows_winner:
|
||||
show_winner_pressed.emit()
|
||||
else:
|
||||
next_pressed.emit()
|
||||
+41
-2
@@ -1,7 +1,8 @@
|
||||
class_name SongLoader
|
||||
extends Node
|
||||
## Downloads songs from the server into the Playlist ahead of time, so the next song
|
||||
## can start at once.
|
||||
## can start at once. With a music folder (see [method Settings.is_offline]) the songs are
|
||||
## picked from the folder instead.
|
||||
##
|
||||
## How many songs it keeps ready is the cache size in the settings
|
||||
## ([code]Playlist.number_of_tracks_to_preload[/code]).
|
||||
@@ -22,7 +23,12 @@ var retry_seconds: float = 5.0
|
||||
## Preloading waits for the list, because loading the list clears the playlist.
|
||||
func load_song_list() -> void:
|
||||
GameLog.debug("load_song_list")
|
||||
if Settings.is_offline():
|
||||
_start_folder_playlist()
|
||||
return
|
||||
var song_list_fetched: Callable = func song_list_fetched(songs: Array[SongInfo]) -> void:
|
||||
if Settings.is_offline():
|
||||
return
|
||||
Playlist.clear_song_list()
|
||||
for info: SongInfo in songs:
|
||||
var new_song_object: SongObject = SongObject.new()
|
||||
@@ -43,6 +49,9 @@ func load_song_list() -> void:
|
||||
## Starts downloading one more song, which keeps downloading until the cache is full.
|
||||
func fetch_song() -> void:
|
||||
GameLog.debug("fetch_song")
|
||||
if Settings.is_offline():
|
||||
_add_folder_songs()
|
||||
return
|
||||
Playlist.is_song_downloading = true
|
||||
_download_random_song(SongObject.new())
|
||||
|
||||
@@ -65,6 +74,9 @@ func fill_cache() -> void:
|
||||
func _download_random_song(new_song_object: SongObject) -> void:
|
||||
var song_downloaded: Callable = func song_downloaded(body: PackedByteArray) -> void:
|
||||
GameLog.debug("song_downloaded")
|
||||
# A music folder was picked while the song was downloading
|
||||
if Settings.is_offline():
|
||||
return
|
||||
Playlist.store_song(new_song_object, body)
|
||||
_add_song_to_queue(new_song_object)
|
||||
MusicServerClient.fetch_random_song(Settings.use_low_played_mode, song_downloaded,
|
||||
@@ -79,6 +91,8 @@ func _add_song_to_queue(new_song_object: SongObject) -> void:
|
||||
func _fetch_song_info(new_song_object: SongObject) -> void:
|
||||
var song_info_fetched: Callable = func song_info_fetched(info: SongInfo) -> void:
|
||||
GameLog.debug("song_info_fetched")
|
||||
if Settings.is_offline():
|
||||
return
|
||||
new_song_object.song_number = info.song_no
|
||||
new_song_object.soundtrack_title = info.soundtrack
|
||||
new_song_object.song_title = info.song
|
||||
@@ -95,4 +109,29 @@ func _fetch_song_info(new_song_object: SongObject) -> void:
|
||||
|
||||
func _retry_later(step: Callable) -> void:
|
||||
GameLog.warning("Song download failed, retrying in %s seconds" % retry_seconds)
|
||||
get_tree().create_timer(retry_seconds).timeout.connect(step)
|
||||
get_tree().create_timer(retry_seconds).timeout.connect(func() -> void:
|
||||
if !Settings.is_offline():
|
||||
step.call())
|
||||
|
||||
|
||||
# A music folder has no playlist on a server, so the game starts with an empty one
|
||||
func _start_folder_playlist() -> void:
|
||||
Playlist.clear_song_list()
|
||||
Playlist.update_last_played_song()
|
||||
Playlist.game_start_song = 0
|
||||
Playlist.is_song_downloading = false
|
||||
song_list_loaded.emit()
|
||||
fetch_song_if_idle()
|
||||
|
||||
|
||||
# Picks songs from the music folder until enough are ready. A prepared quiz runs out
|
||||
# when every song is in the playlist.
|
||||
func _add_folder_songs() -> void:
|
||||
Playlist.is_song_downloading = false
|
||||
while Playlist.should_preload_more_songs():
|
||||
var song_object: SongObject = Settings.music_folder.next_song()
|
||||
if song_object == null:
|
||||
return
|
||||
song_object.song_number = Playlist.song_object_array.size()
|
||||
Playlist.add_to_playlist(song_object)
|
||||
song_added.emit()
|
||||
Reference in new issue
Block a user