Compare commits

...

5 Commits

Author SHA1 Message Date
448e0354ea Added some core loop mechanic 2025-05-07 14:43:22 +02:00
2edb549156 added tick to core 2025-05-06 14:56:27 +02:00
37c404603d Created a test for downloading the core with the core.
added some safe checks in the core
2025-05-06 14:47:51 +02:00
3bd305dc10 Added disconnect 2025-05-06 14:03:58 +02:00
9f3771cea9 Fixed syntax error 2025-05-06 14:01:57 +02:00
2 changed files with 74 additions and 10 deletions

View File

@@ -48,7 +48,9 @@ function Core:read()
return false, "Websocket not initiated";
end
return true, textutils.unserialiseJSON(self.ws.receive())
local data, success = self.ws.receive()
return true, textutils.unserialiseJSON(data)
end
function Core:download(file)
@@ -65,15 +67,15 @@ function Core:download(file)
local success, data = self:read();
if not success then
return false, "Failed to read: "+data;
return false, "Failed to read: "..data;
end
if not data.type then
return false, "Invalid package: "+textutils.serialiseJSON(data);
return false, "Invalid package: "..textutils.serialiseJSON(data);
end
if not (data.type == "FILE_INFO") then
return false, "Invalid package type: "+textutils.serialiseJSON(data);
return false, "Invalid package type: "..textutils.serialiseJSON(data);
end
if not data.exists then
@@ -85,20 +87,73 @@ function Core:download(file)
local success, data = self:read();
if not success then
return false, "Failed to read: "+data;
return false, "Failed to read: "..data;
end
if not data.type then
return false, "Invalid package: "+textutils.serialiseJSON(data);
return false, "Invalid package: "..textutils.serialiseJSON(data);
end
if not data.type == "FILE_DATA" then
return false, "Invalid type: "+textutils.serialiseJSON(data)
return false, "Invalid type: "..textutils.serialiseJSON(data)
end
if fs.exists(file) then
fs.delete(file);
end
local fileStream = io.open(file, "w");
fileStream:write(data.fileContent);
if not fileStream then
return false, "Failed to open fileStream"
end
fileStream:write(data.data);
fileStream:close();
return true, "Success"
end
function Core:tick()
if not self then
error("No instance!")
end
if not self.ws then
return false, "Not initilized"
end
self:send({type = "TICK"});
local success, data = self:read();
if not success then
return false, "Failed to do anything: "..data;
end
print("Server expects Action: "..textutils.serialiseJSON(data))
end
function Core:disconnect()
if not self then
error("No instance!")
end
if not self.ws then
return "Not initilized"
end
self.ws.close();
end
function Core:setLoop(loop)
self.loop = loop;
end
function Core:start()
while true do
if self.loop then
self.loop();
end
end
end

9
downloadCore.lua Normal file
View File

@@ -0,0 +1,9 @@
require("core")
local core = Core.new();
core:connect();
core:download("core.lua");
core:disconnect();