From d95f49be9dde63de3694a56316fe7e07ad4062e7 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Tue, 30 Apr 2024 20:47:13 +0200 Subject: working connection and basic request --- main.lua | 28 +++++++------------------- messages.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 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 @@ ----@diagnostic disable: duplicate-set-field - local funsaac = RegisterMod("funsaac", 1) require("messages") -local client = require("websocket").new("127.0.0.1", 12345, "/") - -CONNECTED = false - -function client:onmessage(message) - HandleResponse(ResponseContent(message)) - if CONNECTED == true then - self:send(GetMessage(Messages.StartScanning)) - CONNECTED = false - end -end - -function client:onopen() - self:send(GetMessage(Messages.RequestServerInfo)) -end - - local render_tick = 0 local s = 0; local function onRender() - client:update() + WsClient:update() render_tick = render_tick + 1 if render_tick >= 60 then @@ -32,4 +13,9 @@ local function onRender() end end -funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) \ No newline at end of file +local function onEnemyDamage(_, entity, amount, damage_flags, source, countdown_frames) + ScalarCmd(1) +end + +funsaac:AddCallback(ModCallbacks.MC_POST_RENDER, onRender) +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 @@ +---@diagnostic disable: duplicate-set-field + local json = require("json") +WsClient = require("websocket").new("127.0.0.1", 12345, "/") + +CONNECTED = false + +function WsClient:onmessage(message) + HandleResponse(ResponseContent(message)) + if CONNECTED == true then + print("StartScanning") + StartScanning() + CONNECTED = false + end +end + +function WsClient:onopen() + RequestServerInfo() +end + Messages = { -- Handshake RequestServerInfo = { RequestServerInfo = { Id = 1, - ClientName = "Funsaac v0.0.2", + ClientName = "Funsaac v0.0.3", MessageVersion = 3 } }, -- Enumeration - + StartScanning = { StartScanning = { Id = 1 } + }, + + -- Generic Device + ScalarCmd = { + ScalarCmd = { + Id = 1, + DeviceIndex = 0, + Scalars = { + { + Index = 0, + Scalar = 1, + ActuatorType = "Vibrate" + } + } + } } } local cnt = 1; +local function getEncodedJson(msg) + return "[" .. json.encode(msg) .. "]" +end + +---Get message with correct id +---@param msg table Message from Messages table function GetMessage(msg) local message = msg message[next(msg)]["Id"] = cnt cnt = cnt + 1 - return "[" .. json.encode(message) .. "]" + return message +end + +-- REQUESTS + +---Request Info from the server +function RequestServerInfo() + WsClient:send(getEncodedJson(GetMessage(Messages.RequestServerInfo))) end +function StartScanning() + WsClient:send(getEncodedJson(GetMessage(Messages.StartScanning))) +end + +function ScalarCmd(strength) + local message = GetMessage(Messages.ScalarCmd) + -- message[next(Messages.ScalarCmd)]["Scalars"][0]["Scalar"] = strength + WsClient:send(getEncodedJson(message)) +end + + +--RESPONSES + function ResponseContent(message) local msg = json.decode(message)[1] local type = next(msg) -- cgit v1.2.3