-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
101 lines (68 loc) · 2.71 KB
/
main.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
import { app } from "electron";
import fs from "fs";
import path from "path";
import Deskot from "./src/Deskot";
import DeskotManager from "./src/DeskotManager";
import Coordinate from "./src/position/Coordinate";
import BehaviorBuilder from "./src/behavior/BehaviorBuilder";
class Main {
private static activeDeskotList: Array<string>;
static config: any;
static deskotManager = new DeskotManager();
static path = path.resolve(__dirname);
static main() {
try {
this.loadConfiguration();
this.loadDeskots();
} catch (e) {
console.error(e);
throw new Error("Failed to load configuration file.");
}
this.deskotManager.start();
}
private static loadConfiguration() {
const raw = fs.readFileSync(path.resolve(__dirname, "config", "./config.json")).toString();
const config = JSON.parse(raw);
this.activeDeskotList = config?.activeDeskotList as Array<string>;
this.config = config;
}
private static loadDeskots() {
for (const deskotName of this.activeDeskotList) {
this.createDeskot(deskotName);
}
}
private static createDeskot(deskotName: string) {
console.log(`Creating Deskot instance... (Deskot Instance Name: ${deskotName})`)
const deskotPath = path.resolve(__dirname, "deskots", deskotName);
const deskotInstance = new Deskot(deskotName);
let actionsXmlPath = path.resolve(deskotPath, "./actions.xml");
let behaviorsXmlPath = path.resolve(deskotPath, "./behaviors.xml");
if (!fs.existsSync(actionsXmlPath)) {
actionsXmlPath = path.resolve(__dirname, "config", "./default-actions.xml");
}
if (!fs.existsSync(behaviorsXmlPath)) {
behaviorsXmlPath = path.resolve(__dirname, "config", "./default-behaviors.xml");
}
deskotInstance.applyXml(actionsXmlPath)
.applyXml(behaviorsXmlPath)
.start();
this.initDeskot(deskotInstance);
}
private static async initDeskot(deskotInstance: Deskot) {
console.log(`Initializing Deskot instance... (${deskotInstance.toString()})`);
deskotInstance.anchor = new Coordinate(-4000, -4000);
deskotInstance.lookRight = Math.random() < 0.5;
try {
const behavior = await BehaviorBuilder.buildRandomBehavior(deskotInstance, null);
deskotInstance.setBehavior(behavior!);
} catch (e) {
console.error(e);
throw new Error("Failed to initalize the first action.");
}
}
}
// Execute main method
app.on("ready", () => {
Main.main();
});
export default Main;