diff --git a/admin.html b/admin.html index 9079e36..c518f75 100644 --- a/admin.html +++ b/admin.html @@ -8,8 +8,8 @@ Waschmarken.io - - + + -

Waschmarken Scoreboard

+

Waschmarken Admin Interface

- - + + +
NameName ScoreMaxIncrease
- + - \ No newline at end of file + + + diff --git a/static/js/admin.js b/static/js/admin.js new file mode 100644 index 0000000..7e06117 --- /dev/null +++ b/static/js/admin.js @@ -0,0 +1,97 @@ +"use strict" + +const token = document.location.pathname.split("/").pop(); +const api_path = `../../api/`; +const admin_api_path = `../../api/token${token}/`; + + + +const greeting = document.getElementById("greeting"), + currentcoins = document.getElementById("currentcoins"), + maxcoins = document.getElementById("maxcoins"), + sellvalue = document.getElementById("sellvalue"); + +// https://stackoverflow.com/a/7616484 +Object.defineProperty(String.prototype, 'hashCode', { + value: function () { + var hash = 0, i, chr; + for (i = 0; i < this.length; i++) { + chr = this.charCodeAt(i); + hash = ((hash << 5) - hash) + chr; + hash |= 0; // Convert to 32bit integer + } + return hash; + } +}); + +function render_table(data) { + const users = Object.values(data.users); + + const tbody = document.querySelector("#scoreboard tbody"); + + while (tbody.children.length) { + tbody.removeChild(tbody.firstChild); + } + + const template = document.querySelector("#scoreboard template"); + + for (const user of users) { + + const clone = template.content.cloneNode(true); + const name = clone.querySelector("#name"), + cur = clone.querySelector("#cur"), + max = clone.querySelector("#max"), + incr = clone.querySelector("#inc"), + number = clone.querySelector("#number"), + plusbutton = clone.querySelector("#plusbutton") + ; + + name.innerText = user.name; + cur.innerText = user.score; + max.innerText = user.maxscore; + + //const button = document.getElementById("plusbutton"); + + plusbutton.addEventListener("click", async () => { + const newmaxscore = parseInt(max.innerText) + parseInt(number.value); + console.log(newmaxscore); + console.log(user.uuid); + + const r = await fetch(admin_api_path + 'set_max_score', { + method: "POST", + body: JSON.stringify({ + uuid: user.uuid, + newmaxscore: newmaxscore, + }), + }); + + if (r.status !== 200) { + console.log("something went wrong"); + return; + } + + update_table(); + }) + + tbody.appendChild(clone); + } +} + +async function update_table() { + + const r = await fetch('../api/get_public_model', { + method: 'POST', + body: JSON.stringify({}), + }); + + if (r.status !== 200) { + console.log("oh no"); + return; + } + + const data = await r.json(); + render_table(data); +} + +update_table(); +// setInterval(update_scoreboard, 1000);