-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.ts
More file actions
71 lines (67 loc) · 2.12 KB
/
parser.ts
File metadata and controls
71 lines (67 loc) · 2.12 KB
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
import fs from "fs";
import readline from "readline";
import { ResourceDefinition, ResourceEffectType, Turn } from "./types";
export async function parseFile(filePath: string): Promise<{
budget: number;
turns: Turn[];
resources: ResourceDefinition[];
}> {
return new Promise((resolve) => {
const readStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: readStream,
crlfDelay: Infinity,
});
let budget = 0;
let resourceCount = 0;
let turnCount = 0;
let lineIndex = 0;
const turns: Turn[] = [];
const resources: ResourceDefinition[] = [];
rl.on("line", (line) => {
const parts = line.trim().split(" ");
if (lineIndex === 0) {
// First line: D R T
budget = parseInt(parts[0], 10);
resourceCount = parseInt(parts[1], 10);
turnCount = parseInt(parts[2], 10);
} else if (lineIndex <= resourceCount) {
// Resource lines
const [
id,
activationCost,
periodicCost,
activeTurns,
maintenanceTurns,
lifecycle,
buildingsPowered,
effectType,
effectValue,
] = parts;
resources.push({
id: parseInt(id, 10),
activationCost: parseInt(activationCost, 10),
periodicCost: parseInt(periodicCost, 10),
activeTurns: parseInt(activeTurns, 10),
maintenanceTurns: parseInt(maintenanceTurns, 10),
lifecycle: parseInt(lifecycle, 10),
buildingsPowered: parseInt(buildingsPowered, 10),
effectType: effectType as ResourceEffectType,
effectValue: effectValue ? parseInt(effectValue, 10) : undefined,
});
} else {
// Turn lines
const [minBuildings, maxBuildings, profitPerBuilding] = parts;
turns.push({
minBuildings: parseInt(minBuildings, 10),
maxBuildings: parseInt(maxBuildings, 10),
profitPerBuilding: parseInt(profitPerBuilding, 10),
});
}
lineIndex++;
});
rl.on("close", () => {
resolve({ budget, turns, resources });
});
});
}