From f8da3dce2e4bb9b58bb2e808d5aed70d5873bf9a Mon Sep 17 00:00:00 2001 From: Zacharias Date: Tue, 6 May 2025 11:51:18 +0200 Subject: [PATCH] Initial Commit --- core.lua | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ excavate.lua | 3 +++ 2 files changed, 67 insertions(+) create mode 100644 core.lua create mode 100644 excavate.lua diff --git a/core.lua b/core.lua new file mode 100644 index 0000000..476f50d --- /dev/null +++ b/core.lua @@ -0,0 +1,64 @@ +-- Websocker: Used for connection between the controle webserver and Units(computers) +local ws + +function Core:connect() + ws, err = assert(http.websocket("ws://localhost:8080")); + + if not ws then + term.setTextColor(colors.red); + print(err); + term.setTextColor(colors.white); + end +end + +function Core:send(package) + if not package then + return false, "Missing package"; + end + + if not ws then + return false, "Websocket not initiated"; + end + + ws.send(textutils.serialiseJSON(package)); +end + +function Core:read() + if not ws then + return false, "Websocket not initiated"; + end + + return true, textutils.unserialiseJSON(ws.read()) +end + +function Core:download(file) + if not ws then + return false, "Websocket not initiated"; + end + + self:send({type = "REQUEST_FILE_INFO", file = file}); + + local success, data = self:read(); + + if not success then + return false, "Failed to read: "+data; + end + + if not data.type then + return false, "Invalid package: "+textutils.serialiseJSON(data); + end + + if not (data.type == "FILE_INFO") then + return false, "Invalid package type: "+textutils.serialiseJSON(data); + end + + if not data.exists then + return false, "File is missing from server" + end + + self:send({type = "REQUEST_FILE_DATA", file = file}) + + local success, data = self:read(); + + +end \ No newline at end of file diff --git a/excavate.lua b/excavate.lua new file mode 100644 index 0000000..15f3a6a --- /dev/null +++ b/excavate.lua @@ -0,0 +1,3 @@ +require("core") + +Core.connect()