Files
StargateControl/handlers.lua

213 lines
6.8 KiB
Lua

--[[
Event Handlers
All event handling functions for the stargate control system
]]
local handlers = {}
-- Module references (set by init)
local config, gate, mon, utils, display, events
-- State variables
local state = {
incomingAddress = {},
incomingEntityType = "",
incomingEntityName = "",
lastReceivedMessage = nil,
enteringPassword = false,
remoteHasComputer = false,
destAddress = {},
destAddressname = ""
}
function handlers.init(cfg, gateInterface, monitor, utilsModule, displayModule, eventsModule)
config = cfg
gate = gateInterface
mon = monitor
utils = utilsModule
display = displayModule
events = eventsModule
end
function handlers.getState()
return state
end
---------------------------------------------
-- CLICK HANDLER (Priority-based)
---------------------------------------------
function handlers.handleDefaultClick(eventType, side, x, y)
-- Default handler that returns click coordinates
-- This has the lowest priority, so other handlers can intercept first
return { x = x, y = y }
end
---------------------------------------------
-- PASSWORD INPUT HANDLER (Highest Priority)
---------------------------------------------
function handlers.handlePasswordInput()
display.showPasswordPrompt()
local password = ""
state.enteringPassword = true
-- Store old handlers to restore later
local oldHandlers = eventHandlers["monitor_touch"] or {}
local oldPriorities = handlerPriorities["monitor_touch"] or {}
-- Register high-priority click handler for password input
local function passwordClickHandler(eventType, side, x, y)
if not state.enteringPassword then
return nil -- No longer active, let other handlers process
end
-- 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)
return true -- Event consumed
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)
return true
elseif x >= 13 and x <= 16 then
-- Zero button
password = password .. "0"
display.updatePasswordDisplay(password)
return true
elseif x >= 18 and x <= 21 then
-- OK button - submit password
state.enteringPassword = false
return "submit"
end
end
return true -- Consume all clicks during password entry
end
-- Clear monitor_touch handlers and register only password handler
eventHandlers["monitor_touch"] = { passwordClickHandler }
handlerPriorities["monitor_touch"] = { events.PRIORITY.PASSWORD_INPUT }
-- Wait for password submission
while state.enteringPassword do
local result = events.pullEvent("monitor_touch")
if result == "submit" then
break
end
-- Don't process the result here - the handler already did it
end
-- Restore old handlers
eventHandlers["monitor_touch"] = oldHandlers
handlerPriorities["monitor_touch"] = oldPriorities
-- If no handlers to restore, re-register connection handlers
if #oldHandlers == 0 then
handlers.setupConnectionHandlers()
end -- Send password attempt
utils.sendPasswordAttempt(password)
return password
end
---------------------------------------------
-- DISCONNECT BUTTON HANDLER
---------------------------------------------
function handlers.handleDisconnectButton(eventType, side, x, y)
-- Only process if not entering password (password handler has higher priority)
if state.enteringPassword then
return nil
end
-- Check if clicking disconnect button (bottom-right corner)
if y >= 17 and y <= 19 and x >= 20 and x <= 28 then
gate.disconnectStargate()
redstone.setOutput("top", false)
utils.log("Manual disconnect triggered")
return "disconnect"
end
return nil -- Not a disconnect button click
end
---------------------------------------------
-- ACTIVATION HANDLER
---------------------------------------------
function handlers.handleActivation(eventType, side, address)
state.incomingAddress = address
utils.log("Incoming wormhole: " .. gate.addressToString(address))
return "activation"
end
---------------------------------------------
-- ENTITY READ HANDLER
---------------------------------------------
function handlers.handleEntityRead(eventType, side, entityType, entityName, ...)
sleep(0.1)
state.incomingEntityType = entityType
state.incomingEntityName = entityName
utils.log("Entity reconstructed: " .. entityName .. " (" .. entityType .. ")")
return "entity"
end
---------------------------------------------
-- DISCONNECT CHECK HANDLER
---------------------------------------------
function handlers.handleDisconnect(eventType, side, disCode)
redstone.setOutput("top", false)
utils.log("Stargate disconnected (code: " .. tostring(disCode) .. ")")
if config.autoOpenIrisAfterDisconnect then
utils.openIris()
end
return "disconnected"
end
---------------------------------------------
-- MESSAGE HANDLER
---------------------------------------------
function handlers.handleMessage(eventType, side, message)
state.lastReceivedMessage = message
return "message"
end
---------------------------------------------
-- SETUP EVENT HANDLERS
---------------------------------------------
function handlers.setupConnectionHandlers()
-- Clear any existing handlers
events.clearHandlers()
-- Register all handlers with their priorities
events.registerHandler("monitor_touch", handlers.handleDisconnectButton, events.PRIORITY.DISCONNECT_BUTTON)
events.registerHandler("monitor_touch", handlers.handleDefaultClick, events.PRIORITY.DEFAULT) -- Lowest priority - returns coordinates
events.registerHandler("stargate_incoming_wormhole", handlers.handleActivation, events.PRIORITY.ACTIVATION)
events.registerHandler("stargate_reconstructing_entity", handlers.handleEntityRead, events.PRIORITY.ENTITY_READ)
events.registerHandler("stargate_disconnected", handlers.handleDisconnect, events.PRIORITY.DISCONNECT_CHECK)
events.registerHandler("stargate_message_received", handlers.handleMessage, events.PRIORITY.MESSAGE)
end
return handlers