-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
198 lines (176 loc) · 5.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var express = require('express');
var app = express();
var Sequelize = require('sequelize');
var sequelize = new Sequelize('mysql://eventgo:asdf12345@best-ever.org:3306/eventgo');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')
const uuidV4 = require('uuid/v4');
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'))
var Evnt = sequelize.define('event', {
name: {
type: Sequelize.STRING
},
longitude: {
type: Sequelize.DECIMAL(9, 6)
},
latitude: {
type: Sequelize.DECIMAL(9, 6)
},
start: {
type: Sequelize.DATE
},
end: {
type: Sequelize.DATE
},
population: {
type: Sequelize.INTEGER
}
});
sequelize.sync();
app.get('/mcgill', function(req, res) {
Evnt.findById(6).then(function(addEvnt) {
addEvnt.population++;
addEvnt.save();
});
});
app.post('/get_loc', function(req, res) {
//console.log(req.body);
if (req.cookies['userid'] == undefined) {
res.cookie('userid', uuidV4());
}
//console.log(req.cookies['userid']);
updateEventCounts(function(a, b) {
handleClosestEvent(a, b);
}, req.cookie, req.body.lng, req.body.lat);
res.send(req.body);
});
var userToEventToCount = {};
var userToEventDeque = {};
function getDistanceFromLatLonInM(lat1,lon1,lat2,lon2) {
var R = 6371000; // Radius of the earth in m
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
function updateEventCounts(fn, cookie, longitude, latitude) {
//console.log("MYlat: " + latitude);
//console.log("MYlon: " + longitude);
var closest = Number.MAX_VALUE;
var closestEvent = null;
Evnt.findAll().then(function(events) {
events.forEach(function(currEvent) {
//console.log("lat: " + currEvent.latitude);
//console.log("lon: " + currEvent.longitude);
var distance = getDistanceFromLatLonInM(latitude, longitude, currEvent.latitude, currEvent.longitude);
if(distance < 1000 && distance < closest) {
//console.log(closestEvent);
closest = distance;
closestEvent = currEvent;
}
});
console.log("addEvent: " + closestEvent);
fn(cookie, closestEvent);
});
return closestEvent;
};
function handleClosestEvent(cookie, closestEvent) {
//var closestEvent = getClosestEvent(longitude, latitude);
//var millisSinceEpoch = Date.now();
if(!(cookie in userToEventToCount)) {
userToEventToCount[cookie] = {};
userToEventDeque[cookie] = [];
}
var eventToCount = userToEventToCount[cookie];
var eventDeque = userToEventDeque[cookie];
var addEvent = closestEvent;
var removeEvent = null;
if(eventDeque.length == 20) {
removeEvent = eventDeque.shift();
}
eventDeque.push(addEvent);
if(addEvent && !(addEvent.id in eventToCount)) {
eventToCount[addEvent.id] = 0;
}
console.log("hi");
if(addEvent && !removeEvent) {
if(eventToCount[addEvent.id] == 10) {
Evnt.findById(addEvent.id).then(function(addEvnt) {
console.log("INCCCCC");
addEvnt.population++;
addEvnt.save();
});
}
eventToCount[addEvent.id]++;
console.log("addEvent.id count: " + eventToCount[addEvent.id]);
} else if(!addEvent && removeEvent) {
if(eventToCount[removeEvent.id] == 11) {
Evnt.findById(removeEvent.id).then(function(rmvEvnt) {
console.log("DECCCCC");
rmvEvnt.population--;
rmvEvnt.save();
});
}
eventToCount[removeEvent.id]--;
console.log("removeEvent.id count: " + eventToCount[removeEvent.id]);
} else if(addEvent && removeEvent && addEvent.id != removeEvent.id) {
if(eventToCount[removeEvent.id] == 11) {
Evnt.findById(removeEvent.id).then(function(rmvEvnt) {
console.log("DECCCCC");
rmvEvnt.population--;
rmvEvnt.save();
});
}
eventToCount[removeEvent.id]--;
console.log("removeEvent.id count: " + eventToCount[removeEvent.id]);
if(eventToCount[addEvent.id] == 10) {
Evnt.findById(addEvent.id).then(function(addEvnt) {
console.log("INCCCCC");
addEvnt.population++;
addEvnt.save();
});
}
eventToCount[addEvent.id]++;
console.log("addEvent.id count: " + eventToCount[addEvent.id]);
}
console.log("bye");
Evnt.findById(6).then(function(events) {
console.log("population: " + events.population);
});
};
app.get('/events/get/all', function(req, res) {
console.log('test');
Evnt.findAll().then(function(events) {
res.json(events);
});
});
app.get('/events/get/:id', function(req, res) {
Evnt.findById(req.params.id).then(function(events) {
res.json(events);
});
});
app.post('/events/add', function(req, res) {
Evnt.create(req.body).then(function(events) {
res.json(events);
});
});
app.get('/helloWorld', function (req, res) {
res.send('Hello World!')
})
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
app.listen(3000, function () {
console.log('EventGo started on http://localhost:3000')
})