-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·46 lines (35 loc) · 1.11 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
console.log('> Script Started')
import express from 'express'
import http from 'http'
import createGame from './public/game.js'
import { Server } from 'socket.io'
const app = express()
const server = http.createServer(app)
const port = 3000
const sockets = new Server(server, { /* OPITIONS */ })
app.use(express.static('public'))
const game = createGame()
game.start()
game.subscribe((command) => {
console.log(`> Emiting ${command.type}`)
sockets.emit(command.type, command)
})
sockets.on('connection', (socket) => {
const playerId = socket.id
console.log(`> Player connected on server ${playerId}`)
game.addPlayer({playerId: playerId});
console.log(`> Player Connected: ${playerId}`)
socket.emit('setup', game.state)
socket.on('disconnect', () => {
game.removePlayer({playerId: playerId})
console.log(`> Player Desconnected: ${playerId}`)
})
socket.on('move-player', (command) => {
command.playerId = playerId
command.type = 'move-player'
game.movePlayer(command)
})
})
server.listen(port, () => {
console.log(`> Server listening on port: ${port}`)
})