-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (32 loc) · 1.04 KB
/
server.js
File metadata and controls
44 lines (32 loc) · 1.04 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
import express from 'express'
import http from 'http'
import socketio from 'socket.io'
import createGame from './public/game.js'
const app = express()
const server = http.createServer(app)
const socket = socketio(server)
app.use(express.static('public'))
const game = createGame()
game.start()
game.subscribe((command) => {
console.log('> Emitting ' + command.type)
socket.emit(command.type, command)
})
socket.on('connection', (client) => {
const playerId = client.id
console.log(`Player connected on Server with id: ${playerId}`)
game.addPlayer({ playerId })
client.emit('setup', game.state)
client.on('move-player', (command) => {
command.playerId = playerId // Cause do not have authentication
command.type = 'move-player' // Cause do not have authentication
game.movePlayer(command)
})
client.on('disconnect', () => {
game.removePlayer({ playerId })
console.log(`Player disconnected on Server with id: ${playerId}`)
})
})
server.listen('3000', () => {
console.log('Server listening on port: 3000')
})