Add Single-Calc for player name. Refactor admin+single-calc dialogs.

This commit is contained in:
MaxJa4
2024-01-21 12:12:47 +01:00
parent 4ff139b217
commit 8edbbb4347
6 changed files with 132 additions and 47 deletions

View File

@@ -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!")
}
}

View File

@@ -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)

100
static/dialogs.js Normal file
View File

@@ -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');
}
});
}

View File

@@ -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 = '<i class="spinner-grow spinner-grow-sm text-info align-baseline me-2" style="margin-left: 0.91rem;" role="status"></i>';
const score = sender.previousElementSibling.children[1];

View File

@@ -67,7 +67,7 @@
</button>
</div>
<div class="col-auto position-absolute end-0">
<button class="btn btn-lg btn-outline-secondary text-secondary-emphasis" type="button" disabled>
<button class="btn btn-lg btn-outline-secondary text-secondary-emphasis" id="singleCalcBtn" onclick="showSingleCalcDialog(this)">
<i class="bi bi-person me-2"></i>
Einzel-Abfrage
</button>
@@ -76,16 +76,15 @@
<script>
document.body.addEventListener('htmx:afterRequest', function (event) {
if (event.detail.pathInfo.requestPath.startsWith("/admin/")) {
Swal.fire({
title: 'Action executed',
text: event.detail.xhr.response,
icon: event.detail.xhr.status === 200 ? 'success' : 'error'
}).then(() => {
if (event.detail.requestConfig.verb === "delete") {
location.reload();
}
});
let detail = event.detail;
let reqPath = detail.pathInfo.requestPath;
let method = detail.requestConfig.verb;
let xhr = detail.xhr;
if (reqPath.startsWith("/admin/")) {
showAdminActionExecutedDialog(xhr, method);
} else if (reqPath.startsWith("/score/") && method === "post") {
showSingleCalcResultDialog(xhr, method);
}
});
</script>

View File

@@ -5,6 +5,7 @@
<title>Infantry Skill Calculator</title>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="../static/index.js"></script>
<script src="../static/dialogs.js"></script>
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">