-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientScript.js
171 lines (146 loc) · 5.24 KB
/
clientScript.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
let clientId
let gameId
let isTurn = false
let yourSymbol
let socket;
let board;
let game
const connectBtn = document.getElementById('connectBtn')
const newGameBtn = document.getElementById('newGame')
const currGames = document.getElementById('currGames')
const joinGame = document.querySelector('button[type="submit"]')
const cells = document.querySelectorAll('#cell')
const gameBoard = document.querySelector('#board')
const userCol = document.querySelector('.flex-col1')
connectBtn.addEventListener('click', () => {
socket = new WebSocket('ws://localhost:8080')
socket.onopen = function(event) {}
newGameBtn.addEventListener('click', () => {
const payLoad = {
'method': 'create',
'clientId': clientId
}
socket.send(JSON.stringify(payLoad))
})
socket.onmessage = function(msg) {
const data = JSON.parse(msg.data)
switch (data.method) {
case 'connect':
clientId = data.clientId
userCol.innerHTML = `UserId: ${clientId}`
userCol.classList.add('joinLabel')
break
case 'create':
// inform you have successfully created the game and been added as player1
gameId = data.game.gameId
yourSymbol = data.game.players[0].symbol
console.log(`game id is ${gameId} and your symbol is ${yourSymbol}`)
cells.forEach(cell => {
cell.classList.remove('x')
cell.classList.remove('cirlce')
})
break
case 'gamesAvail':
while (currGames.firstChild) {
currGames.removeChild(currGames.lastChild)
}
const games = data.games
games.forEach((game) => {
const li = document.createElement('li')
li.addEventListener('click', selectGame)
li.innerText = game
currGames.appendChild(li)
})
break
case 'join':
gameId = data.game.gameId
yourSymbol = data.game.players[1].symbol
console.log(`game id is ${gameId} and your symbol is ${yourSymbol}`)
cells.forEach(cell => {
console.log(`cell classes are ${cell.classList}`)
cell.classList.remove('x')
cell.classList.remove('cirlce')
})
break
case 'updateBoard':
gameBoard.style.display = "grid"
console.log(`game updateBoard is ${data.game.board}`)
game = data.game
board = game.board
const symbolClass = yourSymbol == 'x' ? 'x' : 'circle'
gameBoard.classList.add(symbolClass)
index = 0
cells.forEach(cell => {
if (board[index] == 'x')
cell.classList.add('x')
else if (board[index] == 'o')
cell.classList.add('circle')
else
cell.addEventListener('click', clickCell)
index++
})
game.players.forEach((player) => {
if (player.clientId == +clientId && player.isTurn == true) {
isTurn = true
console.log(`your turn`)
}
})
break
case 'gameEnds':
console.log(`Winner is ${data.winner}`)
window.alert(`Winner is ${data.winner}`)
break;
case 'draw':
alert('Its a draw')
break
}
}
socket.onclose = function(event) {
}
socket.onerror = function(err) {
}
})
function selectGame(src) {
gameId = +src.target.innerText
joinGame.addEventListener('click', joingm, { once: true })
}
function joingm() {
const payLoad = {
'method': 'join',
'clientId': clientId,
'gameId': gameId
}
socket.send(JSON.stringify(payLoad))
}
function clickCell(event) {
if (!isTurn || event.target.classList.contains('x') || (event.target.classList.contains('circle')))
return
const cellclass = yourSymbol == 'x' ? 'x' : 'circle'
event.target.classList.add(cellclass)
index = 0
cells.forEach(cell => {
if (cell.classList.contains('x'))
board[index] = 'x'
if (cell.classList.contains('circle'))
board[index] = 'o'
index++
})
isTurn = false
makeMove()
}
function makeMove() {
index = 0
cells.forEach((cell) => {
if (cell.classList.contains('x'))
game.board[index] == 'x'
if (cell.classList.contains('circle'))
game.board[index] == 'o'
index++
})
cells.forEach(cell => cell.removeEventListener('click', clickCell))
const payLoad = {
'method': 'makeMove',
'game': game
}
socket.send(JSON.stringify(payLoad))
}