-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (32 loc) · 933 Bytes
/
index.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
import express from 'express'
import { Server } from "socket.io"
import http from 'http'
import game from './public/game.js'
const app = express()
const server = http.createServer(app)
const io = new Server(server)
app.use(express.static('public'))
const gameInstance = game()
gameInstance.subscribe((command) => {
io.emit(command.type, command)
})
io.on('connection', (socket) => {
const playerId = socket.id
gameInstance.addPlayer({playerId})
socket.emit('state', gameInstance.state)
socket.on('disconnect', () => {
const playerId = socket.id
gameInstance.removePlayer({playerId})
})
socket.on('playerAction', (command) => {
if (command.playerId == socket.id) {
gameInstance.playerAction(command)
}
})
})
server.listen(3000, () => {
console.log('listening on *:3000');
});
// setInterval(() => {
// gameInstance.addFruit({})
// }, 10000);