From 0f6d4c6c7522684816f02c06a81beccbaf371b5c Mon Sep 17 00:00:00 2001 From: ChaoticByte Date: Thu, 7 Aug 2025 13:12:17 +0200 Subject: [PATCH] Add randomness to next phrase selection, save only last played phrase order, no timestamps --- src/global/CoreGameplayManager.gd | 46 +++++++++++++++---------------- src/global/SaveManager.gd | 2 +- src/ui/settings_menu.gd | 2 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/global/CoreGameplayManager.gd b/src/global/CoreGameplayManager.gd index 3a79223..b96a0ce 100644 --- a/src/global/CoreGameplayManager.gd +++ b/src/global/CoreGameplayManager.gd @@ -14,15 +14,13 @@ var _last_autocleanup: int = 0 # ms since engine start var current_phrase: String = "" var current_status: String = "" -var last_played_phrases: Dictionary = {} # p: [timestamp1, timestamp2, ...] - +# represents the order of last played phrases +var last_played_phrases: Array = [] func register_current_phrase_played(): - var t = Time.get_unix_time_from_system() if current_phrase in last_played_phrases: - last_played_phrases[current_phrase].append(t) - else: - last_played_phrases[current_phrase] = [t] + last_played_phrases.erase(current_phrase) + last_played_phrases.append(current_phrase) SaveManager.save_game() func answer(p_in: String) -> bool: @@ -38,30 +36,32 @@ func next_phrase() -> void: current_phrase = "" current_status = STATUS_NONE_AVAILABLE return - # search for a non played phrase + # pick a random non-played phrase, if possible + var phrases_not_played = [] for p in PhrasesManager.phrases: if not p in last_played_phrases: - current_phrase = p - current_status = STATUS_PLEASE_REPEAT - return - # find the phrase that was played the longest ago - var phrases_last_ts: Dictionary = {} # timestamp: phrase - var phrases_last_ts_keys: Array[float] = [] - for p in last_played_phrases: - if p in PhrasesManager.phrases: - var t_max = last_played_phrases[p].max() - phrases_last_ts[t_max] = p - phrases_last_ts_keys.append(t_max) - # return the phrase with the smallest timestamp (-> longest ago) - current_phrase = phrases_last_ts[phrases_last_ts_keys.min()] - current_status = STATUS_PLEASE_REPEAT + phrases_not_played.append(p) + if len(phrases_not_played) > 0: + current_phrase = phrases_not_played.pick_random() + current_status = STATUS_PLEASE_REPEAT + return + # find the half of phrases that were repeated longest ago + var i_max = max(1, len(last_played_phrases) / 2) + var phrases_played_longest_ago = last_played_phrases.slice(0, i_max) + # pick random phrase + var phrase = phrases_played_longest_ago.pick_random() + if phrase == null: # this shouldn't happen! + current_phrase = "" + current_status = STATUS_NONE_AVAILABLE + else: + current_phrase = phrase + current_status = STATUS_PLEASE_REPEAT func try_overwrite_next_phrase(p: String): if p in PhrasesManager.phrases: current_phrase = p current_status = STATUS_PLEASE_REPEAT - else: - # if this didn't work, choose one automatically + else: # if this didn't work, choose one automatically next_phrase() func cleanup_last_played_phrases(): diff --git a/src/global/SaveManager.gd b/src/global/SaveManager.gd index e32a652..1a9358b 100644 --- a/src/global/SaveManager.gd +++ b/src/global/SaveManager.gd @@ -25,7 +25,7 @@ func _from_dict(data: Dictionary) -> int: for p in data["phrases"]: PhrasesManager.phrases.append(p) successfully_set += 1 #! - if "last_played_phrases" in data and data["last_played_phrases"] is Dictionary: + if "last_played_phrases" in data and data["last_played_phrases"] is Array: CoreGameplayManager.last_played_phrases = data["last_played_phrases"] successfully_set += 1 #! return successfully_set diff --git a/src/ui/settings_menu.gd b/src/ui/settings_menu.gd index 0b7789f..91810f3 100644 --- a/src/ui/settings_menu.gd +++ b/src/ui/settings_menu.gd @@ -8,7 +8,7 @@ func _on_close_settings_button_pressed() -> void: func _on_reset_xp_and_stats_button_pressed() -> void: XpLevelManager.player_xp = 0 - CoreGameplayManager.last_played_phrases = {} + CoreGameplayManager.last_played_phrases = [] SaveManager.save_game() CoreGameplayManager.next_phrase() NotificationQueue.add("Reset XP & Stats.")