summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-30 20:47:13 +0200
committerFxQnLr <[email protected]>2024-04-30 20:47:13 +0200
commitd95f49be9dde63de3694a56316fe7e07ad4062e7 (patch)
treee07af9156af08a15dbb33b6718359a3fc7c004d2
parentab76e442ab4fb4d41ccbc211f9ac05e19aaefd4b (diff)
downloadfunsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.tar
funsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.tar.gz
funsaac-d95f49be9dde63de3694a56316fe7e07ad4062e7.zip
working connection and basic requestHEADmain
-rw-r--r--main.lua28
-rw-r--r--messages.lua66
2 files changed, 70 insertions, 24 deletions
diff --git a/main.lua b/main.lua
index 31555ee..c19ae3c 100644
--- a/main.lua
+++ b/main.lua
@@ -1,29 +1,10 @@
1---@diagnostic disable: duplicate-set-field
2
3local funsaac = RegisterMod("funsaac", 1) 1local funsaac = RegisterMod("funsaac", 1)
4require("messages") 2require("messages")
5 3
6local client = require("websocket").new("127.0.0.1", 12345, "/")
7
8CONNECTED = false
9
10function client:onmessage(message)
11 HandleResponse(ResponseContent(message))
12 if CONNECTED == true then
13 self:send(GetMessage(Messages.StartScanning))
14 CONNECTED = false
15 end
16end
17
18function client:onopen()
19 self:send(GetMessage(Messages.RequestServerInfo))
20end
21
22
23local render_tick = 0 4local render_tick = 0
24local s = 0; 5local s = 0;
25local function onRender() 6local 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
33end 14end
34 15
35funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) \ No newline at end of file 16local function onEnemyDamage(_, entity, amount, damage_flags, source, countdown_frames)
17 ScalarCmd(1)
18end
19
20funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender)
21funsaac: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
1local json = require("json") 3local json = require("json")
2 4
5WsClient = require("websocket").new("127.0.0.1", 12345, "/")
6
7CONNECTED = false
8
9function WsClient:onmessage(message)
10 HandleResponse(ResponseContent(message))
11 if CONNECTED == true then
12 print("StartScanning")
13 StartScanning()
14 CONNECTED = false
15 end
16end
17
18function WsClient:onopen()
19 RequestServerInfo()
20end
21
3Messages = { 22Messages = {
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
22local cnt = 1; 56local cnt = 1;
23 57
58local function getEncodedJson(msg)
59 return "[" .. json.encode(msg) .. "]"
60end
61
62---Get message with correct id
63---@param msg table Message from Messages table
24function GetMessage(msg) 64function 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
69end
70
71-- REQUESTS
72
73---Request Info from the server
74function RequestServerInfo()
75 WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo)))
29end 76end
30 77
78function StartScanning()
79 WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning)))
80end
81
82function ScalarCmd(strength)
83 local message = GetMessage(Messages.ScalarCmd)
84 -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength
85 WsClient:send(getEncodedJson(message))
86end
87
88
89--RESPONSES
90
31function ResponseContent(message) 91function 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)