fix: Refactor message handling to improve status updates and server communication

This commit is contained in:
MayaTheShy
2026-02-16 00:56:59 -05:00
parent 4dacfb53aa
commit a91ad1d4ca

View File

@@ -60,41 +60,47 @@ end
while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
print("Received modem message on channel " .. channel)
if type(message) == "table" then
print(" Message type: " .. (message.type or "no type"))
print(" Turtle ID: " .. tostring(message.turtleID))
else
print(" Message is not a table, it's: " .. type(message))
end
if channel == STATUS_CHANNEL and type(message) == "table" and message.type == "status" then
-- Status update from turtle
print("✓ Valid status from Turtle " .. (message.turtleID or "?"))
-- Only process messages on our channels
if channel == STATUS_CHANNEL or channel == CHANNEL_RECEIVE then
print("Received modem message on channel " .. channel)
-- Update local cache
turtles[message.turtleID] = message
-- Forward to web server
local success = sendToServer(message)
if success then
print(" -> Sent to server")
if type(message) == "table" then
print(" Message type: " .. (message.type or "no type"))
print(" Turtle ID: " .. tostring(message.turtleID))
-- Check for commands from server
local commands = pollCommands(message.turtleID)
-- Forward commands back to turtle
for _, cmd in ipairs(commands) do
print(" -> Command for Turtle " .. message.turtleID .. ": " .. cmd.command)
modem.transmit(100, 101, {
command = cmd.command,
param = cmd.param,
target = message.turtleID
})
if message.type == "status" then
-- Status update from turtle
print("✓ Valid status from Turtle " .. (message.turtleID or "?"))
-- Update local cache
turtles[message.turtleID] = message
-- Forward to web server
local success = sendToServer(message)
if success then
print(" -> Sent to server")
-- Check for commands from server
local commands = pollCommands(message.turtleID)
-- Forward commands back to turtle
for _, cmd in ipairs(commands) do
print(" -> Command for Turtle " .. message.turtleID .. ": " .. cmd.command)
modem.transmit(100, 101, {
command = cmd.command,
param = cmd.param,
target = message.turtleID
})
end
else
print(" -> Failed to send to server")
end
else
print(" Ignoring non-status message")
end
else
print(" -> Failed to send to server")
-- Ignore non-table messages (from other mods/systems)
-- Silently skip to avoid spam
end
end