Advertisement
Mojokojo69

elevator client

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