-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (120 loc) · 4.26 KB
/
Copy pathserver.js
File metadata and controls
155 lines (120 loc) · 4.26 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const mongoose = require("mongoose");
const express = require("express");
const db = require("./config/keys").mongoURI;
const bodyParser = require("body-parser");
const passport = require("passport");
// const Matter = require("matter-js")
const users = require("./routes/api/users");
const stats = require("./routes/api/stats");
const leaderboard = require("./routes/api/leaderboard");
const inventory = require("./routes/api/inventory");
const app = express();
const path = require("path");
const PORT = process.env.PORT || 5000;
app.use(passport.initialize());
require("./config/passport")(passport);
// setup some middleware for body parser:
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
if (process.env.NODE_ENV === "production") {
app.use(express.static("frontend/build"));
app.get("/", (req, res) => {
res.sendFile(path.resolve(__dirname, "frontend", "build", "index.html"));
});
}
app.use("/api/users", users);
app.use("/api/stats", stats);
app.use("/api/leaderboard", leaderboard);
app.use("/api/inventory", inventory);
// websocket dependencies
const http = require("http");
const socketIO = require('socket.io')
const ServerGame = require('./lib/server_game');
// end websocket dependencies
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Connected to MongoDB successfully"))
.catch(err => console.log(err));
mongoose.set('useFindAndModify', false);
app.use('/public', express.static(__dirname + '/public')); // static used for static assests!?
// app.use('/shared', express.static(__dirname + '/shared'));
// Websocket server Initialization
const server = http.createServer(app);
const io = socketIO(server, {
pingInterval: 3000,
pingTimeout: 3000,
});
app.set('port', PORT);
// end websocket server initialization
app.get("/", (req, res) => {
res.sendFile(path.resolve("../frontend/public/index.html"));
});
// Websocket logic below
const clients = {};
const gameList = {};
io.on('connection', (socket) => {
console.log('*** user connected ***')
// lobby room logic /////////////////////////
socket.on('request-gamelist', () => {
io.in('room-lobby').emit('update-gamelist', Object.keys(gameList))
})
socket.on('request-update', roomNum => {
io.in('room-lobby').emit(`update-${roomNum}`, gameList[roomNum].roster.size)
io.in('room-lobby').emit(`start-${roomNum}`, gameList[roomNum].isLive)
})
socket.on('enter-room', roomNum => {
clients[socket.id] = roomNum;
socket.join("room-" + roomNum)
})
socket.on('leave-room', roomNum => {
delete clients[socket.id]
socket.leave("room-" + roomNum)
})
socket.on('set-team', data => {
gameList[data.roomNum].setTeam(socket.id, data.team)
})
socket.on('request-game-start', roomNum => {
gameList[roomNum].init();
})
//////////////////////////////////////////////
// gameplay socket interactions //////////////
socket.on('join-game', data => {
if (!gameList[data.room]) {
gameList[data.room] = new ServerGame(io, data.room)
io.in('room-lobby').emit('update-gamelist', Object.keys(gameList))
}
gameList[data.room].addPlayer(socket.id, data.username, data.options)
});
socket.on('player-action', data => {
let game = gameList[data.room];
game.movePlayer(socket.id, data)
});
socket.on('leave-game', roomNum => {
let game = gameList[roomNum]
game.removePlayer(socket.id)
if (game.roster.size <= 0) destroyGame(roomNum)
})
//////////////////////////////////////////////
// client disconnect logic: delete from client list and remove from game.
socket.on('disconnect', () => {
let roomNum = clients[socket.id]
delete clients[socket.id]
let game = gameList[roomNum]
if (game) {
game.removePlayer(socket.id)
if (game.roster.size <= 0) destroyGame(roomNum)
}
console.log('*** user disconnected ***')
})
})
const destroyGame = function(roomNum) {
delete gameList[roomNum]
io.in('room-lobby').emit('update-gamelist', Object.keys(gameList))
}
// debugger tools
// setInterval(() => {
// console.log("clients", clients)
// console.log("game", Object.keys(gameList))
// io.in('lobby').emit('test', 'testing...')
// }, 1000);
server.listen(PORT, () => console.log(`STARTING SERVER ON PORT: ${PORT}`));