This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwrapper.ts
243 lines (190 loc) · 6.15 KB
/
wrapper.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import axios from "axios";
import Engine from "./src/engine";
import crypto from "crypto";
import { EngineOptions } from "./src/engine";
import { Round } from "./src/enums";
import assert from "assert";
import { set } from "lodash";
export async function init(
nbPlayers: number,
expansions: string[],
options: EngineOptions & { balancedGeneration: boolean },
seed?: string
): Promise<Engine> {
if (!seed) {
seed = crypto.randomBytes(8).toString("base64");
}
let numberSeed: number;
// If the seed is a number, use it directly, otherwise use a number generated from its hash
if ("" + parseInt(seed, 10) === seed) {
numberSeed = parseInt(seed, 10);
} else {
const md5sum = crypto.createHash("md5");
md5sum.update(seed);
numberSeed = "" + parseInt(seed, 10) === seed ? parseInt(seed, 10) : parseInt(md5sum.digest("hex").slice(-10), 16);
}
if (options.balancedGeneration || options.layout === "balanced") {
delete options.balancedGeneration;
const resp = await axios
.post("http://gaia-project.hol.es", { seed: numberSeed, player: nbPlayers })
.then((r) => r.data);
options.map = { sectors: resp.map };
// We use different standards for sides A & B of sectors than the online generator
if (nbPlayers === 2) {
options.map.sectors.forEach((val) => (val.sector = val.sector.replace(/A/, "B")));
} else {
options.map.sectors.forEach((val) => (val.sector = val.sector.replace(/B/, "A")));
}
}
const engine = new Engine([`init ${nbPlayers} ${seed}`], options);
engine.generateAvailableCommandsIfNeeded();
return engine;
}
export function setPlayerMetaData(engine: Engine, player: number, metaData: { name: string }) {
engine.players[player].name = metaData.name;
return engine;
}
export function setPlayerSettings(
engine: Engine,
player: number,
settings: {
autoCharge?: string;
autoIncome?: boolean;
autoBrainstone?: boolean;
itarsAutoChargeToArea3?: boolean;
}
) {
if ("autoCharge" in settings) {
set(engine.players[player], "settings.autoChargePower", Number(settings.autoCharge));
}
if ("autoIncome" in settings) {
set(engine.players[player], "settings.autoIncome", settings.autoIncome);
}
if ("autoBrainstone" in settings) {
set(engine.players[player], "settings.autoBrainstone", settings.autoBrainstone);
}
if ("itarsAutoChargeToArea3" in settings) {
set(engine.players[player], "settings.itarsAutoChargeToArea3", settings.itarsAutoChargeToArea3);
}
return engine;
}
export function playerSettings(engine: Engine, player: number) {
return {
autoCharge: String(engine.players[player].settings?.autoChargePower ?? 1),
autoIncome: !!engine.players[player].settings?.autoIncome,
autoBrainstone: !!engine.players[player].settings?.autoBrainstone,
itarsAutoChargeToArea3: !!engine.players[player].settings?.itarsAutoChargeToArea3,
};
}
export function move(engine: Engine, move: string, player: number) {
if (!move) {
// Don't save
(engine as any).noSave = true;
return engine;
}
if (!(engine instanceof Engine)) {
engine = Engine.fromData(engine);
}
const round = engine.round;
const backup = JSON.stringify(engine);
engine.move(move);
if (!engine.newTurn) {
// Try to complete the move for the player, e.g. if the player does not have auto brainstone
// but does have auto charge
const copy = Engine.fromData(JSON.parse(backup));
if (copy.autoMove(move)) {
engine = copy;
}
}
engine.generateAvailableCommandsIfNeeded();
if (engine.newTurn) {
afterMove(engine, round);
automove(engine);
}
return engine;
}
function afterMove(engine: Engine, oldRound: number) {
if (engine.round > oldRound && engine.round > 0) {
(engine as any).messages = [...((engine as any).messages || []), `Round ${engine.round}`];
}
}
export function automove(engine: Engine) {
let modified: boolean;
do {
modified = false;
let oldRound = engine.round;
while (!cancelled(engine) && !ended(engine) && engine.player(engine.playerToMove).dropped) {
engine.autoPass();
afterMove(engine, oldRound);
modified = true;
oldRound = engine.round;
}
oldRound = engine.round;
while (engine.autoMove()) {
afterMove(engine, oldRound);
modified = true;
oldRound = engine.round;
}
} while (modified);
}
export function ended(engine: Engine) {
return engine.ended;
}
export function cancelled(engine: Engine) {
return engine.ended && engine.round < Round.LastRound;
}
export function scores(engine: Engine) {
return engine.players.map((pl) => pl.data.victoryPoints);
}
export function factions(engine: Engine) {
return engine.players.map((pl) => pl.faction);
}
export async function replay(engine: Engine) {
const oldPlayers = engine.players;
engine = new Engine(engine.moveHistory, engine.options);
assert(engine.newTurn, "Last move of the game is incomplete");
for (let i = 0; i < oldPlayers.length && i < engine.players.length; i++) {
engine.players[i].name = oldPlayers[i].name;
engine.players[i].dropped = oldPlayers[i].dropped;
}
engine.generateAvailableCommandsIfNeeded();
automove(engine);
delete (engine as any).messages;
return engine;
}
export async function dropPlayer(engine: Engine, player: number) {
engine = engine instanceof Engine ? engine : Engine.fromData(engine);
engine.players[player].dropped = true;
if (engine.round <= 0) {
engine.ended = true;
} else {
automove(engine);
}
return engine;
}
export function currentPlayer(engine: Engine) {
return engine.playerToMove;
}
export function toSave(engine: Engine) {
if (!engine.newTurn || (engine as any).noSave) {
return undefined;
}
return engine;
}
export function messages(engine: Engine) {
const messages = (engine as any).messages || [];
delete (engine as any).messages;
return {
messages,
data: engine,
};
}
export function logLength(engine: Engine) {
return engine.moveHistory.length;
}
export function logSlice(engine: Engine, options?: { player?: number; start?: number; end?: number }) {
return { state: engine };
}
export function round(engine: Engine) {
return engine.round;
}