Add admin view
This commit is contained in:
97
static/js/admin.js
Normal file
97
static/js/admin.js
Normal file
@@ -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);
|
||||
Reference in New Issue
Block a user