keypadOS/src/ui.lua
2024-08-18 12:12:48 -04:00

136 lines
3.2 KiB
Lua

local utils = require("utils.lua")
local basalt = require("basalt")
local config = require("config.lua")
local updater = require("updater.lua")
--- @type Monitor
local monitor = utils.Cast(peripheral.find("monitor"))
--- @type drive
local drive = utils.Cast(peripheral.find("drive"))
local mod = {}
--- @class Ui
--- @field pin string
--- @field main any
--- @field pinLabel any
--- @field enterButton any
--- @field resetEverything function
--- @field unlockDoor function
--- @field addToPin function
--- @param ui Ui
local function resetEverything(ui)
sleep(2)
ui.pin = ""
ui.pinLabel:setText("")
redstone.setOutput("front", false)
ui.enterButton:setBackground(colors.blue)
end
--- @param ui Ui
local function unlockDoor(ui)
if drive.isDiskPresent() then
if drive.getDiskLabel() == config.correctPin then
ui.pin = config.correctPin
drive:ejectDisk()
end
end
basalt.debug("test")
if ui.pin == config.correctPin then
ui.enterButton:setBackground(colors.green)
ui.pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(config.correctPin)
ui.pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
ui.pinLabel:setText("Wrong")
ui.enterButton:setBackground(colors.red)
end
ui.main:addThread():start(function()
resetEverything(ui)
end)
end
--- @param ui Ui
local function addToPin(ui, i)
if #ui.pin >= 5 then
return
end
ui.pin = ui.pin .. tostring(i)
ui.pinLabel:setText(ui.pin)
end
function mod.InitUi()
local ui = {
pin = "",
main = basalt.addMonitor(),
}
ui.main:setMonitor(monitor)
ui.pinLabel = ui.main:addLabel()
:setText("")
:setFontSize(1)
ui.enterButton = ui.main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,2)
:setSize(1.5,3.2)
:onClick(function()
unlockDoor(ui)
end)
local btnX = 1
local btnY = 2
ui.main:addButton()
:setPosition(1, 5)
:setText("0")
:setSize(6,1)
:onClick(function()
addToPin(ui, 0)
end)
for i = 1, 9 do
ui.main:addButton()
:setPosition(btnX, btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(function()
addToPin(ui, i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
local status, err = pcall(function ()
parallel.waitForAll(basalt.autoUpdate, updater.UpdateChecker)
end)
if not status and err ~= "Terminated" then
print("Error detected: " .. err)
http.post(config.ntfy_url, err, {Priority = "urgent"}) --exposed ntfy url no spam me pls
sleep(5)
utils.MonReset(0.5)
fs.delete("basalt.lua")
fs.delete("startup.lua")
fs.copy("backup.lua", "startup.lua")
os.reboot()
end
end
return mod