-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
219 lines (200 loc) · 6.11 KB
/
main.ts
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
interface SystemDataRaw {
DD: {
UA: string;
DT?: string;
VP?: string[];
};
PTK: string;
USN: string;
TS: string;
DM?: {
CN?: string;
};
PT?: string;
UID?: string;
RID?: string;
CV?: string;
}
interface SystemData {
Name: string;
Glyphs: string;
Discoverer: string;
Platform: string;
Timestamp: string;
UnixTimestamp?: number;
"Correctly Prefixed"?: boolean;
}
interface RegionData {
[key: string]: {
[key: string]: string;
};
}
const dataFolder = "./data/";
const regions = Deno.readDirSync(dataFolder);
const civ: string = Deno.args[0] || "AllRegions";
const validCivs = Object.keys(getRegionData());
const globalDataArray: SystemData[] = [];
for (const region of regions) {
const regionName = region.name;
const systems = Deno.readDirSync(dataFolder + regionName);
const regionDataArray: SystemData[] = [];
for (const system of systems) {
if (!system.name.endsWith(".json")) continue;
const fileContent = Deno.readTextFileSync(
dataFolder + region.name + "/" + system.name
);
const systemDataObj: SystemDataRaw = (() => {
try {
const json = JSON.parse(fileContent)[0];
return json;
} catch {
return {
DD: { UA: "" },
USN: "",
PTK: "",
TS: "",
};
}
})();
const extractedDataObj = extractData(systemDataObj);
regionDataArray.push(extractedDataObj);
}
createDataFile(regionName, regionDataArray);
if (validCivs.includes(civ.toLowerCase()))
createRegionTxt(regionName, regionDataArray);
console.log(`${regionName} done`);
globalDataArray.push(...regionDataArray);
}
createDataFile(civ, globalDataArray);
console.log(`Finished!`);
function createDataFile(regionName: string, regionDataArray: SystemData[]) {
createCSV(regionName, regionDataArray);
createJSON(regionName, regionDataArray);
}
function extractData(obj: SystemDataRaw): SystemData {
const address = (() => {
const addressString = obj.DD.UA;
const addressArray = addressString.split("");
addressArray.shift();
addressArray.shift(); // remove first two 0
addressArray.splice(4, 2); // NoSonar remove galaxy index;
return addressArray.join("").toUpperCase();
})();
const outputObj: SystemData = {
Name: obj?.DM?.CN || "",
Glyphs: address,
Discoverer: obj.USN,
Platform: obj.PTK,
Timestamp: buildDate(obj.TS),
UnixTimestamp: parseInt(obj.TS) * 1000,
};
if (validCivs.includes(civ.toLowerCase())) {
const { Glyphs, Name } = outputObj;
const tagStatus = isCorrectlyTagged(Glyphs, Name);
outputObj["Correctly Prefixed"] = tagStatus;
}
return outputObj;
}
function createCSV(filepath: string, bodyDataArray: SystemData[]): void {
const csvData = structuredClone(bodyDataArray).map((item: SystemData) => {
delete item.UnixTimestamp;
return item;
});
const dataObj = csvData[0];
if (!dataObj) return;
const bodyString = createCSVBody(csvData);
const sep = `sep=,`;
const header = Object.keys(dataObj).join('","');
const dataArr = [sep, `"${header}"`, bodyString];
Deno.writeTextFileSync(`${filepath}.csv`, dataArr.join("\n"));
}
function createCSVBody(regionDataArray: SystemData[]) {
const outputArray: string[] = [];
for (const systemObj of regionDataArray) {
const systemDataArray = Object.values(systemObj);
outputArray.push(`"${systemDataArray.join('","')}"`);
}
return outputArray.join("\n");
}
function buildDate(timestamp: string) {
if (!timestamp) return "";
const timestampNum = parseInt(timestamp);
const milliseconds = timestampNum * 1000; // NoSonar the game uses seconds, but JS uses milliseconds
const dateObj = new Date(milliseconds);
const dateString = dateObj.toISOString().split('T')[0];
return dateString;
}
function isCorrectlyTagged(glyphs: string, name: string): boolean {
if (!name) return false;
const expected = buildExpectedTag(glyphs);
return name.startsWith(expected);
}
function buildExpectedTag(glyphs: string): string {
const regionData = getRegionData();
const SIV = (() => {
const SID = glyphs.substring(1, 4); // NoSonar this gets the system ID
const SIDDecNumber = parseInt(SID, 16);
const SIDNumber = SIDDecNumber.toString(16).toUpperCase(); // NoSonar this is dec to hex conversion
return SIDNumber;
})();
const region = glyphs.substring(4); // NoSonar this gets the region glyphs
const regionIndex =
Object.keys(regionData[civ.toLowerCase()]).indexOf(region) + 1;
const expected = `EV${regionIndex}-${SIV}`;
return expected;
}
function createRegionTxt(
regionName: string,
regionDataArray: SystemData[]
): void {
const headers = ["Other Systems", "Missing Prefix"].map(
(heading) => `==${heading}==`
);
const other: string[] = [];
const missing: string[] = [];
const arrayCollection = [other, missing];
const txtContent: string[] = [];
for (const system of regionDataArray) {
if (system["Correctly Prefixed"]) {
other.push(system.Name);
} else {
const expected = buildExpectedTag(system.Glyphs);
if (system.Name)
missing.push(
`${system.Name} (Expected Prefix: ${expected.substring(
1,
expected.length - 1
)})`
);
}
}
for (let i = 0; i < arrayCollection.length; i++) {
const array = arrayCollection[i].map(
(item) => "* " + item.replace(/[{\[\]}]/g, "")
); // NoSonar I'm pretty sure the escape characters are necessary
if (!array.length) continue;
array.sort();
txtContent.push(headers[i], ...array, "");
}
const content = txtContent.join("\n");
if (content) Deno.writeTextFileSync(`${regionName}.txt`, content.trim());
}
function createJSON(filepath: string, dataArray: SystemData[]): void {
Deno.writeTextFileSync(
`${filepath}.json`,
JSON.stringify(dataArray, null, 2)
); // NoSonar this sets an indent of 2 spaces in the JSON file
}
function getRegionData(): RegionData {
return {
eisvana: {
A21117FF: "Flinus",
A11117FF: "Imnito",
A11127FF: "Hiuccli",
A21127FF: "Insesu",
A21107FF: "Cuynteto",
A21107FE: "Uskabar",
A21117FE: "Ziwananau",
},
};
}