Advertisement
Mojokojo69

elevator lobby client

Aug 27th, 2023 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. local logo = {
  2. " ___ ",
  3. " / _ \\ ",
  4. " / , _/",
  5. "/_/|_/"
  6. }
  7.  
  8. -- Configuration
  9. local configFileName = "lobby_config.txt"
  10. local computerID = os.getComputerID()
  11. local config = {channel = "", currentFloor = "", computerID = ""}
  12.  
  13.  
  14. local modem = peripheral.find("modem")
  15. local monitor = peripheral.wrap("top")
  16. monitor.setTextScale(1)
  17.  
  18. local elevatorFloor = 0
  19. local lastButtonPress = 0
  20. local cooldown = 2 -- 2-second cooldown
  21. local idleSent = false
  22.  
  23. -- Save Config to file
  24. local function saveConfig(config)
  25. local configData = textutils.serializeJSON(config)
  26. local file = fs.open(configFileName, "w")
  27. file.write(configData)
  28. file.close()
  29. end
  30.  
  31. -- Load Config from file
  32. local function loadConfig()
  33. if fs.exists(configFileName) then
  34. local file = fs.open(configFileName, "r")
  35. local configData = file.readAll()
  36. file.close()
  37. local loadedConfig = textutils.unserializeJSON(configData)
  38. config = loadedConfig
  39. end
  40. end
  41.  
  42. local function refreshMonitor()
  43. monitor.clear()
  44. monitor.setCursorPos(1, 1)
  45. end
  46.  
  47. local function centerText(monitor, text)
  48. local width, height = monitor.getSize()
  49. local x = math.ceil((width - #text) / 2)
  50. local y = math.ceil(height / 2)
  51. monitor.setCursorPos(x, y)
  52. monitor.write(text)
  53. end
  54.  
  55. local function displayLogo(monitor, logo, color)
  56. refreshMonitor()
  57. monitor.setTextColour(color)
  58. local _, height = monitor.getSize()
  59. local yStart = math.ceil(height / 2) - math.ceil(#logo / 2)
  60.  
  61. for y, line in ipairs(logo) do
  62. monitor.setCursorPos(1, yStart + y - 1)
  63. monitor.write(line)
  64. end
  65. end
  66.  
  67. local function chosenDirection(floor, targetFloor)
  68. if floor < targetFloor then
  69. return "down"
  70. elseif floor > targetFloor then
  71. return "up"
  72. end
  73. end
  74.  
  75. local function handleTouch(x, y)
  76. local currentTime = os.epoch("utc")
  77. if currentTime - lastButtonPress >= cooldown * 1000 then
  78. local message = chosenDirection(tonumber(config.currentFloor), elevatorFloor)
  79. modem.transmit(tonumber(config.channel), tonumber(config.channel) + 1, message)
  80. lastButtonPress = currentTime
  81. else
  82. print("Button press ignored due to cooldown.")
  83. end
  84. end
  85.  
  86. local function handleModemMessages(message)
  87. if string.sub(message, 1, 5) == "idle:" then
  88. local newFloor = tonumber(string.sub(message, 6))
  89. print("Elevator floor: " .. newFloor)
  90. elevatorFloor = newFloor
  91. if elevatorFloor == tonumber(config.currentFloor) then
  92. print("Elevator is on this floor.")
  93. displayLogo(monitor, logo, colours.green)
  94. else
  95. print("Elevator is on " .. elevatorFloor)
  96. displayLogo(monitor, logo, colours.purple)
  97. end
  98. end
  99. end
  100.  
  101.  
  102. local function mainLoop(config)
  103. modem.open(tonumber(config.channel))
  104. while true do
  105. local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  106. if event == "modem_message" then
  107. if senderChannel == tonumber(config.channel) then
  108. handleModemMessages(message)
  109. end
  110. end
  111.  
  112. if event == "monitor_touch" then
  113. handleTouch(x, y)
  114. end
  115. end
  116. end
  117.  
  118. -- Initialization
  119. local function initializeClient()
  120. loadConfig()
  121. if not config.channel or config.channel == "" then
  122. print("Please enter the channel to use:")
  123. config.channel = tonumber(read())
  124. end
  125. if not config.currentFloor or config.currentFloor == "" then
  126. print("Please enter the current floor:")
  127. config.currentFloor = tonumber(read())
  128. end
  129. if not config.computerID then
  130. config.computerID = computerID
  131. end
  132. saveConfig(config)
  133. mainLoop(config)
  134. end
  135.  
  136. -- Entry Point
  137. initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement