From 8edbbb4347a4c7d515efd205b9050fd447c7fc41 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Sun, 21 Jan 2024 12:12:47 +0100 Subject: [PATCH] Add Single-Calc for player name. Refactor admin+single-calc dialogs. --- controllers/cache_controller.go | 20 +++++ main.go | 1 + static/dialogs.js | 100 ++++++++++++++++++++++ static/index.js | 36 -------- templates/components/bottom_controls.html | 21 +++-- templates/components/header.html | 1 + 6 files changed, 132 insertions(+), 47 deletions(-) create mode 100644 static/dialogs.js diff --git a/controllers/cache_controller.go b/controllers/cache_controller.go index b140f60..1fffd34 100644 --- a/controllers/cache_controller.go +++ b/controllers/cache_controller.go @@ -123,3 +123,23 @@ func GetScoreByPlayerID(c *gin.Context) { c.JSON(http.StatusBadRequest, "Invalid request!") } } + +// GetScoreByPlayerName POST /score/:player_name +func GetScoreByPlayerName(c *gin.Context) { + playerName := c.Param("player_name") + game, err := GetActiveGame(c) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"}) + return + } + + score := utils.CalcPlayerScore(playerName, game.Tag) + + if score != score || score == -1 { // NaN + c.String(http.StatusNotFound, "Spieler nicht gefunden!") + } else if score != -1 { + c.String(http.StatusOK, fmt.Sprintf("%.2f", score)) + } else { + c.String(http.StatusBadRequest, "Ungültige Abfrage!") + } +} diff --git a/main.go b/main.go index 0347382..02e275f 100644 --- a/main.go +++ b/main.go @@ -126,6 +126,7 @@ func main() { protected.GET("/cache/:player_id", controllers.GetCacheByPlayerID) protected.GET("/score/:player_id", controllers.GetScoreByPlayerID) + protected.POST("/score/:player_name", controllers.GetScoreByPlayerName) protected.GET("/game", controllers.GetGames) protected.GET("/game_html", controllers.GetGamesHTML) diff --git a/static/dialogs.js b/static/dialogs.js new file mode 100644 index 0000000..12551aa --- /dev/null +++ b/static/dialogs.js @@ -0,0 +1,100 @@ +const swalClasses = { + container: 'text-center', + confirmButton: 'btn btn-lg btn-primary', + cancelButton: 'btn btn-lg btn-secondary ms-3', + popup: 'border p-5', + title: 'fs-2', + inputLabel: 'fs-5', + htmlContainer: 'fs-5' +}; + +function showSingleCalcDialog(btn) { + Swal.fire({ + title: 'Einzel-Abfrage', + input: 'text', + inputLabel: 'Welcher Spieler soll abgefragt werden?\nDie Abfrage kann ein paar Sekunden dauern.', + inputPlaceholder: 'Spieler-Name eingeben...', + confirmButtonText: 'Berechnen', + showCancelButton: true, + cancelButtonText: 'Abbrechen', + customClass: swalClasses, + buttonsStyling: false + }).then((result) => { + if (result.isConfirmed) { + htmx.ajax('POST', '/score/' + result.value, {target: '#' + btn.id, swap: 'none'}) + } + }); +} + +function showSingleCalcResultDialog(xhr, reqPath) { + let icon; + switch (xhr.status) { + case 200: + icon = 'success'; + break; + case 404: + icon = 'warning'; + break; + default: + icon = 'error'; + } + + Swal.fire({ + title: 'Abfrage für ' + reqPath.replace("/score/", ''), + text: "Score: " + xhr.response, + icon: icon, + customClass: swalClasses + }); +} + +function showAdminActionExecutedDialog(xhr, method) { + Swal.fire({ + title: 'Action executed', + text: xhr.response, + icon: xhr.status === 200 ? 'success' : 'error' + }).then(() => { + if (method === "delete") { + location.reload(); + } + }); +} + +function confirmAndTrigger(btn) { + Swal.fire({ + title: btn.innerText, + text: 'Do you want to continue?', + confirmButtonText: 'Yes', + confirmButtonColor: '#dd6b55', + denyButtonColor: '#3085d6', + icon: 'warning', + showDenyButton: true, + customClass: swalClasses, + buttonsStyling: false + }).then((result) => { + if (result.isConfirmed) { + htmx.trigger(btn, 'confirmed'); + } + }); +} + +function createCodeDialog(btn) { + Swal.fire({ + title: btn.innerText, + input: 'select', + inputOptions: { + 'READER': 'Reader', + 'AUTHOR': 'Author', + 'ADMIN': 'Admin' + }, + inputPlaceholder: 'Select a role', + confirmButtonText: 'Generate', + showCancelButton: true, + customClass: swalClasses, + buttonsStyling: false + }).then((result) => { + if (result.isConfirmed) { + btn.setAttribute('hx-vals', '{"user_role": "' + result.value + '"}'); + htmx.trigger(btn, 'confirmed'); + } + }); +} \ No newline at end of file diff --git a/static/index.js b/static/index.js index 4c6bc8d..743168b 100644 --- a/static/index.js +++ b/static/index.js @@ -145,42 +145,6 @@ function deselectAllPlayers(playerListId) { checkCounter.innerText = 0; } -function confirmAndTrigger(btn) { - Swal.fire({ - title: btn.innerText, - text: 'Do you want to continue?', - confirmButtonText: 'Yes', - confirmButtonColor: '#dd6b55', - denyButtonColor: '#3085d6', - icon: 'warning', - showDenyButton: true - }).then((result) => { - if (result.isConfirmed) { - htmx.trigger(btn, 'confirmed'); - } - }); -} - -function createCodeDialog(btn) { - Swal.fire({ - title: btn.innerText, - input: 'select', - inputOptions: { - 'READER': 'Reader', - 'AUTHOR': 'Author', - 'ADMIN': 'Admin' - }, - inputPlaceholder: 'Select a role', - confirmButtonText: 'Generate', - showCancelButton: true - }).then((result) => { - if (result.isConfirmed) { - btn.setAttribute('hx-vals', '{"user_role": "' + result.value + '"}'); - htmx.trigger(btn, 'confirmed'); - } - }); -} - function singleCalcSpinner(sender) { const spinner = ''; const score = sender.previousElementSibling.children[1]; diff --git a/templates/components/bottom_controls.html b/templates/components/bottom_controls.html index 18ca1aa..1786ef0 100644 --- a/templates/components/bottom_controls.html +++ b/templates/components/bottom_controls.html @@ -67,7 +67,7 @@