-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (86 loc) · 2.95 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let board = document.getElementById('board');
let baseUrl = null;
let token = null;
let spelerToken = null;
start();
function start(_baseUrl, _token, _spelerToken) {
baseUrl = _baseUrl;
token = _token;
spelerToken = _spelerToken;
getSpel();
setInterval(getSpel,1000);
}
async function getSpel() {
axios.get(baseUrl + '/spel/krijg-spel?token=' + token)
.then(response => {
board.innerHTML = '';
spel = response.data;
let body = '';
let clickHandlers = [];
for ([index, player] of Object.entries(spel.bord)) {
index = index.split(',');
if (index[1] == '0') {
body += `<div class="c-row-f">`;
}
let fiche = '';
if (player == 1) {
fiche = '<div class="c-f-white"></div>';
}
if (player == 2) {
fiche = '<div class="c-f-black"></div>';
}
let uniqueId = `click-${index[0]}-${index[1]}`;
body += `<div id="${uniqueId}" class="c-f">${fiche}</div>`;
clickHandlers.push({'id': spel.token, 'token': spelerToken, 'rij': index[0], 'kolom': index[1], 'handlerId': uniqueId});
if (index[1] == '7') {
body += '</div>';
}
}
board.innerHTML += body;
clickHandlers.forEach((e) => document.getElementById(e.handlerId).addEventListener("click", () => click(e.id, e.token, e.rij, e.kolom)));
})
.catch(e => console.log(e));
}
function click(id, token, rij, kolom) {
let formData = new FormData();
formData.append('id', id);
formData.append('token', token);
formData.append('rij', rij);
formData.append('kolom', kolom);
axios({
method: "post",
url: baseUrl + '/spel/doe-zet',
data: formData,
headers: { "Content-Type": "multipart/form-data" },
})
.then(e => {
if (e.data == 'IMPOSSIBLE') {
showMessage('Deze zet is niet mogelijk', 'error');
} else if (e.data == 'NOTYOURTURN') {
showMessage('Het is niet jouw beurt', 'error');
} else if (e.data == 'GAMEOVER') {
showMessage('Game over', 'error');
} else if (e.data == 'ok') {
getSpel();
}
else {
showMessage('Er is iets mis gegaan', 'error');
}
})
.catch(e => {
showMessage('Er is iets mis gegaan', 'error');
});
}
function showMessage(message, type) {
let msg = document.getElementById('message');
if (type == 'success') {
msg.classList.add('c-m-success');
} else {
msg.classList.add('c-m-error');
}
msg.innerText = message;
msg.style.display = 'block';
setTimeout(function() {
msg.style.display = 'none';
}, 5000);
}