-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory-resolve.js
81 lines (62 loc) · 1.43 KB
/
history-resolve.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
import { range, single } from "./history-reader.js";
export async function resolveHistoricData(server, license, from, till) {
if (till < from) {
throw Error("From must be before till");
}
let data;
try {
data = await range(server, license, from, till);
} catch (err) {
console.warn(err.stack);
}
if (!data || data.length === 0) {
throw Error("No data for the selected period");
}
const result = {};
for (const entry of data) {
result[entry.timestamp] = minifyHistoricEntry(entry);
}
return result;
}
export async function resolveTimestampData(server, timestamp) {
let data;
try {
data = await single(server, timestamp);
} catch (err) {
console.warn(err.stack);
}
if (!data || Object.keys(data).length === 0) {
throw Error("No data for this timestamp");
}
const result = {};
for (const license in data) {
const entry = data[license];
result[license] = minifyTimestampEntry(entry);
}
return result;
}
function minifyHistoricEntry(parsed) {
const characterFlags = parsed.character_flags;
return {
x: parsed.x,
y: parsed.y,
z: parsed.z,
i: !!(characterFlags & 8),
c: !!(characterFlags & 16),
f: !!(characterFlags & 32),
d: !!(characterFlags & 1),
s: parsed.speed,
};
}
function minifyTimestampEntry(parsed) {
return {
_: parsed.character_id,
x: parsed.x,
y: parsed.y,
z: parsed.z,
h: parsed.heading,
s: parsed.speed,
cf: parsed.character_flags,
uf: parsed.user_flags,
};
}