-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
281 lines (277 loc) · 8.12 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Created by Maxwell on 2/14/2015.
*/
var express = require("express");
var http = require("http");
var app = express();
var server = http.createServer(app);
var io = require("socket.io")(server);
var bodyParser = require("body-parser");
var classes = require("./server/js/classes.js");
var port = process.env.PORT || 80;
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use("/js", express.static(__dirname + "/client/js"));
app.use("/css", express.static(__dirname + "/client/css"));
app.use("/fonts", express.static(__dirname + "/client/fonts"));
app.use("/views/about", express.static(__dirname + "/client/views/about/index.html"));
app.get("/", function (request, response)
{
response.sendFile(__dirname + "/client/views/index.html");
});
app.get(/^(.+)$/, function(req, res)
{
console.log("static file request : " + req.params[0]);
res.sendFile( __dirname + req.params[0]);
});
server.listen(port, function()
{
console.log("Listening on port " + port);
});
/*Socket connection functions*/
io.on("connection", function(socket)
{
socket.on("register", function(data)
{
var id = data.id;
var name = data.name;
var password = data.password;
var nameExists = false;
if(id == socket.id)
{
for(var i = 0; i < players.length; i +=1)
{
if(players[i].name == name)
{
nameExists = true;
}
}
if(!nameExists)
{
var player = new classes.Player(id, name, password);
players.push(player);
socket.emit("register-success", {player: player});
}
else
{
socket.emit("register-fail", {message: "That name is taken, please try another."});
}
}
else
{
console.log("ERROR: Socket ID: " + socket.id + " != User ID: " + id);
socket.emit("register-fail", {message: "An error occurred on the server."});
}
});
socket.on("login", function(data)
{
var id = data.id;
var name = data.name;
var password = data.password;
var nameExists = false;
var servPlayer;
if(id == socket.id)
{
for(var i = 0; i < players.length; i +=1)
{
if(players[i].name == name)
{
nameExists = true;
servPlayer = players[i];
players[i].id = data.id;
}
}
if(nameExists)
{
if(password == servPlayer.password)
{
socket.emit("login-success", {player: servPlayer});
}
else
{
socket.emit("login-fail", {message: "Incorrect Password"});
}
}
else
{
socket.emit("login-fail", {message: "That name does not exist. Please register first."});
}
}
else
{
socket.emit("register-fail", {message: "An error occurred on the server."});
}
});
socket.on("gameInput", function(data)
{
if(canDoInput)
{
var name = data.name;
var input = data.input;
var hasDoneInput = false;
for (var i = 0; i < gameInput.length; i +=1)
{
if(gameInput[i].name == name)
{
//gameInput[i].input = input;
hasDoneInput = true;
}
}
if(!hasDoneInput)
{
gameInput.push(data);
for(var i = 0; i < players.length; i +=1)
{
if(name == players[i].name)
{
players[i].numGames += 1;
}
}
}
else
{
for(var i = 0; i < gameInput.length; i += 1)
{
if(gameInput[i].name == name)
{
gameInput[i].input = input;
}
}
}
console.log(gameInput);
}
else
{
socket.emit("cannot-input", {message: "You cannot do input now."});
}
});
socket.on("get-player", function(data)
{
for(var i = 0; i < players.length; i += 1)
{
if(players[i].id == data.id)
{
socket.emit("out-player", {player: players[i]});
}
}
});
});
/************************/
/*Game Related Variables*/
/************************/
/*The last second count at which the user was informed of the game state*/
var lastEmitTime;
/*A boolean dictating when a player may or may not give input*/
var canDoInput;
/*The game, consisting of an adjective and 3 nouns*/
var game = new classes.Game();
/*The list of all players*/
var players = [];
/*The list of players who gave input for the current game as well as their input*/
var gameInput = [];
/*Determines whether or not to score the current game*/
var dataScored = false;
/************************/
setInterval(function()
{
var time = new Date();
var seconds = time.getSeconds();
if(lastEmitTime != seconds)
{
/*Game States*/
switch(true)
{
/*Reset*/
/*In this game state, reset the game variables*/
case (seconds == 0):
game.state = "Reset"
canDoInput = false;
dataScored = false;
gameInput = [];
io.emit("out-result", {results: gameInput});
game = new classes.Game();
break;
/*Input*/
/*In this game state, the player is allowed to do input*/
case (seconds <= 50):
game.state = "Input"
canDoInput = true;
break;
/*Results*/
/*In this game state, report the results of the game to the players*/
default:
game.state = "Results"
canDoInput = false;
if(!dataScored)
{
scoreData();
dataScored = true;
io.emit("out-result", {results: gameInput});
}
break;
}
io.emit("get-time-game", {second : seconds, game: game});
lastEmitTime = seconds;
};
}, 60);
/*Calculate the results from a given game and return the data*/
function scoreData()
{
var input1Score = 0;
var input2Score = 0;
var input3Score = 0;
for(var i = 0; i < gameInput.length; i +=1)
{
switch(gameInput[i].input)
{
case 1:
input1Score += 1;
break;
case 2:
input2Score += 1;
break;
case 3:
input3Score += 1;
break;
default:
console.log("Player made an invalid input");
break;
};
}
var check = [
{value: 1, score: input1Score},
{value: 2, score: input2Score},
{value: 3, score: input3Score}
];
var toReward = [check[0]];
for(var i = 1; i < check.length; i += 1) {
if (check[i].score > toReward[0].score) {
toReward = [];
toReward.push(check[i])
}
else if (check[i].score === toReward[0].score) {
toReward.push(check[i])
}
}
io.emit("got-winners", {winners: toReward});
for(var i = 0; i < toReward.length; i +=1)
{
rewardScore(toReward[i].value);
}
};
function rewardScore(score)
{
for(var i = 0; i < gameInput.length; i += 1)
{
for(var j = 0; j < players.length; j += 1)
{
if(gameInput[i].name === players[j].name)
{
if(gameInput[i].input == score)
{
players[j].numWins += 1;
}
}
}
}
};