feat: Implement shuffle without repetition for fortune selection
- Added Fisher-Yates shuffle with pointer mechanism - Guarantees no repeats until all fortunes have been shown - Global variables: _shuffled_fortunes, _shuffle_index Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
+13
-3
@@ -52,6 +52,8 @@ AVAILABLE_FORTUNE_LISTS = [
|
|||||||
_current_fortune_list = "fortune"
|
_current_fortune_list = "fortune"
|
||||||
_current_fortune = ""
|
_current_fortune = ""
|
||||||
_fortunes = {}
|
_fortunes = {}
|
||||||
|
_shuffled_fortunes = []
|
||||||
|
_shuffle_index = 0
|
||||||
_initialized = False
|
_initialized = False
|
||||||
|
|
||||||
|
|
||||||
@@ -112,8 +114,8 @@ def _load_all_fortune_lists():
|
|||||||
|
|
||||||
|
|
||||||
def _get_random_fortune():
|
def _get_random_fortune():
|
||||||
"""Gibt einen zufaelligen Spruch aus der aktuellen Liste zurueck."""
|
"""Gibt einen zufaelligen Spruch aus der aktuellen Liste zurueck (Shuffle ohne Wiederholung)."""
|
||||||
global _current_fortune_list, _fortunes
|
global _current_fortune_list, _fortunes, _shuffled_fortunes, _shuffle_index
|
||||||
|
|
||||||
_init()
|
_init()
|
||||||
|
|
||||||
@@ -123,7 +125,15 @@ def _get_random_fortune():
|
|||||||
if not _fortunes.get("fortune"):
|
if not _fortunes.get("fortune"):
|
||||||
return "Keine Sprueche verfguebar."
|
return "Keine Sprueche verfguebar."
|
||||||
|
|
||||||
return random.choice(_fortunes[_current_fortune_list])
|
# Neu shufflen wenn Liste leer oder am Ende
|
||||||
|
if not _shuffled_fortunes or _shuffle_index >= len(_shuffled_fortunes):
|
||||||
|
_shuffled_fortunes = _fortunes[_current_fortune_list].copy()
|
||||||
|
random.shuffle(_shuffled_fortunes)
|
||||||
|
_shuffle_index = 0
|
||||||
|
|
||||||
|
fortune = _shuffled_fortunes[_shuffle_index]
|
||||||
|
_shuffle_index += 1
|
||||||
|
return fortune
|
||||||
|
|
||||||
|
|
||||||
def get_initial_fortune():
|
def get_initial_fortune():
|
||||||
|
|||||||
Reference in New Issue
Block a user