-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
81 lines (58 loc) · 1.74 KB
/
server.js
File metadata and controls
81 lines (58 loc) · 1.74 KB
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
import express from 'express'
import http from 'http'
import { Server } from 'socket.io'
import createGame from './public/scripts/game.js'
const app = express()
const server = http.createServer(app)
const sockets = new Server(server)
app.use(express.static('public'))
app.get('/admin-options', function (req, res) {
res.sendFile(`${process.cwd()}/public/game-admin.html`)
})
server.listen(process.env.PORT || 8000)
// game //
const game = createGame()
game.subscribe(command => {
sockets.emit(command.type, command)
})
sockets.on('connection', socket => {
const playerId = socket.id
game.addPlayer({ playerId })
socket.emit('setup', game.state)
socket.on('disconnect', () => {
game.removePlayer({ playerId })
})
socket.on('start-countdown', command => {
game.startCountdown(command)
})
socket.on('game-start', command => {
game.start(command)
})
socket.on('game-stop', () => {
game.stop()
})
socket.on('move-player', command => {
command.playerId = playerId
command.type = 'move-player'
game.movePlayer(command)
})
socket.on('change-username', command => {
command.playerId = playerId
command.type = 'change-username'
game.changeUsername(command)
})
socket.on('change-player-color', command => {
command.playerId = playerId
command.type = 'change-player-color'
game.changePlayerColor(command)
})
socket.on('remove-all-fruits', command => {
game.removeAllFruits(command)
})
socket.on('remove-all-bombs', command => {
game.removeAllBombs(command)
})
socket.on('remove-all-points', command => {
game.removeAllPoints(command)
})
})