From afc1a847f4e93c9fb0ec718286089d409030c95a Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Mon, 13 Apr 2020 15:20:01 -0500 Subject: Update documentation and make setScore idempotent --- src/highscores.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/highscores.js') diff --git a/src/highscores.js b/src/highscores.js index e735eee..82db612 100644 --- a/src/highscores.js +++ b/src/highscores.js @@ -1,10 +1,19 @@ function submitScore(game, score, username){ + // Prompt for the username if(username == undefined){ username = prompt("Enter a name to submit score", ""); } + // If the user filled out the prompt, post the score if (!(username == null || username == "")) { - const request = new Request(`/setScore?game=${game}&username=${username}&score=${score}`); - fetch(request); + // Get a uuid to identify this request by (for idempotency) + fetch(new Request(`/uuid`)) + .then(response => response.text() + .then(uuid => { + const request = new Request(`/setScore?game=${game}&username=${username}&score=${score}&uuid=${uuid}`); + fetch(request); + })); + } + // Return the username for later use by the game return username } \ No newline at end of file -- cgit v1.2.3