-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
43 lines (32 loc) · 1020 Bytes
/
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
import express from 'express'
import http from 'http'
import createGame from './public/game.js'
import socketio from 'socket.io'
const app = express()
const server = http.createServer(app)
const sockets = socketio(server)
app.use(express.static('public'))
const game = createGame()
game.start()
game.subscribe((command) => {
console.log(`> Emitting ${command.type}`)
sockets.emit(command.type, command)
})
sockets.on('connection', (socket) => {
const playerId = socket.id
console.log(`> Player connected: ${playerId}`)
game.addPlayer({ playerId: playerId })
socket.emit('setup', game.state)
socket.on('disconnect', () => {
game.removePlayer({ playerId: playerId })
console.log(`> Player disconnected: ${playerId}`)
})
socket.on('move-player', (command) => {
command.playerId = playerId
command.type = 'move-player'
game.movePlayer(command)
})
})
server.listen(3000, () => {
console.log(`> Server listening on port: 3000`)
})