-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
119 lines (96 loc) · 3.26 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const express = require('express')
const app = express()
const path = require('path')
const bodyParser = require('body-parser')
const randomWords = require('random-words')
const port = process.env.PORT || 3004
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
// Mongoose setup
// const mongoose = require('mongoose')
// mongoose.connect('mongodb://localhost/Rick&MortyDB', { useNewUrlParser: true })
// const api = require('./server/routes/api')
// app.use('/', api)
if (process.env.PORT)//for production enviroment
{
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
})
}
else// for dev enviroment
{
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
res.header('Access-Control-Allow-Credentials', true);
next()
})
}
const server = app.listen(port, () => {
console.log(`server running on ${port}`)
});//http.createServer(app);
const io = require('socket.io').listen(server)
module.exports = io
const Game = require('./server/gameManagerLogic/GameManager')
const Games = {}
let multy = []
// socket.io
io.on('connection', (socket) => {
console.log('connection')
socket.on('singleGame', () => {
console.log('Someone created a new single game')
const gameId = `${randomWords()}-${randomWords()}-${randomWords()}`
const newGame = new Game(gameId)
Games[gameId] = newGame
Games[gameId].joinGame(socket.id)
socket.join(`${gameId}`)
const info = { gameId, playerId: Games[gameId].spaceShips.length - 1 }
socket.emit('joinedGame', info)
})
socket.on('multyGame', () => {
if (multy.length===0){
console.log('Someone created a new game')
const gameId = `${randomWords()}-${randomWords()}-${randomWords()}`
multy.push(gameId)
const newGame = new Game(gameId)
Games[gameId] = newGame
Games[gameId].joinGame(socket.id)
socket.join(`${gameId}`)
const info = { gameId, playerId: Games[gameId].spaceShips.length - 1 }
socket.emit('joinedGame', info)
}
else{
Games[multy[0]].joinGame(socket)
socket.join(multy[0])
socket.emit('joinedGame', { gameId :multy[0], playerId: Games[multy[0]].spaceShips.length - 1 })
io.in(`${multy[0]}`).emit('getReady');
multy = []
}
})
socket.on('startGame', (gameId) => {
Games[gameId].start()
})
socket.on('pauseGame', (gameId) => {
Games[gameId].pauseGame()
})
socket.on('continueGame', (gameId) => {
Games[gameId].continuePlaying()
})
socket.on('finishExplosion', (gameId) => {
Games[gameId].finishExplosion()
})
socket.on('move', (gameId, playerIndex, direction) => {
Games[gameId] ? Games[gameId].move(playerIndex, direction) : null
})
socket.on('shoot', (gameID, playerIndex) => {
Games[gameID].shoot(playerIndex)
})
socket.on('deleteGame', (gameID) => {
delete Games[gameID]
socket.emit('deleteThisGame', null)
})
})