forked from SteveHodge/ed-systems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgc.js
162 lines (153 loc) · 4.96 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
// 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)
function getTGCData(callback, wantDists) {
$.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() {
logAppend('Failed to read from tgcsystems.json\n');
}).always(function() {
// update with any additional data from the server
updateTGCData(function() {
$.getJSON('tgcdistances.json', function(data) {
lastDistFetch = data.date+'Z';
var dists = addDistances(data.distances);
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);
});
});
});
}
function addDistances(distances) {
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() {
var s2key = nameKey(this.name);
if (!(s2key in systemsMap)) {
console.log('Unknown system '+this.name+' in distance data');
return;
}
count++;
if (systemsMap[s1key].calculated) {
if (!('distances' in systemsMap[s1key])) systemsMap[s1key].distances = [];
systemsMap[s1key].distances.push({system: systemsMap[s2key].name, distance: this.dist});
}
if (systemsMap[s2key].calculated) {
if (!('distances' in systemsMap[s2key])) systemsMap[s2key].distances = [];
systemsMap[s2key].distances.push({system: systemsMap[s1key].name, distance: this.dist});
}
});
});
return count;
}
// TODO do something with count/rejected. note "if (this.coord)" probably never fails - which is good since we should exclude systems without coordinates unless the user requires it
function addSystems(systems) {
var count = 0; rejected = 0;
$.each(systems, function() {
if (this.coord) {
systemsMap[nameKey(this.name)] = {
x: this.coord[0],
y: this.coord[1],
z: this.coord[2],
name: this.name,
calculated: this.commandercreate !== 'FD',
cr: this.cr,
contributor: this.commandercreate ? this.commandercreate : '(unknown)',
contributed: this.createdate
};
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;
//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) {
$.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;
//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);
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) {
return $.trim(n.toLowerCase());
}