stairs-client.lua

42 lines · 994 B

Open raw

Remote stairs listener

Copy & run

wget https://perlytiara.github.io/turtles.tips/raw/programs/stairs-client.lua
1-- stairs-client.lua - Remote stairs listener
2local function findModem()
3 for _, side in pairs(rs.getSides()) do
4 if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
5 return side
6 end
7 end
8 error("No modem found!")
9end
10
11rednet.open(findModem())
12local id = os.getComputerID()
13
14print("Stairs client ready (ID: " .. id .. ")")
15print("Waiting for jobs...")
16
17while true do
18 local sender, msg, protocol = rednet.receive()
19
20 local cmd = ""
21 if type(msg) == "table" and msg.command == "RUN" then
22 cmd = msg.args or ""
23 elseif type(msg) == "string" then
24 cmd = msg
25 end
26
27 if cmd ~= "" then
28 print("Running: stairs " .. cmd)
29 local ok, err = pcall(function()
30 shell.run("stairs " .. cmd)
31 end)
32
33 if ok then
34 rednet.send(sender, {status = "done", id = id})
35 print("Job completed")
36 else
37 rednet.send(sender, {status = "error", id = id, error = err})
38 print("Job failed: " .. tostring(err))
39 end
40 end
41end
42