-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparse_input.js
More file actions
103 lines (94 loc) · 2.87 KB
/
parse_input.js
File metadata and controls
103 lines (94 loc) · 2.87 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
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
import { guess_character } from './gamestate_nolog';
import { card_name_to_id_fuzzy, ready } from './card_name_to_id_fuzzy';
await ready;
export default function(dataString) {
let jsonData = null;
if (/^\s*\{/.test(dataString)) {
jsonData = JSON.parse(dataString);
} else {
jsonData = {
a: { physique: 0, max_physique: 0, cards: [] },
b: { physique: 0, max_physique: 0, cards: [] },
};
const lines = dataString.split(/\r\n|\r|\n/).map((s) => s.trim()).filter((s) => s.length);
let section = 0;
for (let line of lines) {
const comment = line.indexOf('#');
if (comment !== -1) {
line = line.substring(0, comment).trimEnd();
if (!line.length) {
continue;
}
}
if (line === 'a:') {
section = 1;
continue;
}
if (line === 'b:') {
section = 2;
continue;
}
const colon = line.indexOf(':');
if (colon === -1) {
(section === 1 ? jsonData.a : jsonData.b).cards.push(line);
} else {
const leftSide = line.substring(0, colon).trimEnd().replaceAll(' ', '_');
let rightSide = line.substring(colon + 1).trimStart();
switch (rightSide) {
case 'true':
case 'yes':
rightSide = true;
break;
case 'false':
case 'no':
rightSide = false;
break;
default:
if (!isNaN(rightSide)) {
rightSide = +rightSide;
}
}
if (section === 0) {
if (leftSide === 'permute') {
if (rightSide === 'a') {
jsonData.permute_a = true;
} else if (rightSide === 'b') {
jsonData.permute_b = true;
}
} else {
jsonData[leftSide] = rightSide;
}
} else {
(section === 1 ? jsonData.a : jsonData.b)[leftSide] = rightSide;
}
}
}
}
if (jsonData.permute_a && jsonData.permute_b) {
console.error('Permuting both players not supported.');
process.exit(1);
}
if (jsonData.players) {
jsonData.a = jsonData.players[0];
jsonData.b = jsonData.players[1];
delete jsonData.players;
}
jsonData.a.max_hp = jsonData.a.hp + jsonData.a.physique;
jsonData.b.max_hp = jsonData.b.hp + jsonData.b.physique;
const deck_slots = jsonData.deck_slots || 8;
while (jsonData.a.cards.length < deck_slots) {
jsonData.a.cards.push('normal attack');
}
while (jsonData.b.cards.length < deck_slots) {
jsonData.b.cards.push('normal attack');
}
jsonData.a.cards = jsonData.a.cards.map(card_name_to_id_fuzzy);
jsonData.b.cards = jsonData.b.cards.map(card_name_to_id_fuzzy);
if (!jsonData.a.character) {
jsonData.a.character = guess_character(jsonData.a);
}
if (!jsonData.b.character) {
jsonData.b.character = guess_character(jsonData.b);
}
return jsonData;
}