-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
184 lines (147 loc) · 4.02 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
// /*
// * NOTE: This is a basic server.js file to test client-side/server-side interactions
// * with index.js/html whenever it's added
// */
// var fs = require("fs")
// var express = require("express");
// var user_data = require("./leaderboards.json");
// //var bodyParser = require("body-parser")
// console.log(user_data);
// var app = express();
// var port = process.env.PORT || 3001;
// //test
// //app.use(bodyParser.json())
// app.use(express.json())
// app.use(express.static('public'));
// app.post("/leaderboards/addEntry", function(req, res) {
// console.log("== req.body:", req.body)
// res.status(200).send()
// console.log("== user_data[" + person + "]:", user_data[person])
// })
// app.get("/ttt", function (req, res, next) {
// res.status(200).sendFile(__dirname + '/public/index.html');
// })
// //Sort LeaderBoard
// var i;
// var j;
// var key;
// for(i = 0; i < user_data.length();i++){
// key = user_data[i].score
// for(j = i-1; j >= 0; j--){
// if(user_data[j].score > key){
// user_data[j+1] = user_data[j]
// }
// }
// user_data[j+1].score = key
// }
// app.listen(port, function () {
// console.log("== Server is listening on port", port);
// })
/*
* NOTE: This is a basic server.js file to test client-side/server-side interactions
* with index.js/html whenever it's added
*/
/*
* NOTE: This is a basic server.js file to test client-side/server-side interactions
* with index.js/html whenever it's added
*/
var fs = require("fs")
var express = require("express");
var exphbs = require("express-handlebars");
var user_data = require("./leaderboards.json");
//var bodyParser = require("body-parser")
console.log(user_data);
var app = express();
var port = process.env.PORT || 3001;
app.engine('handlebars', exphbs({defaultLayout: 'main'}))
app.set('view engine', 'handlebars')
//test
//app.use(bodyParser.json())
app.use(express.json())
app.use(express.static('public'));
app.get(["/ttt", "/"], function (req, res, next) {
res.status(200).render('gamePage',{
user_data
});
})
app.post('/user/add', function (req, res, next) {
var person = req.body
console.log("== person:", person)
//read leaderboards.json
//check for duplicate name
if (user_data[person.name]){
console.log("user found")
//update name/score if exists already
user_data[person.name].score += person.score
sortUserData()
fs.writeFile(__dirname + "/leaderboards.json",
JSON.stringify(user_data, null, 2),
function (err) {
if(err){
res.status(500).send("Error writing new data. Try again")
} else{
// console.log("before sort funct")
// sortUserData()
res.status(200).send()
}
})
//else push new name/score to leaderboards.json
} else {
console.log("new user")
user_data[person.name] = {
"score": person.score
}
sortUserData()
fs.writeFile(__dirname + "/leaderboards.json",
JSON.stringify(user_data, null, 2),
function (err) {
if(err){
res.status(500).send("Error writing new data. Try again")
} else{
res.status(200).send()
}
})
}
})
function sortUserData() {
var i = 0
var name_arr = []
var score_arr = []
var sorted = false;
//separate "name" and "score" into two different arrays
for (key in user_data) {
name_arr.push(key)
score_arr.push(user_data[key].score)
}
//sort these two arrays based on a person's score
while(!sorted) {
sorted = true;
for (i = 0; i < name_arr.length; i++) {
if (i-1 >= 0) {
if (score_arr[i-1] < score_arr[i]) {
sorted = false;
let sc_temp = score_arr[i-1]
score_arr[i-1] = score_arr[i]
score_arr[i] = sc_temp
let nm_temp = name_arr[i-1]
name_arr[i-1] = name_arr[i]
name_arr[i] = nm_temp
}
}
}
}
//rewrite our user data, recombining name and score arrays
user_data = {}
for (i = 0; i < name_arr.length; i++) {
user_data[name_arr[i]] = {
"score": score_arr[i]
}
}
console.log(user_data)
}
app.get('*', function (req, res, next) {
res.status(404).render('404');
})
app.listen(port, function () {
console.log("== Server is listening on port", port);
})