-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_server.js
423 lines (391 loc) · 13.5 KB
/
data_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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//PRS Showdown
//Authors: Sadi Gulcelik, Ayesha Ali
//Date: 1/18/19
//required packages
var express = require('express');
var fs = require('fs');
var favicon = require('serve-favicon');
var app = express();
//variables for login and villain strategies
var villainPrevious=randomChoice();
var userPrevious=randomChoice();
var villainWeapon;
var userName;
var userPSWD;
var error = false;
var svgNames=["the_boss","the_magician","harry","gato","bones","manny","comic_hans","mickey","pixie","regal","spock","mr_modern"];
var colors=["red","blue","green","white","olive","yellow","orange","purple", "navy", "gray", "fuchsia", "lime"];
var svgExtensions=["_waiting","_rock","_scissors","_paper"];
//changes the colors of the svgs (currently called every login)
function changeColors(){
var tempColors=colors.slice(0);
for (var k=0;k<svgNames.length;k++){
var index =Math.floor(Math.random()*tempColors.length);
chosenColor=tempColors[index];
for (var j=0; j<svgExtensions.length;j++){
svgName=__dirname+"/public/images/"+svgNames[k]+svgExtensions[j]+".svg";
if(!svgName.includes("regal_waiting")){
var svgToEdit=fs.readFileSync(svgName, "utf8");
var out=svgToEdit.split("fill");
var output=out[0];
for(var i=1;i<out.length;i++){
output+="fill:"+chosenColor+out[i].substring(out[i].indexOf('"'),out[i].length);
}
fs.writeFileSync(svgName,output, "utf8");
} else {
var svgToEdit=fs.readFileSync(svgName, "utf8");
var out=svgToEdit.split("stroke:");
var output=out[0];
//console.log(out);
for(var i=1;i<out.length;i++){
output+="stroke:"+chosenColor+out[i].substring(out[i].indexOf('"'),out[i].length);
}
fs.writeFileSync(svgName,output, "utf8");
}
}
tempColors.splice(index,1)
}
}
//set up server
app.use(express.static('public'));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(favicon(__dirname + '/public/images/logo.png'));
var port = process.env.PORT || 3000;
app.listen(port, function(){
console.log('Server started at '+ new Date()+', on port ' + port+'!');
});
//first request, renders index
app.get('/', function(request, response){
var user_data={};
userName = "";
userPSWD = "";
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('index', {page:request.url, user:user_data, title:"Index"});
});
//login request; renders either index if password is wrong or game if new user created or correct login entered
app.get('/login', function(request, response){
changeColors();
//set up data
var user_data={};
user_data["name"] = request.query.player_name;
user_data["pswd"] = request.query.pswd;
userName = user_data["name"];
userPSWD = user_data["pswd"];
//manage users in CSV
var csv_data = loadCSV("data/users.csv");
if (user_data["name"] == "") {//if someone accidentally submits login w/o entering anything
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('index', {page:request.url, user:user_data, title:"Index"});
}
if (!findUser(user_data,csv_data,request,response, "logged in")){ //if user isn't found in CSV
newUser(user_data); //create new user
csv_data.push(user_data);
upLoadCSV(csv_data, "data/users.csv");
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('game', {page:request.url, user:user_data, title:"game"});
}
});
//request for throw choice
app.get('/:user/results', function(request, response){
//set up data
var user_data={
name: request.params.user,
weapon: request.query.weapon,
villain: request.query.villain
};//send more stuff under user data
if (fs.existsSync("data/villainPrevious.txt")) {
villainPrevious=fs.readFileSync("data/villainPrevious.txt",'utf8');
}
if (fs.existsSync("data/userPrevious.txt")) {
userPrevious=fs.readFileSync("data/userPrevious.txt",'utf8');
}
if (user_data.weapon=="error"||user_data.villain=="error"){
error = true;
return response.redirect('/playAgain');
} else{
user_data["result"] = handleThrow(user_data.weapon, user_data.villain);
user_data["response"] =villainWeapon;
fs.writeFileSync("data/villainPrevious.txt",villainWeapon,'utf8')
fs.writeFileSync("data/userPrevious.txt",user_data.weapon,'utf8')
var user_csv = loadCSV("data/users.csv");
for (var i = 0; i < user_csv.length; i++) {
if (user_csv[i]["name"] == user_data.name) {
user_csv[i][user_data.weapon] +=1;
user_csv[i]["total_games"]+=1;
switch(user_data["result"]){
case "won":
user_csv[i]["wins"] +=1;
break;
case "lost":
user_csv[i]["losses"] +=1;
break;
}
}
}
//manage villain CSV for games, wins, losses, and weapon used
upLoadCSV(user_csv, "data/users.csv");
var villains_csv = loadCSV("data/villains.csv");
for (var i = 0; i < villains_csv.length; i++) {
if (villains_csv[i]["name"] == user_data.villain) {
villains_csv[i][user_data.response] +=1;
villains_csv[i]["total_games"]+=1;
switch(user_data["result"]){
case "lost":
villains_csv[i]["wins"] +=1;
break;
case "won":
villains_csv[i]["losses"] +=1;
break;
}
}
}
upLoadCSV(villains_csv, "data/villains.csv");
//render results
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('results',{page:request.url, user:user_data, title:"results"});}
});
//request for when user wants to play again; basically exactly the same as the login request w/o having to log in again
app.get('/playAgain', function(request, response){
//use the saved username and password which resets when you return to login page
var user_data={};
user_data["name"] = userName;
user_data["pswd"] = userPSWD;
var csv_data = loadCSV("data/users.csv");
//if the saved username is empty than return to index page
if (user_data["name"] == "") {
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('index', {page:request.url, user:user_data, title:"Index"});
}
if (!findUser(user_data,csv_data,request,response, "playGame")){
newUser(user_data);
csv_data.push(user_data);
upLoadCSV(csv_data, "data/users.csv");
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('game', {page:request.url, user:user_data, title:"playGame"});
}
});
//handles a request for the rules page (sends the user to the rules page)
app.get('/rules', function(request, response){
user_data = {}
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('rules', {page:request.url, user:user_data, title:"rules"});
});
//handles a request for the rules page (inputs the necessary data and sends the user to the newly rendered stats page)
app.get('/stats', function(request, response){
var user_data = loadCSV("data/users.csv");
var villain_data = loadCSV("data/villains.csv")
var data = {};
data["player"] = user_data;
data["villain"] = villain_data
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('stats', {page:request.url, user:data, title:"stats"});
});
//handles a request for the rules page (sends the user to the rules page)
app.get('/about', function(request, response){
user_data = {};
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('about', {page:request.url, user:user_data, title:"about"});
});
//loads villains.csv or users.csv, returning an object that contains the data contained in the spreadsheet
function loadCSV(filename) {
var users_file = fs.readFileSync(filename, "utf8");
//console.log(users_file);
var rows = users_file.split('\n');
var user_data = [];
for(var i = 0; i < rows.length; i++) {
var user_d = rows[i].trim().split(",");
var user = {};
if (filename == "data/users.csv") {
user["name"] = user_d[0];
user["pswd"] = user_d[1];
user["total_games"] = parseInt(user_d[2]);
user["wins"] = parseInt(user_d[3]);
user["losses"] = parseInt(user_d[4]);
user["rock"] = parseFloat(user_d[5]);
user["paper"] = parseFloat(user_d[6]);
user["scissors"] = parseFloat(user_d[7]);
user_data.push(user);
} else if (filename == "data/villains.csv") {
user["name"] = user_d[0];
user["total_games"] = parseInt(user_d[1]);
user["wins"] = parseInt(user_d[2]);
user["losses"] = parseInt(user_d[3]);
user["rock"] = parseFloat(user_d[4]);
user["paper"] = parseFloat(user_d[5]);
user["scissors"] = parseFloat(user_d[6]);
user_data.push(user);
}
}
return user_data;
}
//uploads the csv containing all the gameplay data (used to update villains.csv and users.csv)
function upLoadCSV(user_data, file_name) {
var out="";
user_data.sort(function(a,b) {
var bPercent = 0;
if (b.total_games == 0) {
bPercent = 0;
} else {
bPercent =Math.round((b.wins/b.total_games)*100);
}
var aPercent = 0;
if (a.total_games == 0) {
aPercent = 0;
} else {
aPercent =Math.round((a.wins/a.total_games)*100);
}
return (bPercent-aPercent);
});
for (var i = 0; i < user_data.length; i++) {
arr=Object.keys(user_data[i]);
for (var k=0;k<arr.length;k++){
if(k == arr.length-1) {
out+=user_data[i][arr[k]];
} else {
out+=user_data[i][arr[k]]+",";
}
}
if (i!=user_data.length-1){
out+="\n";
}
}
fs.writeFileSync(file_name, out, "utf8")
}
//creates a new User
function newUser(user_data) {
user_data["games"] =0;
user_data["total_games"] =0;
user_data["wins"] =0;
user_data["losses"] =0;
user_data["rock"] =0;
user_data["paper"] =0;
user_data["scissors"] = 0;
}
//checks to see if a user's login information correspond to an actual user
function findUser(user_data,csv_data,request,response, titleN){
for (var i = 0; i < csv_data.length; i++) {
if (csv_data[i].name == user_data["name"]) {
if (csv_data[i].pswd == user_data["pswd"]) {
response.status(200);
response.setHeader('Content-Type', 'text/html')
if (error) {
error = false;
response.render('game', {page:request.url, user:user_data, title:"valid"});
} else {
response.render('game', {page:request.url, user:user_data, title:titleN});
}
return true;
break;
} else {
user_data["failure"] = 4;
userName = "";
userPSWD = "";
response.status(200);
response.setHeader('Content-Type', 'text/html')
response.render('index', {page:request.url, user:user_data, title:"Index"});
return true;
break;
}
}
}
return false;
}
//handles a user's throw, returning the result and setting the value of the global variable villainWeapon
function handleThrow(userWeapon, villain){
villainWeapon=villainStrategies(villain,villainPrevious,userPrevious,userWeapon);
switch(userWeapon){
case villainWeapon:
return("drew");
case winAgainst(villainWeapon):
return("won");
case loseAgainst(villainWeapon):
return("lost");
}
}
//makes a random choice of weapon for use in the default scenario
function randomChoice(){
var choices=["rock","paper","scissors"];
return choices[(3*Math.random())|0];
}
//calculates the villain's choice of weapon based on the inputs and the villain's possible strategies
function villainStrategies(villain,villainPrevious,userPrevious,userCurrent){
var rand=Math.random();
var choice=randomChoice();
switch(villain){
case "Bones":
if (rand>0.5)
return choice;
else
return villainPrevious;
case "Comic_Hans":
if (rand>0.7)
return choice;
else
return loseAgainst(userCurrent)
case "Gato":
return choice;
case "Harry":
return choice;
case "Manny":
return choice;
case "Mickey":
if(rand>0.6)
return loseAgainst(villainPrevious);
else
return choice;
case "Mr_Modern":
if(rand>0.7)
return "rock";
else
return choice;
case "Pixie":
return loseAgainst(userPrevious);
case "Regal":
if (rand>0.4)
return winAgainst(userPrevious);
else
return winAgainst(userCurrent);
case "The_Boss":
return winAgainst(userCurrent);
case "The_Magician":
return choice;
case "Spock":
return choice;
}
}
//returns the weapon that defeats the input weapon
function winAgainst(weapon){
switch(weapon){
case "rock":
return "paper";
case "paper":
return "scissors";
case "scissors":
return "rock";
/*default:
return "Mjölnir"*/
}
}
//returns the weapon that loses to the input weapon
function loseAgainst(weapon){
switch(weapon){
case "rock":
return "scissors";
case "paper":
return "rock";
case "scissors":
return "paper";
/*default:
return "Mjölnir"*/
}
}