-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patheditor-client.ts
110 lines (86 loc) · 2.33 KB
/
editor-client.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
/// <reference path="./editor.d.ts" />
import { StandardWebSocketClient } from "https://deno.land/x/[email protected]/mod.ts";
const ws = new StandardWebSocketClient("ws://localhost:46735");
ws.on("error", (e: Error) => {
console.error("Encountered error in editor connection:", e);
});
ws.on("close", () => {
console.log("Connection to editor closed.");
});
function sendMessage(msg: EditorRequest) {
const jsonMsg = JSON.stringify(msg);
console.log("Sending message to editor:", jsonMsg);
ws.send(jsonMsg);
}
ws.on("message", (message) => {
try {
const msg: EditorEvent = JSON.parse(message.data);
console.log("Received message from editor:", msg);
if (msg.type === "welcome") {
console.log("The editor has welcomed us. Trying to select an entity.");
sendMessage({
type: "selectEntity",
entity: {
id: "6a561b6be27290db",
tblu: "0093BA77DCA54AD5",
},
});
console.log("Requesting a list of all entities.");
sendMessage({
type: "listEntities",
});
return;
}
if (msg.type === "entityList") {
console.log("Received a list of entities:", msg.entities);
console.log("Getting details for the 9th entity in the list for no particular reason.");
sendMessage({
type: "getEntityDetails",
entity: msg.entities[9],
});
console.log("Making hitman invisible.");
sendMessage({
type: "setEntityProperty",
entity: {
id: "6a561b6be27290db",
tblu: "0093BA77DCA54AD5",
},
property: "m_bVisible",
value: false,
});
return;
}
if (msg.type === "entityDetails") {
console.log("Received details for an entity:", msg.entity);
if (msg.entity.properties.m_eidParent.data !== null) {
console.log("Setting parent property to null and getting details again.");
sendMessage({
type: "setEntityProperty",
entity: {
id: msg.entity.id,
tblu: msg.entity.tblu,
},
property: "m_eidParent",
value: null,
});
sendMessage({
type: "getEntityDetails",
entity: {
id: msg.entity.id,
tblu: msg.entity.tblu,
},
});
}
return;
}
} catch (e) {
console.error("Encountered error while parsing message from editor:", e, message.data);
}
});
ws.on("open", () => {
console.log("Connected to SDK Editor.");
sendMessage({
type: "hello",
identifier: "Editor Test Client"
});
});