-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (41 loc) · 1.15 KB
/
server.js
File metadata and controls
55 lines (41 loc) · 1.15 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
import express from 'express'
import createGame from './public/game.js'
import path from 'path'
const app = express()
const port=3333
const io=require('socket.io')(port, {
cors: {
origin: '*',
}
})
const gameServer=createGame()
app.use(express.static('./public'))
app.set('views',path.join(__dirname,'public'))
app.engine('html',require('ejs').renderFile)
// app.set('view engine','html')
app.use('/',(req,res)=>{
res.render('index.html')
})
gameServer.start()
// Eventos customizados
gameServer.subscribe((comand)=>{
io.emit(comand.type,comand)
})
io.on('connection',(socket)=>{
const playerID=socket.id
gameServer.addPlayer({playerID:playerID})
socket.emit('setup',gameServer.state)
socket.on('disconnect',()=>{
console.log(`O jogador ${playerID} saiu`)
gameServer.removePlayer({playerID:playerID})
})
// Recebe o evento do front e atualizado gameServer
socket.on('move-player',(comand)=>{
comand.playerID=playerID
comand.type=comand.type
gameServer.movePlayer(comand)
})
})
app.listen(8000,()=>{
console.log(`Rodando em http://localhost:${8000}`)
})