Advertisement
D_Puppy

lanteacraft controller

Jul 20th, 2016
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.93 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local gui = require("gui")
  4. local sg = component.stargate
  5. local event = require("event")
  6. local ser = require("serialization")
  7.  
  8. local radix = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  9.                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  10.            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "+"}
  11.  
  12.  
  13.  
  14. local startAddress = {"A", "A", "A", "A", "A", "A", "A", "A", "A"}
  15.  
  16. function getStartAddress()
  17.   return startAddress[1] .. startAddress[2] .. startAddress[3] .. startAddress[4] .. startAddress[5] .. startAddress[6] .. startAddress[7] .. startAddress[8] .. startAddress[9]
  18. end
  19.  
  20. local callIncoming = false
  21. local isAutodialing = false
  22.  
  23. local sgAddress = sg.getAddress()
  24. local gates = {}
  25.  
  26. -- all gui parts for global using
  27. local myGui, ownAddressLabel, isConnectedLabel
  28. local sgList, addressLabel, locationLabel, statusLabel
  29. local dialButton, editButton, disconnectButton, newButton
  30. local chevron = {}, autodialLabel, autodialButton
  31.  
  32. local editGui, editCloseButton, editStatusList, editAddressText, editLocationText
  33.  
  34.  
  35. local prgName = "Stargate Control"
  36. local version = "v0.1a"
  37.  
  38. function save()
  39.   local file = assert(io.open("stargates.txt", "w"))
  40.   for key, value in pairs(gates) do
  41.     file:write(ser.serialize(value) .. "\n")
  42.   end
  43.   file:close()
  44. end
  45.  
  46. function load()
  47.   local count = 0
  48.   local file = io.open("stargates.txt", "r")
  49.   if file then
  50.     for line in file:lines() do
  51.       table.insert(gates, ser.unserialize(line))
  52.       count = count + 1
  53.       os.sleep(0)
  54.     end
  55.     file:close()
  56.   end
  57. end
  58.  
  59. function buttonCallback(guiID, id)
  60.   local result = gui.getYesNo("", "Do you really want to exit?", "")
  61.   if result == true then
  62.     gui.exit()
  63.   end
  64. end
  65.  
  66. function sgListCallback(guiID, listID, selectedID)
  67.   if #gates > 0 then
  68.     local address = gates[selectedID].address
  69.     local location = gates[selectedID].location
  70.     local status = gates[selectedID].status
  71.  
  72.     gui.setText(myGui, addressLabel, address)
  73.     gui.setText(myGui, locationLabel, location)
  74.     gui.setText(myGui, statusLabel, status)
  75.   end
  76. end
  77.  
  78. function refreshList()
  79.   local entry = ""
  80.   gui.clearList(myGui, sgList)
  81.   for key,value in pairs(gates) do
  82.     entry = string.format("%-10s %-10s", value.address, value.location)
  83.     gui.insertList(myGui, sgList, entry)
  84.   end
  85.   gui.setSelected(myGui, sgList, 1)
  86.   sgListCallback(myGui, sgList, 1)
  87. end
  88.  
  89. function refreshStatus(autodial)
  90.   if autodial == true then
  91.     return
  92.   end
  93.   local selectedID = gui.getSelected(myGui, sgList)
  94.   if selectedID > #gates then
  95.     return
  96.   end
  97.   local address = gates[selectedID].address
  98.   if sg.isConnected() == true then
  99.     gui.setEnable(myGui, dialButton, false)
  100.     gui.setEnable(myGui, autodialButton, false)
  101.     gui.setEnable(myGui, disconnectButton, true)
  102.     if autodial == false then
  103.       gui.setText(myGui, isConnectedLabel, "Connected")
  104.     end
  105.   else
  106.     gui.setEnable(myGui, dialButton, true)
  107.     gui.setEnable(myGui, autodialButton, true)
  108.     gui.setEnable(myGui, disconnectButton, false)
  109.     if autodial == false then
  110.       gui.setText(myGui, isConnectedLabel, "Idle")
  111.     end
  112.     gui.setBottom("                         ")
  113.     for i = 1, 9 do
  114.       gui.setText(myGui, chevron[i], "open")
  115.     end
  116.   end
  117.   if sg.isDialing() == true then
  118.     gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
  119.   end
  120.   gui.setText(myGui, autodialLabel, getStartAddress())
  121. end
  122.  
  123. function dialButtonCallback(guiID, id)
  124.   local selectedID = gui.getSelected(guiID, sgList)
  125.   if selectedID > #gates then
  126.     return
  127.   end
  128.   local address = gates[selectedID].address
  129.   if sg.isConnected() == false then
  130.     gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
  131.     sg.dial(address)
  132.   end
  133. end
  134.  
  135. function disconnectButtonCallback(guiID, id)
  136.   if sg.isConnected() == true then
  137.     sg.disconnect()
  138.   end
  139.   isAutodialing = false
  140. end
  141.  
  142. local editRunning
  143. function editButtonCallback(guiID, id)
  144.   local selectedID = gui.getSelected(guiID, sgList)
  145.   if selectedID > #gates then
  146.     return
  147.   end
  148.   gui.setText(editGui, editAddressText, gates[selectedID].address)
  149.   gui.setText(editGui, editLocationText, gates[selectedID].location)
  150.   editRunning = true
  151.   while editRunning == true do
  152.     gui.runGui(editGui)
  153.   end
  154.   gates[selectedID].address = gui.getText(editGui, editAddressText)
  155.   gates[selectedID].location = gui.getText(editGui, editLocationText)
  156.   _, gates[selectedID].status = gui.getSelected(editGui, editStatusList)
  157.   refreshList()
  158.   save()
  159. end
  160.  
  161. function newButtonCallback(guiID, id)
  162.   editRunning = true
  163.   while editRunning == true do
  164.     gui.runGui(editGui)
  165.   end
  166.   local address = gui.getText(editGui, editAddressText)
  167.   local location = gui.getText(editGui, editLocationText)
  168.   if address ~= "" and location ~= "" then
  169.     local _, status = gui.getSelected(editGui, editStatusList)
  170.     local value = {["address"] = address, ["location"] = location, ["status"] = status}
  171.     table.insert(gates, value)
  172.   end
  173.   refreshList()
  174.   save()
  175. end
  176.  
  177. function deleteButtonCallback(guiID, id)
  178.   local selectedID = gui.getSelected(guiID, sgList)
  179.   local tmpTable = gates
  180.   gates = {}
  181.   for i = 1, #tmpTable do
  182.     if i ~= selectedID then
  183.       table.insert(gates, tmpTable[i])
  184.     end
  185.   end
  186.   refreshList()
  187.   save()
  188. end
  189.  
  190. function editCloseCallback(guiID, id)
  191.   editRunning = false
  192. end
  193.  
  194. function eventIncomingCallback(e)
  195.   if e == "sgIncoming" then
  196.     callIncoming = true
  197.     gui.setBottom("Incoming call")
  198.   end
  199.   event.listen("sgIncoming", eventCallback)
  200. end
  201.  
  202. function eventOutgoingCallback(e)
  203.   if e == "sgOutgoing" then
  204.     callIncoming = false
  205.     gui.setBottom("Outgoing call")
  206.   end
  207.   event.listen("sgOutgoing", eventCallback)
  208. end
  209.  
  210. function eventChevronCallback(e, addr, ch)
  211.   if e == "sgChevronEncode" then
  212.     gui.setText(myGui, chevron[ch], "engaged")
  213.   end
  214.   event.listen("sgChevronEncode", eventCallback)
  215. end
  216.  
  217. function saveActAddress()
  218.   local file = io.open("sglist.tmp", "w")
  219.   file:write(ser.serialize(startAddress) .. "\n")
  220.   file:close()
  221. end
  222.  
  223. function loadActAddress()
  224.   local file = io.open("sglist.tmp", "r")
  225.   if file then
  226.     line = file:read("*line")
  227.     startAddress = ser.unserialize(line)
  228.     file:close()
  229.   end
  230. end
  231.  
  232.  
  233. function getStartPos(number)
  234.   local startPos = 1
  235.   local s = startAddress[number]
  236.   for key, value in pairs(radix) do
  237.     if value == s then
  238.       startPos = key
  239.     end
  240.   end
  241.   return startPos
  242. end
  243.  
  244. function searchGates()
  245.   for Ai = getStartPos(1), #radix do
  246.     local A = radix[Ai]
  247.     for Bi = getStartPos(2), #radix do
  248.       local B = radix[Bi]
  249.       for Ci = getStartPos(3), #radix do
  250.     local C = radix[Ci]
  251.     for Di = getStartPos(4), #radix do
  252.       local D = radix[Di]
  253.       for Ei = getStartPos(5), #radix do
  254.         local E = radix[Ei]
  255.         for Fi = getStartPos(6), #radix do
  256.           local F = radix[Fi]
  257.           for Gi = getStartPos(7), #radix do
  258.         local G = radix[Gi]
  259.         for Hi = getStartPos(8), #radix do
  260.           local H = radix[Hi]
  261.           for Ii = getStartPos(9), #radix do
  262.             local I = radix[Ii]
  263.             gui.setText(myGui, autodialLabel, getStartAddress())
  264.             local address = A .. B .. C .. D .. E .. F .. G .. H .. I
  265.             gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
  266.             refreshStatus(true)
  267.             gui.runGui(myGui)
  268.             sg.dial(address)
  269.             while sg.isDialing() == true do
  270.               os.sleep(0.1)
  271.               gui.runGui(myGui)
  272.             end
  273.             if sg.isConnected() == true and isAutodialing == true then
  274.               local value = {["address"] = address, ["location"] = "unknown", ["status"] = "unknown"}
  275.               table.insert(gates, value)
  276.               save()
  277.               refreshList()
  278.               os.sleep(2)
  279.               sg.disconnect()
  280.               os.sleep(2)
  281.             end
  282.             startAddress[1] = A
  283.             startAddress[2] = B
  284.             startAddress[3] = C
  285.             startAddress[4] = D
  286.             startAddress[5] = E
  287.             startAddress[6] = F
  288.             startAddress[7] = G
  289.             startAddress[8] = H
  290.             startAddress[9] = I
  291.             if isAutodialing == false then
  292.               return
  293.             end
  294.             saveActAddress()
  295.             for i = 1, 9 do
  296.               gui.setText(myGui, chevron[i], "open")
  297.             end
  298.             gui.setText(myGui, autodialLabel, getStartAddress())
  299.           end
  300.           startAddress[9] = "A"
  301.         end
  302.         startAddress[8] = "A"
  303.           end
  304.           startAddress[7] = "A"
  305.         end
  306.         startAddress[6] = "A"
  307.       end
  308.       startAddress[5] = "A"
  309.     end
  310.     startAddress[4] = "A"
  311.       end
  312.       startAddress[3] = "A"
  313.     end
  314.     startAddress[2] = "A"
  315.   end
  316.   startAddress[1] = "A"
  317. end
  318.  
  319.  
  320. function autodialCallback()
  321.   gui.setEnable(myGui, dialButton, false)
  322.   gui.setEnable(myGui, autodialButton, false)
  323.   gui.setEnable(myGui, disconnectButton, true)
  324.   isAutodialing = true
  325.   loadActAddress()
  326.   searchGates()
  327.   gui.setEnable(myGui, dialButton, true)
  328.   gui.setEnable(myGui, autodialButton, true)
  329.   gui.setEnable(myGui, disconnectButton, false)
  330. end
  331.  
  332. load()
  333.  
  334. myGui = gui.newGui(2, 2, 78, 23, true)
  335. button = gui.newButton(myGui, "center", 21, "exit", buttonCallback)
  336. gui.newFrame(myGui, 1, 1, 37, 4)
  337. gui.newLabel(myGui, 2, 2, "Local address  : ")
  338. ownAddressLabel = gui.newLabel(myGui, 19, 2, sgAddress)
  339. gui.newLabel(myGui, 2, 3, "Status         : ")
  340. isConnectedLabel = gui.newLabel(myGui, 19, 3, "not connected")
  341.  
  342. gui.newFrame(myGui, 42, 1, 34, 19)
  343. sgList = gui.newList(myGui, 44, 2, 30, 9, {}, sgListCallback, "Known gates")
  344. gui.newLabel(myGui, 44, 12, "Address    : ")
  345. addressLabel = gui.newLabel(myGui, 57, 12, "---------")
  346. gui.newLabel(myGui, 44, 13, "Location   : ")
  347. locationLabel = gui.newLabel(myGui, 57, 13, "unknown")
  348. gui.newLabel(myGui, 44, 14, "Status     : ")
  349. statusLabel = gui.newLabel(myGui, 57, 14, "unknown")
  350. dialButton = gui.newButton(myGui, 44, 16, "dial", dialButtonCallback)
  351. disconnectButton = gui.newButton(myGui, 53, 16, "disconnect", disconnectButtonCallback)
  352. editButton = gui.newButton(myGui, 68, 16, "edit", editButtonCallback)
  353. newButton = gui.newButton(myGui, 44, 18, "new", newButtonCallback)
  354. deleteButton = gui.newButton(myGui, 53, 18, "delete", deleteButtonCallback)
  355.  
  356. gui.newFrame(myGui, 1, 5, 30, 11)
  357. gui.newLabel(myGui, 2, 6, "Chevron 1 : ")
  358. gui.newLabel(myGui, 2, 7, "Chevron 2 : ")
  359. gui.newLabel(myGui, 2, 8, "Chevron 3 : ")
  360. gui.newLabel(myGui, 2, 9, "Chevron 4 : ")
  361. gui.newLabel(myGui, 2, 10, "Chevron 5 : ")
  362. gui.newLabel(myGui, 2, 11, "Chevron 6 : ")
  363. gui.newLabel(myGui, 2, 12, "Chevron 7 : ")
  364. gui.newLabel(myGui, 2, 13, "Chevron 8 : ")
  365. gui.newLabel(myGui, 2, 14, "Chevron 9 : ")
  366.  
  367. chevron[1] = gui.newLabel(myGui, 14, 6, "open")
  368. chevron[2] = gui.newLabel(myGui, 14, 7, "open")
  369. chevron[3] = gui.newLabel(myGui, 14, 8, "open")
  370. chevron[4] = gui.newLabel(myGui, 14, 9, "open")
  371. chevron[5] = gui.newLabel(myGui, 14, 10, "open")
  372. chevron[6] = gui.newLabel(myGui, 14, 11, "open")
  373. chevron[7] = gui.newLabel(myGui, 14, 12, "open")
  374. chevron[8] = gui.newLabel(myGui, 14, 13, "open")
  375. chevron[9] = gui.newLabel(myGui, 14, 14, "open")
  376.  
  377. gui.newLabel(myGui, 2, 17, "Lanteacraft version : " .. sg.getInterfaceVersion())
  378. gui.newLabel(myGui, 2, 19, "Last autodial addr. : ")
  379. loadActAddress()
  380. autodialLabel = gui.newLabel(myGui, 24, 19, getStartAddress())
  381.  
  382. autodialButton = gui.newButton(myGui, 2, 21, "autodial", autodialCallback)
  383.  
  384.  
  385. refreshList()
  386.  
  387. editGui = gui.newGui("center", 4, 34, 14, true)
  388. editCloseButton = gui.newButton(editGui, "center", 12, "close", editCloseCallback)
  389. gui.newLabel(editGui, 2, 1, "Address  :")
  390. editAddressText = gui.newText(editGui, 12, 1, 10, "")
  391. gui.newLabel(editGui, 2, 3, "Location :")
  392. editLocationText = gui.newText(editGui, 12, 3, 20, "")
  393. editStatusList = gui.newList(editGui, 2, 5, 30, 6, {"unknown", "safe", "dangerous"}, nil, "Status")
  394.  
  395.  
  396.  
  397. gui.clearScreen()
  398. gui.setTop(prgName .. " " .. version)
  399. sg.disconnect()
  400. event.listen("sgIncoming", eventIncomingCallback)
  401. event.listen("sgOutgoing", eventOutgoingCallback)
  402. event.listen("sgChevronEncode", eventChevronCallback)
  403. while true do
  404.   refreshStatus(false)
  405.   gui.runGui(myGui)
  406. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement