-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
131 lines (123 loc) · 5.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Dependencies
const express = require('express');
const http = require('http');
const path = require('path');
const { Server } = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const PORT = process.env.PORT || 5000;
app.set('port', PORT);
app.use('/', express.static(__dirname + '/static'));
// Routing
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname, 'index.html'));
});
let GAMEINFO = [];
let CONNECTIONS = [];
let TOTALS = []
// Starts the server.
server.listen(PORT, function() {
console.log('Starting server on port ' + PORT);
});
// Add the WebSocket handlers
io.on('connection', socket => {
socket.on('join-lobby', data => {
//Join game
const joinPair = JSON.parse(data);
joinPair.socket = socket;
CONNECTIONS.push(joinPair);
console.log(`${joinPair.user} requested to join game ${joinPair.game}.`);
//Set starting life total to 0
TOTALS.push({user: joinPair.user, tag: "life", value: 0})
//Check if initialization is needed
if (CONNECTIONS.filter(conn => conn.game === joinPair.game).length > 1) {
console.log("Existing game found.")
//If game exists, set life total to the starting life total preference if it exists
let foundStartingLife = GAMEINFO.filter( pref => pref.tag === "starting-total" && pref.game === joinPair.game);
if (foundStartingLife.length > 0) {
for (var t = 0; t < TOTALS.length; t++) {
if (TOTALS[t].user === joinPair.user && TOTALS[t].tag === "life"){
TOTALS[t].value = foundStartingLife[0].value;
}
}
}
//Send life totals to the client
getAllTotals(joinPair.game, socket);
} else {
//If game not found, pass over to the initialization workflow
console.log("No existing game found, starting init process...");
socket.emit('request-init');
}
//Make user ID leave all other lobbies
for (var c = 0; c < CONNECTIONS.length; c++){
if (CONNECTIONS[c].user === joinPair.user && CONNECTIONS[c].game !== joinPair.game){
CONNECTIONS[c].pop();
}
}
});
socket.on('join-lobby-twitch', data => {
//Join game
const joinPair = JSON.parse(data);
joinPair.socket = socket;
CONNECTIONS.push(joinPair);
console.log(`${joinPair.user} requested to join game ${joinPair.game} as a spectator.`);
//Send life totals to the client
getAllTotals(joinPair.game, socket);
});
socket.on('set-prefs', prefsString => {
let prefs = JSON.parse(prefsString);
GAMEINFO.push(prefs);
console.log(`Preference ${prefs.tag} set to ${prefs.value} for game ${prefs.game}.`);
if (prefs.tag === "starting-total") {
let users = [];
for (var connectionsIndex = 0; connectionsIndex < CONNECTIONS.length; connectionsIndex++) {
if (CONNECTIONS[connectionsIndex].game === prefs.game) {
users.push(CONNECTIONS[connectionsIndex].user)
}
}
for (var totalsIndex = 0; totalsIndex < TOTALS.length; totalsIndex++) {
if (users.includes(TOTALS[totalsIndex].user) && TOTALS[totalsIndex].tag === "life"){
TOTALS[totalsIndex].value = prefs.value
}
}
}
getAllTotals(prefs.game, socket);
});
socket.on('set-total', totalString => {
let total = JSON.parse(totalString);
let matchingTotal = TOTALS.filter(tempTotal => tempTotal.user === total.user && tempTotal.tag === total.tag);
if (matchingTotal.length > 0) {
matchingTotal[0].value = total.value;
} else {
TOTALS.push(total);
}
console.log(`Total ${total.tag} for user ${total.user} set to ${total.value}.`);
//Push update to all users in game
let game = CONNECTIONS.filter(con => con.user === total.user)[0].game
for (var c = 0; c < CONNECTIONS.length; c++){
if (CONNECTIONS[c].game === game){
console.log(`Pushing total change to ${CONNECTIONS[c].user}.`)
CONNECTIONS[c].socket.emit("lifetotal-change", JSON.stringify(total));
}
}
});
socket.emit('message', 'Server connected.');
});
function getAllTotals(game, socket){
let users = [];
for (var connectionsIndex = 0; connectionsIndex < CONNECTIONS.length; connectionsIndex++) {
if (CONNECTIONS[connectionsIndex].game === game) {
users.push(CONNECTIONS[connectionsIndex].user)
}
}
let totals = [];
for (var totalsIndex = 0; totalsIndex < TOTALS.length; totalsIndex++) {
if (users.includes(TOTALS[totalsIndex].user)){
totals.push(TOTALS[totalsIndex]);
}
}
console.log("Sending totals to client for game " + game);
console.log(JSON.stringify(totals));
socket.emit('joined-existing-lobby', JSON.stringify(totals))
}