Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local logo = {
- " ___ ",
- " / _ \\ ",
- " / , _/",
- "/_/|_/"
- }
- -- Configuration
- local configFileName = "lobby_config.txt"
- local computerID = os.getComputerID()
- local config = {channel = "", currentFloor = "", computerID = ""}
- local modem = peripheral.find("modem")
- local monitor = peripheral.wrap("top")
- monitor.setTextScale(1)
- local elevatorFloor = 0
- local lastButtonPress = 0
- local cooldown = 2 -- 2-second cooldown
- local idleSent = false
- -- Save Config to file
- local function saveConfig(config)
- local configData = textutils.serializeJSON(config)
- local file = fs.open(configFileName, "w")
- file.write(configData)
- file.close()
- end
- -- Load Config from file
- local function loadConfig()
- if fs.exists(configFileName) then
- local file = fs.open(configFileName, "r")
- local configData = file.readAll()
- file.close()
- local loadedConfig = textutils.unserializeJSON(configData)
- config = loadedConfig
- end
- end
- local function refreshMonitor()
- monitor.clear()
- monitor.setCursorPos(1, 1)
- end
- local function centerText(monitor, text)
- local width, height = monitor.getSize()
- local x = math.ceil((width - #text) / 2)
- local y = math.ceil(height / 2)
- monitor.setCursorPos(x, y)
- monitor.write(text)
- end
- local function displayLogo(monitor, logo, color)
- refreshMonitor()
- monitor.setTextColour(color)
- local _, height = monitor.getSize()
- local yStart = math.ceil(height / 2) - math.ceil(#logo / 2)
- for y, line in ipairs(logo) do
- monitor.setCursorPos(1, yStart + y - 1)
- monitor.write(line)
- end
- end
- local function chosenDirection(floor, targetFloor)
- if floor < targetFloor then
- return "down"
- elseif floor > targetFloor then
- return "up"
- end
- end
- local function handleTouch(x, y)
- local currentTime = os.epoch("utc")
- if currentTime - lastButtonPress >= cooldown * 1000 then
- local message = chosenDirection(tonumber(config.currentFloor), elevatorFloor)
- modem.transmit(tonumber(config.channel), tonumber(config.channel) + 1, message)
- lastButtonPress = currentTime
- else
- print("Button press ignored due to cooldown.")
- end
- end
- local function handleModemMessages(message)
- if string.sub(message, 1, 5) == "idle:" then
- local newFloor = tonumber(string.sub(message, 6))
- print("Elevator floor: " .. newFloor)
- elevatorFloor = newFloor
- if elevatorFloor == tonumber(config.currentFloor) then
- print("Elevator is on this floor.")
- displayLogo(monitor, logo, colours.green)
- else
- print("Elevator is on " .. elevatorFloor)
- displayLogo(monitor, logo, colours.purple)
- end
- end
- end
- local function mainLoop(config)
- modem.open(tonumber(config.channel))
- while true do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
- if event == "modem_message" then
- if senderChannel == tonumber(config.channel) then
- handleModemMessages(message)
- end
- end
- if event == "monitor_touch" then
- handleTouch(x, y)
- end
- end
- end
- -- Initialization
- local function initializeClient()
- loadConfig()
- if not config.channel or config.channel == "" then
- print("Please enter the channel to use:")
- config.channel = tonumber(read())
- end
- if not config.currentFloor or config.currentFloor == "" then
- print("Please enter the current floor:")
- config.currentFloor = tonumber(read())
- end
- if not config.computerID then
- config.computerID = computerID
- end
- saveConfig(config)
- mainLoop(config)
- end
- -- Entry Point
- initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement