Show a graph of the game on the Winner popup
Build / test (push) Successful in 38s
Build / build (push) Successful in 1m12s

The Winner popup now shows every player's score after each song as a
step line, with the winning score marked, and three facts under it: the
longest streak, the longest standstill and the song most players scored
on. The data comes from the points each song already records, through
the new ScoreHistory. The popup grows to 1600x920 with the graph across
the full width, so a 60-song game with five players stays readable.

Reset points now starts a new game: it clears the points on the songs
and the graph starts after the song that was playing. Songs already on
the server at startup count as an earlier game.

The screenshot tour has a long five-player game, and only the Winner
popup differs from before.
This commit is contained in:
Sansan committed 2026-10-07 10:40:46 +02:00
1 parent b40b160fde
commit e1e05f3904
21 files changed
+2461 -182

No files matched your search

File diff suppressed because it is too large. Load diff
+73 -1
View File
@@ -47,6 +47,8 @@ const POPUPS: Array[Array] = [
]
var _main: Control
var _scores_before_win: Array[int] = []
var _winning_score_before_win: int = 0
func _init(main: Control) -> void:
@@ -104,6 +106,30 @@ static func soundtrack_statistics() -> Array[Dictionary]:
return statistics
## Who scored on each song of a 60-song game to 20 points, the same on every run: the
## third of [param players] wins on the last song, nobody scores on songs 21–27 and the
## others stay below 20. Needs at least three players.
static func long_game(players: Array[PlayerObject]) -> Array:
var random: RandomNumberGenerator = RandomNumberGenerator.new()
random.seed = 7
var winner: int = 2
var scores: Array[int] = []
scores.resize(players.size())
var song_scorers: Array = []
for song: int in 60:
var scorers: Array[String] = []
for index: int in players.size():
var limit: int = 19 if index == winner else 18
var chance: float = 0.36 if index == winner else 0.2 + 0.02 * index
if song < 59 && (song < 20 || song >= 27) && scores[index] < limit \
&& random.randf() < chance:
scorers.append(players[index].id)
scores[index] += 1
song_scorers.append(scorers)
song_scorers[59].append(players[winner].id)
return song_scorers
## The pictures in [constant CHARACTERS_DIR], sorted by name.
static func character_names() -> Array[String]:
var names: Array[String] = []
@@ -221,6 +247,24 @@ func run(on_state: Callable) -> void:
_main.show_tab(&"quiz")
await _settle()
# Last, as it adds players: a long game with five players on the Winner popup
for player_name: String in ["Lucca", "Frog", "Robo"]:
_add_player(player_name)
await _settle()
var players: Array[PlayerObject] = Settings.player_array
var song_scorers: Array = long_game(players)
var history: ScoreHistory = ScoreHistory.from_songs(players, song_scorers)
for player: PlayerObject in players:
player.player_score = history.final_score(player.id)
Settings.winning_score = 20
var winner_popup: WinnerPopup = _node("WinnerPopup")
winner_popup.show_winner(players[2])
winner_popup.show_history(history)
await _settle()
await on_state.call("popup_WinnerWindow_long_game", winner_popup)
winner_popup.close()
await _settle()
## Returns where every Control and Window in [param subtree] is on screen, keyed by its
## path from [param main]: [code]{"rect": [x, y, width, height], "visible": bool}[/code].
@@ -328,13 +372,41 @@ func _open_popup(popup_name: String) -> Node:
"CharacterSelectPopup":
popup.show_grid("Crono")
"WinnerPopup":
popup.show_winner(Settings.player_array[0])
_show_winner_after_a_game(popup)
_:
popup.open()
return popup
## Shows the Winner popup after a whole game, so the graph has something to show. The
## scores are put back afterwards by [method _close_popup].
func _show_winner_after_a_game(popup: WinnerPopup) -> void:
_scores_before_win = []
for player: PlayerObject in Settings.player_array:
_scores_before_win.append(player.player_score)
var crono: PlayerObject = Settings.player_array[0]
var marle: PlayerObject = Settings.player_array[1]
var song_scorers: Array = []
for song: int in range(1, 17):
var scorers: Array[String] = []
if song in [2, 4, 5, 10, 11, 12, 13, 16]:
scorers.append(crono.id)
if song in [1, 3, 10, 14, 15]:
scorers.append(marle.id)
song_scorers.append(scorers)
crono.player_score = 8
marle.player_score = 5
_winning_score_before_win = Settings.winning_score
Settings.winning_score = 8
popup.show_winner(crono)
popup.show_history(ScoreHistory.from_songs(Settings.player_array, song_scorers))
func _close_popup(popup: Node) -> void:
if popup is WinnerPopup:
Settings.winning_score = _winning_score_before_win
for i: int in _scores_before_win.size():
Settings.player_array[i].player_score = _scores_before_win[i]
if popup is BasePopup:
popup.close()
else:
@@ -39,3 +39,12 @@ func test_failed_song_list_is_retried_before_preloading() -> void:
assert_int(_server.count("/music/rand")).is_equal(1)
assert_int(Playlist.get_preloaded_songs()).is_equal(1)
func test_game_starts_after_the_songs_already_on_the_server() -> void:
_server.release("/music/list")
await _play_first_song()
assert_int(Playlist.game_start_song).is_equal(1)
assert_int(ScoreHistory.from_playlist(Settings.player_array).songs.size()).is_equal(1)
+15 -3
View File
@@ -36,6 +36,15 @@ const STYLES: Dictionary = {
"TabButton": "Button",
}
## Theme types that only scripts read from, so they are not styles a node can pick.
const CODE_ONLY_TYPES: Dictionary = {
"Palette": ["backdrop"],
"ScoreGraph": [
"player_1", "player_2", "player_3", "player_4", "player_5", "player_6", "player_7",
"grid", "axis_text", "winning_line",
],
}
var _theme: Theme = load(THEME_PATH)
@@ -79,8 +88,11 @@ func test_theme_has_every_named_style() -> void:
.is_equal(STYLES[style])
func test_theme_has_the_palette() -> void:
assert_bool(_theme.has_color("backdrop", "Palette")).is_true()
func test_theme_has_the_colours_scripts_use() -> void:
for type: String in CODE_ONLY_TYPES:
for color: String in CODE_ONLY_TYPES[type]:
assert_bool(_theme.has_color(color, type)) \
.override_failure_message("%s/%s is missing" % [type, color]).is_true()
func test_every_style_named_in_scenes_and_scripts_exists() -> void:
@@ -89,7 +101,7 @@ func test_every_style_named_in_scenes_and_scripts_exists() -> void:
for path: String in _game_files(".tscn") + _game_files(".gd"):
for found: RegExMatch in regex.search_all(FileAccess.get_file_as_string(path)):
var style: String = found.get_string(1)
if !(style in STYLES) && style != "Palette":
if !(style in STYLES) && !(style in CODE_ONLY_TYPES):
missing.append("%s: %s" % [path, style])
assert_array(missing).override_failure_message(
"Styles not in the theme:\n" + "\n".join(missing)).is_empty()
+160
View File
@@ -0,0 +1,160 @@
extends MainWindowTestSuite
## The Winner popup's graph of the game and the facts under it, built from the points
## given with the keybinds, and Reset points starting a new game.
var _crono: PlayerObject
var _marle: PlayerObject
var _popup: WinnerPopup
func before_test() -> void:
super()
_crono = _add_player("Crono")
_marle = _add_player("Marle")
_popup = _quiz.get_node("WinnerPopup")
await _play_first_song()
Settings.winning_score = 3
## Moves on to a new song, like Next does, without waiting for a download. Songs the
## game downloaded ahead are left unplayed.
func _next_song() -> void:
var song: SongObject = SongObject.new()
song.song_number = Playlist.song_object_array.size()
Playlist.add_to_playlist(song)
Playlist.set_currently_playing_song(song.song_number)
Playlist.song_has_played(song.song_number)
func _legend_texts() -> Array[String]:
var texts: Array[String] = []
for entry: HBoxContainer in _popup.legend.get_children():
texts.append((entry.get_child(1) as Label).text)
return texts
# Song 1: Crono. Song 2: nobody. Song 3: Crono and Marle. Song 4: Crono wins.
func _play_a_game() -> void:
await _give_point_with_keybind(1)
_next_song()
_next_song()
await _give_point_with_keybind(1)
await _give_point_with_keybind(2)
_next_song()
await _give_point_with_keybind(1)
func test_winning_shows_how_the_game_went() -> void:
await _play_a_game()
assert_bool(_popup.visible).is_true()
var history: ScoreHistory = _popup.score_graph._history
assert_int(history.songs.size()).is_equal(4)
assert_array(history.scores_after_songs(_crono.id)).contains_exactly([0, 1, 1, 2, 3])
assert_array(history.scores_after_songs(_marle.id)).contains_exactly([0, 0, 0, 1, 1])
assert_array(_legend_texts()).contains_exactly(["Crono 3", "Marle 1"])
assert_str(_popup.streak_label.text).is_equal("Crono, 2 songs in a row (3–4)")
assert_str(_popup.standstill_label.text).is_equal("Song 2, nobody scored")
assert_str(_popup.shared_label.text).is_equal("Song 3, Crono and Marle")
func test_graph_draws_a_line_per_player_up_to_their_score() -> void:
await _play_a_game()
var graph: ScoreGraph = _popup.score_graph
for index: int in 2:
var points: PackedVector2Array = graph.line_points(index)
# Every line starts at song 0 with no points and ends at the last song
assert_float(points[0].x).is_less(points[points.size() - 1].x)
assert_float(points[0].y).is_greater(points[points.size() - 1].y)
# Crono's line ends higher than Marle's
var crono_end: Vector2 = graph.line_points(0)[graph.line_points(0).size() - 1]
var marle_end: Vector2 = graph.line_points(1)[graph.line_points(1).size() - 1]
assert_float(crono_end.y).is_less(marle_end.y)
assert_object(graph.player_color(0)).is_not_equal(graph.player_color(1))
func test_point_taken_back_is_not_in_the_graph() -> void:
await _give_point_with_keybind(2)
# The -1 button; after Ctrl+Alt the test runner keeps Ctrl held for later keys
_player_row(_marle).minus_button.pressed.emit()
await _give_point_with_keybind(1)
await _give_point_with_keybind(1)
await _give_point_with_keybind(1)
assert_array(_popup.score_graph._history.songs[0]).contains_exactly([_crono.id])
func test_reset_points_starts_a_new_game() -> void:
await _play_a_game()
_popup.close()
_press("QuizToolbar/ResetPointsButton")
for song: int in 3:
_next_song()
await _give_point_with_keybind(2)
assert_bool(_popup.visible).is_true()
var history: ScoreHistory = _popup.score_graph._history
assert_int(history.songs.size()).is_equal(3)
assert_array(_legend_texts()).contains_exactly(["Crono 0", "Marle 3"])
assert_str(_popup.streak_label.text).is_equal("Marle, 3 songs in a row (1–3)")
func test_reset_points_removes_the_points_from_the_songs() -> void:
await _play_a_game()
_popup.close()
_press("QuizToolbar/ResetPointsButton")
await _runner.simulate_frames(1)
for song: SongObject in Playlist.song_object_array:
assert_array(song.players_given_point).is_empty()
for row: SongRow in _quiz.music_list_panel._rows.get_children():
assert_int(row.song_list_players.get_child_count()).is_equal(0)
assert_int(Playlist.game_start_song).is_equal(Playlist.currently_playing_song + 1)
func test_long_game_with_five_players_stays_readable() -> void:
for player_name: String in ["Lucca", "Frog", "Robo"]:
_add_player(player_name)
Settings.winning_score = 20
var players: Array[PlayerObject] = Settings.player_array
var history: ScoreHistory = ScoreHistory.from_songs(players, UiTour.long_game(players))
for player: PlayerObject in players:
player.player_score = history.final_score(player.id)
assert_int(players[2].player_score).is_equal(20)
_popup.show_winner(players[2])
_popup.show_history(history)
await _runner.simulate_frames(2)
var graph: ScoreGraph = _popup.score_graph
assert_float(graph.size.x).override_failure_message("The graph is not full width") \
.is_greater(1400.0)
# Song numbers along the bottom are at least a label's width apart
var songs: Array[int] = graph.labelled_songs()
assert_int(songs.front()).is_equal(1)
assert_int(songs.back()).is_equal(60)
for i: int in range(1, songs.size()):
assert_float(graph.song_x(songs[i]) - graph.song_x(songs[i - 1])).is_greater_equal(40.0)
# Scores at the end of the lines do not overlap and stay inside the graph
var font_size: int = graph.get_theme_font_size(&"font_size", ScoreGraph.THEME_TYPE)
var label_ys: Array[float] = []
for position: Vector2 in graph.score_label_positions():
assert_bool(Rect2(Vector2.ZERO, graph.size).has_point(position)).is_true()
label_ys.append(position.y)
label_ys.sort()
for i: int in range(1, label_ys.size()):
assert_float(label_ys[i] - label_ys[i - 1]).is_greater_equal(font_size)
# Every line stays inside the graph
for index: int in players.size():
for point: Vector2 in graph.line_points(index):
assert_bool(Rect2(Vector2.ZERO, graph.size).has_point(point)).is_true()
# The facts fit on one line each
for label: Label in [_popup.streak_label, _popup.standstill_label, _popup.shared_label]:
assert_int(label.get_line_count()).override_failure_message(label.text + " wraps") \
.is_equal(1)
# The whole popup fits in the window
assert_bool(Rect2(Vector2.ZERO, Vector2(1920, 1080)).encloses(
_popup.winner_panel.get_global_rect())).is_true()
@@ -0,0 +1 @@
uid://fxnodtwhtyo6
+20
View File
@@ -161,6 +161,26 @@ func test_remove_player_removes_only_that_players_points() -> void:
assert_array(songs[1].players_given_point).contains_exactly(["crono-id"])
func test_clear_points_empties_every_song() -> void:
var songs: Array[SongObject] = _add_songs(2)
songs[0].players_given_point.assign(["crono-id", "marle-id"])
songs[1].players_given_point.assign(["crono-id"])
Playlist.clear_points()
assert_array(songs[0].players_given_point).is_empty()
assert_array(songs[1].players_given_point).is_empty()
func test_reset_playlist_starts_a_new_game() -> void:
_add_songs(3)
Playlist.game_start_song = 2
Playlist.reset_playlist()
assert_int(Playlist.game_start_song).is_equal(0)
func test_reset_playlist_clears_songs_and_position() -> void:
_add_songs(3)
Playlist.latest_played_song = 2
+164
View File
@@ -0,0 +1,164 @@
extends GdUnitTestSuite
## ScoreHistory: the scores after each song of a game and the facts shown under the
## graph on the Winner popup.
var _crono: PlayerObject
var _marle: PlayerObject
var _lucca: PlayerObject
var _players: Array[PlayerObject] = []
var _snapshot: AutoloadSnapshot
func before_test() -> void:
_snapshot = AutoloadSnapshot.take([Playlist])
Playlist.reset_playlist()
_crono = PlayerObject.new("Crono")
_marle = PlayerObject.new("Marle")
_lucca = PlayerObject.new("Lucca")
_players = [_crono, _marle, _lucca]
func after_test() -> void:
Playlist.reset_playlist()
_snapshot.restore()
## A history where song n (counting from 1) was scored on by the players in
## [param scorers][n - 1].
func _history(scorers: Array) -> ScoreHistory:
var song_scorers: Array = []
for players: Array in scorers:
song_scorers.append(players.map(func(player: PlayerObject) -> String: return player.id))
return ScoreHistory.from_songs(_players, song_scorers)
func _add_songs(scorers: Array) -> void:
for players: Array in scorers:
var song: SongObject = SongObject.new()
for player: PlayerObject in players:
song.add_point(player.id)
Playlist.add_to_playlist(song)
func test_scores_after_each_song() -> void:
var history: ScoreHistory = _history([[_crono], [], [_crono, _marle], [_marle]])
assert_array(history.scores_after_songs(_crono.id)).contains_exactly([0, 1, 1, 2, 2])
assert_array(history.scores_after_songs(_marle.id)).contains_exactly([0, 0, 0, 1, 2])
assert_array(history.scores_after_songs(_lucca.id)).contains_exactly([0, 0, 0, 0, 0])
assert_int(history.final_score(_crono.id)).is_equal(2)
func test_from_playlist_takes_the_songs_of_the_current_game() -> void:
_add_songs([[_lucca], [_crono], [_marle], [_crono], [_lucca]])
for song_no: int in 4:
Playlist.song_object_array[song_no].has_played = true
Playlist.game_start_song = 1
Playlist.currently_playing_song = 3
var history: ScoreHistory = ScoreHistory.from_playlist(_players)
# Song 0 was the last game, song 4 is not played yet
assert_int(history.songs.size()).is_equal(3)
assert_array(history.scores_after_songs(_crono.id)).contains_exactly([0, 1, 1, 2])
assert_int(history.final_score(_lucca.id)).is_equal(0)
func test_from_playlist_leaves_out_songs_not_played() -> void:
_add_songs([[_crono], [_marle], [_crono], [_marle]])
Playlist.song_object_array[0].has_played = true
Playlist.song_object_array[2].has_played = true
Playlist.currently_playing_song = 3
var history: ScoreHistory = ScoreHistory.from_playlist(_players)
assert_int(history.songs.size()).is_equal(3)
assert_array(history.scores_after_songs(_marle.id)).contains_exactly([0, 0, 0, 1])
func test_from_playlist_goes_on_to_the_last_played_song() -> void:
_add_songs([[_crono], [_marle], [_crono]])
for song: SongObject in Playlist.song_object_array:
song.has_played = true
# An older song picked in the music list
Playlist.currently_playing_song = 0
var history: ScoreHistory = ScoreHistory.from_playlist(_players)
assert_int(history.songs.size()).is_equal(3)
func test_from_playlist_before_any_song() -> void:
_add_songs([[], []])
var history: ScoreHistory = ScoreHistory.from_playlist(_players)
assert_array(history.songs).is_empty()
assert_str(history.longest_streak_text()).is_equal("No songs played")
assert_str(history.longest_standstill_text()).is_equal("No songs played")
assert_str(history.most_shared_text()).is_equal("No songs played")
func test_points_of_removed_players_are_left_out() -> void:
var history: ScoreHistory = ScoreHistory.from_songs([_crono],
[[_crono.id, "removed-player"], ["removed-player"]])
assert_array(history.songs[0]).contains_exactly([_crono.id])
assert_array(history.songs[1]).is_empty()
func test_longest_streak() -> void:
var history: ScoreHistory = _history(
[[_marle], [_marle], [], [_crono], [_crono], [_crono], [_marle]])
assert_str(history.longest_streak_text()).is_equal("Crono, 3 songs in a row (4–6)")
func test_longest_streak_tie_goes_to_the_earliest() -> void:
var history: ScoreHistory = _history([[_crono], [_crono], [_marle], [_marle]])
assert_str(history.longest_streak_text()).is_equal("Crono, 2 songs in a row (1–2)")
history = _history([[_marle], [_marle], [_crono], [_crono]])
assert_str(history.longest_streak_text()).is_equal("Marle, 2 songs in a row (1–2)")
func test_no_streak() -> void:
var history: ScoreHistory = _history([[_crono], [_marle], [_crono], []])
assert_str(history.longest_streak_text()).is_equal("Nobody scored twice in a row")
func test_longest_standstill() -> void:
var history: ScoreHistory = _history([[_crono], [], [], [_marle], [], [], [], [_crono]])
assert_str(history.longest_standstill_text()).is_equal("Songs 5–7, nobody scored")
func test_standstill_of_one_song() -> void:
var history: ScoreHistory = _history([[_crono], [], [_marle]])
assert_str(history.longest_standstill_text()).is_equal("Song 2, nobody scored")
func test_no_standstill() -> void:
var history: ScoreHistory = _history([[_crono], [_marle]])
assert_str(history.longest_standstill_text()).is_equal("Someone scored on every song")
func test_most_shared_song_names_two_players_in_player_order() -> void:
var history: ScoreHistory = _history([[_crono], [_lucca, _marle], [_crono, _lucca]])
assert_str(history.most_shared_text()).is_equal("Song 2, Marle and Lucca")
func test_most_shared_song_counts_more_than_two_players() -> void:
var history: ScoreHistory = _history([[_crono, _marle], [_crono, _marle, _lucca]])
assert_str(history.most_shared_text()).is_equal("Song 2, 3 players")
func test_no_shared_song() -> void:
var history: ScoreHistory = _history([[_crono], [_marle], []])
assert_str(history.most_shared_text()).is_equal("No song had two scorers")
+1
View File
@@ -0,0 +1 @@
uid://c6xby73bvflq6