-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtgc.js
225 lines (211 loc) · 7.25 KB
/
tgc.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
// TODO refactor into class
var systemsMap; // reference system data
var lastFetch = '2014-09-09 12:13:14Z'; // last system fetch time (default is before earliest TGC data)
var lastDistFetch = '2014-09-09 12:13:14Z'; // last distance fetch time (default is before earliest TGC data)
// system properties:
// name - system name
// x, y, z - coordinates if known
// calculated - true if the system was not supplied by FD
// cr - TGC "confidence rating" (5+ = FD supplied, 2 = coordinates, 1 = unlocated)
// contributor - name of first person to submit the system
// contributed - datetime system was first submitted
// distances - array of distances to other systems
// tgcunlocated - true if TGC doesn't have a location for this system
function getTGCData(callback, wantDists, refDists) {
$.getJSON('tgcsystems.json', function(data) {
lastFetch = data.date+'Z';
systemsMap = {};
addSystems(data.systems);
logAppend('Loaded tgcsystems.json: data up to '+data.date+'. Total number of systems: '+Object.keys(systemsMap).length+'\n');
}).fail(function(xhr, txt, err) {
logAppend('Failed to read from tgcsystems.json:\n');
logAppend(err.message+'\n');
}).always(function() {
// update with any additional data from the server
updateTGCData(
wantDists ?
function() {
$.getJSON('tgcdistances.json', function(data) {
lastDistFetch = data.date+'Z';
var dists = addDistances(data.distances, refDists);
logAppend('Loaded tgcdistances.json: data up to '+data.date+'. Added '+dists+'\n');
}).fail(function() {
logAppend('Failed to read from tgcdistances.json\n');
}).always(function() {
fetchTGCDistances(callback, refDists);
});
}
: callback
);
});
}
function getEDSMData(callback, wantDists, refDists) {
$.getJSON('edsmsystems.json', function(data) {
// lastFetch = data.date+'Z';
systemsMap = {};
addSystems(data.systems);
logAppend('Loaded edsmsystems.json: data up to '+data.date+'. Total number of systems: '+Object.keys(systemsMap).length+'\n');
}).fail(function(xhr, txt, err) {
logAppend('Failed to read from tgcsystems.json:\n');
logAppend(err.message+'\n');
}).always(function() {
// update with any additional data from the server
callback();
});
}
function refreshTGCData(callback, wantDists, refDists) {
updateTGCData(wantDists ? function() {fetchTGCDistances(callback, refDists);} : callback);
}
function addDistances(distances, refDists) {
var count = 0;
$.each(distances, function() {
var s1key = nameKey(this.name);
if (!(s1key in systemsMap)) {
console.log('Unknown system '+this.name+' in distance data');
return;
}
$.each(this.refs, function(dist) {
var s2key = nameKey(this.name);
if (!(s2key in systemsMap)) {
console.log('Unknown system '+this.name+' in distance data');
return;
}
count++;
if (systemsMap[s1key].calculated || refDists) {
if (!('distances' in systemsMap[s1key])) systemsMap[s1key].distances = [];
var found = false;
$.each(systemsMap[s1key].distances, function() {
if (this.name === systemsMap[s2key].name && this.dist === dist.dist) {
found = true;
return false;
}
});
if (!found) {
systemsMap[s1key].distances.push({system: systemsMap[s2key].name, distance: this.dist, creator: this.commandercreate, created: this.createdate});
}
}
if (systemsMap[s2key].calculated || refDists) {
if (!('distances' in systemsMap[s2key])) systemsMap[s2key].distances = [];
var found = false;
$.each(systemsMap[s2key].distances, function() {
if (this.name === systemsMap[s1key].name && this.dist === dist.dist) {
found = true;
return false;
}
});
if (!found) {
systemsMap[s2key].distances.push({system: systemsMap[s1key].name, distance: this.dist, creator: this.commandercreate, created: this.createdate});
}
}
});
});
return count;
}
// TODO do something with count/rejected
function addSystems(systems) {
var count = 0; rejected = 0;
$.each(systems, function() {
var key = nameKey(this.name);
if (!(key in systemsMap) || systemsMap[key].contributor !== 'FD') {
// we don't replace FD systems with new data
if (!(key in systemsMap)) systemsMap[key] = {};
systemsMap[key].x = this.coord[0];
systemsMap[key].y = this.coord[1];
systemsMap[key].z = this.coord[2];
systemsMap[key].name = this.name;
systemsMap[key].calculated = this.commandercreate !== 'FD' || this.cr < 5;
systemsMap[key].cr = this.cr;
systemsMap[key].contributor = this.commandercreate ? this.commandercreate : '(unknown)';
systemsMap[key].contributed = this.createdate;
if ('tgcunlocated' in this) {
systemsMap[key].tgcunlocated = this.tgcunlocated;
} else {
delete systemsMap[key].tgcunlocated;
}
count++;
} else {
rejected++;
}
});
// if (rejected > 0) msg += 'Rejected '+rejected+' systems. ';
}
function updateTGCData(callback) {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'http://edstarcoordinator.com/api.asmx/GetSystems',
data: JSON.stringify({data: {
ver: 2,
outputmode: 2,
filter: {
cr: 1,
date: lastFetch
}
}}),
dataType: 'json',
success: function(data, status, xhr) {
data = data.d;
//lastFetch = data.date+'Z';
//console.log(JSON.stringify(data, null, 2));
if (data.status.input[0].status.statusnum !== 0) {
logAppend('Error from TGC server: '+data.status.input[0].status.statusnum +' '+data.status.input[0].status.msg+'\n');
} else {
addSystems(data.systems);
logAppend('Fetched '+data.systems.length+' systems from TGC. Total number of systems: '+Object.keys(systemsMap).length+'\n');
}
callback();
},
error: function(xhr, status, error) {
//console.log(xhr.responseText);
logAppend(error+'\n');
console.log('Error from TGC server: '+error);
console.log('Request was:');
console.log(JSON.stringify(query, null, 2));
callback();
}
});
}
function fetchTGCDistances(callback, refDists) {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'http://edstarcoordinator.com/api.asmx/GetDistances',
data: JSON.stringify({data: {
ver: 2,
outputmode: 2,
filter: {
cr: 0,
date: lastFetch
}
}}),
dataType: 'json',
success: function(data, status, xhr) {
data = data.d;
//lastDistFetch = data.date+'Z';
//console.log(JSON.stringify(data, null, 2));
if (data.status.input[0].status.statusnum !== 0) {
logAppend('Error from TGC server: '+data.status.input[0].status.statusnum +' '+data.status.input[0].status.msg+'\n');
} else {
var dists = addDistances(data.distances, refDists);
logAppend('Fetched '+dists+' distances from TGC\n');
}
callback();
},
error: function(xhr, status, error) {
//console.log(xhr.responseText);
logAppend(error+'\n');
console.log('Error from TGC server: '+error);
console.log('Request was:');
console.log(JSON.stringify(query, null, 2));
callback();
}
});
}
function logAppend(str) {
console.log(str);
}
// move to trilateration.js??
function nameKey(n) {
if (!n) return n;
return $.trim(n.toLowerCase());
}