forked from YoDobchev/manastirskata-biblioteka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (149 loc) · 5.09 KB
/
index.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
const express = require("express");
var Datastore = require("nedb");
const app = express();
const session = require("express-session");
const port = 3000;
const bodyParser = require("body-parser");
const jsonParser = bodyParser.json({ limit: "1mb" });
app.use(jsonParser);
app.set("view engine", "ejs");
const db = require("./db.js");
const oneDay = 1000 * 60 * 60 * 24;
app.use(
session({
secret: "ijfrjeogphgp4hgpo",
saveUninitialized: true,
cookie: { maxAge: oneDay },
resave: false,
})
);
app.use(express.static(__dirname + "/public"));
const isLoggedIn = (req, res, next) => {
if (req.session.ID) {
next();
} else {
// next();
res.redirect("/login");
}
};
app.get("/", isLoggedIn, (req, res) => {
res.redirect("/home");
});
app.listen(port, () => {
console.log(`Nebuluous listening on ${port}`);
});
app.post("/locationEvent", isLoggedIn, (req, res) => {
console.log("someone called location event");
let oldLocation;
let newDist;
let oldDist;
console.log(req.body);
db.users.findOne({ username: req.session.user }, (err, docs) => {
if (!err) {
if (docs.currentAdventure != null) {
oldLocation = docs.location;
let newLocation = req.body;
let nextGoal;
let progressIndex = docs.currentAdventure.progressIndex;
console.log(docs.currentAdventure);
db.adventures.findOne(
{ _id: docs.currentAdventure.id },
(err, docsad) => {
if (!err && docsad != null) {
console.log(
distanceBetweenPoints(
req.body.latitude,
req.body.longitude,
docsad.locations[progressIndex].latitude,
docsad.locations[progressIndex].longitude
)
);
if (
distanceBetweenPoints(
req.body.latitude,
req.body.longitude,
docsad.locations[progressIndex].latitude,
docsad.locations[progressIndex].longitude
) < 10 && docsad.locations.length != progressIndex + 1
) {
progressIndex++;
db.users.update(
{ username: req.session.user },
{
$set: {
currentAdventure: {
id: docs.currentAdventure.id,
progressIndex: progressIndex,
},
},
},
{ new: true },
(err, doc) => {
console.log(doc);
}
);
nextGoal = docsad.locations[progressIndex];
}
db.users.findOne({ username: req.session.user }, (err, docs) => {
res.json({ tokens: docs.tokens, nextGoal: nextGoal });
});
console.log(docsad);
console.log("progrIndex " + progressIndex);
nextGoal = docsad.locations[progressIndex];
oldDist = Math.hypot(
oldLocation.latitude - nextGoal.latitude,
oldLocation.longitude - nextGoal.longitude
);
newDist = Math.hypot(
newLocation.latitude - nextGoal.latitude,
newLocation.longitude - nextGoal.longitude
);
// console.log(newDist, oldDist);
// if (newDist < oldDist) {
// db.users.update(
// { username: req.session.user },
// { $inc: { tokens: (newDist - oldDist) * 13 } },
// { new: true },
// (err, doc) => {
// console.log(doc);
// console.log(
// "if this is correct this should print" +
// doc.currentAdventure.progressIndex
// );
// }
// );
// }
}
}
);
}
}
});
db.users.update(
{ username: req.session.user },
{ $set: { location: req.body } },
{},
(err) => {}
);
// res.json({ delta: newDist - oldDist });
});
function distanceBetweenPoints(lat1, lon1, lat2, lon2) {
var R = 6371e3; // Earth's radius in meters
var phi1 = (lat1 * Math.PI) / 180;
var phi2 = (lat2 * Math.PI) / 180;
var deltaPhi = ((lat2 - lat1) * Math.PI) / 180;
var deltaLambda = ((lon2 - lon1) * Math.PI) / 180;
var a =
Math.sin(deltaPhi / 2) * Math.sin(deltaPhi / 2) +
Math.cos(phi1) *
Math.cos(phi2) *
Math.sin(deltaLambda / 2) *
Math.sin(deltaLambda / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var distance = R * c; // Distance in meters
return distance;
}
app.use("/login", require("./routes/login.js"));
app.use("/signup", require("./routes/signup.js"));
app.use("/", isLoggedIn, require("./routes/adventures.js"));
app.use("/", isLoggedIn, require("./routes/home.js"));