From 6757e1eb106e4afa166b9fec3c3ea7e35ad8e6c5 Mon Sep 17 00:00:00 2001 From: brulijam Date: Sat, 8 Jun 2024 20:35:45 +0200 Subject: [PATCH] . --- src/brulijam/lib/networking.lua | 4 +- src/brulijam/systemlock.lua | 103 +++++++++++++++++++++++++++-- src/brulijam/turtle-miner-fibo.lua | 33 ++++----- src/startup.lua | 3 +- 4 files changed, 117 insertions(+), 26 deletions(-) diff --git a/src/brulijam/lib/networking.lua b/src/brulijam/lib/networking.lua index 03ab1b4..cd7c005 100755 --- a/src/brulijam/lib/networking.lua +++ b/src/brulijam/lib/networking.lua @@ -48,10 +48,10 @@ function sendLog(to, targetPath, logMsg) local payloadTable = {} payloadTable[0] = targetPath - payloadTable[1] = basic.getCurrentTime() .. ": " .. logMsg + payloadTable[1] = "[" .. basic.getCurrentTime() .. "]: " .. logMsg sendMessage(to, "log", payloadTable) - basic.ntfy(os.getComputerLabel() .. ": " .. payloadTable[1]) + basic.ntfy("[" .. os.getComputerLabel() .. "] " .. payloadTable[1]) end function sendFile(to, originPath, targetPath) diff --git a/src/brulijam/systemlock.lua b/src/brulijam/systemlock.lua index a64275d..7584be5 100644 --- a/src/brulijam/systemlock.lua +++ b/src/brulijam/systemlock.lua @@ -2,21 +2,112 @@ basic = require("lib.basic") hash = require("lib.hash") local pwHash = "" +local config = {} -local function setState() - basic.writeToFile("/brulijam/config/systemlock.config", asdasd) +local function setConfig() + basic.writeToFile("/brulijam/config/systemlock.config", textutils.serialize(config)) end -local function getState() +local function getConfig() if not fs.exists "/brulijam/config/systemlock.config" then - config = {} config["locked"] = true config["pwHash"] = createPwHash() + config["keepUnlockedFor"] = 600 + setConfig() + else + config = basic.readFileAll("brulijam/config/systemlock.config") + config = textutils.unserialize(config) + end + return config end local function createPwHash() - print("Create a new password: ") + write("Create a new password : ") local input = read("*") pwHash = hash.digestStr(input .. os.getComputerID()) return pwHash -end \ No newline at end of file +end + +local function inputTimeout() + sleep(10) + eventTimeout = true +end + +local function inputPrompt() + write("Enter Password: ") + inputHash = hash.digestStr(read("*") .. os.getComputerID()) +end + + +local function unlockDevice() + config = getConfig() + pwHash = config["pwHash"] + inputHash = "" + eventTimeout = false + + while true do + parallel.waitForAny(inputTimeout, inputPrompt) + local pwCorrect = pwHash == inputHash + + if pwCorrect then --correct input + config["locked"] = false + setConfig() + print("Unlocking.") + sleep(0.2) + elseif eventTimeout then --timeout + config["locked"] = true + setConfig() + print("Timeout...") + sleep(0.5) + os.reboot() + else --wrong input + config["locked"] = true + setConfig() + print("Wrong password...") + sleep(1) + print("Rebooting...") + sleep(1) + os.reboot() + end + end +end + +local function shellRun() + shell.run("shell") +end + +local function shellStopCheck() + parallel.waitForAny(shellStopTimer) +end + +local function shellStopTimer() + while true do + if timer < 1 then + os.reboot() + end + timer = timer - 1 + sleep(1) + end +end + +local function shellStopKeyEvent() + eventName = nil + while true do + eventName, _ = os.pullEvent("key") + if eventName == "key" then + timer = config["keepUnlockedFor"] + end + end +end + +local function main() + config = getConfig() + local timer = config["keepUnlockedFor"] + + unlockDevice() + + parallel.waitForAny(shellRun, shellStopCheck) + os.reboot() +end + +main() \ No newline at end of file diff --git a/src/brulijam/turtle-miner-fibo.lua b/src/brulijam/turtle-miner-fibo.lua index 078158b..652ca6a 100644 --- a/src/brulijam/turtle-miner-fibo.lua +++ b/src/brulijam/turtle-miner-fibo.lua @@ -26,16 +26,17 @@ oreList = { keepList = { [0] = "computercraft:wireless_modem_advanced", [1] = "minecraft:chest", - [2] = "minecraft:raw_gold", - [3] = "minecraft:raw_iron", - [4] = "minecraft:coal", - [5] = "minecraft:lapis_lazuli", - [6] = "minecraft:diamond", - [7] = "minecraft:redstone", - [8] = "minecraft:emerald", - [9] = "minecraft:quartz", - [10] = "minecraft:gold_nugget", - [11] = "minecraft:ancient_debris", + [2] = "minecraft:barrel", + [3] = "minecraft:raw_gold", + [4] = "minecraft:raw_iron", + [5] = "minecraft:coal", + [6] = "minecraft:lapis_lazuli", + [7] = "minecraft:diamond", + [8] = "minecraft:redstone", + [9] = "minecraft:emerald", + [10] = "minecraft:quartz", + [11] = "minecraft:gold_nugget", + [12] = "minecraft:ancient_debris", } @@ -76,11 +77,11 @@ local function savePos(prefix) turtle.equipRight() posZ, posY, posX = gps.locate() sleep(1) - if prefix == "Chest placed" then + if prefix == "Chest placed at" then posY = posY + 1 end - basic.appendToFileNl("/brulijam/files/chests-turtle-miner-fibo.txt", "[" .. prefix .. "] " .. posX .. ", " .. posY .. ", " .. posZ) - networking.sendLog("all", "/brulijam/logs/chests-to-collect.txt", "[" .. prefix .. "] " .. posX .. ", " .. posY .. ", " .. posZ) + basic.appendToFileNl("/brulijam/files/chests-turtle-miner-fibo.txt", prefix .. posX .. ", " .. posY .. ", " .. posZ) + networking.sendLog("all", "/brulijam/logs/chests-to-collect.txt", prefix .. posX .. ", " .. posY .. ", " .. posZ) turtle.equipRight() end end @@ -99,7 +100,7 @@ local function inventoryRework() if itemDetail.name == "minecraft:coal" then turtle.refuel() spaceAvailable = true - elseif itemDetail.name == "minecraft:chest" then + elseif itemDetail.name == "minecraft:chest" or itemDetail.name == "minecraft:barrel" then chestAvailable = true chestSlot = i else --check if item can be thrown away @@ -127,13 +128,13 @@ local function inventoryRework() basic.secureDigUp() turtle.select(chestSlot) turtle.placeUp() - savePos("Chest placed") + savePos("Chest placed at") for k=1, 16 do turtle.select(k) itemDetail = turtle.getItemDetail() if itemDetail ~= nil then - if itemDetail.name ~= "minecraft:chest" and itemDetail.name ~= "computercraft:wireless_modem_advanced" then + if (itemDetail.name ~= "minecraft:chest" or itemDetail.name ~= "minecraft:barrel") and itemDetail.name ~= "computercraft:wireless_modem_advanced" then turtle.dropUp() end end diff --git a/src/startup.lua b/src/startup.lua index 768c78f..8fdff83 100644 --- a/src/startup.lua +++ b/src/startup.lua @@ -3,8 +3,6 @@ local repo = "https://git.brulijam.com/brulijam/Computercraft/raw/branch/main/sr local labelPrefix = "SPHEAL" - - --fetch sync script getRequest = http.get(repo .. "/brulijam/sync.lua") file = fs.open("/brulijam/sync.lua", "w") @@ -33,6 +31,7 @@ if os.getComputerLabel() == nil then os.setComputerLabel(labelPrefix .. "-" .. labelMainPart) end + --shell.run("/brulijam/systemlock.lua")