From 8fd16a6e42dd3f72d5adfadbb025a9fa563f7a5a Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Mon, 4 Nov 2024 21:02:12 +0100 Subject: [PATCH] Hooking up the API --- src/alfagok/main.py | 8 +-- src/alfagok/static/copy.js | 6 +-- src/alfagok/static/game.css | 8 +++ src/alfagok/static/game.js | 87 ++++++++++++++++++++++++++++++++ src/alfagok/templates/index.html | 23 +++++---- 5 files changed, 115 insertions(+), 17 deletions(-) diff --git a/src/alfagok/main.py b/src/alfagok/main.py index 1697bc0..9f94459 100644 --- a/src/alfagok/main.py +++ b/src/alfagok/main.py @@ -75,7 +75,7 @@ def what_game(): return {'game': get_game_id()} -@app.get('/api/guess') +@app.get('/api/guess/{word}') def handle_guess(word: Union[str, None] = None): """Handle incoming guess.""" current_game_id = get_game_id() @@ -97,8 +97,8 @@ def handle_guess(word: Union[str, None] = None): return {'game': current_game_id, 'hint': hint} -@app.get("/api/answer/{item_id}") +@app.get('/api/answer/{item_id}') def read_item(item_id: int, guess: Union[str, None] = None): - """Get the word item.""" + """Get the word for the current game.""" word = words[item_id].strip() - return {"item_id": item_id, "guess": guess, 'word': word} + return {'item_id': item_id, 'guess': guess, 'word': word} diff --git a/src/alfagok/static/copy.js b/src/alfagok/static/copy.js index e226563..ec5e4d8 100644 --- a/src/alfagok/static/copy.js +++ b/src/alfagok/static/copy.js @@ -1,10 +1,10 @@ var clip = new Clipboard('.copy'); clip.on("success", function(e) { - document.getElementById('copyresults').innerHTML = '

Copied! Share your results with friends.

'; + document.getElementById('copyresults').innerHTML = '

Gekopieerd! Deel je resultaat.

'; e.clearSelection(); }); clip.on("error", function() { - document.getElementById('copyresults').innerHTML = '

Error. Please copy manually...

'; -}); \ No newline at end of file + document.getElementById('copyresults').innerHTML = '

Fout. Graag handmatig kopiëren...

'; +}); diff --git a/src/alfagok/static/game.css b/src/alfagok/static/game.css index fa5adb9..a1175f0 100644 --- a/src/alfagok/static/game.css +++ b/src/alfagok/static/game.css @@ -2,3 +2,11 @@ body { background-color: #333; color: #FFF; } + +button { + background-color: #FF9800; + color: #333; + + border: 0; + padding: .2rem; +} diff --git a/src/alfagok/static/game.js b/src/alfagok/static/game.js index e69de29..a109503 100644 --- a/src/alfagok/static/game.js +++ b/src/alfagok/static/game.js @@ -0,0 +1,87 @@ +/* API calls **/ +async function getGameID() { + let response = await fetch('/api/game'); + let result = await response.json(); + console.log(result); + if (result.game) { + return result.game; + } +} + +async function doGuess(guessWord, guessError) { + let response = await fetch('/api/guess/' + guessWord); + let result = await response.json(); + console.log(result); + if (result.error) { + console.log('Error occurred during guess'); + if (result.error === 'Word not in dictionary') { + guessError = 'Woord komt niet in de woordenlijst voor'; + } + console.log(guessError); + } + return result; +} + +/* Time formatting **/ + +function getFormattedTime(milliseconds) { + if (!Number.isInteger(milliseconds)) { + return ''; + } + let seconds = Math.round((milliseconds) / 1000); + const hours = Math.floor(seconds / 3600); + seconds %= 3600; + const minutes = Math.floor(seconds / 60); + seconds %= 60; + + const formattedTime = []; + if (hours) { + formattedTime.push(`${hours}h`); + } + if (minutes) { + formattedTime.push(`${minutes}m`); + } + if (seconds) { + formattedTime.push(`${seconds}s`); + } + + return formattedTime.join(' ') || '0s'; +} + +/* Clipboard stuff **/ + +var clip = new Clipboard('.copy'); + +clip.on("success", function(e) { + document.getElementById('copyresults').innerHTML = '

Gekopieerd! Deel je resultaat.

'; + e.clearSelection(); +}); + +clip.on("error", function() { + document.getElementById('copyresults').innerHTML = '

Fout. Graag handmatig kopiëren...

'; +}); + + +/* Game timer, original from alphaguess.com **/ + +function go() { + window.timerID = window.setInterval(timer, 0); +} + +function timer(){ + var nextgame = document.getElementById('nextgame'); + var now = new Date(); + var midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1, 0, 0, 0); + var diff = Math.floor((midnight - now)/1000); + var hoursRemain = Math.floor(diff/(60*60)); + var minutesRemain = Math.floor((diff-hoursRemain*60*60)/60); + var secondsRemain = Math.floor(diff%60); + nextgame.innerHTML = ''+addZero(hoursRemain)+':'+addZero(minutesRemain)+':'+addZero(secondsRemain)+' over'; +} + +function addZero(num){ + if(num <=9) return '0'+num; + else return num; +} + +go(); diff --git a/src/alfagok/templates/index.html b/src/alfagok/templates/index.html index 61d7764..2c32be9 100644 --- a/src/alfagok/templates/index.html +++ b/src/alfagok/templates/index.html @@ -5,6 +5,7 @@ + @@ -26,6 +27,8 @@ guessValue: '', gameID: 0, + guessError: '', + get filteredItems() { return this.items.filter( i => i.startsWith(this.search) @@ -34,8 +37,8 @@ }" > - alfagok puzzle #[[ gameID ]] • -

+ alfagok puzzle # +

Raad het woord van de dag. Elke gok geeft een hint over waar het woord zich in het alfabet bevindt.

@@ -51,9 +54,10 @@ - +

Je huidige gok is:

- + +