From 2312355418a33a562c0921860bd86633a91ce30c Mon Sep 17 00:00:00 2001 From: Moonlit Productions Date: Mon, 29 Dec 2025 01:14:56 -0500 Subject: [PATCH] entirely rework event handling --- startup.lua | 165 ++++++++++++++++++---------------------------------- 1 file changed, 56 insertions(+), 109 deletions(-) diff --git a/startup.lua b/startup.lua index 1f05879..47a07f8 100644 --- a/startup.lua +++ b/startup.lua @@ -19,6 +19,8 @@ local config = require("config") local addresses = require("addresses") local utils = require("utils") local display = require("display") +local events = require("events") +local handlers = require("handlers") --------------------------------------------- -- INITIALIZATION @@ -52,6 +54,7 @@ end -- Initialize modules utils.init(config, gate) display.init(mon, config, addresses, utils) +handlers.init(config, gate, mon, utils, display, events) -- Ensure gate starts disconnected gate.disconnectStargate() @@ -68,69 +71,52 @@ end local dialing = false local totalstate = nil local disconnect = false -local incomingAddress = {} -local incomingEntityType = "" -local incomingEntityName = "" -local destAddress = {} -local destAddressname = "" local selx, sely = 0, 0 local y = 0 -local lastReceivedMessage = nil -local remoteHasComputer = false -local enteringPassword = false + +-- Get shared state from handlers +local function getState() + return handlers.getState() +end --------------------------------------------- --- EVENT HANDLERS +-- EVENT HANDLERS (Using centralized event system) --------------------------------------------- local function GetClick() mon.setTextScale(1) - local event, _, xPos, yPos = os.pullEvent("monitor_touch") - return xPos, yPos + handlers.setupConnectionHandlers() + local result = events.pullEvent("monitor_touch") + return result == "disconnect" and 0 or 1 end local function GetActivation() - _, _, incomingAddress = os.pullEvent("stargate_incoming_wormhole") - utils.log("Incoming wormhole: " .. gate.addressToString(incomingAddress)) + handlers.setupConnectionHandlers() + local result = events.pullEvent("stargate_incoming_wormhole") return 1 end local function ParaDisconnect() - local dx, dy = 0, 0 - _, _, dx, dy = os.pullEvent("monitor_touch") - - -- Ignore clicks during password entry - if enteringPassword then - return nil + -- Now handled by event system with priority + handlers.setupConnectionHandlers() + while true do + local result = events.pullEvent("monitor_touch") + if result == "disconnect" then + return 1 + end end - - -- Only disconnect if clicking the disconnect button (bottom-right corner) - if dy >= 17 and dy <= 19 and dx >= 20 and dx <= 28 then - gate.disconnectStargate() - redstone.setOutput("top", false) - utils.log("Manual disconnect triggered") - return 1 - end - -- Return nothing to keep waiting if clicked elsewhere - return nil end local function EntityRead() sleep(0.1) - _, _, incomingEntityType, incomingEntityName, _ = os.pullEvent("stargate_reconstructing_entity") - utils.log("Entity reconstructed: " .. incomingEntityName .. " (" .. incomingEntityType .. ")") + handlers.setupConnectionHandlers() + local result = events.pullEvent("stargate_reconstructing_entity") return 1 end local function DisconnectCheck() - local _, _, disCode = os.pullEvent("stargate_disconnected") - redstone.setOutput("top", false) - utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")") - - if config.autoOpenIrisAfterDisconnect then - utils.openIris() - end - + handlers.setupConnectionHandlers() + local result = events.pullEvent("stargate_disconnected") return 2 end @@ -140,57 +126,13 @@ local function Paratimeout() end local function GetMessage() - local _, _, message = os.pullEvent("stargate_message_received") - lastReceivedMessage = message + handlers.setupConnectionHandlers() + local result = events.pullEvent("stargate_message_received") return 1 end local function HandlePasswordEntry() - -- Display password prompt - display.showPasswordPrompt() - - local password = "" - enteringPassword = true - - while enteringPassword do - -- Use os.pullEvent directly to avoid conflicts with ParaDisconnect - local _, _, x, y = os.pullEvent("monitor_touch") - - -- Check number buttons (1-9) - if y >= 7 and y <= 15 then - local row = math.floor((y - 7) / 3) - local col = math.floor((x - 8) / 5) - - if col >= 0 and col <= 2 and row >= 0 and row <= 2 then - local num = row * 3 + col + 1 - if num >= 1 and num <= 9 then - password = password .. tostring(num) - display.updatePasswordDisplay(password) - end - end - end - - -- Check bottom row buttons - if y >= 16 and y <= 18 then - if x >= 8 and x <= 11 then - -- Clear button - password = "" - display.updatePasswordDisplay(password) - elseif x >= 13 and x <= 16 then - -- Zero button - password = password .. "0" - display.updatePasswordDisplay(password) - elseif x >= 18 and x <= 21 then - -- OK button - submit password - enteringPassword = false - end - end - end - - -- Send password attempt - utils.sendPasswordAttempt(password) - - return password + return handlers.handlePasswordInput() end local function HandleIncomingPasswordRequest(password) @@ -210,6 +152,7 @@ end local function MonitorRemoteIris() -- Continuously monitor remote iris state while connected local lastIrisState = nil + local state = getState() while gate.isStargateConnected() do sleep(0.5) -- Check every half second @@ -228,7 +171,7 @@ local function MonitorRemoteIris() end -- If remote has computer and iris just became fully closed, offer password entry - if remoteHasComputer and remoteIrisState == 100 and (not lastIrisState or lastIrisState < 100) then + if state.remoteHasComputer and remoteIrisState == 100 and (not lastIrisState or lastIrisState < 100) then utils.log("Remote iris closed but computer detected - showing password prompt") local password = HandlePasswordEntry() @@ -241,8 +184,8 @@ local function MonitorRemoteIris() local result = parallel.waitForAny(GetMessage, WaitForResponse) if result == 1 then - local response = lastReceivedMessage - lastReceivedMessage = nil + local response = state.lastReceivedMessage + state.lastReceivedMessage = nil if response == "IRIS_OPEN" then display.showPasswordResult(true) utils.log("Password accepted - iris opened") @@ -275,7 +218,7 @@ local function MonitorRemoteIris() display.drawIrisStatus() display.drawDisconnectButton() else - display.showConnected(destAddressname, destAddress) + display.showConnected(state.destAddressname, state.destAddress) end else -- Remote iris is open (0 or nil) @@ -283,7 +226,7 @@ local function MonitorRemoteIris() -- Iris just opened utils.log("Remote iris opened - connection safe") end - display.showConnected(destAddressname, destAddress) + display.showConnected(state.destAddressname, state.destAddress) end lastIrisState = remoteIrisState @@ -298,6 +241,8 @@ end --------------------------------------------- local function handleIncomingWormhole() + local state = getState() + mon.setBackgroundColor(colors.black) mon.clear() mon.setBackgroundColor(colors.red) @@ -305,11 +250,11 @@ local function handleIncomingWormhole() mon.setCursorPos(9, 4) mon.write("INCOMING") - incomingAddress = utils.addressToTable(incomingAddress) + state.incomingAddress = utils.addressToTable(state.incomingAddress) -- Check security - local allowed, reason = utils.isAddressAllowed(incomingAddress) - local addressString = gate.addressToString(incomingAddress) or "Unknown" + local allowed, reason = utils.isAddressAllowed(state.incomingAddress) + local addressString = gate.addressToString(state.incomingAddress) or "Unknown" utils.log("Incoming wormhole from: " .. addressString .. " " .. reason) @@ -342,12 +287,12 @@ local function handleIncomingWormhole() -- Whichever happens first, that function returns and we handle it local result = parallel.waitForAny(EntityRead, DisconnectCheck, GetMessage, ParaDisconnect) if (result == 1) then - display.showEntity(incomingEntityType, incomingEntityName, allowed) - incomingEntityType = "" - incomingEntityName = "" + display.showEntity(state.incomingEntityType, state.incomingEntityName, allowed) + state.incomingEntityType = "" + state.incomingEntityName = "" elseif (result == 3) then -- Received a message - local message = lastReceivedMessage + local message = state.lastReceivedMessage utils.log("Received message: " .. message) -- Check if it's a password attempt @@ -355,7 +300,7 @@ local function handleIncomingWormhole() local password = message:sub(15) HandleIncomingPasswordRequest(password) end - lastReceivedMessage = nil + state.lastReceivedMessage = nil else disconnect = true end @@ -444,6 +389,7 @@ local function dialGate(address) end local function selectGateFromList() + local state = getState() local selecting = true while dialing == false and selecting == true do selx, sely = GetClick() @@ -452,8 +398,8 @@ local function selectGateFromList() for i = 1, #buttonXY do if (sely == buttonXY[i][3]) and ((selx >= buttonXY[i][1]) and (selx <= buttonXY[i][2])) then dialGate(computerAddresses[i]) - destAddressname = computerNames[i] - destAddress = computerAddresses[i] + state.destAddressname = computerNames[i] + state.destAddress = computerAddresses[i] dialing = true sely = 0 selx = 0 @@ -542,6 +488,7 @@ local function selectCategory() end local function handleOutgoingDial() + local state = getState() totalstate = true local PDO = 0 PDO = parallel.waitForAny(selectCategory, Paratimeout) @@ -552,7 +499,7 @@ local function handleOutgoingDial() os.pullEvent("stargate_outgoing_wormhole") -- Wait briefly for version message from remote gate - remoteHasComputer = false + state.remoteHasComputer = false local function WaitForVersion() sleep(1) -- Wait up to 1 second for version message return -1 @@ -560,10 +507,10 @@ local function handleOutgoingDial() local result = parallel.waitForAny(GetMessage, WaitForVersion) if result == 1 then - local message = lastReceivedMessage - lastReceivedMessage = nil + local message = state.lastReceivedMessage + state.lastReceivedMessage = nil if message:sub(1, 6) == "SGCS_V" then - remoteHasComputer = true + state.remoteHasComputer = true local version = message:sub(7) utils.log("Remote gate has control system version " .. version) end @@ -600,7 +547,7 @@ local function handleOutgoingDial() display.drawDisconnectButton() else -- Remote iris is open or no iris present - display.showConnected(destAddressname, destAddress) + display.showConnected(state.destAddressname, state.destAddress) end if (gate.isStargateConnected() == true) then @@ -617,9 +564,9 @@ local function handleOutgoingDial() end display.clearButtonData() - destAddress = {} - destAddressname = "" - remoteHasComputer = false + state.destAddress = {} + state.destAddressname = "" + state.remoteHasComputer = false end ---------------------------------------------