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
@@ -0,0 +1,30 @@
|
||||
class_name AudioFile
|
||||
extends RefCounted
|
||||
## Reads songs from files: the songs downloaded from the server, and the songs in a music
|
||||
## folder.
|
||||
|
||||
## The file types Godot can play from a file while the game runs.
|
||||
const PLAYABLE_EXTENSIONS: Array[String] = ["mp3", "ogg", "wav"]
|
||||
|
||||
|
||||
## Whether [param path] is a file type the game can play.
|
||||
static func is_playable(path: String) -> bool:
|
||||
return path.get_extension().to_lower() in PLAYABLE_EXTENSIONS
|
||||
|
||||
|
||||
## Reads the song in [param path]. Returns null if the file is missing, is not a playable
|
||||
## type, or is broken.
|
||||
static func load(path: String) -> AudioStream:
|
||||
if !FileAccess.file_exists(path):
|
||||
return null
|
||||
var stream: AudioStream = null
|
||||
match path.get_extension().to_lower():
|
||||
"mp3":
|
||||
stream = AudioStreamMP3.load_from_file(path)
|
||||
"ogg":
|
||||
stream = AudioStreamOggVorbis.load_from_file(path)
|
||||
"wav":
|
||||
stream = AudioStreamWAV.load_from_file(path)
|
||||
if stream == null || stream.get_length() <= 0.0:
|
||||
return null
|
||||
return stream
|
||||
@@ -0,0 +1 @@
|
||||
uid://drle78nqw2ve0
|
||||
@@ -10,7 +10,8 @@ extends RefCounted
|
||||
## [/codeblock]
|
||||
## A cached picture is used while the server's change time matches the index. Servers that
|
||||
## can't tell when pictures changed answer 404 for [code]/character/details[/code]; then a
|
||||
## cached picture is used until it is [constant MAX_AGE_SECONDS] old.
|
||||
## cached picture is used until it is [constant MAX_AGE_SECONDS] old. When the server
|
||||
## can't be reached at all, the cached pictures are used as they are.
|
||||
|
||||
const FOLDER_NAME: String = "characters"
|
||||
const INDEX_FILE_NAME: String = "index.json"
|
||||
@@ -24,6 +25,8 @@ const UNKNOWN_TIME: int = -1
|
||||
var _index: Dictionary = {}
|
||||
# When the server last changed each picture on the list, or UNKNOWN_TIME
|
||||
var _modified: Dictionary[String, int] = {}
|
||||
# True when the server could not be reached and only cached pictures are shown
|
||||
var _cache_only: bool = false
|
||||
|
||||
|
||||
## The folder the pictures are kept in.
|
||||
@@ -36,6 +39,7 @@ static func folder() -> String:
|
||||
## deleted.
|
||||
func load_list(on_done: Callable) -> void:
|
||||
_load_index()
|
||||
_cache_only = false
|
||||
var details_fetched: Callable = func details_fetched(details: Array) -> void:
|
||||
_modified.clear()
|
||||
for character: CharacterDetails in details:
|
||||
@@ -48,7 +52,7 @@ func load_list(on_done: Callable) -> void:
|
||||
## disk when the cached one is still fresh, otherwise from the server.
|
||||
func fetch_image(character_name: String, on_done: Callable) -> void:
|
||||
var path: String = _file_path(character_name)
|
||||
if _is_fresh(character_name) && FileAccess.file_exists(path):
|
||||
if (_cache_only || _is_fresh(character_name)) && FileAccess.file_exists(path):
|
||||
var cached: Image = CharacterCard.image_from_bytes(FileAccess.get_file_as_bytes(path))
|
||||
if cached != null:
|
||||
on_done.call(cached)
|
||||
@@ -72,7 +76,19 @@ func _load_names_only(on_done: Callable) -> void:
|
||||
for character_name: Variant in list:
|
||||
_modified[str(character_name)] = UNKNOWN_TIME
|
||||
_list_loaded(on_done)
|
||||
MusicServerClient.fetch_character_list(list_fetched)
|
||||
MusicServerClient.fetch_character_list(list_fetched, _load_cached_only.bind(on_done))
|
||||
|
||||
|
||||
# The server can't be reached, so the pictures already cached are shown and kept
|
||||
func _load_cached_only(on_done: Callable) -> void:
|
||||
GameLog.warning("Could not get the characters from the server, showing the cached ones")
|
||||
_cache_only = true
|
||||
var names: Array[String] = []
|
||||
for character_name: String in _index:
|
||||
if FileAccess.file_exists(_file_path(character_name)):
|
||||
names.append(character_name)
|
||||
names.sort_custom(func(a: String, b: String) -> bool: return a.naturalnocasecmp_to(b) < 0)
|
||||
on_done.call(names)
|
||||
|
||||
|
||||
func _list_loaded(on_done: Callable) -> void:
|
||||
|
||||
Reference in new issue
Block a user