-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystems.js
181 lines (154 loc) · 6.09 KB
/
systems.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
'use strict';
const LRU = require("lru-cache");
const statioport = require("./station");
const caching = require("./caching");
const request = require("request-promise");
module.exports = class Systems {
constructor () {
this.staCache = new LRU({ max: parseInt(process.env.CACHE_MAX_ITEMS), maxAge: parseInt(process.env.STATION_MAX_AGE) });
try {
caching.read(process.env.STATIONS_CACHE, this.staCache);
} catch (error) {
console.error(error);
}
}
async stations (systemName) {
let cached = this.staCache.get(systemName);
if (cached) {
let delta = Date.now() - cached["timestamp"];
if (delta < process.env.SYSTEM_MAX_AGE) {
return cached['response'];
}
}
var options = {
url: process.env.EDSM_STATIONS,
method: 'GET',
json: true,
resolveWithFullResponse: true,
simple: false,
qs: {"systemName": systemName}
};
let response = await request(options);
if (response.statusCode != 200) {
return false;
}
this.staCache.set(systemName, {"date": new Date(), "response": response.body['stations'], "timestamp": Date.now()});
caching.write(process.env.STATIONS_CACHE, this.staCache);
return response.body['stations'];
}
closestStation (stations, service) {
let overall = undefined;
let withShipyard = undefined;
let withLargeLandingPads = undefined;
for (var station in stations) {
let sta = new statioport(stations[station]);
if (service && !sta.hasService(service)) continue;
if (overall == undefined) {
overall = stations[station]; // TODO replace with a proper station object
} else if (stations[station]['distanceToArrival'] < overall['distanceToArrival']) {
overall = stations[station];
}
if (sta.hasLargeLandingPads()) {
withLargeLandingPads = stations[station];
}
if (!sta.hasShipyard()) continue;
if (withShipyard == undefined) {
withShipyard = stations[station];
} else if (sta.distanceToArrival() < overall['distanceToArrival']) {
withShipyard = stations[station];
}
}
return {'overall': overall, 'withShipyard': withShipyard, 'withLargeLandingPads': withLargeLandingPads};
}
closestDestination (sysAndSta1, sysAndSta2, scDistance) {
if (sysAndSta1 == undefined) {
return sysAndSta2;
}
if (sysAndSta2 == undefined) {
return sysAndSta1;
}
if (sysAndSta1['station']['distanceToArrival'] > scDistance && sysAndSta2['station']['distanceToArrival'] > scDistance) {
if (Math.abs(sysAndSta1['distance'] - sysAndSta2['distance']) < 5) {
return sysAndSta1['station']['distanceToArrival'] < sysAndSta2['station']['distanceToArrival'] ? sysAndSta1 : sysAndSta2;
} else {
return sysAndSta1['distance'] < sysAndSta2['distance'] ? sysAndSta1 : sysAndSta2;
}
}
if (sysAndSta1['station']['distanceToArrival'] > scDistance) {
return sysAndSta2;
}
if (sysAndSta2['station']['distanceToArrival'] > scDistance) {
return sysAndSta1;
}
return sysAndSta1['distance'] < sysAndSta2['distance'] ? sysAndSta1 : sysAndSta2;
}
async ifactorsInSystem (system) {
let ifactors = undefined;
if (!system || system['requirePermit']) {
return new Promise((resolve, reject) => {
resolve(ifactors);
});
}
let sta = await this.stations(system['name']);
if (!sta || sta.length == 0) {
return undefined;
}
let closest = this.closestStation(sta, 'Interstellar Factors Contact');
if (closest['overall']) {
ifactors = {'overall': system};
ifactors['overall']['station'] = closest['overall'];
if (closest['withLargeLandingPads']) {
ifactors = {'withLargeLandingPads': system};
ifactors['withLargeLandingPads']['station'] = closest['withLargeLandingPads'];
}
}
return ifactors;
}
async matTradersInSystem (system) {
let rawTrader = undefined;
let manTrader = undefined;
let encTrader = undefined;
if (!system || system['requirePermit']) {
return {'raw': rawTrader, 'encoded': encTrader, 'manufactured': manTrader};
// TODO promise probably not needed
/*return new Promise((resolve, reject) => {
resolve({'raw': rawTrader, 'encoded': encTrader, 'manufactured': manTrader});
});*/
}
let info = system['information'];
info['security'] = info['security'] || 'N/A';
info['economy'] = info['economy'] || 'N/A';
if (info['government'] == 'Anarchy' || !['high', 'medium'].includes(info['security'].toLowerCase()) || info['population'] < 1000000 || info['population'] > 22000000 || !['extraction', 'refinery', 'industrial', 'high tech', 'military'].includes(info['economy'].toLowerCase())) {
return {'raw': rawTrader, 'encoded': encTrader, 'manufactured': manTrader}
/*return new Promise((resolve, reject) => {
resolve({'raw': rawTrader, 'encoded': encTrader, 'manufactured': manTrader});
});*/
}
let sta = await this.stations(system['name']);
if (!sta || sta.length == 0) {
return {'raw': undefined, 'encoded': undefined, 'manufactured': undefined};
}
if (['extraction', 'refinery'].includes(info['economy'].toLowerCase())) {
let closest = this.closestStation(sta, 'Material Trader');
if (closest['overall']) {
rawTrader = system;
rawTrader['station'] = closest['overall'];
}
}
if (info['economy'].toLowerCase() == 'industrial') {
let closest = this.closestStation(sta, 'Material Trader');
if (closest['overall']) {
manTrader = system;
manTrader['station'] = closest['overall'];
}
}
if (['high tech', 'military'].includes(info['economy'].toLowerCase())) {
let closest = this.closestStation(sta, 'Material Trader');
if (closest['overall']) {
encTrader = system;
encTrader['station'] = closest['overall'];
}
}
return {'raw': rawTrader, 'encoded': encTrader, 'manufactured': manTrader};
}
}