Add randomness to next phrase selection, save only last played phrase order, no timestamps

This commit is contained in:
ChaoticByte 2025-08-07 13:12:17 +02:00
parent 5d373ca74e
commit 0f6d4c6c75
No known key found for this signature in database
3 changed files with 25 additions and 25 deletions

View file

@ -14,15 +14,13 @@ var _last_autocleanup: int = 0 # ms since engine start
var current_phrase: String = "" var current_phrase: String = ""
var current_status: 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(): func register_current_phrase_played():
var t = Time.get_unix_time_from_system()
if current_phrase in last_played_phrases: if current_phrase in last_played_phrases:
last_played_phrases[current_phrase].append(t) last_played_phrases.erase(current_phrase)
else: last_played_phrases.append(current_phrase)
last_played_phrases[current_phrase] = [t]
SaveManager.save_game() SaveManager.save_game()
func answer(p_in: String) -> bool: func answer(p_in: String) -> bool:
@ -38,30 +36,32 @@ func next_phrase() -> void:
current_phrase = "" current_phrase = ""
current_status = STATUS_NONE_AVAILABLE current_status = STATUS_NONE_AVAILABLE
return return
# search for a non played phrase # pick a random non-played phrase, if possible
var phrases_not_played = []
for p in PhrasesManager.phrases: for p in PhrasesManager.phrases:
if not p in last_played_phrases: if not p in last_played_phrases:
current_phrase = p phrases_not_played.append(p)
if len(phrases_not_played) > 0:
current_phrase = phrases_not_played.pick_random()
current_status = STATUS_PLEASE_REPEAT current_status = STATUS_PLEASE_REPEAT
return return
# find the phrase that was played the longest ago # find the half of phrases that were repeated longest ago
var phrases_last_ts: Dictionary = {} # timestamp: phrase var i_max = max(1, len(last_played_phrases) / 2)
var phrases_last_ts_keys: Array[float] = [] var phrases_played_longest_ago = last_played_phrases.slice(0, i_max)
for p in last_played_phrases: # pick random phrase
if p in PhrasesManager.phrases: var phrase = phrases_played_longest_ago.pick_random()
var t_max = last_played_phrases[p].max() if phrase == null: # this shouldn't happen!
phrases_last_ts[t_max] = p current_phrase = ""
phrases_last_ts_keys.append(t_max) current_status = STATUS_NONE_AVAILABLE
# return the phrase with the smallest timestamp (-> longest ago) else:
current_phrase = phrases_last_ts[phrases_last_ts_keys.min()] current_phrase = phrase
current_status = STATUS_PLEASE_REPEAT current_status = STATUS_PLEASE_REPEAT
func try_overwrite_next_phrase(p: String): func try_overwrite_next_phrase(p: String):
if p in PhrasesManager.phrases: if p in PhrasesManager.phrases:
current_phrase = p current_phrase = p
current_status = STATUS_PLEASE_REPEAT current_status = STATUS_PLEASE_REPEAT
else: else: # if this didn't work, choose one automatically
# if this didn't work, choose one automatically
next_phrase() next_phrase()
func cleanup_last_played_phrases(): func cleanup_last_played_phrases():

View file

@ -25,7 +25,7 @@ func _from_dict(data: Dictionary) -> int:
for p in data["phrases"]: for p in data["phrases"]:
PhrasesManager.phrases.append(p) PhrasesManager.phrases.append(p)
successfully_set += 1 #! 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"] CoreGameplayManager.last_played_phrases = data["last_played_phrases"]
successfully_set += 1 #! successfully_set += 1 #!
return successfully_set return successfully_set

View file

@ -8,7 +8,7 @@ func _on_close_settings_button_pressed() -> void:
func _on_reset_xp_and_stats_button_pressed() -> void: func _on_reset_xp_and_stats_button_pressed() -> void:
XpLevelManager.player_xp = 0 XpLevelManager.player_xp = 0
CoreGameplayManager.last_played_phrases = {} CoreGameplayManager.last_played_phrases = []
SaveManager.save_game() SaveManager.save_game()
CoreGameplayManager.next_phrase() CoreGameplayManager.next_phrase()
NotificationQueue.add("Reset XP & Stats.") NotificationQueue.add("Reset XP & Stats.")