entirely rework event handling
This commit is contained in:
163
startup.lua
163
startup.lua
@ -19,6 +19,8 @@ local config = require("config")
|
|||||||
local addresses = require("addresses")
|
local addresses = require("addresses")
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
local display = require("display")
|
local display = require("display")
|
||||||
|
local events = require("events")
|
||||||
|
local handlers = require("handlers")
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
-- INITIALIZATION
|
-- INITIALIZATION
|
||||||
@ -52,6 +54,7 @@ end
|
|||||||
-- Initialize modules
|
-- Initialize modules
|
||||||
utils.init(config, gate)
|
utils.init(config, gate)
|
||||||
display.init(mon, config, addresses, utils)
|
display.init(mon, config, addresses, utils)
|
||||||
|
handlers.init(config, gate, mon, utils, display, events)
|
||||||
|
|
||||||
-- Ensure gate starts disconnected
|
-- Ensure gate starts disconnected
|
||||||
gate.disconnectStargate()
|
gate.disconnectStargate()
|
||||||
@ -68,69 +71,52 @@ end
|
|||||||
local dialing = false
|
local dialing = false
|
||||||
local totalstate = nil
|
local totalstate = nil
|
||||||
local disconnect = false
|
local disconnect = false
|
||||||
local incomingAddress = {}
|
|
||||||
local incomingEntityType = ""
|
|
||||||
local incomingEntityName = ""
|
|
||||||
local destAddress = {}
|
|
||||||
local destAddressname = ""
|
|
||||||
local selx, sely = 0, 0
|
local selx, sely = 0, 0
|
||||||
local y = 0
|
local y = 0
|
||||||
local lastReceivedMessage = nil
|
|
||||||
local remoteHasComputer = false
|
-- Get shared state from handlers
|
||||||
local enteringPassword = false
|
local function getState()
|
||||||
|
return handlers.getState()
|
||||||
|
end
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
-- EVENT HANDLERS
|
-- EVENT HANDLERS (Using centralized event system)
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
|
|
||||||
local function GetClick()
|
local function GetClick()
|
||||||
mon.setTextScale(1)
|
mon.setTextScale(1)
|
||||||
local event, _, xPos, yPos = os.pullEvent("monitor_touch")
|
handlers.setupConnectionHandlers()
|
||||||
return xPos, yPos
|
local result = events.pullEvent("monitor_touch")
|
||||||
|
return result == "disconnect" and 0 or 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function GetActivation()
|
local function GetActivation()
|
||||||
_, _, incomingAddress = os.pullEvent("stargate_incoming_wormhole")
|
handlers.setupConnectionHandlers()
|
||||||
utils.log("Incoming wormhole: " .. gate.addressToString(incomingAddress))
|
local result = events.pullEvent("stargate_incoming_wormhole")
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function ParaDisconnect()
|
local function ParaDisconnect()
|
||||||
local dx, dy = 0, 0
|
-- Now handled by event system with priority
|
||||||
_, _, dx, dy = os.pullEvent("monitor_touch")
|
handlers.setupConnectionHandlers()
|
||||||
|
while true do
|
||||||
-- Ignore clicks during password entry
|
local result = events.pullEvent("monitor_touch")
|
||||||
if enteringPassword then
|
if result == "disconnect" then
|
||||||
return nil
|
|
||||||
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
|
return 1
|
||||||
end
|
end
|
||||||
-- Return nothing to keep waiting if clicked elsewhere
|
end
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function EntityRead()
|
local function EntityRead()
|
||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
_, _, incomingEntityType, incomingEntityName, _ = os.pullEvent("stargate_reconstructing_entity")
|
handlers.setupConnectionHandlers()
|
||||||
utils.log("Entity reconstructed: " .. incomingEntityName .. " (" .. incomingEntityType .. ")")
|
local result = events.pullEvent("stargate_reconstructing_entity")
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function DisconnectCheck()
|
local function DisconnectCheck()
|
||||||
local _, _, disCode = os.pullEvent("stargate_disconnected")
|
handlers.setupConnectionHandlers()
|
||||||
redstone.setOutput("top", false)
|
local result = events.pullEvent("stargate_disconnected")
|
||||||
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
|
|
||||||
|
|
||||||
if config.autoOpenIrisAfterDisconnect then
|
|
||||||
utils.openIris()
|
|
||||||
end
|
|
||||||
|
|
||||||
return 2
|
return 2
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -140,57 +126,13 @@ local function Paratimeout()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function GetMessage()
|
local function GetMessage()
|
||||||
local _, _, message = os.pullEvent("stargate_message_received")
|
handlers.setupConnectionHandlers()
|
||||||
lastReceivedMessage = message
|
local result = events.pullEvent("stargate_message_received")
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local function HandlePasswordEntry()
|
local function HandlePasswordEntry()
|
||||||
-- Display password prompt
|
return handlers.handlePasswordInput()
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function HandleIncomingPasswordRequest(password)
|
local function HandleIncomingPasswordRequest(password)
|
||||||
@ -210,6 +152,7 @@ end
|
|||||||
local function MonitorRemoteIris()
|
local function MonitorRemoteIris()
|
||||||
-- Continuously monitor remote iris state while connected
|
-- Continuously monitor remote iris state while connected
|
||||||
local lastIrisState = nil
|
local lastIrisState = nil
|
||||||
|
local state = getState()
|
||||||
|
|
||||||
while gate.isStargateConnected() do
|
while gate.isStargateConnected() do
|
||||||
sleep(0.5) -- Check every half second
|
sleep(0.5) -- Check every half second
|
||||||
@ -228,7 +171,7 @@ local function MonitorRemoteIris()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- If remote has computer and iris just became fully closed, offer password entry
|
-- 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")
|
utils.log("Remote iris closed but computer detected - showing password prompt")
|
||||||
|
|
||||||
local password = HandlePasswordEntry()
|
local password = HandlePasswordEntry()
|
||||||
@ -241,8 +184,8 @@ local function MonitorRemoteIris()
|
|||||||
|
|
||||||
local result = parallel.waitForAny(GetMessage, WaitForResponse)
|
local result = parallel.waitForAny(GetMessage, WaitForResponse)
|
||||||
if result == 1 then
|
if result == 1 then
|
||||||
local response = lastReceivedMessage
|
local response = state.lastReceivedMessage
|
||||||
lastReceivedMessage = nil
|
state.lastReceivedMessage = nil
|
||||||
if response == "IRIS_OPEN" then
|
if response == "IRIS_OPEN" then
|
||||||
display.showPasswordResult(true)
|
display.showPasswordResult(true)
|
||||||
utils.log("Password accepted - iris opened")
|
utils.log("Password accepted - iris opened")
|
||||||
@ -275,7 +218,7 @@ local function MonitorRemoteIris()
|
|||||||
display.drawIrisStatus()
|
display.drawIrisStatus()
|
||||||
display.drawDisconnectButton()
|
display.drawDisconnectButton()
|
||||||
else
|
else
|
||||||
display.showConnected(destAddressname, destAddress)
|
display.showConnected(state.destAddressname, state.destAddress)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Remote iris is open (0 or nil)
|
-- Remote iris is open (0 or nil)
|
||||||
@ -283,7 +226,7 @@ local function MonitorRemoteIris()
|
|||||||
-- Iris just opened
|
-- Iris just opened
|
||||||
utils.log("Remote iris opened - connection safe")
|
utils.log("Remote iris opened - connection safe")
|
||||||
end
|
end
|
||||||
display.showConnected(destAddressname, destAddress)
|
display.showConnected(state.destAddressname, state.destAddress)
|
||||||
end
|
end
|
||||||
|
|
||||||
lastIrisState = remoteIrisState
|
lastIrisState = remoteIrisState
|
||||||
@ -298,6 +241,8 @@ end
|
|||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
|
|
||||||
local function handleIncomingWormhole()
|
local function handleIncomingWormhole()
|
||||||
|
local state = getState()
|
||||||
|
|
||||||
mon.setBackgroundColor(colors.black)
|
mon.setBackgroundColor(colors.black)
|
||||||
mon.clear()
|
mon.clear()
|
||||||
mon.setBackgroundColor(colors.red)
|
mon.setBackgroundColor(colors.red)
|
||||||
@ -305,11 +250,11 @@ local function handleIncomingWormhole()
|
|||||||
mon.setCursorPos(9, 4)
|
mon.setCursorPos(9, 4)
|
||||||
mon.write("INCOMING")
|
mon.write("INCOMING")
|
||||||
|
|
||||||
incomingAddress = utils.addressToTable(incomingAddress)
|
state.incomingAddress = utils.addressToTable(state.incomingAddress)
|
||||||
|
|
||||||
-- Check security
|
-- Check security
|
||||||
local allowed, reason = utils.isAddressAllowed(incomingAddress)
|
local allowed, reason = utils.isAddressAllowed(state.incomingAddress)
|
||||||
local addressString = gate.addressToString(incomingAddress) or "Unknown"
|
local addressString = gate.addressToString(state.incomingAddress) or "Unknown"
|
||||||
|
|
||||||
utils.log("Incoming wormhole from: " .. addressString .. " " .. reason)
|
utils.log("Incoming wormhole from: " .. addressString .. " " .. reason)
|
||||||
|
|
||||||
@ -342,12 +287,12 @@ local function handleIncomingWormhole()
|
|||||||
-- Whichever happens first, that function returns and we handle it
|
-- Whichever happens first, that function returns and we handle it
|
||||||
local result = parallel.waitForAny(EntityRead, DisconnectCheck, GetMessage, ParaDisconnect)
|
local result = parallel.waitForAny(EntityRead, DisconnectCheck, GetMessage, ParaDisconnect)
|
||||||
if (result == 1) then
|
if (result == 1) then
|
||||||
display.showEntity(incomingEntityType, incomingEntityName, allowed)
|
display.showEntity(state.incomingEntityType, state.incomingEntityName, allowed)
|
||||||
incomingEntityType = ""
|
state.incomingEntityType = ""
|
||||||
incomingEntityName = ""
|
state.incomingEntityName = ""
|
||||||
elseif (result == 3) then
|
elseif (result == 3) then
|
||||||
-- Received a message
|
-- Received a message
|
||||||
local message = lastReceivedMessage
|
local message = state.lastReceivedMessage
|
||||||
utils.log("Received message: " .. message)
|
utils.log("Received message: " .. message)
|
||||||
|
|
||||||
-- Check if it's a password attempt
|
-- Check if it's a password attempt
|
||||||
@ -355,7 +300,7 @@ local function handleIncomingWormhole()
|
|||||||
local password = message:sub(15)
|
local password = message:sub(15)
|
||||||
HandleIncomingPasswordRequest(password)
|
HandleIncomingPasswordRequest(password)
|
||||||
end
|
end
|
||||||
lastReceivedMessage = nil
|
state.lastReceivedMessage = nil
|
||||||
else
|
else
|
||||||
disconnect = true
|
disconnect = true
|
||||||
end
|
end
|
||||||
@ -444,6 +389,7 @@ local function dialGate(address)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function selectGateFromList()
|
local function selectGateFromList()
|
||||||
|
local state = getState()
|
||||||
local selecting = true
|
local selecting = true
|
||||||
while dialing == false and selecting == true do
|
while dialing == false and selecting == true do
|
||||||
selx, sely = GetClick()
|
selx, sely = GetClick()
|
||||||
@ -452,8 +398,8 @@ local function selectGateFromList()
|
|||||||
for i = 1, #buttonXY do
|
for i = 1, #buttonXY do
|
||||||
if (sely == buttonXY[i][3]) and ((selx >= buttonXY[i][1]) and (selx <= buttonXY[i][2])) then
|
if (sely == buttonXY[i][3]) and ((selx >= buttonXY[i][1]) and (selx <= buttonXY[i][2])) then
|
||||||
dialGate(computerAddresses[i])
|
dialGate(computerAddresses[i])
|
||||||
destAddressname = computerNames[i]
|
state.destAddressname = computerNames[i]
|
||||||
destAddress = computerAddresses[i]
|
state.destAddress = computerAddresses[i]
|
||||||
dialing = true
|
dialing = true
|
||||||
sely = 0
|
sely = 0
|
||||||
selx = 0
|
selx = 0
|
||||||
@ -542,6 +488,7 @@ local function selectCategory()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function handleOutgoingDial()
|
local function handleOutgoingDial()
|
||||||
|
local state = getState()
|
||||||
totalstate = true
|
totalstate = true
|
||||||
local PDO = 0
|
local PDO = 0
|
||||||
PDO = parallel.waitForAny(selectCategory, Paratimeout)
|
PDO = parallel.waitForAny(selectCategory, Paratimeout)
|
||||||
@ -552,7 +499,7 @@ local function handleOutgoingDial()
|
|||||||
os.pullEvent("stargate_outgoing_wormhole")
|
os.pullEvent("stargate_outgoing_wormhole")
|
||||||
|
|
||||||
-- Wait briefly for version message from remote gate
|
-- Wait briefly for version message from remote gate
|
||||||
remoteHasComputer = false
|
state.remoteHasComputer = false
|
||||||
local function WaitForVersion()
|
local function WaitForVersion()
|
||||||
sleep(1) -- Wait up to 1 second for version message
|
sleep(1) -- Wait up to 1 second for version message
|
||||||
return -1
|
return -1
|
||||||
@ -560,10 +507,10 @@ local function handleOutgoingDial()
|
|||||||
|
|
||||||
local result = parallel.waitForAny(GetMessage, WaitForVersion)
|
local result = parallel.waitForAny(GetMessage, WaitForVersion)
|
||||||
if result == 1 then
|
if result == 1 then
|
||||||
local message = lastReceivedMessage
|
local message = state.lastReceivedMessage
|
||||||
lastReceivedMessage = nil
|
state.lastReceivedMessage = nil
|
||||||
if message:sub(1, 6) == "SGCS_V" then
|
if message:sub(1, 6) == "SGCS_V" then
|
||||||
remoteHasComputer = true
|
state.remoteHasComputer = true
|
||||||
local version = message:sub(7)
|
local version = message:sub(7)
|
||||||
utils.log("Remote gate has control system version " .. version)
|
utils.log("Remote gate has control system version " .. version)
|
||||||
end
|
end
|
||||||
@ -600,7 +547,7 @@ local function handleOutgoingDial()
|
|||||||
display.drawDisconnectButton()
|
display.drawDisconnectButton()
|
||||||
else
|
else
|
||||||
-- Remote iris is open or no iris present
|
-- Remote iris is open or no iris present
|
||||||
display.showConnected(destAddressname, destAddress)
|
display.showConnected(state.destAddressname, state.destAddress)
|
||||||
end
|
end
|
||||||
|
|
||||||
if (gate.isStargateConnected() == true) then
|
if (gate.isStargateConnected() == true) then
|
||||||
@ -617,9 +564,9 @@ local function handleOutgoingDial()
|
|||||||
end
|
end
|
||||||
|
|
||||||
display.clearButtonData()
|
display.clearButtonData()
|
||||||
destAddress = {}
|
state.destAddress = {}
|
||||||
destAddressname = ""
|
state.destAddressname = ""
|
||||||
remoteHasComputer = false
|
state.remoteHasComputer = false
|
||||||
end
|
end
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user