Advertisement
Alakazard12

Proxy host

Dec 20th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. local client
  2.  
  3. local sides = {"top", "bottom", "left", "right", "front", "back"}
  4.  
  5. local function ports()
  6.     for i,v in pairs(sides) do
  7.         rednet.open(v)
  8.     end
  9. end
  10.  
  11. ports()
  12.  
  13. while true do
  14.     local id, msg = rednet.receive()
  15.     sleep(0)
  16.     local isP = false
  17.     if id == client then
  18.         isP = true
  19.     end
  20.     if isP == false and string.sub(msg, 1, 6) == "PROXY:" then 
  21.         isP = true
  22.         client = id
  23.         print("New client")
  24.     end
  25.     if isP == true and string.sub(msg, 1, 6) == "PROXY:" then
  26.         local tbl = textutils.unserialize(string.sub(msg, 7))
  27.         if not tbl then tbl = {} end
  28.         if tbl.send and type(tbl.send) == "number" and tbl.send > -1 then
  29.             pcall(function()
  30.                 rednet.send(tbl.send, tbl.message)
  31.                 print("Sent new message to "..tbl.send)
  32.             end)
  33.         elseif tbl.send and tbl.send == "broadcast" then
  34.             pcall(function()
  35.                 rednet.broadcast(tbl.message)
  36.                 print("Broadcasted message")
  37.             end)
  38.         end
  39.     elseif client ~= nil then
  40.         pcall(function()
  41.             rednet.send(client, textutils.serialize({rep=id, message=msg}))
  42.             print("Sent message to client")
  43.         end)
  44.     else
  45.         print("Message was not valid isP was "..tostring(isP))
  46.     end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement