Sxw1212

Tent

Aug 2nd, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.67 KB | None | 0 0
  1. -- Tent by Sxw1212
  2. -- Copyright (c) 2013 Sxw1212
  3. -- All rights reserved
  4. -- Config
  5. local cfg = {master = "", monitor = "", modem = "", sg = "", glass = "", sgs = "https://raw.github.com/Sxw1212/Tent/master/sgs.lua"}
  6. if fs.exists("/tentconfig") then
  7.     local fh = fs.open("/tentconfig", "r")
  8.     cfg = textutils.unserialize(fh.readAll())
  9.     fh.close()
  10. else
  11.     local fh = fs.open("/tentconfig", "w")
  12.     fh.write(textutils.serialize(cfg))
  13.     fh.close()
  14.     error("Please edit the config.")
  15. end
  16. -- End config
  17.  
  18. local oldPull = os.pullEvent
  19. os.pullEvent = os.pullEventRaw
  20.  
  21. local updateurl = "https://raw.github.com/Sxw1212/Tent/master/main.lua"
  22.  
  23. _G["modem"] = peripheral.wrap(cfg.modem)
  24. local glass = peripheral.wrap(cfg.glass)
  25. local sg = peripheral.wrap(cfg.sg)
  26. local monitor = peripheral.wrap(cfg.monitor)
  27. master = cfg.master
  28.  
  29. assert(glass)
  30. assert(sg)
  31. assert(monitor)
  32. assert(modem)
  33. assert(master)
  34.  
  35. monitor.setTextScale(2.5)
  36. monitor.clear()
  37.  
  38. glass.clear()
  39.  
  40. function split(inputstr, sep)
  41.         if sep == nil then
  42.                 sep = "%s"
  43.         end
  44.         t={} ; i=1
  45.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  46.                 t[i] = str
  47.                 i = i + 1
  48.         end
  49.         return t
  50. end
  51.  
  52. local function getCenter(text)
  53.     local extraSupport
  54.     if #text >= 18 then
  55.         extraSupport = 5
  56.     else
  57.         gWidth = 24
  58.         extraSupport = 0
  59.     end
  60.     return (((gWidth/2)-(#text/2))*6)+20+extraSupport
  61. end
  62.  
  63. local function setText(text, object)
  64.     object.setText("")
  65.     object.setX(getCenter(text))
  66.     object.setText(text)
  67. end
  68.  
  69. local col = {}
  70. col.red = 0xff3333
  71. col.blue = 0x7dd2e4
  72. col.yellow = 0xffff4d
  73. col.green = 0x4dff4d
  74. col.gray = 0xe0e0e0
  75. col.textGray = 0x818181
  76. col.text = 0x5a5a5a
  77. col.rain = 0x2e679f
  78.  
  79. -- Boxes
  80.  
  81. local disp = glass.addBox(20, 20, 146, 50, col.gray, 0.8)
  82.  
  83. local left = glass.addBox(19, 19, 1, 52, col.blue, 0.8)
  84. local right = glass.addBox(166, 19, 1, 52, col.blue, 0.8)
  85.  
  86. -- Text
  87.  
  88. local title = glass.addText(80, 25, "", col.textGray)
  89. --title.setZIndex(5)
  90. local status = glass.addText(45, 35, "", col.textGray)
  91. --status.setZIndex(5)
  92. local main = glass.addText(45, 45, "", col.textGray)
  93. --main.setZIndex(5)
  94. -- xD
  95. local copyright = glass.addText(45, 55, "(c) 2013 Sxw1212", col.textGray)
  96. --copyright.setZIndex(5)
  97.  
  98. setText("Tent", title)
  99. setText("Loading...", status)
  100. setText("", main)
  101.  
  102. local sgraw = http.get(cfg.sgs)
  103. local sgs = {}
  104.  
  105. if sgraw then
  106.     sgs = loadstring("return " .. sgraw.readAll())()
  107. end
  108.  
  109. function toAddr(alias, ret)
  110.     for k,v in pairs(sgs) do
  111.         if k == alias or v[1] == alias then
  112.             return v[2]
  113.         end
  114.     end
  115.     if ret then
  116.         return alias
  117.     end
  118. end
  119.  
  120. function toShort(addr)
  121.     for k,v in pairs(sgs) do
  122.         if v[2] == addr then
  123.             return k
  124.         end
  125.     end
  126.     return addr
  127. end
  128.  
  129. function toLong(addr)
  130.     for k,v in pairs(sgs) do
  131.         if v[2] == addr then
  132.             return v[1]
  133.         end
  134.     end
  135.     return addr
  136. end
  137.  
  138. function queueClear(t)
  139.     os.queueEvent("tent_clear", os.clock() + (t or 5))
  140. end
  141.  
  142. function stopClear()
  143.     os.queueEvent("tent_clear", nil)
  144. end
  145.  
  146. function chat()
  147.     while true do
  148.         local _, msg, user=os.pullEvent("chat_command")
  149.         if user == master then
  150.         local cmd = split(msg or "", " ")
  151.         if cmd[1] == "dial" then
  152.             stopClear()
  153.             local addr = cmd[2] or ""
  154.             addr = toAddr(addr, true)
  155.             if string.len(addr) == 7 then
  156.                 print("Trying to dial ".. addr .. ".")
  157.                 setText("Validating...", main)
  158.                 sg.disconnect()
  159.                 pcall(sg.connect, addr)
  160.                 sleep(3)
  161.                 if sg.getDialledAddress() == addr then
  162.                     print("Dialed!")
  163.                 else
  164.                     setText("Connection failed.", main)
  165.                     print("Failed")
  166.                 end
  167.             else
  168.                 setText("Improper address.", main)
  169.             end
  170.             queueClear()
  171.         elseif cmd[1] == "disconnect" then
  172.             sg.disconnect()
  173.             setText("Disconnected.", main)
  174.             print("Disconnected.")
  175.             queueClear()
  176.         elseif cmd[1] == "lock" then
  177.             if fs.exists("/.tentsglock") then
  178.                 fs.delete("/.tentsglock")
  179.                 setText("Unlocked.", main)
  180.             else
  181.                 fs.makeDir("/.tentsglock")
  182.                 setText("Locked.", main)
  183.             end
  184.             queueClear()
  185.         elseif cmd[1] == "shell" then
  186.             setText("Running shell, quit to resume.", main)
  187.             os.pullEvent = oldPull
  188.             shell.run("shell")
  189.             os.pullEvent = os.pullEventRaw
  190.             queueClear()
  191.         elseif cmd[1] == "update" then
  192.             setText("Updating...", main)
  193.             local fh = http.get(updateurl)
  194.             if fh then
  195.                 fs.delete("/startup")
  196.                 local startup = fs.open("/startup", "w")
  197.                 startup.write(fh.readAll())
  198.                 startup.close()
  199.                 os.reboot()
  200.             else
  201.                 setText("Update failed.", main)
  202.                 queueClear()
  203.             end
  204.         elseif cmd[1] == "lua" then
  205.             cmd[1] = ""
  206.             local luac = table.concat(cmd, " ")
  207.             pcall(loadstring(luac))
  208.         else
  209.             setText("Unknown command.", main)
  210.             queueClear()
  211.         end
  212.     end
  213.     end
  214. end
  215.  
  216. function lock()
  217.     local stime = nil
  218.     while true do
  219.         sleep(0.5)
  220.         if sg.getDialledAddress() ~= "" then
  221.             if not stime then
  222.                 stime = os.clock() + 17
  223.             end
  224.             local addr = sg.getDialledAddress()
  225.             name = toShort(addr)
  226.             if sg.isConnected() == "true" then
  227.                 setText(name .. " connected.", status)
  228.                 monitor.setBackgroundColor(colors.lime)
  229.                 monitor.clear()
  230.                 monitor.setCursorPos(1, 1)
  231.                 monitor.write("-Connected-")
  232.                 monitor.setCursorPos(1, 2)
  233.                 monitor.write("--" .. sg.getDialledAddress() .. "--")
  234.             else
  235.                 setText("Dialing " .. name .. " - " .. math.ceil(stime - os.clock()), status)
  236.                 monitor.setBackgroundColor(colors.blue)
  237.                 monitor.clear()
  238.                 monitor.setCursorPos(1, 1)
  239.                 local time = math.ceil(stime - os.clock()) .. ""
  240.                 if string.len(time) == 1 then
  241.                     time = "0" .. time
  242.                 end
  243.                 monitor.write(time .. " to blast")
  244.                 monitor.setCursorPos(1, 2)
  245.                 monitor.write("--" .. sg.getDialledAddress() .. "--")
  246.             end
  247.             if sg.isInitiator() == "false" then
  248.                 if fs.exists("/.tentsglock") then
  249.                     sg.disconnect()
  250.                 end
  251.             end
  252.         else
  253.             setText("Nobody connected.", status)
  254.             monitor.setBackgroundColor(colors.red)
  255.             monitor.clear()
  256.             monitor.setCursorPos(1, 1)
  257.             monitor.write("---Ready---")
  258.             monitor.setCursorPos(1, 2)
  259.             local status = "Waiting"
  260.             if fs.exists("/.tentsglock") then
  261.                 status = "Secured"
  262.             end
  263.             monitor.write("--" .. status .. "--")
  264.             stime = nil
  265.         end
  266.         if fs.exists("/.tentsglock") then
  267.                 setText("Tent - Locked", title)
  268.         else
  269.             setText("Tent - Open", title)
  270.         end
  271.     end
  272. end
  273.  
  274. function clear()
  275.     local trigger = nil
  276.     local timer = os.startTimer(0.5)
  277.     while true do
  278.         local e, p = os.pullEvent()
  279.         if e == "tent_clear" then
  280.             trigger = p
  281.         elseif e == "timer" and p == timer then
  282.             timer = os.startTimer(0.5)
  283.         end
  284.         if trigger and os.clock() >= trigger then
  285.             setText("", main)
  286.         end
  287.     end
  288. end
  289.  
  290. parallel.waitForAny(chat, lock, clear)
Advertisement
Add Comment
Please, Sign In to add comment