Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pocket Client
- local PROTOCOL = "room_mgmt"
- peripheral.find("modem", rednet.open)
- -- Setup Username
- settings.load()
- local USER_ID = settings.get("room_username")
- if not USER_ID then
- term.clear()
- term.setCursorPos(1,1)
- print("Welcome to Room Network!")
- write("Enter your User Name:\n> ")
- USER_ID = read()
- settings.set("room_username", USER_ID)
- settings.save()
- end
- local function getServer()
- local id = rednet.lookup(PROTOCOL, "server")
- return id
- end
- local function fetchList()
- local server = getServer()
- if not server then return {}, 0, 0 end
- rednet.send(server, { type = "request_list", user = USER_ID }, PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL, 2)
- if msg and msg.type == "list_response" then
- return msg.data, msg.balance, msg.time
- end
- return {}, 0, 0
- end
- local function sendAction(action, roomId, extra)
- local payload = { type = action, id = roomId, user = USER_ID }
- if extra then
- for k,v in pairs(extra) do payload[k] = v end
- end
- rednet.send(getServer(), payload, PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL, 5)
- if msg then
- term.clear()
- term.setCursorPos(1,1)
- print(msg.msg)
- sleep(2)
- end
- end
- local function formatTime(ms)
- if ms < 0 then return "OVERDUE!" end
- local s = math.floor(ms / 1000)
- return string.format("%02dh %02dm %02ds", math.floor(s/3600), math.floor((s%3600)/60), s%60)
- end
- local function viewOwnedRoom(id, room)
- local timerId = os.startTimer(1)
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("--- Room " .. id .. " ---")
- print("Status: " .. room.state)
- local now = os.epoch("utc")
- if room.state == "rented" then
- local left = room.due - now
- print("Rent Due: " .. formatTime(left))
- if left < 0 then
- local graceLeft = (room.due + room.info.grace_period) - now
- print("Eviction in: " .. formatTime(graceLeft))
- end
- end
- print("\n[T] Toggle Door")
- print("[K] Set Password")
- if room.state == "rented" then print("[C] Cancel Rent") end
- if room.state == "sold" then print("[S] Sell Back") end
- print("[BACKSPACE] Return")
- local e, p1 = os.pullEvent()
- if e == "timer" and p1 == timerId then
- timerId = os.startTimer(1)
- elseif e == "key" then
- if p1 == keys.backspace then break end
- if p1 == keys.t then sendAction("toggle_door", id); break end
- if p1 == keys.c and room.state == "rented" then sendAction("cancel", id); break end
- if p1 == keys.s and room.state == "sold" then sendAction("sell", id); break end
- if p1 == keys.k then
- term.clear()
- term.setCursorPos(1,1)
- write("Enter new numeric PIN:\n> ")
- local pw = read()
- sendAction("set_password", id, { password = pw })
- break
- end
- end
- end
- end
- local function drawMenu(title, options, selected, balance)
- term.clear()
- term.setCursorPos(1,1)
- print("--- " .. title .. " ---")
- print("Bank Balance: " .. balance)
- if #options == 0 then
- print("\nNo rooms found.")
- print("\n[BACKSPACE] Return")
- return
- end
- for i, opt in ipairs(options) do
- if i == selected then print("> " .. opt.label) else print(" " .. opt.label) end
- end
- print("\n[ENTER] Select [BACK] Return")
- end
- local function browseList(isOwned)
- local db, bal, now = fetchList()
- if not db then return end
- local options = {}
- for id, room in pairs(db) do
- if isOwned and room.owner == USER_ID then
- table.insert(options, {label = id .. " [" .. room.state .. "]", id = id, room = room})
- elseif not isOwned and room.state == "available" then
- table.insert(options, {label = id .. " (" .. room.info.mode .. ")", id = id, room = room})
- end
- end
- local selected = 1
- while true do
- drawMenu(isOwned and "My Rooms" or "Available", options, selected, bal)
- local _, k = os.pullEvent("key")
- if #options == 0 and k == keys.backspace then return end
- if #options > 0 then
- if k == keys.up and selected > 1 then selected = selected - 1
- elseif k == keys.down and selected < #options then selected = selected + 1
- elseif k == keys.enter then
- if isOwned then viewOwnedRoom(options[selected].id, options[selected].room)
- else
- term.clear()
- term.setCursorPos(1,1)
- local r = options[selected].room
- print("Rent: " .. r.info.rent_price .. " | Buy: " .. r.info.sale_price)
- print("\n[R] Rent [B] Buy")
- local _, ch = os.pullEvent("char")
- if ch == "r" then sendAction("rent", options[selected].id) end
- if ch == "b" then sendAction("buy", options[selected].id) end
- end
- return
- elseif k == keys.backspace then return end
- end
- end
- end
- local mainSelection = 1
- local mainOptions = {"Available Rooms", "My Rooms", "Deposit to Bank", "Refresh/Exit"}
- while true do
- local db, bal, now = fetchList()
- term.clear()
- term.setCursorPos(1,1)
- print("User: " .. USER_ID .. " | Bal: " .. (bal or 0))
- print("-------------------------")
- for i, opt in ipairs(mainOptions) do
- if i == mainSelection then print("> " .. opt) else print(" " .. opt) end
- end
- local _, k = os.pullEvent("key")
- if k == keys.up and mainSelection > 1 then mainSelection = mainSelection - 1
- elseif k == keys.down and mainSelection < #mainOptions then mainSelection = mainSelection + 1
- elseif k == keys.enter then
- if mainSelection == 1 then browseList(false)
- elseif mainSelection == 2 then browseList(true)
- elseif mainSelection == 3 then
- term.clear()
- term.setCursorPos(1,1)
- print("Place items in the Server")
- print("Drop Box chest.")
- print("\nPress ENTER to process...")
- local _, keyWait = os.pullEvent("key")
- if keyWait == keys.enter then sendAction("deposit") end
- elseif mainSelection == 4 then fetchList() end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment