Advertisement
Guest User

msgBoard

a guest
Jan 30th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 KB | None | 0 0
  1. rednet.open("bottom")
  2. --ease of use function ftw!
  3. function reply(text)
  4.   rednet.broadcast(text)
  5.   print("replied: "..text)
  6.   end
  7.  
  8. function rec(timeout)
  9.   if timeout == nil then
  10.     id, msg, d = rednet.receive()
  11.   else
  12.     id, msg, d = rednet.receive(timeout)
  13.     end
  14.   print("Message received!")
  15.   end
  16.  
  17. --Real code now;
  18.  
  19. function loadFile()
  20.   local check = fs.exists("messages")
  21.   if check == true then
  22.     shell.run("messages")
  23.   else
  24.     saveFile(true)
  25.     end
  26.   end
  27.  
  28. function saveFile(gen)
  29.   if gen == nil then
  30.     w = fs.open("messages", "a")
  31.     w.writeLine("center("..[["]]..MESSAGE..[["]]..")")
  32.     w.writeLine("p.setTextColor(colors.yellow)")
  33.     w.writeLine("center("..[["]].."-"..USER..[["]]..")")
  34.     w.writeLine("p.setTextColor(colors.white)")
  35.     w.close()
  36.     os.reboot()
  37.   else
  38.     w = fs.open("messages", "w")
  39.     w.writeLine("p = peripheral.wrap("..[["]].."top"..[["]]..")")
  40.     w.writeLine("p.setTextScale(1)")
  41.     w.writeLine("p.clear()")
  42.     w.writeLine("p.setCursorPos(1,1)")
  43.     w.writeLine("function center(text)")
  44.     w.writeLine("local x, y = p.getSize()")
  45.     w.writeLine("local x2, y2 = p.getCursorPos()")
  46.     w.writeLine("local length = text:len()")
  47.     w.writeLine("if length > x then")
  48.     w.writeLine("while true do")
  49.     w.writeLine("if length >= x then")
  50.     w.writeLine("p.write(text)")
  51.     w.writeLine("text = string.sub(text, (x + 1))")
  52.     w.writeLine("length = length - x")
  53.     w.writeLine("y2 = y2 + 1")
  54.     w.writeLine("p.setCursorPos(1, y2)")
  55.     w.writeLine("else")
  56.     w.writeLine("p.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)")
  57.     w.writeLine("p.write(text)")
  58.     w.writeLine("y2 = y2 + 1")
  59.     w.writeLine("p.setCursorPos(1, y2)")
  60.     w.writeLine("break")
  61.     w.writeLine("end")
  62.     w.writeLine("end")
  63.     w.writeLine("else")
  64.     w.writeLine("p.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)")
  65.     w.writeLine("p.write(text)")
  66.     w.writeLine("y2 = y2 + 1")
  67.     w.writeLine("p.setCursorPos(1, y2)")
  68.     w.writeLine("end")
  69.     w.writeLine("end")
  70.     w.writeLine("p.setTextColor(colors.orange)")
  71.     w.writeLine("center("..[["]].."Server Message Board"..[["]]..")")
  72.     w.writeLine("local x, y = p.getSize()")
  73.     w.writeLine("for i = 1, x do")
  74.     w.writeLine("p.write("..[["]].."~"..[["]]..")")
  75.     w.writeLine("end")
  76.     w.writeLine("p.setCursorPos(1, 3)")
  77.     w.writeLine("p.setTextColor(colors.white)")
  78.     w.close()
  79.     os.reboot()
  80.     end
  81.   end
  82.  
  83. function re()
  84.   rec()
  85.   if msg == "start" then
  86.     reply("go on")
  87.     while true do
  88.     id, mssg, d = rednet.receive(15)
  89.     if mssg == "user" then
  90.       reply(".")
  91.       id, USER, d = rednet.receive(15)
  92.       print("received user: "..USER)
  93.       reply(".")
  94.     elseif mssg == "message" then
  95.       reply(".")
  96.       id, MESSAGE, d = rednet.receive(15)
  97.       print("received msg: "..MESSAGE)
  98.       reply(".")
  99.     elseif mssg == "end" then
  100.       print("Transmission over")
  101.       saveFile()
  102.       break
  103.       end
  104.       end
  105.   else
  106.     os.reboot()
  107.     end
  108.   end
  109.  
  110. loadFile()
  111. re()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement