-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·244 lines (208 loc) · 5.78 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
var path = require('path');
var express = require('express');
var exphbs = require('express-handlebars');
var fs = require('fs');
var bodyParser = require('body-parser');
var suspectData = require('./suspectData');
var app = express();
var port = process.env.PORT || 3000;
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res, next) //Renders main page for use
{
if (req.url === '/')
{
var peopleList = Object.keys(suspectData);
var index = Math.floor(Math.random() * peopleList.length)
var randMurderIndex = peopleList[index];
var murderData = suspectData[randMurderIndex];
murderData.murderer +=1;
var hintArray = murderData; //array to hold randomly chosen hints
if (hintArray.hints.length !== 3) //loop through and randomly delete hints until three are sent to be served to page
{
for (var i = 0; i < hintArray.hints.length - 3; i++)
{
console.log("in");
var random = Math.floor(Math.random() * hintArray.hints.length)
hintArray.hints.splice(random, 1);
}
}
console.log("Murderer: ", murderData);
console.log("hintsArray: ", hintArray.hints);
var suspectArgs =
{
suspectPeople: hintArray.hints,
murdererName: JSON.stringify(murderData),
murdererIndex: index,
suspect: suspectData
}
res.render('mainPage', suspectArgs);
res.status(200);
}
else
{
next();
}
});
app.get('/main', function(req, res, next) //Renders main page for use
{
if (req.url === '/main')
{
var peopleList = Object.keys(suspectData);
var index = Math.floor(Math.random() * peopleList.length)
var randMurderIndex = peopleList[index];
var murderData = suspectData[randMurderIndex];
murderData.murderer +=1;
var hintArray = murderData; //array to hold randomly chosen hints
if (hintArray.hints.length !== 3) //loop through and randomly delete hints until three are sent to be served to page
{
for (var i = 0; i < hintArray.hints.length - 3; i++)
{
console.log("in");
var random = Math.floor(Math.random() * hintArray.hints.length)
hintArray.hints.splice(random, 1);
}
}
console.log("Murderer: ", murderData);
console.log("hintsArray: ", hintArray.hints);
var suspectArgs =
{
suspectPeople: hintArray.hints,
murdererName: JSON.stringify(murderData),
murdererIndex: index,
suspect: suspectData
}
res.render('mainPage', suspectArgs);
res.status(200);
}
else
{
next();
}
});
app.get('/suspects', function(req, res, next) //Renders page which shows suspects
{
var suspectArgs =
{
suspect: suspectData
}
res.render('suspects', suspectArgs);
res.status(200);
});
app.get('/suspectInfo/:suspect', function(req, res, next)
{
var suspect = req.params.suspect;
var singleData = suspectData[suspect];
var peopleList = Object.keys(suspectData);
var suspectsArray = [];
if (singleData)
{
for (var i = 0; i < suspectData.length; i++)
{
suspectsArray[i] = suspectData[peopleList[i]]; //populate holder array
}
for (var i = 0; i < suspectsArray.length; i++)
{
if (singleData === suspectsArray[i])
{
suspectsArray.splice(i, 1);
break;
}
}
var suspectArgs =
{
name: singleData.name,
url: singleData.url,
about: singleData.about,
suspect: singleData,
suspects: suspectsArray
}
res.render('suspectInfo', suspectArgs);
res.status(200);
}
else
{
next();
}
});
app.post('/suspect/:suspect/addSuspect', function(req, res, next)
{
var editSuspect = suspectData[req.params.suspect];
console.log(editSuspect);
if (editSuspect)
{
if (req.body)
{
var newHint =
{
hint: req.body.hint
};
console.log(newHint);
editSuspect.hints = editSuspect.hints || [];
editSuspect.hints.push(newHint);
fs.writeFile('suspectData.json', JSON.stringify(suspectData), function (err)
{
if (err)
{
res.status(500).send("Unable to save hint to \"database\".");
}
else
{
res.status(200).send();
}
});
}
else
{
res.status(400).send("Hint field is missing.");
}
}
else
{
console.log("In the else.");
next();
}
});
app.get('/about', function(req, res, next) //Renders about page
{
res.render('about');
res.status(200);
});
app.get('/statistics', function(req, res, next)
{
var peopleList = Object.keys(suspectData);
var leadersArray = []; //initilize storage for convicts
for (var i = 0; i < peopleList.length; i++)
{
leadersArray[i] = suspectData[peopleList[i]]; //populate with suspect's data
}
for (var i = 0; i < peopleList.length; i++) //bubble sort the suspects in order of greatest murder rate
{
for (var j = 0; j < peopleList.length - i - 1; j++)
{
if (leadersArray[j].murderer < leadersArray[j + 1].murderer)
{
var temp = leadersArray[j];
leadersArray[j] = leadersArray[j + 1];
leadersArray[j + 1] = temp;
}
}
}
var suspectArgs =
{
suspect: leadersArray
}
res.render('statistics', suspectArgs);
res.status(200);
});
app.get('*', function (req, res) //If file is not found catches and displays 404 error
{
res.render('404Page');
res.status(404);
});
app.listen(port, function ()
{
console.log("Server is running.", port);
});