94 lines
2.0 KiB
JavaScript
94 lines
2.0 KiB
JavaScript
var spage = document.getElementById('startpage')
|
|
var mform = document.getElementById('mainform')
|
|
|
|
if (qp.checkpoint) {
|
|
spage.style.display = 'none'
|
|
mform.style.display = 'block'
|
|
}
|
|
|
|
// Get the name field if it was successfully entered before
|
|
var savedName = localStorage.getItem('name')
|
|
if (qp && qp.name) {
|
|
// Forced Admin checkout - prefill qp name and auto-agree
|
|
document.getElementById('name').value = qp.name.replace(/-/g, ' ').toUpperCase();
|
|
document.getElementById('agree').checked = true
|
|
document.getElementById('agree').parentElement.style.display = 'none'
|
|
} else if (savedName && qp) {
|
|
// Prefill the client's locally saved name
|
|
document.getElementById('name').value = savedName
|
|
}
|
|
|
|
if (qp.askname)
|
|
document.getElementById('name').disabled = false
|
|
|
|
// 2nd script, server API communication
|
|
var name, datetime, agreed, tested
|
|
mform.onsubmit = function(e) {
|
|
|
|
e.preventDefault()
|
|
|
|
var i = 0;
|
|
name = e.srcElement[i++].value
|
|
if (qp.edittime && qp.edittime == 1) {
|
|
var value = e.srcElement[i++].value
|
|
datetime = new Date(value).toISOString()
|
|
}
|
|
agreed = e.srcElement[i++].checked
|
|
if (qp.action && qp.action == 'arrival')
|
|
tested = e.srcElement[i++].checked
|
|
|
|
|
|
sendMainData()
|
|
|
|
}
|
|
|
|
function sendMainData() {
|
|
|
|
// POST JSON. See docs/API.md
|
|
var payload = {
|
|
'game': qp.game,
|
|
'checkpoint': qp.checkpoint,
|
|
'name': name
|
|
}
|
|
|
|
post("/game/checkin", payload)
|
|
|
|
}
|
|
|
|
function post(url, payload) {
|
|
|
|
console.log("Sending payload:", payload)
|
|
|
|
return fetch(url, {
|
|
method: "POST",
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(payload)
|
|
}).then(res => {
|
|
handleResponse(res)
|
|
})
|
|
|
|
}
|
|
|
|
function handleResponse(res) {
|
|
|
|
console.log("Request complete! response:", res)
|
|
|
|
if (Math.floor(res.status / 100) == 2) {
|
|
|
|
// Success
|
|
mform = document.getElementById('mainform')
|
|
smsg = qp.success_message ? decodeURI(qp.success_message) : "Eingecheckt. Weiter so!"
|
|
mform.innerHTML = "<h2>"+smsg+"</h2>"
|
|
localStorage.setItem('name', name)
|
|
|
|
} else {
|
|
|
|
// Any other generic error
|
|
res.text().then(function (text) {
|
|
alert(text)
|
|
})
|
|
|
|
}
|
|
|
|
}
|