rtmc/keypadOS.lua
2024-08-18 02:08:11 +03:00

182 lines
4.0 KiB
Lua

-- FILE: src/boot.lua
-- keycardOS "bootloader", has no access to basalt
-- intended for checking for updates, and automatically updating basalt if it is missing
LAST_USED = os.time()
local function checkForUpdate()
current_time = os.time()
difference = math.abs(current_time - LAST_USED)
print("Checking for update... difference: " .. tostring(difference))
--its been considerable time since the keypad was interacted with
--therefore it's time to force an update down the users throat (microsoft moment)
-- if difference > 4 then
if difference > .5 then
--logic for updating
end
end
function updateChecker()
while true do
checkForUpdate()
sleep(1)
end
end
local function getBasalt()
if fs.exists("basalt.lua") then
mPrint("Basalt found!")
else
mPrint("Download basalt...")
basalt_code = http.get("https://raw.githubusercontent.com/Pyroxenium/Basalt/master/docs/versions/latest.lua").readAll()
mPrint("Install basalt...")
local file = fs.open("basalt.lua", "w")
file.write(basalt_code)
file.close()
mPrint("Rebooting...")
os.reboot()
end
end
KEYPADOS_VERSION = "3.0"
MONITOR = peripheral.find("monitor")
local function boot()
mon_reset(0.5);
mPrint("keypadOS v" .. KEYPADOS_VERSION)
mon_reset(1);
end
boot()
local files = {}
local globalRequire = require -- Store default require reference
local require = function(path) -- Will return saved file or attempt default lua require
return files[path] or globalRequire(path)
end
files["boot.lua"] = function(...)
local function boot()
mon_reset(0.5);
mPrint("keypadOS v" .. KEYPADOS_VERSION)
mon_reset(1);
end
return {
boot
}
end
-- FILE: src/init.lua
local basalt = require("basalt")
local monitor = peripheral.find("monitor")
local drive = peripheral.find("drive")
local main = basalt.addMonitor()
main:setMonitor(monitor)
btnX = 1
btnY = 3
pin = ""
-- FILE: src/config.lua
correctPin = "42169"
-- FILE: src/unlock_door.lua
function unlockDoor()
if drive.isDiskPresent() then
if drive.getDiskLabel() == correctPin then
pin = correctPin
drive:ejectDisk()
end
end
basalt.debug("test")
if pin == correctPin then
enterButton:setBackground(colors.green)
pinLabel:setText("Welcome")
redstone.setOutput("front", true)
if drive.isDiskPresent() then
if drive.getDiskLabel() == nil then
drive.setDiskLabel(correctPin)
pinLabel:setText("Crd set")
drive:ejectDisk()
end
end
else
pinLabel:setText("Wrong")
enterButton:setBackground(colors.red)
end
local thread = main:addThread()
:start(resetEverything)
end
function resetEverything()
sleep(2)
pin = ""
pinLabel:setText("")
redstone.setOutput("front", false)
enterButton:setBackground(colors.blue)
end
function addToPin(i)
if #pin >= 5 then
return
end
pin = pin .. tostring(i)
pinLabel:setText(pin)
end
-- FILE: src/utils.lua
MONITOR_Y = 1
function mPrint(text)
MONITOR.setCursorPos(1,y)
MONITOR.write(text)
MONITOR_Y = MONITOR_Y + 1
end
function mon_reset(scale)
MONITOR.clear()
MONITOR.setTextScale(scale)
end
-- FILE: src/main.lua
pinLabel = main:addLabel()
:setText("")
:setFontSize(1)
enterButton = main:addButton()
:setText(">>>>")
:setBackground(colors.blue)
:setPosition(6,3)
:setSize(1.5,3.2)
:onClick(unlockDoor)
for i = 1, 9 do
local button = main:addButton()
:setPosition(btnX,btnY)
:setText(tostring(i))
:setSize(2,1)
:onClick(
function()
addToPin(i)
end)
btnX = btnX + 2
if btnX >= 6 then
btnY = btnY + 1
btnX = 1
end
end
parallel.waitForAll(basalt.autoUpdate, updateChecker)