-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (139 loc) · 5.3 KB
/
Copy pathserver.js
File metadata and controls
160 lines (139 loc) · 5.3 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
156
157
158
159
160
const express = require("express");
const coup = require("./game.js");
const port = process.env.PORT || 3000;
const app = express();
const WebSocket = require("ws");
const cors = require("cors");
const path = require("path");
const http = require("http").createServer(app);
const wss = new WebSocket.Server({ server: http });
app.use(cors());
app.use(express.json());
let rooms = {}, interval;
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(msg) {
let message = JSON.parse(msg);
switch(message.type) {
// socket is created and JOIN_ROOM is sent after POST /create-room -> GET /rooms/:id (admin: true),
// POST /join-room -> GET /rooms/:id
// or just GET /rooms/:id for an existing & open id (admin: false)
case 'JOIN_ROOM':
if (!(message.room in rooms)) { // this shouldn't be able to happen...
ws.send(JSON.stringify({type: "JOIN_FAILED", reason: 'room'}));
} else if (message.name in rooms[message.room].players) {
ws.send(JSON.stringify({type: "JOIN_FAILED", reason: 'name'}));
} else {
let isAdmin = Object.keys(rooms[message.room].players).length === 0;
rooms[message.room].players[message.name] = {admin: isAdmin, name: message.name, connection: ws};
ws.send(JSON.stringify({type: "ROOM_JOINED", admin: isAdmin, players: Object.keys(rooms[message.room].players)}));
ws.room = message.room; ws.name = message.name;
if(!isAdmin) {
// broadcast join to other members of room
Object.keys(rooms[message.room].players).forEach(player => {
if(player !== message.name) {
rooms[message.room].players[player].connection.send(JSON.stringify({type: 'PLAYER_JOINED', name: message.name}));
}
});
}
if(Object.keys(rooms[message.room].players).length === 6)
rooms[message.room].open = false;
}
break;
case 'START_GAME':
// send GAME_STARTED to all other players in room
rooms[ws.room].open = false;
Object.keys(rooms[ws.room].players).forEach(player => {
if(player !== ws.name) {
rooms[ws.room].players[player].connection.send(JSON.stringify({type: 'GAME_STARTED'}));
}
});
setTimeout(() => {
rooms[ws.room].game = new coup.Game(rooms[ws.room].players);
}, 1000);
break;
case 'PING':
// keeping connection alive for Heroku
ws.send(JSON.stringify({type: 'PONG'}));
break;
default:
rooms[ws.room].game.onMessage(message);
break;
}
});
ws.on('close', function() {
if(ws.hasOwnProperty('room') && ws.hasOwnProperty('name') && ws.room in rooms) {
if(rooms[ws.room].game !== null && Object.keys(rooms[ws.room].players).length > 1) {
rooms[ws.room].game.playerLeft(ws.name);
}
if(ws.name in rooms[ws.room].players) {
let wasAdmin = rooms[ws.room].players[ws.name].admin;
delete rooms[ws.room].players[ws.name];
if(Object.keys(rooms[ws.room].players).length === 0) {
delete rooms[ws.room];
} else {
if(rooms[ws.room].game === null) {
if(wasAdmin) {
rooms[ws.room].players[Object.keys(rooms[ws.room].players)[0]].admin = true;
}
Object.keys(rooms[ws.room].players).forEach(player => {
rooms[ws.room].players[player].connection.send(JSON.stringify({type: 'PLAYER_LEFT', name: ws.name, updated_admin: rooms[ws.room].players[player].admin}));
});
if(Object.keys(rooms[ws.room].players.length < 6))
rooms[ws.room].open = true;
}
}
}
}
})
});
let forcessl = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
app.use(forcessl);
app.use(express.static(path.resolve(__dirname, 'client/public'), {
setHeaders: (res, path) => {
if (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css')) {
res.setHeader('Cache-Control', 'no-cache');
} else {
res.setHeader('Cache-Control', 'public, max-age=31536000');
}
},
}));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client/public', 'index.html'));
});
app.post("/create-room", (req, res) => {
clearInterval(interval);
let id = Math.floor(1000 + Math.random() * 9000);
while(id in rooms) {
id = Math.floor(1000 + Math.random() * 9000);
}
rooms[id] = {game: null, open: true, room: id, players: {}};
interval = setInterval(() => {
Object.keys(rooms).forEach(room => {
if(Object.keys(rooms[room].players).length === 0) {
console.log('culling room ' + room);
delete rooms[room];
}
});
if(Object.keys(rooms).length === 0) {
clearInterval(interval);
}
}, 300000);
res.json({room: id})
});
// return 404 if room doesn't exist
app.post("/join-room", (req, res) => {
let body = req.body;
if(body.room in rooms) {
res.json({exists: true, open: rooms[body.room].open, curr_players: Object.keys(rooms[body.room].players)});
} else {
res.json({exists: false});
}
});
http.listen(port, () => {
console.log(`App listening on ${port}`);
});