190 lines
5.8 KiB
Lua
190 lines
5.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,
|
|
remotePasswordRequired = 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
|
|
|
|
-- Wait for password submission - handle events directly without the event system
|
|
while state.enteringPassword do
|
|
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
|
|
state.enteringPassword = false
|
|
end
|
|
end
|
|
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
|
|
|
|
-- Only process if stargate is actually connected
|
|
if not gate.isStargateConnected() 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) .. ")")
|
|
|
|
-- Set iris to default state
|
|
if config.irisClosedByDefault then
|
|
utils.closeIris()
|
|
else
|
|
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
|