forked from cs4241-21a/a2-shortstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.improved.js
More file actions
218 lines (163 loc) · 5.89 KB
/
Copy pathserver.improved.js
File metadata and controls
218 lines (163 loc) · 5.89 KB
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
const http = require('http'),
fs = require('fs'),
// IMPORTANT: you must run `npm install` in the directory for this assignment
// to install the mime library used in the following line of code
mime = require('mime'),
dir = 'public/',
port = 3000
let appdata = []
let winner = []
let sum = 0;
const server = http.createServer(function(request, response) {
if (request.method === 'GET') {
handleGet(request, response)
} else if (request.method === 'POST') {
handlePost(request, response)
}
})
const handleGet = function(request, response) {
console.log("handleGet")
const filename = dir + request.url.slice(1)
if (request.url === '/') {
sendFile(response, 'public/index.html')
} else {
sendFile(response, filename)
}
}
//handle post requests for submitting, deleting, and modifying data
const handlePost = function(request, response) {
if (request.url === "/submit") {
submit(request, response)
} else if (request.url === "/delete") {
remove(request, response)
} else if (request.url === "/modify") {
modify(request, response)
}
}
//remove entry from data by passing in the unique ID of the entry
const remove = function(request, response) {
console.log("remove")
let dataString = ''
request.on('data', function(data) { //retrieving passed in data from scripts.js
dataString += data
})
request.on('end', function() {
console.log(dataString)
let id = parseInt(JSON.parse(dataString))
index = appdata.findIndex(player => player.id === id);
appdata.splice(index, 1);
var json = JSON.stringify(appdata);
response.writeHead(200, "OK")
response.end(json)
})
}
//modify entry from data by passing in the unique ID of the entry
const modify = function(request, response) {
console.log("modify")
let dataString = ''
request.on('data', function(data) { //retrieving passed in data from scripts.js
dataString += data
})
request.on('end', function() {
console.log("Datastring: " + dataString)
let playerData = JSON.parse(dataString)
const score = parseInt(playerData['score'])
const firstname = String(playerData['firstname'])
const lastname = String(playerData['lastname'])
const id = parseInt(playerData['id'])
index = appdata.findIndex(player => player.id === id);
appdata.splice(index, 1);
var player = { 'rank': 0, 'score': score, 'firstname': firstname, 'lastname': lastname, 'id': id }
var newAppdata = []
order(newAppdata, player)
appdata = newAppdata
rank(appdata)
var json = JSON.stringify(appdata);
response.writeHead(200, "OK")
response.end(json)
})
}
//add a new entry into the data
const submit = function(request, response) {
console.log("submit")
let dataString = ''
request.on('data', function(data) { //retrieving passed in data from scripts.js
dataString += data
})
request.on('end', function() {
let playerData = JSON.parse(dataString)
const score = parseInt(playerData['score'])
const firstname = String(playerData['firstname'])
const lastname = String(playerData['lastname'])
const id = parseInt(playerData['id'])
console.log("Score: " + score)
console.log("Firstname: " + firstname)
console.log("Lastname: " + lastname)
console.log("ID: " + id)
var player = { 'rank': 0, 'score': score, 'firstname': firstname, 'lastname': lastname, 'id': id }
var newAppdata = []
order(newAppdata, player)
appdata = newAppdata
rank(appdata)
console.log(appdata)
var json = JSON.stringify(appdata);
response.writeHead(200, "OK")
response.end(json) //what you put in end(..) gets returned from fetch request
})
}
//Given a new entry object, place it in the correct order from highest score to lowest score
const order = function(newAppdata, player) {
if (appdata.length === 0) {
newAppdata.push(player)
} else {
let isAdded = false
for (let i = 0; i < appdata.length; i++) {
if (player.score >= appdata[i].score && isAdded === false) {
console.log("1st Condition")
newAppdata.push(player)
newAppdata.push(appdata[i])
isAdded = true
} else if (i === appdata.length - 1 && isAdded === false) {
console.log("2nd Condition")
newAppdata.push(appdata[i])
newAppdata.push(player)
} else {
console.log("3rd Condition")
newAppdata.push(appdata[i])
}
}
}
}
//Rank the entries from 1st for the highest score to lowest score
const rank = function(appdata) {
let currentRank = 1
let pastScore = -1
for (let i = 0; i < appdata.length; i++) {
if (pastScore === -1) {
pastScore = appdata[i].score
appdata[i].rank = currentRank
} else if (pastScore === appdata[i].score) {
appdata[i].rank = currentRank
} else {
currentRank++
pastScore = appdata[i].score
appdata[i].rank = currentRank
}
}
}
const sendFile = function(response, filename) {
const type = mime.getType(filename)
fs.readFile(filename, function(err, content) {
// if the error = null, then we've loaded the file successfully
if (err === null) {
// status code: https://httpstatuses.com
response.writeHeader(200, { 'Content-Type': type })
response.end(content)
} else {
// file not found, error code 404
response.writeHeader(404)
response.end('404 Error: File Not Found')
}
})
}
server.listen(process.env.PORT || port)