Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local USERS_FOLDER = "/disk/users/"
- local WARNING_TIME = 5 -- Time in seconds to display the warning message
- -- Function to draw a centered popup window with a warning message
- local function drawWarningWindow(message)
- term.clear() -- Clear the screen before drawing the warning window
- local w, h = term.getSize()
- local maxLength = #message
- local windowWidth = maxLength + 4 -- Width of the warning window
- local windowHeight = 5 -- Height of the warning window (fixed for simplicity)
- local xStart = math.floor(w / 2 - windowWidth / 2)
- local yStart = math.floor(h / 2 - windowHeight / 2)
- -- Draw border
- term.setCursorPos(xStart, yStart)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- term.setCursorPos(xStart, yStart + 1)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- term.setCursorPos(xStart, yStart + 2)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- term.setCursorPos(xStart, yStart + 3)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- term.setCursorPos(xStart, yStart + 4)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- -- Draw message
- term.setCursorPos(xStart + 2, yStart + 2)
- term.write(message)
- end
- -- Function to monitor for disk events
- local function monitorDisks()
- local lastDiskIDs = {} -- Store the last known disk IDs
- -- Function to get current disk IDs
- local function getCurrentDiskIDs()
- local peripherals = peripheral.getNames()
- local diskIDs = {}
- for _, name in ipairs(peripherals) do
- if peripheral.getType(name) == "drive" then
- local diskID = disk.getID(name)
- if diskID then
- table.insert(diskIDs, diskID)
- end
- end
- end
- return diskIDs
- end
- -- Initialize with the current disk IDs
- lastDiskIDs = getCurrentDiskIDs()
- while true do
- os.sleep(1) -- Check every second
- local currentDiskIDs = getCurrentDiskIDs()
- local diskRemoved = false
- -- Check for disk removal
- for _, lastID in ipairs(lastDiskIDs) do
- local found = false
- for _, currentID in ipairs(currentDiskIDs) do
- if lastID == currentID then
- found = true
- break
- end
- end
- if not found then
- diskRemoved = true
- break
- end
- end
- if diskRemoved then
- drawWarningWindow("Warning: Disk has been removed!")
- os.sleep(WARNING_TIME) -- Display the warning message for the specified time
- -- Force logoff by running the logoff script
- shell.run("/disk/ACPI/logoff")
- break -- Exit the monitoring loop after logging off
- end
- -- Update the last known disk IDs
- lastDiskIDs = currentDiskIDs
- end
- end
- -- Function to start the monitoring script in a new shell and switch to it
- local function setupMultishell()
- -- Launch the monitoring script in a new shell
- local shellID = multishell.launch("CardSystem_Service", "card_service.lua")
- -- Switch to the new shell
- multishell.setActive(shellID)
- end
- -- Run the multishell setup and monitoring function
- setupMultishell()
- parallel.waitForAny(monitorDisks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement