-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcron.js
More file actions
277 lines (267 loc) · 10.3 KB
/
cron.js
File metadata and controls
277 lines (267 loc) · 10.3 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
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
const cron = require('node-cron');
const db = require('./db');
const users = db.Database().collection('users');
const maps = db.Database().collection('maps');
const systems = db.Database().collection('systems');
const throttledQueue = require('throttled-queue');
const throttle = throttledQueue(30, 1000, true);
const axios = require('axios');
const qs = require('querystring');
const ObjectID = require('mongodb').ObjectID;
const EveService = require('./eveService');
const {io} = require('./main');
io.on('connection', (socket) => {
console.log('Socket Connected');
socket.on('map', (event) => {
Object.keys(socket.rooms).forEach(room => {
socket.leave(room);
});
socket.join(event.id);
});
});
const eveService = new EveService();
const ESIAuth = axios.create({
baseURL: 'https://login.eveonline.com'
});
const RefreshToken = async ({expires_in, access_token, refresh_token}) => {
if (Date.now() < expires_in) {
return access_token;
}
const response = await ESIAuth.post('/oauth/token', qs.stringify({
grant_type: 'refresh_token',
refresh_token: refresh_token
}), {
auth: {
username: process.env.EVE_CLIENT_ID,
password: process.env.EVE_APP_SECRET
},
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Host": 'login.eveonline.com'
}
});
return response.data.access_token;
};
const updatePilotShip = async (accessToken, pilot) => {
const ship = await eveService.getPilotShip({token: accessToken, CharacterID: pilot.CharacterID});
const shipType = await eveService.getType(ship.ship_type_id);
/* Check for unicode escape in ship name and unescape if necessary */
var user = {};
if (ship.ship_name.substring(0, 2) == 'u\''){
var shipName = ship.ship_name.substr(2, ship.ship_name.length - 3);
user = {
ship: {
name: JSON.parse('"' + shipName + '"'),
type: shipType.name
}
};
} else {
user = {
ship: {
name: ship.ship_name,
type: shipType.name
}
};
}
/* Encased in try block to handle pilot not having a ship yet. */
try {
if (pilot.ship.name !== user.ship.name || pilot.ship.type !== user.ship.type) {
await users.updateOne({
_id: ObjectID(pilot._id)
}, {
$set: user
});
const doc = {
name: pilot.CharacterName,
ship: user.ship
};
io.to(pilot.map).emit('updatePilotShip', doc);
/* Encased in try block to handle pilot not having a location yet. */
try {
await maps.updateOne({
'locations.system_id': parseInt(pilot.location.solar_system_id),
'locations.pilots': {$ne: pilot.CharacterName}
}, {
$set: {
'locations.$[location].pilots.$[pilot]': doc
}
}, {
arrayFilters: [{'location.system_id': pilot.location.solar_system_id}, {'pilot.name': pilot.CharacterName}]
});
} catch (e) {
if (e instanceof TypeError){
console.log('Pilot has no location. Need to add location for him.');
} else {
console.log(e);
}
}
}
} catch (e) {
if (e instanceof TypeError){
/* user does not have ship assigned. Assign. */
await users.updateOne({
_id: ObjectID(pilot._id)
}, {
$set: user
});
return;
}
/* Other error. Print. */
console.log(e);
}
};
const updatePilotLocation = async (accessToken, pilot) => {
if (pilot.map) {
const onlineStatus = await eveService.getPilotStatus({token: accessToken, CharacterID: pilot.CharacterID});
const location = await eveService.getPilotLocation({token: accessToken, CharacterID: pilot.CharacterID});
const user = {
online: onlineStatus.online,
location: location,
};
await users.updateOne({
_id: ObjectID(pilot._id)
}, {
$set: user
});
/* Encased in try block to handle pilot not having a location yet. */
try {
if (pilot.location.solar_system_id !== user.location.solar_system_id) {
const systemFrom = await systems.findOne({
system_id: pilot.location.solar_system_id
});
const systemTo = await systems.findOne({
system_id: user.location.solar_system_id
});
const systemFromUpdate = await maps.updateOne({
_id: ObjectID(pilot.map),
'locations.system_id': {$ne: systemFrom.system_id}
}, {
$push: {
'locations': {
...systemFrom,
connections: [systemTo.system_id],
pilots: []
}
}
});
if (systemFromUpdate.modifiedCount === 1) {
io.to(pilot.map).emit('addLocation', {
system: {
...systemFrom,
connections: [systemTo.system_id],
pilots: []
}
});
} else {
await maps.findOneAndUpdate({
_id: ObjectID(pilot.map)
}, {
$pull: {
'locations.$[location].pilots': {'name': pilot.CharacterName},
},
}, {
arrayFilters: [{'location.system_id': systemFrom.system_id}]
});
io.to(pilot.map).emit('removePilot', {
from: systemFrom.system_id,
pilot: {
name: pilot.CharacterName,
ship: pilot.ship
}
});
}
const systemToUpdate = await maps.updateOne({
_id: ObjectID(pilot.map),
'locations.system_id': {$ne: systemTo.system_id}
}, {
$push: {
'locations': {
...systemTo,
connections: [systemFrom.system_id],
pilots: [{
name: pilot.CharacterName,
ship: pilot.ship
}]
}
},
});
if (systemToUpdate.modifiedCount === 1) {
io.to(pilot.map).emit('addLocation', {
system: {
...systemTo,
connections: [systemFrom.system_id],
pilots: [{
name: pilot.CharacterName,
ship: pilot.ship
}]
}
});
} else {
await maps.updateOne({
'locations.system_id': parseInt(user.location.solar_system_id)
}, {
$push: {
'locations.$.pilots': {
name: pilot.CharacterName,
ship: pilot.ship
}
}
});
io.to(pilot.map).emit('addPilot', {
to: systemTo.system_id,
pilot: {
name: pilot.CharacterName,
ship: pilot.ship
}
});
}
} else {
/* Check if pilot is in list for location, else add him to list */
if (!await maps.findOne({
'locations.system_id': parseInt(user.location.solar_system_id),
'locations.pilots.name': pilot.CharacterName,
})){
await maps.updateOne({
'locations.system_id': parseInt(user.location.solar_system_id),
}, {
$addToSet: {
'locations.$.pilots': {
name: pilot.CharacterName,
ship: pilot.ship
}
}
});
}
}
} catch (e) {
if (e instanceof TypeError){
console.log('Pilot', pilot.CharacterName, 'has no location assigned. Skipping to wait for db update.');
}
console.log(e);
}
} else {
/* For dev: Add Test Map to user if user has no map. Future: add some default map to user. */
console.log('User', pilot.CharacterName, 'has no map assigned. Assigning Test Map.');
const map = await maps.findOne({name: 'Test Map'});
const user = {
map: map._id
};
await users.updateOne({
_id: ObjectID(pilot._id)
}, {
$set: user
});
}
};
cron.schedule('*/5 * * * * *', async () => {
const allUsers = await users.find({}).toArray();
allUsers.map(async pilot => {
throttle(async () => {
const accessToken = await RefreshToken(pilot);
await Promise.all([
updatePilotLocation(accessToken, pilot),
updatePilotShip(accessToken, pilot)
]);
});
});
});