Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Room Client
- local RS_SIDE = "back"
- local PROTOCOL = "room_mgmt"
- peripheral.find("modem", rednet.open)
- local mon = peripheral.find("monitor")
- -- Auto-generate info.txt
- if not fs.exists("info.txt") then
- local def = {
- id = "Room_" .. os.getComputerID(),
- mode = "both",
- rent_price = 5,
- sale_price = 100,
- rent_frequency = 1800000, -- 30 mins in ms
- grace_period = 900000 -- 15 mins in ms
- }
- local f = fs.open("info.txt", "w")
- f.write(textutils.serializeJSON(def))
- f.close()
- end
- local file = fs.open("info.txt", "r")
- local info = textutils.unserializeJSON(file.readAll())
- file.close()
- local currentInput = ""
- local roomState = { status = "Connecting", owner = nil, overdue = false, isOpen = false }
- local function drawMonitor()
- if not mon then return end
- mon.setTextScale(1)
- mon.clear()
- mon.setCursorPos(1,1)
- mon.write("Room: " .. info.id)
- mon.setCursorPos(1,2)
- local statStr = roomState.status
- if roomState.overdue then statStr = statStr .. " (OVERDUE)" end
- mon.write("Status: " .. statStr)
- if roomState.owner then
- mon.setCursorPos(1,3)
- mon.write("Owner: " .. roomState.owner)
- mon.setCursorPos(1,4)
- mon.write("Door: " .. (roomState.isOpen and "UNLOCKED" or "LOCKED"))
- -- Draw compact Keypad
- local pad = {"1","2","3","4","5","6","7","8","9","C","0","E"}
- for i=0, 3 do
- mon.setCursorPos(1, 6 + i)
- mon.write(" " .. pad[i*3 + 1] .. " " .. pad[i*3 + 2] .. " " .. pad[i*3 + 3])
- end
- mon.setCursorPos(1, 10)
- mon.write("PIN: " .. string.rep("*", #currentInput))
- end
- end
- local function heartbeatLoop()
- while true do
- local serverId = rednet.lookup(PROTOCOL, "server")
- if serverId then
- rednet.send(serverId, { type = "register", info = info }, PROTOCOL)
- end
- sleep(15)
- end
- end
- local function listenLoop()
- while true do
- local sender, msg = rednet.receive(PROTOCOL)
- if type(msg) == "table" and msg.target == info.id and msg.command == "update" then
- roomState.status = msg.status
- roomState.owner = msg.owner
- roomState.isOpen = msg.isOpen
- roomState.overdue = msg.overdue
- rs.setOutput(RS_SIDE, roomState.isOpen)
- drawMonitor()
- end
- end
- end
- local function keypadLoop()
- local padMap = {"1","2","3","4","5","6","7","8","9","C","0","E"}
- while true do
- local e, side, x, y = os.pullEvent("monitor_touch")
- if roomState.owner and y >= 6 and y <= 9 and x >= 1 and x <= 9 then
- local col = math.ceil(x / 3)
- local row = y - 5
- local key = padMap[(row-1)*3 + col]
- if key == "C" then
- currentInput = ""
- elseif key == "E" then
- local serverId = rednet.lookup(PROTOCOL, "server")
- if serverId and #currentInput > 0 then
- rednet.send(serverId, { type = "toggle_door", id = info.id, password = currentInput }, PROTOCOL)
- end
- currentInput = ""
- else
- if #currentInput < 8 then currentInput = currentInput .. key end
- end
- drawMonitor()
- end
- end
- end
- print("Room Client " .. info.id .. " running.")
- parallel.waitForAny(heartbeatLoop, listenLoop, keypadLoop)
Advertisement
Add Comment
Please, Sign In to add comment