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