Retry failed song downloads instead of getting stuck
MusicServerClient functions now take an optional on_failed callback, called when a request fails or returns invalid JSON. Before, a failed request was silently dropped, so a failure while preloading a song left Playlist.is_song_downloading set forever and no more songs were fetched. Song fetching retries the step that failed (download, queue or info) every song_fetch_retry_seconds, so the server and the game keep the same playlist.
This commit is contained in:
1 parent
c985a6ce5b
commit
f67dccbf52
4 files changed
+167
-65
No files matched your search
+55
-40
@@ -17,108 +17,123 @@ func _ready() -> void:
|
||||
|
||||
## Fetches the songs already in the server playlist, as an Array of
|
||||
## [code]{SongNo, Soundtrack, Song}[/code] dictionaries.
|
||||
func fetch_song_list(on_done: Callable) -> void:
|
||||
get_json("/music/list", on_done)
|
||||
func fetch_song_list(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/music/list", on_done, on_failed)
|
||||
|
||||
|
||||
## Downloads a random song as MP3 bytes. With [param low_played] the server prefers
|
||||
## soundtracks that have been played the least.
|
||||
func fetch_random_song(low_played: bool, on_done: Callable) -> void:
|
||||
get_bytes("/music/rand/low" if low_played else "/music/rand", on_done)
|
||||
func fetch_random_song(
|
||||
low_played: bool, on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_bytes("/music/rand/low" if low_played else "/music/rand", on_done, on_failed)
|
||||
|
||||
|
||||
## Adds the most recently fetched random song to the server playlist.
|
||||
func add_to_queue(on_done: Callable) -> void:
|
||||
send("/music/addQue", on_done)
|
||||
func add_to_queue(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
send("/music/addQue", on_done, on_failed)
|
||||
|
||||
|
||||
## Fetches [code]{SongNo, Soundtrack, Song}[/code] for the most recently queued song.
|
||||
func fetch_song_info(on_done: Callable) -> void:
|
||||
get_json("/music/info", on_done)
|
||||
func fetch_song_info(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/music/info", on_done, on_failed)
|
||||
|
||||
|
||||
## Downloads a song from the server playlist as MP3 bytes.
|
||||
func fetch_song(song_no: int, on_done: Callable) -> void:
|
||||
get_bytes("/music?song=" + str(song_no), on_done)
|
||||
func fetch_song(song_no: int, on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_bytes("/music?song=" + str(song_no), on_done, on_failed)
|
||||
|
||||
|
||||
## Downloads the sound test song as MP3 bytes.
|
||||
func fetch_sound_test_song(on_done: Callable) -> void:
|
||||
get_bytes("/music/soundTest", on_done)
|
||||
func fetch_sound_test_song(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_bytes("/music/soundTest", on_done, on_failed)
|
||||
|
||||
|
||||
## Clears the server playlist.
|
||||
func reset_playlist(on_done: Callable) -> void:
|
||||
send("/music/reset", on_done)
|
||||
func reset_playlist(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
send("/music/reset", on_done, on_failed)
|
||||
|
||||
|
||||
## Fetches every soundtrack name as an Array of Strings.
|
||||
func fetch_all_soundtracks(on_done: Callable) -> void:
|
||||
get_json("/music/all", on_done)
|
||||
func fetch_all_soundtracks(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/music/all", on_done, on_failed)
|
||||
|
||||
|
||||
## Fetches every soundtrack name, sorted, as an Array of Strings.
|
||||
func fetch_all_soundtracks_ordered(on_done: Callable) -> void:
|
||||
get_json("/music/all/order", on_done)
|
||||
func fetch_all_soundtracks_ordered(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/music/all/order", on_done, on_failed)
|
||||
|
||||
|
||||
## Records in the server statistics that a song has been played.
|
||||
func mark_song_played(song_no: int) -> void:
|
||||
send("/music/played?song=" + str(song_no), Callable(), HTTPClient.METHOD_PUT)
|
||||
send("/music/played?song=" + str(song_no), Callable(), Callable(), HTTPClient.METHOD_PUT)
|
||||
|
||||
|
||||
## Fetches the names of all character images.
|
||||
func fetch_character_list(on_done: Callable) -> void:
|
||||
get_json("/characters", on_done)
|
||||
func fetch_character_list(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/characters", on_done, on_failed)
|
||||
|
||||
|
||||
## Downloads a character image as PNG bytes.
|
||||
func fetch_character_image(character_name: String, on_done: Callable) -> void:
|
||||
get_bytes("/character?name=" + character_name.uri_encode(), on_done)
|
||||
func fetch_character_image(
|
||||
character_name: String, on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_bytes("/character?name=" + character_name.uri_encode(), on_done, on_failed)
|
||||
|
||||
|
||||
## Starts syncing the server's soundtrack library with its music folder.
|
||||
func start_sync(on_done: Callable) -> void:
|
||||
send("/sync", on_done)
|
||||
func start_sync(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
send("/sync", on_done, on_failed)
|
||||
|
||||
|
||||
## Fetches the sync state: [code]{progress, time_spent}[/code] while running, or the
|
||||
## sync result once finished.
|
||||
func fetch_sync_progress(on_done: Callable) -> void:
|
||||
get_json("/sync/progress", on_done)
|
||||
func fetch_sync_progress(on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
get_json("/sync/progress", on_done, on_failed)
|
||||
|
||||
|
||||
## Requests [param path] and calls [param on_done] with the parsed JSON.
|
||||
## Nothing is called if the request fails or the body is not valid JSON.
|
||||
func get_json(path: String, on_done: Callable) -> void:
|
||||
## [param on_failed] is called with no arguments if the request fails or the body is
|
||||
## not valid JSON.
|
||||
func get_json(path: String, on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
_request(path, HTTPClient.METHOD_GET, func(body: PackedByteArray) -> void:
|
||||
var json: JSON = JSON.new()
|
||||
if json.parse(body.get_string_from_utf8()) != OK:
|
||||
push_warning("Invalid JSON from %s: %s" % [path, json.get_error_message()])
|
||||
_call_if_valid(on_failed)
|
||||
return
|
||||
on_done.call(json.data)
|
||||
)
|
||||
, on_failed)
|
||||
|
||||
|
||||
## Requests [param path] and calls [param on_done] with the raw response body.
|
||||
func get_bytes(path: String, on_done: Callable) -> void:
|
||||
_request(path, HTTPClient.METHOD_GET, on_done)
|
||||
## Requests [param path] and calls [param on_done] with the raw response body, or
|
||||
## [param on_failed] with no arguments if the request fails.
|
||||
func get_bytes(path: String, on_done: Callable, on_failed: Callable = Callable()) -> void:
|
||||
_request(path, HTTPClient.METHOD_GET, on_done, on_failed)
|
||||
|
||||
|
||||
## Requests [param path] and calls [param on_done] with no arguments when it succeeds.
|
||||
## [param on_done] may be an empty Callable when the result does not matter.
|
||||
func send(path: String, on_done: Callable, method: int = HTTPClient.METHOD_GET) -> void:
|
||||
## Requests [param path] and calls [param on_done] with no arguments when it succeeds,
|
||||
## or [param on_failed] when it fails. Either may be an empty Callable.
|
||||
func send(
|
||||
path: String,
|
||||
on_done: Callable,
|
||||
on_failed: Callable = Callable(),
|
||||
method: int = HTTPClient.METHOD_GET,
|
||||
) -> void:
|
||||
_request(path, method, func(_body: PackedByteArray) -> void:
|
||||
if on_done.is_valid():
|
||||
on_done.call()
|
||||
)
|
||||
_call_if_valid(on_done)
|
||||
, on_failed)
|
||||
|
||||
|
||||
func _request(path: String, method: int, on_success: Callable) -> void:
|
||||
func _request(path: String, method: int, on_success: Callable, on_failed: Callable) -> void:
|
||||
var url: String = Settings.default_path + path
|
||||
transport.request(url, method, func(response_code: int, body: PackedByteArray) -> void:
|
||||
if response_code != 200:
|
||||
push_warning("Request to %s failed with response code %d" % [url, response_code])
|
||||
_call_if_valid(on_failed)
|
||||
return
|
||||
on_success.call(body)
|
||||
)
|
||||
|
||||
|
||||
static func _call_if_valid(callback: Callable) -> void:
|
||||
if callback.is_valid():
|
||||
callback.call()
|
||||
Reference in new issue
Block a user