140 lines
3.6 KiB
JavaScript
140 lines
3.6 KiB
JavaScript
"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;
|
|
}
|
|
});
|
|
|
|
async function get_secret(user) {
|
|
|
|
const r = await fetch(admin_api_path + 'get_secret', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
uuid: user.uuid
|
|
}),
|
|
});
|
|
console.log(r);
|
|
|
|
if (r.status !== 200) {
|
|
console.log("oh no");
|
|
return;
|
|
}
|
|
|
|
const data = await r.json();
|
|
return data[user.uuid]
|
|
}
|
|
|
|
async function render_table(data) {
|
|
const users = Object.values(data.users);
|
|
|
|
const newusername = document.querySelector("#newusername");
|
|
const newuserbutton = document.querySelector("#newuserbutton");
|
|
|
|
newuserbutton.addEventListener("click", async () => {
|
|
const r = await fetch(admin_api_path + 'add_user', {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
username: newusername.value,
|
|
}),
|
|
});
|
|
|
|
if (r.status !== 200) {
|
|
console.log("something went wrong");
|
|
return;
|
|
}
|
|
|
|
update_table();
|
|
})
|
|
|
|
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")
|
|
;
|
|
|
|
cur.innerText = user.score;
|
|
max.innerText = user.maxscore;
|
|
|
|
const secret = await get_secret(user);
|
|
name.innerText = user.name;
|
|
name.href = `../dealer/${secret}`;
|
|
// console.log(secret);
|
|
|
|
//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);
|