-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
310 lines (269 loc) · 8.39 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
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
const express = require("express");
const session = require("express-session");
const axios = require("axios");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
resave: false,
saveUninitialized: false,
cookie: {maxAge: 86400000}, // One day
secret: "qweasdzxczxcasdqwe"
}));
app.use(express.static('client'));
// Favicon
// crypto
const WEATHER_BUFFER_SIZE = 10;
const WEATHER_EXPIRATION_TIME = 86400000;
let WEATHER_BUFFER = [];
const API_TO_PREC = [0, 0.25, 1, 4, 10, 16, 30, 50, 75, 100];
app.post('/weather', async (req, res) => {
let coords = {
lat: Math.round(req.body.coords.lat),
lon: Math.round(req.body.coords.lon)
};
for (let i = 0; i < WEATHER_BUFFER.length; i++) {
let data = WEATHER_BUFFER[i];
// If data is too old - remove
if (Date.now() - data.recorded > WEATHER_EXPIRATION_TIME) {
WEATHER_BUFFER.splice(i, 1);
i--;
continue;
}
// If data exists - send it
if (data.lat === coords.lat && data.lon === coords.lon) {
res.status(200).send(data.weather);
return;
}
};
// Make new entry
let weather = [];
let failed = false;
await axios.get(`https://api.openweathermap.org/data/2.5/onecall?lat=${coords.lat}&lon=${coords.lon}&exclude=current,minutely,hourly,alerts&appid=${process.env.OW_KEY}`).then((response) => {
let unfilteredWeather = response.data;
unfilteredWeather.daily.forEach(datapoint => {
let data = {
clouds: datapoint.clouds,
prec_type: (datapoint.rain) ? "rain" : "none",
prec_amount: (datapoint.rain) ? datapoint.rain : 0,
};
weather.push(data);
});
}).catch(error => {
failed = true;
console.error("OpenWeather request failed. Switching to backup");
});
if (failed) {
weather = [];
await axios.get(`http://www.7timer.info/bin/api.pl?lon=${coords.lon}&lat=${coords.lat}&product=civil&unit=metric&output=json`).then((response) => {
let unfilteredWeather = response.data;
let i = 0;
unfilteredWeather.dataseries.forEach(datapoint => {
if (i > 55) return;
let data = {
clouds: datapoint.cloudcover,
prec_type: datapoint.prec_type,
prec_amount: API_TO_PREC[datapoint.prec_amount],
};
i++;
weather.push(data);
});
}).catch(error => {
console.error("Backup weather request failed");
res.status(418).send("Backup weather request failed. Set default weather cycle");
});
}
WEATHER_BUFFER.push({
lat: coords.lat,
lon: coords.lon,
weather: weather,
recorded: Date.now()
});
// Delete oldest
if (WEATHER_BUFFER.length > WEATHER_BUFFER_SIZE) {
let oldestIndex = 0;
let oldestRecord = WEATHER_BUFFER[0].recorded;
for (let i = 1; i < WEATHER_BUFFER.length; i++) {
if (WEATHER_BUFFER[i].recorded < oldestRecord) {
oldestRecord = WEATHER_BUFFER[i].recorded;
oldestIndex = i;
}
}
WEATHER_BUFFER.splice(oldestIndex, 1);
}
console.log("Weather buffer usage: " + WEATHER_BUFFER.length + "/" + WEATHER_BUFFER_SIZE);
res.status(201).send(weather);
});
// Send session data
app.get('/session/data', async (req, res) => {
if (req.session.data) {
res.status(200).send(req.session.data);
} else {
res.status(200).send(undefined);
}
});
// MongoDB database variables
const MongoClient = require('mongodb').MongoClient;
const name = process.env.DB_NAME;
const pass = process.env.DB_PASS;
const connectionString = `mongodb+srv://${name}:${pass}@cluster0.kawhk.mongodb.net/Delta?retryWrites=true&w=majority`;
// Update best score
app.put('/session/score', async (req, res) => {
if (!req.session.data) {
res.status(401).send("Not logged in");
return;
}
let score = Number(req.body.score);
let client = MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.connect();
let db = client.db("Delta");
let data = db.collection("Data");
await data.findOneAndUpdate({
user: req.session.data.user
},
{
$max: {
score: score
}
},
{
upsert: true
});
req.session.data.score = score;
res.status(200).send({
msg: "Score updated",
score: score
});
client.close();
});
// Update theme setting
app.put('/session/theme', async (req, res) => {
req.session.data.theme_id = Number(req.body.theme_id);
if (req.body.saveToDB) {
let client = MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.connect();
let db = client.db("Delta");
let data = db.collection("Data");
await data.findOneAndUpdate({
user: req.session.data.user
},
{
$set: {
theme_id: req.session.data.theme_id
}
},
{
upsert: true
});
res.status(200).send({
msg: "Theme updated and saved",
theme_id: req.session.data.theme_id
});
client.close();
} else {
res.status(200).send({
msg: "Theme updated",
theme_id: req.session.data.theme_id
});
}
});
const minLoginLength = 4;
const maxLoginLength = 12;
const minPassLength = 6;
const maxPassLength = 20;
//=== Singing in/up/out ===//
function checkInput(str, minLength, maxLength) {
return (/^[0-9a-zA-Z]+$/.test(str))
&& (str.length >= minLength && str.length <= maxLength);
}
// Sign in
app.post('/signin', async (req, res) => {
let login = req.body.login;
let password = req.body.password;
if (!checkInput(login, minLoginLength, maxLoginLength)
|| !checkInput(password, minPassLength, maxPassLength)) {
res.status(400).send("Login or password is incorrect");
return;
}
let client = MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.connect();
let db = client.db("Delta");
let users = db.collection("Users");
let correct = false;
await users.find({login: login}).forEach(entry => {
if (entry.password === password) correct = true;
});
if (!correct) {
res.status(400).send("Login or password is incorrect");
return;
}
req.session.data = {user: login};
await db.collection("Data").find({user: login}).forEach(entry => {
req.session.data.score = entry.score;
req.session.data.theme_id = entry.theme_id;
});
res.status(200).send({
msg: "Signed in!",
data: req.session.data
});
client.close();
});
// Sing up
app.post('/signup', async (req, res) => {
let login = req.body.login;
let password = req.body.password;
if (!checkInput(login, minLoginLength, maxLoginLength)
|| !checkInput(password, minPassLength, maxPassLength)) {
res.status(400).send("Login/Password incorrect");
return;
}
let client = MongoClient(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
});
await client.connect();
let db = client.db("Delta");
let users = db.collection("Users");
let exists = false;
await users.find({login: login}).forEach(entry => {
exists = true;
});
if (exists) {
res.status(400).send("User already exists");
return;
}
req.session.data = {
user: login,
score: 0,
theme_id: 0
};
await users.insertOne({
login: login,
password: password
});
await db.collection("Data").insertOne(req.session.data);
res.status(201).send({
msg: "Account created",
data: req.session.data
});
client.close();
});
// Sing out
app.get("/signout", async (req, res) => {
req.session.destroy(() => {
res.redirect('/');
});
});
let PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("Access at port: " + PORT);
});