Save the quiz settings to the config folder
Build / test (push) Successful in 45s
Build / build (push) Successful in 1m20s

The quiz card's settings are saved to quiz_settings.cfg in OS.get_config_dir()
on every change and loaded at start. Debug builds use their own file, and
MUSICPLAYER_CONFIG_DIR moves the file elsewhere so tests never touch it.

Count plays in statistics and Prefer less played soundtracks are not saved and
move from the quiz card to the Settings popup.
This commit is contained in:
Sansan committed 2026-10-07 18:13:00 +02:00
1 parent b510aa47f2
commit 92e3a66075
15 files changed
+835 -591

No files matched your search

+51 -1
View File
@@ -17,6 +17,17 @@ const SERVERS: Array[String] = [
const DEBUG_SERVER: String = "http://localhost:8080"
## The winning score debug builds start with, so a game is quick to win while testing.
const DEBUG_WINNING_SCORE: int = 5
## The quiz settings that are saved to [method quiz_settings_path] and loaded at start.
## Count plays in statistics and Prefer less played soundtracks are left out on purpose.
const QUIZ_SETTINGS: Array[StringName] = [
&"auto_repeat_song", &"stop_after_current", &"hide_next_track", &"hide_beginning",
&"hide_for_seconds", &"hide_length", &"hide_ticks", &"winning_score",
&"inspiration_list_speed",
]
## When this environment variable is set, the settings file is kept in that folder instead
## of the user's config folder. The tests set it so they never touch the real file.
const CONFIG_DIR_ENV: String = "MUSICPLAYER_CONFIG_DIR"
const QUIZ_SETTINGS_SECTION: String = "quiz"
var default_path: String = "https://music.sanplex.xyz"
## Password for getting an API token from the server. Any password works when the
@@ -49,13 +60,52 @@ var version: String = ProjectSettings.get_setting("application/config/version",
# Autoloads are ready before any scene, so debug builds use the local server and the
# low winning score from the very first request
# low winning score from the very first request. The saved quiz settings come after, so
# the debug winning score is only used until another one is saved.
func _ready() -> void:
if OS.has_feature("debug"):
is_debug = true
default_path = DEBUG_SERVER
selected_server = SERVERS.find(DEBUG_SERVER)
winning_score = DEBUG_WINNING_SCORE
load_quiz_settings()
## Where the quiz settings are saved: a MusicPlayer folder in the user's config folder,
## e.g. [code]~/.config[/code] on Linux and [code]%APPDATA%[/code] on Windows. Debug
## builds use a file of their own, so testing never changes the real settings.
func quiz_settings_path() -> String:
var config_dir: String = OS.get_environment(CONFIG_DIR_ENV)
if config_dir.is_empty():
config_dir = OS.get_config_dir().path_join("MusicPlayer")
var file_name: String = "quiz_settings_debug.cfg" if is_debug else "quiz_settings.cfg"
return config_dir.path_join(file_name)
## Loads the saved quiz settings. Settings missing from the file, or saved with the
## wrong type, keep their current value.
func load_quiz_settings() -> void:
var file: ConfigFile = ConfigFile.new()
if file.load(quiz_settings_path()) != OK:
return
for setting: StringName in QUIZ_SETTINGS:
var current: Variant = get(setting)
var saved: Variant = file.get_value(QUIZ_SETTINGS_SECTION, setting, current)
if typeof(saved) == typeof(current):
set(setting, saved)
## Saves the quiz settings, so they are the same the next time the game starts.
func save_quiz_settings() -> void:
var path: String = quiz_settings_path()
DirAccess.make_dir_recursive_absolute(path.get_base_dir())
var file: ConfigFile = ConfigFile.new()
for setting: StringName in QUIZ_SETTINGS:
file.set_value(QUIZ_SETTINGS_SECTION, setting, get(setting))
var error: Error = file.save(path)
if error != OK:
GameLog.warning("Could not save the quiz settings to %s: %s"
% [path, error_string(error)])
func delete_children(node: Node) -> void:
+2 -2
View File
@@ -98,9 +98,9 @@ unique_name_in_owner = true
visible = false
layout_mode = 1
offset_left = 700.0
offset_top = 454.0
offset_top = 419.0
offset_right = 700.0
offset_bottom = 454.0
offset_bottom = 419.0
[node name="SoundtrackStatsPopup" parent="." unique_id=1213999001 instance=ExtResource("soundtrack_stats_popup_scene")]
visible = false
+16 -2
View File
@@ -16,7 +16,7 @@ panel = NodePath("SettingsPanel")
unique_name_in_owner = true
layout_mode = 0
offset_right = 520.0
offset_bottom = 172.0
offset_bottom = 242.0
theme_type_variation = &"PopupBackground"
[node name="VBoxContainer" type="VBoxContainer" parent="SettingsPanel" unique_id=1796635163]
@@ -25,7 +25,7 @@ layout_mode = 0
offset_left = 20.0
offset_top = 12.0
offset_right = 500.0
offset_bottom = 160.0
offset_bottom = 230.0
[node name="Label" type="Label" parent="SettingsPanel/VBoxContainer" unique_id=1111531402]
layout_mode = 2
@@ -85,6 +85,20 @@ focus_mode = 0
action_mode = 0
text = "+1"
[node name="LowPlayedCheckButton" type="CheckButton" parent="SettingsPanel/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
focus_mode = 0
action_mode = 0
text = "Prefer less played soundtracks"
[node name="AddToDatabaseCheckButton" type="CheckButton" parent="SettingsPanel/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
focus_mode = 0
action_mode = 0
text = "Count plays in statistics"
[node name="LocalButton" type="CheckButton" parent="SettingsPanel/VBoxContainer" unique_id=905844805]
unique_name_in_owner = true
visible = false
+11 -2
View File
@@ -1,7 +1,8 @@
class_name SettingsPopup
extends BasePopup
## The application settings: which MusicServer to use, fullscreen and how many songs to
## download ahead. Opened from the Settings button in the top bar.
## The application settings: which MusicServer to use, fullscreen, how many songs to
## download ahead, whether to prefer less played soundtracks and whether plays count in
## the statistics. Opened from the Settings button in the top bar.
##
## The quiz settings, such as the winning score, are in [QuizSettingsCard].
@@ -14,6 +15,8 @@ signal server_changed
@onready var cache_label: Label = %CacheLabel
@onready var lower_cache_button: Button = %LowerCacheButton
@onready var increase_cache_button: Button = %IncreaseCacheButton
@onready var low_played_button: CheckButton = %LowPlayedCheckButton
@onready var add_to_database_button: CheckButton = %AddToDatabaseCheckButton
@onready var local_button: CheckButton = %LocalButton
@@ -29,6 +32,9 @@ func _ready() -> void:
lower_cache_button.pressed.connect(lower_cache)
increase_cache_button.pressed.connect(increase_cache)
select_server_button.item_selected.connect(select_server)
low_played_button.toggled.connect(
func(on: bool) -> void: Settings.use_low_played_mode = on)
add_to_database_button.toggled.connect(func(on: bool) -> void: Settings.add_to_stats = on)
visibility_changed.connect(_on_visibility_changed)
# Alt+Enter switches fullscreen from the main window, which resizes the window
get_window().size_changed.connect(_update_fullscreen_button)
@@ -75,9 +81,12 @@ func _use_server(new_server: int) -> void:
server_changed.emit()
# Winning a game turns statistics off, so the toggles are refreshed every time this opens
func _on_visibility_changed() -> void:
if visible:
_update_fullscreen_button()
low_played_button.set_pressed_no_signal(Settings.use_low_played_mode)
add_to_database_button.set_pressed_no_signal(Settings.add_to_stats)
func _update_fullscreen_button() -> void:
-14
View File
@@ -104,20 +104,6 @@ focus_mode = 0
action_mode = 0
text = "Hide ticks in the playbar"
[node name="LowPlayedCheckButton" type="CheckButton" parent="CardPanel/VBox"]
unique_name_in_owner = true
layout_mode = 2
focus_mode = 0
action_mode = 0
text = "Prefer less played soundtracks"
[node name="AddToDatabaseCheckButton" type="CheckButton" parent="CardPanel/VBox"]
unique_name_in_owner = true
layout_mode = 2
focus_mode = 0
action_mode = 0
text = "Count plays in statistics"
[node name="WinningScoreRow" type="HBoxContainer" parent="CardPanel/VBox"]
layout_mode = 2
+23 -19
View File
@@ -1,8 +1,10 @@
class_name QuizSettingsCard
extends BasePopup
## The quiz settings, in a small card that opens above the Quiz settings button by the
## music player: how songs are played and hidden, statistics, the winning score and the
## inspiration list. It does not dim the quiz, so it can be changed during a song.
## music player: how songs are played and hidden, the winning score and the inspiration
## list. It does not dim the quiz, so it can be changed during a song.
##
## Every change is saved at once with [method Settings.save_quiz_settings].
##
## The card's bottom edge is where the card is placed, just above the player bar, and
## it grows upward to fit its rows.
@@ -26,8 +28,6 @@ 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 low_played_button: CheckButton = %LowPlayedCheckButton
@onready var add_to_database_button: CheckButton = %AddToDatabaseCheckButton
@onready var score_label: Label = %ScoreLabel
@onready var lower_winning_score_button: Button = %LowerButton
@onready var increase_winning_score_button: Button = %IncreaseButton
@@ -37,17 +37,12 @@ const PADDING: Vector2 = Vector2(16, 12)
func _ready() -> void:
auto_repeat_button.toggled.connect(func(on: bool) -> void: Settings.auto_repeat_song = on)
stop_after_current_button.toggled.connect(
func(on: bool) -> void: Settings.stop_after_current = on)
hide_next_track_button.toggled.connect(
func(on: bool) -> void: Settings.hide_next_track = on)
auto_repeat_button.toggled.connect(_save_toggle.bind(&"auto_repeat_song"))
stop_after_current_button.toggled.connect(_save_toggle.bind(&"stop_after_current"))
hide_next_track_button.toggled.connect(_save_toggle.bind(&"hide_next_track"))
hide_beginning_button.toggled.connect(_on_hide_beginning_toggled)
hide_length_button.toggled.connect(func(on: bool) -> void: Settings.hide_length = on)
hide_ticks_button.toggled.connect(func(on: bool) -> void: Settings.hide_ticks = on)
low_played_button.toggled.connect(
func(on: bool) -> void: Settings.use_low_played_mode = on)
add_to_database_button.toggled.connect(func(on: bool) -> void: Settings.add_to_stats = on)
hide_length_button.toggled.connect(_save_toggle.bind(&"hide_length"))
hide_ticks_button.toggled.connect(_save_toggle.bind(&"hide_ticks"))
lower_winning_score_button.pressed.connect(lower_winning_score)
increase_winning_score_button.pressed.connect(increase_winning_score)
lower_inspiration_speed_button.pressed.connect(lower_inspiration_speed)
@@ -58,8 +53,8 @@ func _ready() -> void:
show_settings()
## Shows the current settings. Some change outside this card, e.g. winning turns
## statistics off, so this runs every time the card opens.
## Shows the current settings. This runs every time the card opens, in case a setting
## was changed somewhere else.
func show_settings() -> void:
auto_repeat_button.set_pressed_no_signal(Settings.auto_repeat_song)
stop_after_current_button.set_pressed_no_signal(Settings.stop_after_current)
@@ -67,8 +62,6 @@ func show_settings() -> void:
hide_beginning_button.set_pressed_no_signal(Settings.hide_beginning)
hide_length_button.set_pressed_no_signal(Settings.hide_length)
hide_ticks_button.set_pressed_no_signal(Settings.hide_ticks)
low_played_button.set_pressed_no_signal(Settings.use_low_played_mode)
add_to_database_button.set_pressed_no_signal(Settings.add_to_stats)
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)
@@ -88,30 +81,41 @@ func increase_winning_score() -> void:
func set_winning_score(new_score: int) -> void:
Settings.winning_score = maxi(new_score, 1)
score_label.text = str(Settings.winning_score)
Settings.save_quiz_settings()
func lower_inspiration_speed() -> void:
Settings.inspiration_list_speed = maxi(Settings.inspiration_list_speed - 1, 1)
inspiration_speed_label.text = str(Settings.inspiration_list_speed)
Settings.save_quiz_settings()
func increase_inspiration_speed() -> void:
Settings.inspiration_list_speed += 1
inspiration_speed_label.text = str(Settings.inspiration_list_speed)
Settings.save_quiz_settings()
func lower_hide_for_seconds() -> void:
Settings.hide_for_seconds = maxf(Settings.hide_for_seconds - 1.0, 0.0)
hide_for_seconds_label.text = "%d" % Settings.hide_for_seconds
Settings.save_quiz_settings()
func increase_hide_for_seconds() -> void:
Settings.hide_for_seconds = minf(Settings.hide_for_seconds + 1.0, MAX_HIDE_SECONDS)
hide_for_seconds_label.text = "%d" % Settings.hide_for_seconds
Settings.save_quiz_settings()
# Sets the on/off quiz setting named [param setting] and saves the quiz settings
func _save_toggle(on: bool, setting: StringName) -> void:
Settings.set(setting, on)
Settings.save_quiz_settings()
func _on_hide_beginning_toggled(on: bool) -> void:
Settings.hide_beginning = on
_save_toggle(on, &"hide_beginning")
hide_for_seconds_row.visible = on
_fit_to_rows()
+5
View File
@@ -32,6 +32,11 @@ find_godot() {
godot_bin="$(find_godot)"
# Keeps the tests away from the real quiz settings file, see Settings.CONFIG_DIR_ENV
MUSICPLAYER_CONFIG_DIR="$(mktemp -d)"
export MUSICPLAYER_CONFIG_DIR
trap 'rm -rf "$MUSICPLAYER_CONFIG_DIR"' EXIT
paths=("$@")
if [[ ${#paths[@]} -eq 0 ]]; then
paths=(res://tests/unit res://tests/integration)
File diff suppressed because it is too large. Load diff
+23 -4
View File
@@ -69,8 +69,6 @@ func test_each_toggle_changes_its_setting() -> void:
"hide_beginning_button": "hide_beginning",
"hide_length_button": "hide_length",
"hide_ticks_button": "hide_ticks",
"low_played_button": "use_low_played_mode",
"add_to_database_button": "add_to_stats",
}
await _open()
for button_name: String in toggles:
@@ -89,12 +87,12 @@ func test_each_toggle_changes_its_setting() -> void:
func test_card_shows_settings_changed_elsewhere() -> void:
await _open()
_card.close()
Settings.add_to_stats = false
Settings.hide_next_track = false
Settings.auto_repeat_song = false
await _open()
assert_bool(_card.add_to_database_button.button_pressed).is_false()
assert_bool(_card.hide_next_track_button.button_pressed).is_false()
assert_bool(_card.auto_repeat_button.button_pressed).is_false()
@@ -158,3 +156,24 @@ func test_card_grows_upward_for_the_seconds_row() -> void:
assert_float(open_row.size.y).is_greater(closed_row.size.y)
# Every row stays inside the card
assert_bool(open_row.encloses(_card.rows.get_global_rect())).is_true()
func test_statistics_settings_are_in_the_settings_popup_instead() -> void:
await _open()
assert_object(_card.find_child("LowPlayedCheckButton")).is_null()
assert_object(_card.find_child("AddToDatabaseCheckButton")).is_null()
func test_changes_are_saved_at_once() -> void:
Settings.hide_next_track = true
Settings.winning_score = 20
await _open()
await _click_at(_card.hide_next_track_button.get_global_rect().get_center())
_card.lower_winning_score_button.pressed.emit()
var saved: ConfigFile = ConfigFile.new()
assert_int(saved.load(Settings.quiz_settings_path())).is_equal(OK)
assert_bool(saved.get_value("quiz", "hide_next_track")).is_false()
assert_int(saved.get_value("quiz", "winning_score")).is_equal(19)
+55
View File
@@ -0,0 +1,55 @@
extends MainWindowTestSuite
## The statistics settings in the application Settings popup.
var _popup: SettingsPopup
func before_test() -> void:
super()
_popup = _window.get_node("SettingsPopup")
func test_less_played_toggle_changes_the_setting() -> void:
Settings.use_low_played_mode = false
_popup.open()
await _runner.simulate_frames(1)
assert_bool(_popup.low_played_button.button_pressed).is_false()
await _click_at(_popup.low_played_button.get_global_rect().get_center())
assert_bool(Settings.use_low_played_mode).is_true()
func test_statistics_toggle_changes_the_setting() -> void:
Settings.add_to_stats = true
_popup.open()
await _runner.simulate_frames(1)
assert_bool(_popup.add_to_database_button.button_pressed).is_true()
await _click_at(_popup.add_to_database_button.get_global_rect().get_center())
assert_bool(Settings.add_to_stats).is_false()
func test_statistics_settings_are_not_saved_with_the_quiz_settings() -> void:
Settings.add_to_stats = false
Settings.use_low_played_mode = true
Settings.save_quiz_settings()
var saved: ConfigFile = ConfigFile.new()
assert_int(saved.load(Settings.quiz_settings_path())).is_equal(OK)
assert_bool(saved.has_section_key("quiz", "add_to_stats")).is_false()
assert_bool(saved.has_section_key("quiz", "use_low_played_mode")).is_false()
func test_popup_fits_its_rows_and_is_centred() -> void:
_popup.open()
await _runner.simulate_frames(1)
var panel: Rect2 = _popup.settings_panel.get_global_rect()
var rows: Control = _popup.settings_panel.get_node("VBoxContainer")
assert_bool(panel.encloses(rows.get_global_rect())).is_true()
# The rows' own height fits, so the last toggle is not squeezed or cut off
assert_float(rows.get_combined_minimum_size().y).is_less_equal(rows.size.y)
assert_vector(panel.get_center()).is_equal(Vector2(960, 540))
@@ -0,0 +1 @@
uid://cp3f85h44jh3x
+2 -2
View File
@@ -12,7 +12,7 @@ func before_test() -> void:
func _stats_checkbox() -> CheckButton:
return _quiz.get_node("QuizSettingsCard").get_node("%AddToDatabaseCheckButton")
return _window.get_node("SettingsPopup").get_node("%AddToDatabaseCheckButton")
func test_winning_turns_statistics_off() -> void:
@@ -41,7 +41,7 @@ func test_statistics_turned_off_by_hand_stay_off_after_reset() -> void:
func test_settings_checkbox_shows_statistics_off_after_win() -> void:
await _give_point_with_keybind(1)
_press("QuizSettingsButton")
(_window.get_node("SettingsPopup") as SettingsPopup).open()
assert_bool(_stats_checkbox().button_pressed).is_false()
+97
View File
@@ -0,0 +1,97 @@
extends GdUnitTestSuite
## Saving the quiz settings to a file in the config folder and loading them back.
const SAVED_VALUES: Dictionary = {
&"auto_repeat_song": false, &"stop_after_current": false, &"hide_next_track": false,
&"hide_beginning": true, &"hide_for_seconds": 7.0, &"hide_length": true,
&"hide_ticks": true, &"winning_score": 13, &"inspiration_list_speed": 4,
}
var _snapshot: AutoloadSnapshot
var _config_dir_before: String
var _config_dir: String
func before_test() -> void:
_snapshot = AutoloadSnapshot.take([Settings])
_config_dir_before = OS.get_environment(Settings.CONFIG_DIR_ENV)
_config_dir = create_temp_dir("quiz_settings_file")
OS.set_environment(Settings.CONFIG_DIR_ENV, _config_dir)
DirAccess.remove_absolute(Settings.quiz_settings_path())
func after_test() -> void:
OS.set_environment(Settings.CONFIG_DIR_ENV, _config_dir_before)
_snapshot.restore()
func _set_settings(values: Dictionary) -> void:
for setting: StringName in values:
Settings.set(setting, values[setting])
func test_saved_settings_load_back() -> void:
_set_settings(SAVED_VALUES)
Settings.save_quiz_settings()
_snapshot.restore()
Settings.load_quiz_settings()
for setting: StringName in SAVED_VALUES:
assert_that(Settings.get(setting)).override_failure_message(setting) \
.is_equal(SAVED_VALUES[setting])
func test_every_quiz_setting_is_saved() -> void:
assert_array(Settings.QUIZ_SETTINGS).contains_exactly_in_any_order(SAVED_VALUES.keys())
func test_missing_file_keeps_the_current_settings() -> void:
Settings.winning_score = 9
Settings.load_quiz_settings()
assert_int(Settings.winning_score).is_equal(9)
func test_value_of_the_wrong_type_is_ignored() -> void:
var file: ConfigFile = ConfigFile.new()
file.set_value("quiz", "winning_score", "lots")
file.set_value("quiz", "hide_ticks", true)
DirAccess.make_dir_recursive_absolute(Settings.quiz_settings_path().get_base_dir())
file.save(Settings.quiz_settings_path())
Settings.winning_score = 9
Settings.hide_ticks = false
Settings.load_quiz_settings()
assert_int(Settings.winning_score).is_equal(9)
assert_bool(Settings.hide_ticks).is_true()
func test_statistics_settings_are_not_saved() -> void:
Settings.save_quiz_settings()
var file: ConfigFile = ConfigFile.new()
file.load(Settings.quiz_settings_path())
assert_bool(file.has_section_key("quiz", "add_to_stats")).is_false()
assert_bool(file.has_section_key("quiz", "use_low_played_mode")).is_false()
func test_file_is_in_the_folder_from_the_environment() -> void:
assert_str(Settings.quiz_settings_path().get_base_dir()).is_equal(_config_dir)
func test_file_is_in_the_config_folder_without_the_environment() -> void:
OS.unset_environment(Settings.CONFIG_DIR_ENV)
assert_str(Settings.quiz_settings_path().get_base_dir()) \
.is_equal(OS.get_config_dir().path_join("MusicPlayer"))
func test_debug_builds_use_a_file_of_their_own() -> void:
Settings.is_debug = true
var debug_path: String = Settings.quiz_settings_path()
Settings.is_debug = false
assert_str(debug_path).is_not_equal(Settings.quiz_settings_path())
@@ -0,0 +1 @@
uid://5qjw3px3wve6
+4 -1
View File
@@ -2,7 +2,10 @@ extends SceneTree
## Takes a screenshot of the main window in every UiTour state, against a fake server.
##
## Usage:
## godot --path . -s tools/take_screenshots.gd -- <output dir>
## MUSICPLAYER_CONFIG_DIR=$(mktemp -d) godot --path . -s tools/take_screenshots.gd -- <dir>
##
## Setting [code]MUSICPLAYER_CONFIG_DIR[/code] keeps your saved quiz settings out of the
## pictures.
##
## Writes [code]<state>.png[/code] for each state and [code]layout.json[/code] with where
## every node is in each state. With [code]--headless[/code] only the layout is written.