73 lines
1.8 KiB
JavaScript
73 lines
1.8 KiB
JavaScript
"use strict"
|
|
|
|
const token = document.location.pathname.split("/").pop();
|
|
const api_path = `../../api/token${token}/`;
|
|
|
|
|
|
const greeting = document.getElementById("greeting"),
|
|
currentcoins = document.getElementById("currentcoins"),
|
|
maxcoins = document.getElementById("maxcoins"),
|
|
sellvalue = document.getElementById("sellvalue"),
|
|
|
|
errormesssage = document.getElementById("errormessage"),
|
|
errortext = document.getElementById("errortext"),
|
|
errorclose = document.getElementById("errorclose");
|
|
|
|
async function redraw() {
|
|
|
|
const r = await fetch(api_path + 'get_user', {
|
|
method: 'POST',
|
|
body: JSON.stringify({}),
|
|
});
|
|
|
|
if (r.status !== 200) {
|
|
console.log("oh no");
|
|
return;
|
|
}
|
|
|
|
const user = await r.json();
|
|
|
|
greeting.innerText = `Hey, ${user.name}`;
|
|
currentcoins.innerText = user.score;
|
|
maxcoins.innerText = user.maxscore;
|
|
sellvalue.max = user.maxscore - user.score;
|
|
sellvalue.value = Math.min(sellvalue.value, sellvalue.max);
|
|
}
|
|
|
|
redraw();
|
|
|
|
const button = document.getElementById("sell");
|
|
|
|
button.addEventListener("click", async () => {
|
|
const newscore = parseInt(currentcoins.innerText) + parseInt(sellvalue.value);
|
|
|
|
const r = await fetch(api_path + 'set_score', {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
newscore: newscore,
|
|
}),
|
|
});
|
|
|
|
if (r.status !== 200) {
|
|
|
|
if (r.status !== 400) {
|
|
console.log("Something went very very wrong!");
|
|
return;
|
|
}
|
|
|
|
const data = await r.json();
|
|
|
|
if (data.error) {
|
|
errortext.innerText = data.error;
|
|
errormesssage.style.display = "flex";
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
redraw();
|
|
})
|
|
|
|
errorclose.addEventListener("click", () => {
|
|
errormesssage.style.display = "none";
|
|
}) |