-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
61 lines (47 loc) · 1.53 KB
/
server.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
const PORT = process.env.PORT || 30000
const game = require('./game')
let io = require('socket.io')
let server = io.listen(PORT)
console.log('websocket server listening on ' + PORT)
server.on('connection', (client) => {
console.log(`player connected with id: ${client.id}`)
client.on('initialize', (data) => {
console.log(`player ${client.id} set nickname "${data.nickname}"`)
if(!game.started()) game.start()
game.add_player(client.id, data)
client.emit('initialize', {
player: game.state.players[client.id],
objects: game.state.objects
})
})
client.on('event', game.handle_event)
client.on('disconnect', () => {
console.log(`player ${client.id} disconnected`)
if(game.state.players[client.id]){
delete game.state.players[client.id]
game.updates.disconnected.push(client.id)
}
})
})
const game_loop = () => {
if(!game.started())return;
if(!Object.keys(game.state.players).length)return;
const winner = game.check_winner()
if(!game.overed()){
if(winner.fox || winner.panda){
game.set_winner(winner)
setTimeout(() => {
game.cleanup()
Object.keys(server.sockets.sockets)
.forEach(key => server.sockets.sockets[key].disconnect(true))
}, 10000)
}
}
game.update()
// update players status
server.local.emit('update', game.updates)
if(game.updates.disconnected)game.updates.disconnected = []
if(game.updates.attacks)game.updates.attacks = []
}
// 1 tick = 1/20 second
setInterval(game_loop, 1000 / 50)