-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeData.cjs
161 lines (136 loc) · 5.37 KB
/
makeData.cjs
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
const fs = require("fs");
const path = require("path");
const dataTxtPath = path.join(__dirname, "src/data/stats.txt");
const outputPath = path.join(__dirname, "src/data/stats.ts");
const data = fs.readFileSync(dataTxtPath, "utf8")
.split("\n")
.map((line) => line.trim())
.filter(line => line && !line.startsWith("#"));
const statKeys = data[0].split("\t").map((key) => key.trim().toLowerCase());
console.log(`Stat keys: ${statKeys.join(", ")}`);
if (!data[1].startsWith("[")) {
throw new Error("Expected category before anything else after the header");
}
const statEquals = (a, b) => {
for (let i = 0; i < statKeys.length; i++) {
if (a[statKeys[i]] !== b[statKeys[i]]) {
return false;
}
}
return true;
};
const categories = [];
const names = {};
let currentCategory = null;
const obj = {};
for (let i = 1; i < data.length; i++) {
const name = data[i];
if (name.startsWith("[") && name.endsWith("]")) {
currentCategory = name.slice(1, name.length - 1);
categories.push(currentCategory);
names[currentCategory] = [];
obj[currentCategory] = [];
continue;
}
names[currentCategory].push(name);
i++;
const stats = data[i].split("\t").map((value) => parseInt(value.trim()));
if (stats.length !== statKeys.length) {
throw new Error(`Expected ${statKeys.length} stats, got ${stats.length}. Name is ${name}`);
}
const statObj = {};
for (let j = 0; j < statKeys.length; j++) {
statObj[statKeys[j]] = stats[j];
}
let isDuplicate = false;
for (let j = 0; j < obj[currentCategory].length; j++) {
if (statEquals(obj[currentCategory][j].statObj, statObj)) {
obj[currentCategory][j].name += ` / ${name}`;
isDuplicate = true;
break;
}
}
if (isDuplicate) {
continue;
}
obj[currentCategory].push({
name,
statObj
});
}
console.log(`Categories: ${categories.join(", ")}`);
categories.forEach((category) => {
console.log(` ${category}: ${obj[category].length} unique items`);
});
let outputString = `// This file is generated by makeData.cjs
import Fuse from "fuse.js";
import { fuzzySearch, FuseOptions } from "./fuzzy";
`;
/// Stat type
outputString += "export type Stat = {\n";
for (let i = 0; i < statKeys.length; i++) {
outputString += ` ${statKeys[i]}: number;\n`;
}
outputString += "};\n\n";
outputString += "export type StatKey = keyof Stat\n\n";
outputString += `export const StatKeys = [${statKeys.map(key => JSON.stringify(key)).join(", ")}] as const;\n\n`;
/// hashStat function
outputString += "export const hashStat = (stat: Stat): number => {\n";
outputString += " const c = 43;\n";
outputString += " let h = 0;\n";
for (let i = 0; i < statKeys.length; i++) {
outputString += ` h = h * c + stat.${statKeys[i]};\n`;
}
outputString += " return h;\n";
outputString += "};\n\n";
/// Data
categories.forEach((category) => {
outputString += `export const ${category}Data = {\n`;
const names = [];
obj[category].forEach(({name, statObj}) => {
const nameString = JSON.stringify(name);
outputString += ` ${nameString}: { ${statKeys.map(key => `${key}: ${statObj[key]}`).join(", ")}},\n`;
names.push(nameString);
});
outputString += "} as const;\n"
outputString += `export type ${category} = keyof typeof ${category}Data;\n\n`;
outputString += `export const ${category}s = [${names.join(", ")}] as const;\n\n`;
});
/// Build type
outputString += `export type Build = [${categories.join(", ")}]\n`;
/// category function
categories.forEach((category, i) => {
outputString += `export const ${category}Names = [${names[category].map(n => JSON.stringify(n)).join(", ")}] as const;\n`
outputString += `export const Fuzzy${category}Finder = new Fuse(${category}Names, FuseOptions);\n`;
outputString += `export const find${category} = (searchTerm: string): ${category}[] => {\n`;
outputString += ` return fuzzySearch(searchTerm, Fuzzy${category}Finder, ${category}s);\n`;
outputString += "};\n\n";
outputString += `export const switch${category} = (build: Build, newPart: ${category}): Build => {\n`;
outputString += " const output = [...build] as Build;\n";
outputString += ` output[${i}] = newPart;\n`;
outputString += " return output;\n";
outputString += "};\n\n";
});
/// Iterator
outputString += "export const forEachBuild = async (callback: (build: Build) => Promise<void>): Promise<void> => {\n";
categories.forEach((category) => {
outputString += ` for (const ${category} of ${category}s) {\n`;
});
outputString += ` await callback([${categories.join(", ")}]);\n`;
categories.forEach(() => {
outputString += " }\n";
});
outputString += " };\n\n";
/// getBuildStat function
outputString += "export const getBuildStat = (build: Build): Stat => {\n";
outputString += ` const stat = {...${categories[0]}Data[build[0]]}\n`;
for (let i = 1; i < categories.length; i++) {
outputString += ` const ${categories[i]}Stat = ${categories[i]}Data[build[${i}]];\n`;
}
statKeys.forEach((key) => {
outputString += ` stat.${key} += ${categories.slice(1).map((category) => `${category}Stat.${key}`).join(" + ")};\n`;
});
outputString += " return stat;\n";
outputString += "};\n\n";
fs.writeFileSync(outputPath, outputString);
console.log(`Wrote to ${outputPath}`);