diff options
author | FxQnLr <[email protected]> | 2024-04-30 20:47:13 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-30 20:47:13 +0200 |
commit | d95f49be9dde63de3694a56316fe7e07ad4062e7 (patch) | |
tree | e07af9156af08a15dbb33b6718359a3fc7c004d2 | |
parent | ab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b (diff) | |
download | funsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.tar funsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.tar.gz funsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.zip |
-rw-r--r-- | main.lua | 28 | ||||
-rw-r--r-- | messages.lua | 66 |
2 files changed, 70 insertions, 24 deletions
@@ -1,29 +1,10 @@ | |||
1 | ---@diagnostic disable: duplicate-set-field | ||
2 | |||
3 | local funsaac = RegisterMod("funsaac", 1) | 1 | local funsaac = RegisterMod("funsaac", 1) |
4 | require("messages") | 2 | require("messages") |
5 | 3 | ||
6 | local client = require("websocket").new("127.0.0.1", 12345, "/") | ||
7 | |||
8 | CONNECTED = false | ||
9 | |||
10 | function client:onmessage(message) | ||
11 | HandleResponse(ResponseContent(message)) | ||
12 | if CONNECTED == true then | ||
13 | self:send(GetMessage(Messages.StartScanning)) | ||
14 | CONNECTED = false | ||
15 | end | ||
16 | end | ||
17 | |||
18 | function client:onopen() | ||
19 | self:send(GetMessage(Messages.RequestServerInfo)) | ||
20 | end | ||
21 | |||
22 | |||
23 | local render_tick = 0 | 4 | local render_tick = 0 |
24 | local s = 0; | 5 | local s = 0; |
25 | local function onRender() | 6 | local function onRender() |
26 | client:update() | 7 | WsClient:update() |
27 | 8 | ||
28 | render_tick = render_tick + 1 | 9 | render_tick = render_tick + 1 |
29 | if render_tick >= 60 then | 10 | if render_tick >= 60 then |
@@ -32,4 +13,9 @@ local function onRender() | |||
32 | end | 13 | end |
33 | end | 14 | end |
34 | 15 | ||
35 | funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) \ No newline at end of file | 16 | local function onEnemyDamage(_, entity, amount, damage_flags, source, countdown_frames) |
17 | ScalarCmd(1) | ||
18 | end | ||
19 | |||
20 | funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) | ||
21 | funsaac:AddCallback(ModCallbacks.MC_ENTITY_TAKE_DMG, onEnemyDamage) \ No newline at end of file | ||
diff --git a/messages.lua b/messages.lua index 69576e1..a3f8d77 100644 --- a/messages.lua +++ b/messages.lua | |||
@@ -1,33 +1,93 @@ | |||
1 | ---@diagnostic disable: duplicate-set-field | ||
2 | |||
1 | local json = require("json") | 3 | local json = require("json") |
2 | 4 | ||
5 | WsClient = require("websocket").new("127.0.0.1", 12345, "/") | ||
6 | |||
7 | CONNECTED = false | ||
8 | |||
9 | function WsClient:onmessage(message) | ||
10 | HandleResponse(ResponseContent(message)) | ||
11 | if CONNECTED == true then | ||
12 | print("StartScanning") | ||
13 | StartScanning() | ||
14 | CONNECTED = false | ||
15 | end | ||
16 | end | ||
17 | |||
18 | function WsClient:onopen() | ||
19 | RequestServerInfo() | ||
20 | end | ||
21 | |||
3 | Messages = { | 22 | Messages = { |
4 | -- Handshake | 23 | -- Handshake |
5 | RequestServerInfo = { | 24 | RequestServerInfo = { |
6 | RequestServerInfo = { | 25 | RequestServerInfo = { |
7 | Id = 1, | 26 | Id = 1, |
8 | ClientName = "Funsaac v0.0.2", | 27 | ClientName = "Funsaac v0.0.3", |
9 | MessageVersion = 3 | 28 | MessageVersion = 3 |
10 | } | 29 | } |
11 | }, | 30 | }, |
12 | 31 | ||
13 | -- Enumeration | 32 | -- Enumeration |
14 | 33 | ||
15 | StartScanning = { | 34 | StartScanning = { |
16 | StartScanning = { | 35 | StartScanning = { |
17 | Id = 1 | 36 | Id = 1 |
18 | } | 37 | } |
38 | }, | ||
39 | |||
40 | -- Generic Device | ||
41 | ScalarCmd = { | ||
42 | ScalarCmd = { | ||
43 | Id = 1, | ||
44 | DeviceIndex = 0, | ||
45 | Scalars = { | ||
46 | { | ||
47 | Index = 0, | ||
48 | Scalar = 1, | ||
49 | ActuatorType = "Vibrate" | ||
50 | } | ||
51 | } | ||
52 | } | ||
19 | } | 53 | } |
20 | } | 54 | } |
21 | 55 | ||
22 | local cnt = 1; | 56 | local cnt = 1; |
23 | 57 | ||
58 | local function getEncodedJson(msg) | ||
59 | return "[" .. json.encode(msg) .. "]" | ||
60 | end | ||
61 | |||
62 | ---Get message with correct id | ||
63 | ---@param msg table Message from Messages table | ||
24 | function GetMessage(msg) | 64 | function GetMessage(msg) |
25 | local message = msg | 65 | local message = msg |
26 | message[next(msg)]["Id"] = cnt | 66 | message[next(msg)]["Id"] = cnt |
27 | cnt = cnt + 1 | 67 | cnt = cnt + 1 |
28 | return "[" .. json.encode(message) .. "]" | 68 | return message |
69 | end | ||
70 | |||
71 | -- REQUESTS | ||
72 | |||
73 | ---Request Info from the server | ||
74 | function RequestServerInfo() | ||
75 | WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo))) | ||
29 | end | 76 | end |
30 | 77 | ||
78 | function StartScanning() | ||
79 | WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning))) | ||
80 | end | ||
81 | |||
82 | function ScalarCmd(strength) | ||
83 | local message = GetMessage(Messages.ScalarCmd) | ||
84 | -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength | ||
85 | WsClient:send(getEncodedJson(message)) | ||
86 | end | ||
87 | |||
88 | |||
89 | --RESPONSES | ||
90 | |||
31 | function ResponseContent(message) | 91 | function ResponseContent(message) |
32 | local msg = json.decode(message)[1] | 92 | local msg = json.decode(message)[1] |
33 | local type = next(msg) | 93 | local type = next(msg) |