forked from ku-eecs-capstone/lab2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
271 lines (252 loc) · 9 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
var express = require('express');
// Api Reference - https://github.com/expressjs/cookies
var Cookies = require( "cookies" );
// Api Reference - https://github.com/expressjs/body-parser
var bodyParser = require('body-parser')
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// Api Reference - https://github.com/felixge/node-mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
port : '8889',
database : 'adventure'
});
connection.connect();
var app = express();
/**
* Base Route
*
* Checks if a user is logged in, if not redirect to log in
*/
app.get('/', function(req, res){
var cookies = new Cookies(req, res);
if(cookies.get("user_id") == undefined) {
res.redirect('/users/login');
} else {
res.status(200);
res.sendFile(__dirname + "/index.html");
}
});
/**
* Users Routes
*
* get - returns a json array of user data based on cookie
* get login - returns login page
* post login - logs user in and sets cookies based on post data
* get register - returns register page
* post register - adds a user to the database
*/
app.get('/users', function(req, res){
// check user cookie
var cookies = new Cookies(req, res);
if(cookies.get('user_id')) {
var query = "SELECT `location_id` FROM `users` WHERE `id` = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, user) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
// get items as well
query = "SELECT `users_items`.*, `items`.`name` " +
"FROM users_items " +
"LEFT JOIN items ON users_items.item_id = items.id " +
"WHERE users_items.user_id = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, items) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
res.set({'Content-Type': 'application/json'});
res.status(200);
user[0]['items'] = items;
res.send(user[0]);
}
});
}
});
} else {
return;
}
});
app.get('/users/login', function(req, res){
res.status(200);
res.sendFile(__dirname + "/login.html");
});
app.post('/users/login', urlencodedParser, function(req, res){
// check post data for user in the database
var query = "SELECT `id`, `username` FROM `users` WHERE " +
"`username` = " + connection.escape(req.body.username) + " AND " +
"`password` = " + connection.escape(req.body.password);
connection.query(query, function(err, user) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
// if in database set cookies and send to base route
if(user[0]) {
var cookies = new Cookies(req, res);
cookies.set('user_id', user[0]['id']);
cookies.set('username', user[0]['username']);
res.redirect('/');
// otherwise back to login
} else {
res.redirect('/users/login');
}
}
});
});
app.get('/users/register', function(req, res){
res.status(200);
res.sendFile(__dirname + "/register.html");
});
app.post('/users/register', urlencodedParser, function(req, res){
// add user to database and redirect to login page
var query = "INSERT INTO `adventure`.`users` " +
"(`id`, `username`, `password`, `location_id`, `updated`) " +
"VALUES (NULL, " + connection.escape(req.body.username) + ", "
+ connection.escape(req.body.password) + ", '5', '');";
connection.query(query, function(err, user) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
if(user.affectedRows) {
// add items around the map and player invenotry
query = "SELECT * FROM `items`";
connection.query(query, function(err, items){
if(err) {
console.error('Error: ' + err.stack);
return;
} else {
for(var i in items) {
query = "INSERT INTO `adventure`.`users_items` " +
"(`id`, `user_id`, `item_id`, `location_id`) " +
"VALUES (NULL, '" + user.insertId + "', '"
+ items[i]['id'] + "', '" + items[i]['default_location_id'] + "');";
connection.query(query, function(err, item) {
if (err) {
console.error('Error: ' + err.stack);
return;
}
});
}
res.redirect('/');
}
});
}
}
});
});
app.get('/users/logout', function(req, res){
// destroy cookies
var cookies = new Cookies(req, res);
cookies.set('user_id', undefined);
cookies.set('username', undefined);
// redirect to login page
res.redirect('/users/login');
});
/**
* Location Routes
*
* get - returns a locations information based off id
* post - updates the users last location
* put - use an item at the desired location
* delete - pick up an item from a location
*/
app.get('/location/:id', function(req, res){
// fetch database for location and related item
var query = "SELECT `locations`.* " +
"FROM `locations` " +
"WHERE `locations`.`id` = " + connection.escape(req.params.id);
connection.query(query, function(err, location) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
res.set({'Content-Type': 'application/json'});
res.status(200);
res.send(location[0]);
}
});
});
app.post('/location/:id', function(req, res){
// update user based on cookie
var cookies = new Cookies(req, res);
var query = "UPDATE `adventure`.`users` " +
"SET `location_id` = " + connection.escape(req.params.id) + " " +
"WHERE `users`.`id` = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, location) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
// if in database return json array
if(location.affectedRows) {
res.status(200);
} else {
return;
}
}
});
});
/**
* Image Routes
*/
app.get('/img/:name', function(req, res){
res.status(200);
res.sendFile(__dirname + "/img/" + req.params.name);
});
/**
* Inventory Routes
*
* put - update inventory item to location
* delete - update inventory item to 0 (in pocket)
*/
app.put('/inventory/:user_item_id/:location_id/:item_id', function(req, res){
// update user based on cookie
var cookies = new Cookies(req, res);
var query = "UPDATE `adventure`.`users_items` " +
"SET `location_id` = " + connection.escape(req.params.location_id) + " " +
"WHERE `users_items`.`id` = " + connection.escape(req.params.user_item_id) + " " +
"AND `users_items`.`user_id` = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, user_item) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
// check for special events
// laptop at eaton
if(req.params.item_id == 5 && req.params.location_id == 3) {
// send player to secret room
query = "UPDATE `users` SET `location_id` = 11 WHERE " +
"`users`.`id` = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, result) {
if(err) {
console.error('Error: ' + err.stack);
return
}
});
}
res.status(200);
}
});
});
app.delete('/inventory/:user_item_id', function(req, res){
// update user based on cookie
var cookies = new Cookies(req, res);
var query = "UPDATE `adventure`.`users_items` " +
"SET `location_id` = 0 " +
"WHERE `users_items`.`id` = " + connection.escape(req.params.user_item_id) + " " +
"AND `users_items`.`user_id` = " + connection.escape(cookies.get('user_id'));
connection.query(query, function(err, user_item) {
if (err) {
console.error('Error: ' + err.stack);
return;
} else {
res.status(200);
}
});
});
app.listen(3000);