-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
186 lines (171 loc) · 6.23 KB
/
script.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var match_id = 0
const baseURL = 'https://tictactoespring.onrender.com'
async function verifyGameStatus() {
const url = `${baseURL}/match/${match_id}`
try {
const response = await fetch(url, {
method: "GET"
})
if (response.ok) {
updateGame()
} else if (response.status == 404) {
console.log("Game not created yet")
} else {
console.log(`Unexpected error: ${response.status}`)
}
} catch (error) {
console.error(`Network error while verifying game status: ${error}`)
}
}
async function createMatch() {
const url = `${baseURL}/match/create`
try {
const response = await fetch(url, {
method: "POST"
})
if (response.ok) {
const obj = await response.json();
match_id = obj.id
}
if (response.ok) {
console.log("Game sucessfully created.")
updateGame()
} else {
window.alert("[ERROR] Match creation error")
}
} catch (error) {
console.error(`Error creating match: ${error}`)
window.alert("[ERROR] Network error while creating match.")
}
}
async function restartMatch() {
const url = `${baseURL}/match/reset/${match_id}`
try {
const response = await fetch(url, {
method: "PUT"
})
if (response.ok) {
console.log("Match sucessfully restarted")
updateGame()
} else {
window.alert("[ERROR] Match failed to restart")
}
} catch (error) {
console.error(`Error restarting match: ${error}`)
window.alert("[ERROR] Network error while restarting match.")
}
}
async function restartRound() {
const url = `${baseURL}/games/restart/${match_id}`
try {
const response = await fetch(url, {
method: "PUT"
})
if (response.status == 200) {
console.log("Round sucessfully restarted")
updateGame()
} else {
window.alert("[ERROR] Round failed to restart")
}
} catch (error) {
console.error(`Error restarting round: ${error}`)
window.alert("[ERROR] Network error while restarting round.")
}
}
async function makeMove(num) {
const url = `${baseURL}/games/play/${match_id}/${num}`
try {
const response = await fetch(url, {
method: 'PUT'
})
if (response.ok) {
await updateGame()
} else {
window.alert("[ERROR] Something went wrong while making a move")
console.log(`Error making move: ${response.status} ${response.statusText}`)
}
} catch(error) {
console.error(`Error making move: ${error}`)
window.alert("[ERROR] Network error while making a move.")
}
}
async function isRunning() {
const url = `${baseURL}/games/${match_id}`
try {
const response = await fetch(url, {
method: 'GET'
})
if (response.ok) {
const obj = await response.json();
return obj?.isRunning || false
} else {
window.alert("[ERROR] Failed to verify game running status")
console.error(`Error verifying game running status: ${response.status} ${response.statusText}`)
return false
}
} catch(error) {
console.error(`Error verifying game running status: ${error}`)
window.alert("[ERROR] Network error while verifying game running status.")
return false
}
}
async function updateGame() {
const places = [
window.document.getElementById('place1'),
window.document.getElementById('place2'),
window.document.getElementById('place3'),
window.document.getElementById('place4'),
window.document.getElementById('place5'),
window.document.getElementById('place6'),
window.document.getElementById('place7'),
window.document.getElementById('place8'),
window.document.getElementById('place9')
]
const gameStatus = window.document.getElementById('gameStatus')
const xcount = window.document.getElementById('xcount')
const ocount = window.document.getElementById('ocount')
const drawcount = window.document.getElementById('drawcount')
const roundnumber = window.document.getElementById('roundnumber')
const url = `${baseURL}/match/${match_id}`
try {
const response = await fetch(url, {
method: 'GET'
})
if (response.ok) {
const match = await response.json();
const { ticTacToe: game, roundsPlayed, xVictories, oVictories, draws } = match;
// update game places
table = String(game.firstLine + game.secondLine + game.thirdLine)
places.forEach((place, index) => {
if (table.charAt(index) === 'X' ) {
place.innerHTML = table.charAt(index)
} else if (table.charAt(index) === 'O') {
place.innerHTML = table.charAt(index)
} else {
place.innerHTML = ''
}
});
// update game status
if (game.isRunning) {
gameStatus.innerHTML="<p>The game started!</p>"
} else if (game.roundWinner != null) {
if (game.roundWinner == "D") {
gameStatus.innerHTML="<p>The game ended in a draw!</p>"
} else {
gameStatus.innerHTML=`<p>Game finished, ${game.roundWinner} won the round!</p>`
}
}
// update counters
roundnumber.innerHTML="Rounds played: " + match.roundsPlayed
xcount.innerHTML="[X]: " + match.xVictories
ocount.innerHTML="[O]: " + match.oVictories
drawcount.innerHTML="[Draw]: " + match.draws
} else {
window.alert("Something went wrong while updating game data")
console.error(`Error fetching game data: ${response.status} ${response.statusText}`)
}
} catch (error) {
console.error(`Network error while updating game data: ${error}`)
window.alert("[ERROR] network error while updating game data.")
}
}