Sedrowow

room client

Jul 28th, 2026 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.50 KB | None | 0 0
  1. -- Room Client
  2. local RS_SIDE = "back"
  3. local PROTOCOL = "room_mgmt"
  4.  
  5. peripheral.find("modem", rednet.open)
  6. local mon = peripheral.find("monitor")
  7.  
  8. -- Auto-generate info.txt
  9. if not fs.exists("info.txt") then
  10.     local def = {
  11.         id = "Room_" .. os.getComputerID(),
  12.         mode = "both",
  13.         rent_price = 5,
  14.         sale_price = 100,
  15.         rent_frequency = 1800000, -- 30 mins in ms
  16.         grace_period = 900000 -- 15 mins in ms
  17.     }
  18.     local f = fs.open("info.txt", "w")
  19.     f.write(textutils.serializeJSON(def))
  20.     f.close()
  21. end
  22.  
  23. local file = fs.open("info.txt", "r")
  24. local info = textutils.unserializeJSON(file.readAll())
  25. file.close()
  26.  
  27. local currentInput = ""
  28. local roomState = { status = "Connecting", owner = nil, overdue = false, isOpen = false }
  29.  
  30. local function drawMonitor()
  31.     if not mon then return end
  32.     mon.setTextScale(1)
  33.     mon.clear()
  34.     mon.setCursorPos(1,1)
  35.     mon.write("Room: " .. info.id)
  36.     mon.setCursorPos(1,2)
  37.    
  38.     local statStr = roomState.status
  39.     if roomState.overdue then statStr = statStr .. " (OVERDUE)" end
  40.     mon.write("Status: " .. statStr)
  41.    
  42.     if roomState.owner then
  43.         mon.setCursorPos(1,3)
  44.         mon.write("Owner: " .. roomState.owner)
  45.         mon.setCursorPos(1,4)
  46.         mon.write("Door: " .. (roomState.isOpen and "UNLOCKED" or "LOCKED"))
  47.        
  48.         -- Draw compact Keypad
  49.         local pad = {"1","2","3","4","5","6","7","8","9","C","0","E"}
  50.         for i=0, 3 do
  51.             mon.setCursorPos(1, 6 + i)
  52.             mon.write(" " .. pad[i*3 + 1] .. "  " .. pad[i*3 + 2] .. "  " .. pad[i*3 + 3])
  53.         end
  54.         mon.setCursorPos(1, 10)
  55.         mon.write("PIN: " .. string.rep("*", #currentInput))
  56.     end
  57. end
  58.  
  59. local function heartbeatLoop()
  60.     while true do
  61.         local serverId = rednet.lookup(PROTOCOL, "server")
  62.         if serverId then
  63.             rednet.send(serverId, { type = "register", info = info }, PROTOCOL)
  64.         end
  65.         sleep(15)
  66.     end
  67. end
  68.  
  69. local function listenLoop()
  70.     while true do
  71.         local sender, msg = rednet.receive(PROTOCOL)
  72.         if type(msg) == "table" and msg.target == info.id and msg.command == "update" then
  73.             roomState.status = msg.status
  74.             roomState.owner = msg.owner
  75.             roomState.isOpen = msg.isOpen
  76.             roomState.overdue = msg.overdue
  77.            
  78.             rs.setOutput(RS_SIDE, roomState.isOpen)
  79.             drawMonitor()
  80.         end
  81.     end
  82. end
  83.  
  84. local function keypadLoop()
  85.     local padMap = {"1","2","3","4","5","6","7","8","9","C","0","E"}
  86.     while true do
  87.         local e, side, x, y = os.pullEvent("monitor_touch")
  88.         if roomState.owner and y >= 6 and y <= 9 and x >= 1 and x <= 9 then
  89.             local col = math.ceil(x / 3)
  90.             local row = y - 5
  91.             local key = padMap[(row-1)*3 + col]
  92.            
  93.             if key == "C" then
  94.                 currentInput = ""
  95.             elseif key == "E" then
  96.                 local serverId = rednet.lookup(PROTOCOL, "server")
  97.                 if serverId and #currentInput > 0 then
  98.                     rednet.send(serverId, { type = "toggle_door", id = info.id, password = currentInput }, PROTOCOL)
  99.                 end
  100.                 currentInput = ""
  101.             else
  102.                 if #currentInput < 8 then currentInput = currentInput .. key end
  103.             end
  104.             drawMonitor()
  105.         end
  106.     end
  107. end
  108.  
  109. print("Room Client " .. info.id .. " running.")
  110. parallel.waitForAny(heartbeatLoop, listenLoop, keypadLoop)
Advertisement
Add Comment
Please, Sign In to add comment