Sedrowow

user client (pocket computer)

Jul 28th, 2026 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.53 KB | None | 0 0
  1. -- Pocket Client
  2. local PROTOCOL = "room_mgmt"
  3. peripheral.find("modem", rednet.open)
  4.  
  5. -- Setup Username
  6. settings.load()
  7. local USER_ID = settings.get("room_username")
  8. if not USER_ID then
  9.     term.clear()
  10.     term.setCursorPos(1,1)
  11.     print("Welcome to Room Network!")
  12.     write("Enter your User Name:\n> ")
  13.     USER_ID = read()
  14.     settings.set("room_username", USER_ID)
  15.     settings.save()
  16. end
  17.  
  18. local function getServer()
  19.     local id = rednet.lookup(PROTOCOL, "server")
  20.     return id
  21. end
  22.  
  23. local function fetchList()
  24.     local server = getServer()
  25.     if not server then return {}, 0, 0 end
  26.     rednet.send(server, { type = "request_list", user = USER_ID }, PROTOCOL)
  27.     local id, msg = rednet.receive(PROTOCOL, 2)
  28.     if msg and msg.type == "list_response" then
  29.         return msg.data, msg.balance, msg.time
  30.     end
  31.     return {}, 0, 0
  32. end
  33.  
  34. local function sendAction(action, roomId, extra)
  35.     local payload = { type = action, id = roomId, user = USER_ID }
  36.     if extra then
  37.         for k,v in pairs(extra) do payload[k] = v end
  38.     end
  39.     rednet.send(getServer(), payload, PROTOCOL)
  40.     local id, msg = rednet.receive(PROTOCOL, 5)
  41.     if msg then
  42.         term.clear()
  43.         term.setCursorPos(1,1)
  44.         print(msg.msg)
  45.         sleep(2)
  46.     end
  47. end
  48.  
  49. local function formatTime(ms)
  50.     if ms < 0 then return "OVERDUE!" end
  51.     local s = math.floor(ms / 1000)
  52.     return string.format("%02dh %02dm %02ds", math.floor(s/3600), math.floor((s%3600)/60), s%60)
  53. end
  54.  
  55. local function viewOwnedRoom(id, room)
  56.     local timerId = os.startTimer(1)
  57.     while true do
  58.         term.clear()
  59.         term.setCursorPos(1,1)
  60.         print("--- Room " .. id .. " ---")
  61.         print("Status: " .. room.state)
  62.        
  63.         local now = os.epoch("utc")
  64.         if room.state == "rented" then
  65.             local left = room.due - now
  66.             print("Rent Due: " .. formatTime(left))
  67.             if left < 0 then
  68.                 local graceLeft = (room.due + room.info.grace_period) - now
  69.                 print("Eviction in: " .. formatTime(graceLeft))
  70.             end
  71.         end
  72.        
  73.         print("\n[T] Toggle Door")
  74.         print("[K] Set Password")
  75.         if room.state == "rented" then print("[C] Cancel Rent") end
  76.         if room.state == "sold" then print("[S] Sell Back") end
  77.         print("[BACKSPACE] Return")
  78.        
  79.         local e, p1 = os.pullEvent()
  80.         if e == "timer" and p1 == timerId then
  81.             timerId = os.startTimer(1)
  82.         elseif e == "key" then
  83.             if p1 == keys.backspace then break end
  84.             if p1 == keys.t then sendAction("toggle_door", id); break end
  85.             if p1 == keys.c and room.state == "rented" then sendAction("cancel", id); break end
  86.             if p1 == keys.s and room.state == "sold" then sendAction("sell", id); break end
  87.             if p1 == keys.k then
  88.                 term.clear()
  89.                 term.setCursorPos(1,1)
  90.                 write("Enter new numeric PIN:\n> ")
  91.                 local pw = read()
  92.                 sendAction("set_password", id, { password = pw })
  93.                 break
  94.             end
  95.         end
  96.     end
  97. end
  98.  
  99. local function drawMenu(title, options, selected, balance)
  100.     term.clear()
  101.     term.setCursorPos(1,1)
  102.     print("--- " .. title .. " ---")
  103.     print("Bank Balance: " .. balance)
  104.     if #options == 0 then
  105.         print("\nNo rooms found.")
  106.         print("\n[BACKSPACE] Return")
  107.         return
  108.     end
  109.     for i, opt in ipairs(options) do
  110.         if i == selected then print("> " .. opt.label) else print("  " .. opt.label) end
  111.     end
  112.     print("\n[ENTER] Select  [BACK] Return")
  113. end
  114.  
  115. local function browseList(isOwned)
  116.     local db, bal, now = fetchList()
  117.     if not db then return end
  118.    
  119.     local options = {}
  120.     for id, room in pairs(db) do
  121.         if isOwned and room.owner == USER_ID then
  122.             table.insert(options, {label = id .. " [" .. room.state .. "]", id = id, room = room})
  123.         elseif not isOwned and room.state == "available" then
  124.             table.insert(options, {label = id .. " (" .. room.info.mode .. ")", id = id, room = room})
  125.         end
  126.     end
  127.    
  128.     local selected = 1
  129.     while true do
  130.         drawMenu(isOwned and "My Rooms" or "Available", options, selected, bal)
  131.         local _, k = os.pullEvent("key")
  132.        
  133.         if #options == 0 and k == keys.backspace then return end
  134.         if #options > 0 then
  135.             if k == keys.up and selected > 1 then selected = selected - 1
  136.             elseif k == keys.down and selected < #options then selected = selected + 1
  137.             elseif k == keys.enter then
  138.                 if isOwned then viewOwnedRoom(options[selected].id, options[selected].room)
  139.                 else
  140.                     term.clear()
  141.                     term.setCursorPos(1,1)
  142.                     local r = options[selected].room
  143.                     print("Rent: " .. r.info.rent_price .. " | Buy: " .. r.info.sale_price)
  144.                     print("\n[R] Rent  [B] Buy")
  145.                     local _, ch = os.pullEvent("char")
  146.                     if ch == "r" then sendAction("rent", options[selected].id) end
  147.                     if ch == "b" then sendAction("buy", options[selected].id) end
  148.                 end
  149.                 return
  150.             elseif k == keys.backspace then return end
  151.         end
  152.     end
  153. end
  154.  
  155. local mainSelection = 1
  156. local mainOptions = {"Available Rooms", "My Rooms", "Deposit to Bank", "Refresh/Exit"}
  157. while true do
  158.     local db, bal, now = fetchList()
  159.     term.clear()
  160.     term.setCursorPos(1,1)
  161.     print("User: " .. USER_ID .. " | Bal: " .. (bal or 0))
  162.     print("-------------------------")
  163.     for i, opt in ipairs(mainOptions) do
  164.         if i == mainSelection then print("> " .. opt) else print("  " .. opt) end
  165.     end
  166.    
  167.     local _, k = os.pullEvent("key")
  168.     if k == keys.up and mainSelection > 1 then mainSelection = mainSelection - 1
  169.     elseif k == keys.down and mainSelection < #mainOptions then mainSelection = mainSelection + 1
  170.     elseif k == keys.enter then
  171.         if mainSelection == 1 then browseList(false)
  172.         elseif mainSelection == 2 then browseList(true)
  173.         elseif mainSelection == 3 then
  174.             term.clear()
  175.             term.setCursorPos(1,1)
  176.             print("Place items in the Server")
  177.             print("Drop Box chest.")
  178.             print("\nPress ENTER to process...")
  179.             local _, keyWait = os.pullEvent("key")
  180.             if keyWait == keys.enter then sendAction("deposit") end
  181.         elseif mainSelection == 4 then fetchList() end
  182.     end
  183. end
Advertisement
Add Comment
Please, Sign In to add comment