forked from boardgamers/gaia-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-contained.ts
43 lines (33 loc) · 1.32 KB
/
self-contained.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
import launch from "./launcher";
import Engine from "@engine";
import Wrapper from "./components/Wrapper.vue";
import Game from "./components/Game.vue";
function launchSelfContained(selector = "#app", debug = true) {
const emitter = launch(selector, debug ? Wrapper : Game);
let engine = new Engine(["init 2 randomSeed2"], { layout: "xshape", advancedRules: true });
engine.generateAvailableCommandsIfNeeded();
const unsub = emitter.store.subscribeAction(({ payload, type }) => {
if (type === "gaiaViewer/loadFromJSON") {
const egData: Engine = payload;
engine = new Engine(egData.moveHistory, egData.options);
engine.generateAvailableCommandsIfNeeded();
emitter.emit("state", JSON.parse(JSON.stringify(engine)));
}
});
emitter.app.$once("hook:beforeDestroy", unsub);
emitter.on("move", (move: string) => {
console.log("executing move", move);
const copy = Engine.fromData(JSON.parse(JSON.stringify(engine)));
if (move) {
copy.move(move);
copy.generateAvailableCommandsIfNeeded();
// Only save updated version if a full turn was done
if (copy.newTurn) {
engine = copy;
}
}
emitter.emit("state", JSON.parse(JSON.stringify(copy)));
});
emitter.emit("state", JSON.parse(JSON.stringify(engine)));
}
export default launchSelfContained;