added word shuffle functionality

This commit is contained in:
tom.hempel
2025-09-18 10:26:33 +02:00
parent 6a6b2b9936
commit e6d73a2bcb
3 changed files with 48 additions and 12 deletions

View File

@ -79,6 +79,9 @@ class Word(BaseModel):
timeSeconds: float
word: str
class WordList(BaseModel):
words: list[str]
@app.post("/word")
def read_word(word: Word):
msg = f"CHARADE:{word.lastWordStatus};{word.timeSeconds};{word.word}"
@ -86,6 +89,13 @@ def read_word(word: Word):
sock.sendto(msg.encode('utf-8'), (word.target, 5000))
return { "status": "ok" }
@app.post("/shuffle")
def shuffle_words(word_list: WordList):
import random
shuffled = word_list.words.copy()
random.shuffle(shuffled)
return { "status": "ok", "shuffled_words": shuffled }
# SSE endpoint
@app.get("/news")

View File

@ -299,6 +299,7 @@
<br>
<div class="button-container">
<button type="button" id="button-shuffle-words">Shuffle</button>
<button type="button" id="button-create-word-items">Modify</button>
</div>
</div>
@ -441,6 +442,40 @@
window.open('data:text/csv;charset=utf-8,' + escape(data), '_blank');
});
document.getElementById("button-shuffle-words").addEventListener("click", async () => {
const wordList = document.getElementById("word-list");
const text = wordList.value.trim();
if (!text) {
alert("Please enter words to shuffle first.");
return;
}
// Don't allow shuffle if word list is already running
if (frameId !== undefined) {
alert("Cannot shuffle while word list is running. Please stop first.");
return;
}
const words = text.split('\n').map(word => word.trim()).filter(word => word);
try {
const response = await fetch("/shuffle", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ words: words }),
});
const result = await response.json();
if (result.status === "ok") {
wordList.value = result.shuffled_words.join('\n');
}
} catch (error) {
console.error("Error shuffling words:", error);
alert("Failed to shuffle words. Please try again.");
}
});
document.getElementById("button-create-word-items").addEventListener("click", () => {
createWordItems();
if (frameId) {