-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
409 lines (337 loc) · 11.2 KB
/
index.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { createServer } from 'http'
import { Player } from './player.js'
import { Server } from 'socket.io'
import { Ball } from './ball.js'
import * as THREE from 'three'
const court_width = 30
const court_length = 60
const player_height = 2
const ball_radius = 0.5
let playerMap = new Map()
let gameInSession = false
let numPlayersAliveRed = 0
let numPlayersAliveBlue = 0
let ballMap = new Map()
const numBalls = 1
const httpServer = createServer()
const io = new Server(httpServer, {
cors: {
origin: '*',
},
})
io.on('connection', (socket) => {
const player = new Player()
playerMap.set(player.uuid, player)
socket.join('lobby')
console.log(`Player ${player.uuid} connected.`)
if (gameInSession) socket.emit('game-on-going', {})
else socket.emit('game-not-on-going', {})
socket.on('disconnect', () => {
console.log(`Player ${player.uuid} disconnected.`)
playerMap.delete(player.uuid)
socket.leave('lobby')
socket.leave('game')
broadcastTeamSelectionInfo(socket)
if (gameInSession) {
if (player.live) {
if (player.team == 'red') numPlayersAliveRed--
else if (player.team == 'blue') numPlayersAliveBlue--
}
if (numPlayersAliveRed == 0 || numPlayersAliveBlue == 0) {
endGame()
}
} else {
checkIfAllReadyAndStartGame()
}
})
socket.on('enter-name', (data) => {
const packet = JSON.parse(data)
const username = packet.username
if (username == '')
console.log(`Player ${player.uuid} set invalid username.`)
else {
console.log(`Player ${player.uuid} set username to ${username}`)
player.username = username
broadcastTeamSelectionInfo(socket)
}
})
socket.on('select-team', (data) => {
const packet = JSON.parse(data)
const team = packet.team
if (player.username == '') {
console.log(
`Player ${player.uuid} selected team before setting username.`
)
} else if (team != 'red' && team != 'blue')
console.log(`Player ${player.uuid} selected invalid team.`)
else {
console.log(`Player ${player.uuid} set team to ${team}.`)
player.team = team
player.ready = false
broadcastTeamSelectionInfo(socket)
}
})
socket.on('request-team-selection-info', (data) =>
broadcastTeamSelectionInfo(socket)
)
socket.on('confirm-ready', (data) => {
const packet = JSON.parse(data)
const ready = packet.ready
if (player.username == '' || player.team == '') {
console.log(
`Player ${player.uuid} confirmed ready before entering username or selecting team.`
)
} else if (ready != false && ready != true)
console.log(`Player ${player.uuid} selected invalid ready state.`)
else {
console.log(`Player ${player.uuid} set ready state to ${ready}`)
player.ready = ready
broadcastTeamSelectionInfo(socket)
if (player.ready) {
socket.leave('lobby')
socket.join('game')
checkIfAllReadyAndStartGame()
} else {
socket.leave('game')
socket.join('lobby')
}
}
})
socket.on('ready-to-start-game', (data) => {
if (!gameInSession) return
if (player.username == '') {
socket.emit('return-to-enter-name', {})
console.log('Emitting return-to-enter-name')
return
} else if (player.team == '') {
socket.emit('return-to-select-team', {})
console.log('Emitting return-to-select-team')
return
}
socket.emit(
'init',
JSON.stringify({
ballMap: constructClientBallMap(),
playerMap: constructClientPlayerMap(player.uuid),
})
)
if (numPlayersAliveRed == 0 || numPlayersAliveBlue == 0) {
endGame()
}
})
// receive a message from the client
socket.on('updatePlayer', (data) => {
if (!gameInSession) return
else if (!playerMap.has(player.uuid)) {
return
}
const packet = JSON.parse(data)
player.position = packet.position
if (player.live && !packet.live) {
if (player.team == 'red') numPlayersAliveRed--
else if (player.team == 'blue') numPlayersAliveBlue--
}
player.vel = packet.vel
player.live = packet.live
socket.to('game').emit(
'updatePlayer',
JSON.stringify({
uuid: player.uuid,
position: player.position,
vel: player.vel,
live: player.live,
})
)
if (numPlayersAliveRed == 0 || numPlayersAliveBlue == 0) {
endGame()
}
})
// receive a message from the client
socket.on('updateBall', (data) => {
if (!gameInSession) return
const packet = JSON.parse(data)
const uuid = packet.uuid
const ball = ballMap.get(uuid)
ball.position = packet.position
ball.quaternion = packet.quaternion
ball.vel = packet.vel
ball.live = packet.live
socket.to('game').emit('updateBall', data)
})
socket.on('in-game-wanderer', () => {
console.log('invoke in-game-wanderer')
socket.leave('game')
if (gameInSession) {
if (player.live) {
if (player.team == 'red') numPlayersAliveRed--
else if (player.team == 'blue') numPlayersAliveBlue--
}
if (numPlayersAliveRed == 0 || numPlayersAliveBlue == 0) {
endGame()
}
}
})
})
const checkIfAllReadyAndStartGame = () => {
// Are all players ready?
let allReady = true
// Potential race condition if someone sends not ready around here?
playerMap.forEach((player, _) => {
if (player.team != '') allReady = allReady && player.ready
})
if (allReady) {
// Start the game
console.log('Sending start-game')
gameInSession = true
numPlayersAliveRed = 0
numPlayersAliveBlue = 0
playerMap.forEach((player, _) => {
if (player.team == 'red') numPlayersAliveRed++
else if (player.team == 'blue') numPlayersAliveBlue++
})
io.sockets.in('game').emit('start-game', JSON.stringify({}))
io.sockets.in('lobby').emit('game-on-going', {})
}
}
const endGame = () => {
if (numPlayersAliveRed == 0 || numPlayersAliveBlue == 0) {
gameInSession = false
playerMap.forEach((player, _) => {
player.username = ''
player.team = ''
player.ready = false
player.live = true
})
let result = 'Draw'
if (numPlayersAliveRed > 0) result = 'Red Wins'
else if (numPlayersAliveBlue > 0) result = 'Blue Wins'
io.sockets.in('game').emit(
'game-over',
JSON.stringify({
result: result,
})
)
io.sockets.in('lobby').emit('game-not-on-going', {})
io.in('game')
.fetchSockets()
.then((socketList) =>
socketList.forEach((s) => {
s.leave('game')
s.join('lobby')
})
)
}
}
httpServer.listen(process.env.PORT || 4000, () => {
const port = httpServer.address().port
console.log('Server is listening on port %s.', port)
})
const broadcastTeamSelectionInfo = (socket) => {
const redTeam = []
const blueTeam = []
const unselectedTeam = []
playerMap.forEach((player, _) => {
const playerInfo = {
username: player.username,
ready: player.ready,
}
if (player.team == 'red') redTeam.push(playerInfo)
else if (player.team == 'blue') blueTeam.push(playerInfo)
else unselectedTeam.push(playerInfo)
})
console.log('Emitting team-selection-info')
io.sockets.in('lobby').emit(
'team-selection-info',
JSON.stringify({
redTeam: redTeam,
blueTeam: blueTeam,
unselectedTeam: unselectedTeam,
})
)
}
const constructClientBallMap = () => {
computeInitBallOrientations()
const clientBallMap = {}
ballMap.forEach((ball, uuid) => {
clientBallMap[uuid] = {
uuid: ball.uuid,
position: ball.position,
vel: ball.vel,
}
})
return clientBallMap
}
const constructClientPlayerMap = (playerUuid) => {
computeInitPlayerOrientations()
const clientPlayerMap = {}
playerMap.forEach((player, uuid) => {
if (player.team != '')
clientPlayerMap[uuid] = {
playable: uuid == playerUuid,
uuid: player.uuid,
position: player.position,
vel: player.vel,
team: player.team,
facing: player.facing,
name: player.username,
}
})
return clientPlayerMap
}
const computeInitPlayerOrientations = () => {
let numRedTeam = 0
let numBlueTeam = 0
playerMap.forEach((player, _) => {
if (player.team == 'red') numRedTeam++
else if (player.team == 'blue') numBlueTeam++
})
const redTeamSpacing = court_width / (numRedTeam + 1)
const blueTeamSpacing = court_width / (numBlueTeam + 1)
let i = 1
let j = 1
playerMap.forEach((player, _) => {
if (player.team == 'red') {
player.position.x = -court_width / 2 + redTeamSpacing * i++
player.position.y = player_height / 2
player.position.z = -court_length / 2 + player_height + 15
player.facing.x = 0
player.facing.y = 0
player.facing.z = 1
} else if (player.team == 'blue') {
player.position.x = -court_width / 2 + blueTeamSpacing * j++
player.position.y = player_height / 2
player.position.z = court_length / 2 - player_height - 15
player.facing.x = 0
player.facing.y = 0
player.facing.z = -1
}
})
}
const computeInitBallOrientations = () => {
if (ballMap.size == numBalls) {
const ballSpacing = court_width / (numBalls + 1)
let i = 0
ballMap.forEach((ball, _) => {
ball.position = new THREE.Vector3(
-court_width / 2 + ballSpacing * (i + 1),
ball_radius,
0
)
ball.vel = new THREE.Vector3(0, 0, 0)
i++
})
} else {
const ballSpacing = court_width / (numBalls + 1)
for (let i = 0; i < numBalls; i++) {
const ball = new Ball({
position: new THREE.Vector3(
-court_width / 2 + ballSpacing * (i + 1),
ball_radius,
0
),
vel: new THREE.Vector3(0, 0, 0),
})
ballMap.set(ball.uuid, ball)
}
}
}