|
|
|
@ -150,6 +150,9 @@
|
|
|
|
|
clone.querySelector("." + suffix).name = prefix + suffix;
|
|
|
|
|
clone.querySelector("." + suffix + "-orig").name = prefix + suffix + "-orig";
|
|
|
|
|
});
|
|
|
|
|
for(let el of clone.querySelectorAll('select')) {
|
|
|
|
|
el.addEventListener("change", formElementChanged);
|
|
|
|
|
}
|
|
|
|
|
effDiv.appendChild(clone);
|
|
|
|
|
}
|
|
|
|
|
function addEmptyEffects(clearAll) {
|
|
|
|
@ -188,7 +191,6 @@
|
|
|
|
|
if(oldStatus.hash == newStatus.hash) return true;
|
|
|
|
|
if(oldStatus.phase_id != newStatus.phase_id
|
|
|
|
|
|| oldStatus.turn != newStatus.turn) {
|
|
|
|
|
// Reload for new phase.
|
|
|
|
|
addEmptyEffects(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -214,6 +216,10 @@
|
|
|
|
|
}
|
|
|
|
|
document.getElementById(prefix + "ready-orig").value = ready;
|
|
|
|
|
document.getElementById(prefix + "ready").checked = ready;
|
|
|
|
|
|
|
|
|
|
for(let el of document.querySelectorAll(".player-" + ord + " select")) {
|
|
|
|
|
el.disabled = ready;
|
|
|
|
|
}
|
|
|
|
|
} else if(pkey == "total_fear") {
|
|
|
|
|
let total_fear = player[pkey];
|
|
|
|
|
for(let el of document.getElementsByClassName(prefix + "total-fear")) {
|
|
|
|
@ -260,25 +266,124 @@
|
|
|
|
|
return true;
|
|
|
|
|
{% endblock %}
|
|
|
|
|
}
|
|
|
|
|
setInterval(function() {
|
|
|
|
|
fetch(new Request("{% url 'status' access_code=access_code %}"
|
|
|
|
|
+ statusObj.hash + "/"))
|
|
|
|
|
.then(response => {
|
|
|
|
|
if(response.status === 304) {
|
|
|
|
|
// TODO Just skip the next step?
|
|
|
|
|
return statusObj;
|
|
|
|
|
var activeRequests = new Set();
|
|
|
|
|
function formElementChanged(e) {
|
|
|
|
|
let form = document.forms[0];
|
|
|
|
|
let el = e.target;
|
|
|
|
|
let name = el.name;
|
|
|
|
|
let origName = name + "-orig";
|
|
|
|
|
let origEl = form.elements[origName];
|
|
|
|
|
|
|
|
|
|
let newValue = el.type == "checkbox" ? el.checked : el.value;
|
|
|
|
|
let oldValue = origEl.value;
|
|
|
|
|
|
|
|
|
|
el.disabled = true;
|
|
|
|
|
if(el.name.endsWith("ready") && newValue) {
|
|
|
|
|
let ord = name.split("-")[1];
|
|
|
|
|
// If marking a player ready, disable all selects immediately.
|
|
|
|
|
for(let el of document.querySelectorAll(".player-" + ord + " select")) {
|
|
|
|
|
el.disabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeRequests.add(el);
|
|
|
|
|
statusObj.hash = '';
|
|
|
|
|
|
|
|
|
|
// From https://stackoverflow.com/a/5588435
|
|
|
|
|
function getCookie(name) {
|
|
|
|
|
var cookieValue = null;
|
|
|
|
|
if (document.cookie && document.cookie != '') {
|
|
|
|
|
var cookies = document.cookie.split(';');
|
|
|
|
|
for (var i = 0; i < cookies.length; i++) {
|
|
|
|
|
var cookie = cookies[i];
|
|
|
|
|
// Does this cookie string begin with the name we want?
|
|
|
|
|
if (cookie.substring(0, name.length + 1) == (name + '=')) {
|
|
|
|
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cookieValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetch("{% url 'update_game' access_code=access_code %}",
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
|
"X-CSRFToken": getCookie("csrftoken"),
|
|
|
|
|
},
|
|
|
|
|
body: name + "=" + newValue + "&" + origName + "=" + oldValue
|
|
|
|
|
+ "&game_turn=" + statusObj["turn"]
|
|
|
|
|
+ "&game_phase=" + statusObj["phase_id"]
|
|
|
|
|
+ "&phase_name=" + statusObj["phase"]
|
|
|
|
|
+ "&csrfmiddlewaretoken=" + form.elements["csrfmiddlewaretoken"].value,
|
|
|
|
|
}).then(response => response.json()
|
|
|
|
|
).then(data => {
|
|
|
|
|
if("success" in data && data["success"]) {
|
|
|
|
|
form.elements[origName].value = newValue;
|
|
|
|
|
} else {
|
|
|
|
|
var updatedValue = oldValue;
|
|
|
|
|
if("value" in data) {
|
|
|
|
|
let updatedValue = data['value'];
|
|
|
|
|
origEl.value = updatedValue;
|
|
|
|
|
}
|
|
|
|
|
if(el.type == "checkbox") {
|
|
|
|
|
el.checked = updatedValue == "true";
|
|
|
|
|
} else {
|
|
|
|
|
return response.json();
|
|
|
|
|
el.value = updatedValue;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
if(!handleNewStatus(statusObj, data)) {
|
|
|
|
|
document.getElementById("button-refresh").click();
|
|
|
|
|
|
|
|
|
|
if("errors" in data) {
|
|
|
|
|
alert(data["errors"]);
|
|
|
|
|
} else {
|
|
|
|
|
statusObj = data;
|
|
|
|
|
alert("Unknown error: " + data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 5000);
|
|
|
|
|
}
|
|
|
|
|
el.disabled = false;
|
|
|
|
|
activeRequests.delete(el);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
window.addEventListener("DOMContentLoaded", e => {
|
|
|
|
|
for(let updateButton of document.getElementsByClassName("update-button")) {
|
|
|
|
|
updateButton.disabled = true;
|
|
|
|
|
updateButton.style.display = "none";
|
|
|
|
|
}
|
|
|
|
|
let form = document.forms[0];
|
|
|
|
|
for(let el of form.elements) {
|
|
|
|
|
if(el.type == 'checkbox' && !el.name.endsWith("visible")) {
|
|
|
|
|
el.addEventListener("change", formElementChanged);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(let el of document.querySelectorAll('select')) {
|
|
|
|
|
el.addEventListener("change", formElementChanged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setInterval(function() {
|
|
|
|
|
if(activeRequests.size != 0) return;
|
|
|
|
|
fetch(new Request("{% url 'status' access_code=access_code %}"
|
|
|
|
|
+ (statusObj.hash != ""
|
|
|
|
|
? statusObj.hash + "/"
|
|
|
|
|
: "")))
|
|
|
|
|
.then(response => {
|
|
|
|
|
if(response.status === 304) {
|
|
|
|
|
// TODO Just skip the next step?
|
|
|
|
|
return statusObj;
|
|
|
|
|
} else {
|
|
|
|
|
return response.json();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
if(activeRequests.size != 0) return;
|
|
|
|
|
if(!handleNewStatus(statusObj, data)) {
|
|
|
|
|
document.getElementById("button-refresh").click();
|
|
|
|
|
} else {
|
|
|
|
|
statusObj = data;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 5000);
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
{% endif %}
|
|
|
|
|
{% endblock %}
|
|
|
|
|