Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local gui = require("gui")
- local sg = component.stargate
- local event = require("event")
- local ser = require("serialization")
- local radix = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
- "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "+"}
- local startAddress = {"A", "A", "A", "A", "A", "A", "A", "A", "A"}
- function getStartAddress()
- return startAddress[1] .. startAddress[2] .. startAddress[3] .. startAddress[4] .. startAddress[5] .. startAddress[6] .. startAddress[7] .. startAddress[8] .. startAddress[9]
- end
- local callIncoming = false
- local isAutodialing = false
- local sgAddress = sg.getAddress()
- local gates = {}
- -- all gui parts for global using
- local myGui, ownAddressLabel, isConnectedLabel
- local sgList, addressLabel, locationLabel, statusLabel
- local dialButton, editButton, disconnectButton, newButton
- local chevron = {}, autodialLabel, autodialButton
- local editGui, editCloseButton, editStatusList, editAddressText, editLocationText
- local prgName = "Stargate Control"
- local version = "v0.1a"
- function save()
- local file = assert(io.open("stargates.txt", "w"))
- for key, value in pairs(gates) do
- file:write(ser.serialize(value) .. "\n")
- end
- file:close()
- end
- function load()
- local count = 0
- local file = io.open("stargates.txt", "r")
- if file then
- for line in file:lines() do
- table.insert(gates, ser.unserialize(line))
- count = count + 1
- os.sleep(0)
- end
- file:close()
- end
- end
- function buttonCallback(guiID, id)
- local result = gui.getYesNo("", "Do you really want to exit?", "")
- if result == true then
- gui.exit()
- end
- end
- function sgListCallback(guiID, listID, selectedID)
- if #gates > 0 then
- local address = gates[selectedID].address
- local location = gates[selectedID].location
- local status = gates[selectedID].status
- gui.setText(myGui, addressLabel, address)
- gui.setText(myGui, locationLabel, location)
- gui.setText(myGui, statusLabel, status)
- end
- end
- function refreshList()
- local entry = ""
- gui.clearList(myGui, sgList)
- for key,value in pairs(gates) do
- entry = string.format("%-10s %-10s", value.address, value.location)
- gui.insertList(myGui, sgList, entry)
- end
- gui.setSelected(myGui, sgList, 1)
- sgListCallback(myGui, sgList, 1)
- end
- function refreshStatus(autodial)
- if autodial == true then
- return
- end
- local selectedID = gui.getSelected(myGui, sgList)
- if selectedID > #gates then
- return
- end
- local address = gates[selectedID].address
- if sg.isConnected() == true then
- gui.setEnable(myGui, dialButton, false)
- gui.setEnable(myGui, autodialButton, false)
- gui.setEnable(myGui, disconnectButton, true)
- if autodial == false then
- gui.setText(myGui, isConnectedLabel, "Connected")
- end
- else
- gui.setEnable(myGui, dialButton, true)
- gui.setEnable(myGui, autodialButton, true)
- gui.setEnable(myGui, disconnectButton, false)
- if autodial == false then
- gui.setText(myGui, isConnectedLabel, "Idle")
- end
- gui.setBottom(" ")
- for i = 1, 9 do
- gui.setText(myGui, chevron[i], "open")
- end
- end
- if sg.isDialing() == true then
- gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
- end
- gui.setText(myGui, autodialLabel, getStartAddress())
- end
- function dialButtonCallback(guiID, id)
- local selectedID = gui.getSelected(guiID, sgList)
- if selectedID > #gates then
- return
- end
- local address = gates[selectedID].address
- if sg.isConnected() == false then
- gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
- sg.dial(address)
- end
- end
- function disconnectButtonCallback(guiID, id)
- if sg.isConnected() == true then
- sg.disconnect()
- end
- isAutodialing = false
- end
- local editRunning
- function editButtonCallback(guiID, id)
- local selectedID = gui.getSelected(guiID, sgList)
- if selectedID > #gates then
- return
- end
- gui.setText(editGui, editAddressText, gates[selectedID].address)
- gui.setText(editGui, editLocationText, gates[selectedID].location)
- editRunning = true
- while editRunning == true do
- gui.runGui(editGui)
- end
- gates[selectedID].address = gui.getText(editGui, editAddressText)
- gates[selectedID].location = gui.getText(editGui, editLocationText)
- _, gates[selectedID].status = gui.getSelected(editGui, editStatusList)
- refreshList()
- save()
- end
- function newButtonCallback(guiID, id)
- editRunning = true
- while editRunning == true do
- gui.runGui(editGui)
- end
- local address = gui.getText(editGui, editAddressText)
- local location = gui.getText(editGui, editLocationText)
- if address ~= "" and location ~= "" then
- local _, status = gui.getSelected(editGui, editStatusList)
- local value = {["address"] = address, ["location"] = location, ["status"] = status}
- table.insert(gates, value)
- end
- refreshList()
- save()
- end
- function deleteButtonCallback(guiID, id)
- local selectedID = gui.getSelected(guiID, sgList)
- local tmpTable = gates
- gates = {}
- for i = 1, #tmpTable do
- if i ~= selectedID then
- table.insert(gates, tmpTable[i])
- end
- end
- refreshList()
- save()
- end
- function editCloseCallback(guiID, id)
- editRunning = false
- end
- function eventIncomingCallback(e)
- if e == "sgIncoming" then
- callIncoming = true
- gui.setBottom("Incoming call")
- end
- event.listen("sgIncoming", eventCallback)
- end
- function eventOutgoingCallback(e)
- if e == "sgOutgoing" then
- callIncoming = false
- gui.setBottom("Outgoing call")
- end
- event.listen("sgOutgoing", eventCallback)
- end
- function eventChevronCallback(e, addr, ch)
- if e == "sgChevronEncode" then
- gui.setText(myGui, chevron[ch], "engaged")
- end
- event.listen("sgChevronEncode", eventCallback)
- end
- function saveActAddress()
- local file = io.open("sglist.tmp", "w")
- file:write(ser.serialize(startAddress) .. "\n")
- file:close()
- end
- function loadActAddress()
- local file = io.open("sglist.tmp", "r")
- if file then
- line = file:read("*line")
- startAddress = ser.unserialize(line)
- file:close()
- end
- end
- function getStartPos(number)
- local startPos = 1
- local s = startAddress[number]
- for key, value in pairs(radix) do
- if value == s then
- startPos = key
- end
- end
- return startPos
- end
- function searchGates()
- for Ai = getStartPos(1), #radix do
- local A = radix[Ai]
- for Bi = getStartPos(2), #radix do
- local B = radix[Bi]
- for Ci = getStartPos(3), #radix do
- local C = radix[Ci]
- for Di = getStartPos(4), #radix do
- local D = radix[Di]
- for Ei = getStartPos(5), #radix do
- local E = radix[Ei]
- for Fi = getStartPos(6), #radix do
- local F = radix[Fi]
- for Gi = getStartPos(7), #radix do
- local G = radix[Gi]
- for Hi = getStartPos(8), #radix do
- local H = radix[Hi]
- for Ii = getStartPos(9), #radix do
- local I = radix[Ii]
- gui.setText(myGui, autodialLabel, getStartAddress())
- local address = A .. B .. C .. D .. E .. F .. G .. H .. I
- gui.setText(myGui, isConnectedLabel, "Dialing " .. address)
- refreshStatus(true)
- gui.runGui(myGui)
- sg.dial(address)
- while sg.isDialing() == true do
- os.sleep(0.1)
- gui.runGui(myGui)
- end
- if sg.isConnected() == true and isAutodialing == true then
- local value = {["address"] = address, ["location"] = "unknown", ["status"] = "unknown"}
- table.insert(gates, value)
- save()
- refreshList()
- os.sleep(2)
- sg.disconnect()
- os.sleep(2)
- end
- startAddress[1] = A
- startAddress[2] = B
- startAddress[3] = C
- startAddress[4] = D
- startAddress[5] = E
- startAddress[6] = F
- startAddress[7] = G
- startAddress[8] = H
- startAddress[9] = I
- if isAutodialing == false then
- return
- end
- saveActAddress()
- for i = 1, 9 do
- gui.setText(myGui, chevron[i], "open")
- end
- gui.setText(myGui, autodialLabel, getStartAddress())
- end
- startAddress[9] = "A"
- end
- startAddress[8] = "A"
- end
- startAddress[7] = "A"
- end
- startAddress[6] = "A"
- end
- startAddress[5] = "A"
- end
- startAddress[4] = "A"
- end
- startAddress[3] = "A"
- end
- startAddress[2] = "A"
- end
- startAddress[1] = "A"
- end
- function autodialCallback()
- gui.setEnable(myGui, dialButton, false)
- gui.setEnable(myGui, autodialButton, false)
- gui.setEnable(myGui, disconnectButton, true)
- isAutodialing = true
- loadActAddress()
- searchGates()
- gui.setEnable(myGui, dialButton, true)
- gui.setEnable(myGui, autodialButton, true)
- gui.setEnable(myGui, disconnectButton, false)
- end
- load()
- myGui = gui.newGui(2, 2, 78, 23, true)
- button = gui.newButton(myGui, "center", 21, "exit", buttonCallback)
- gui.newFrame(myGui, 1, 1, 37, 4)
- gui.newLabel(myGui, 2, 2, "Local address : ")
- ownAddressLabel = gui.newLabel(myGui, 19, 2, sgAddress)
- gui.newLabel(myGui, 2, 3, "Status : ")
- isConnectedLabel = gui.newLabel(myGui, 19, 3, "not connected")
- gui.newFrame(myGui, 42, 1, 34, 19)
- sgList = gui.newList(myGui, 44, 2, 30, 9, {}, sgListCallback, "Known gates")
- gui.newLabel(myGui, 44, 12, "Address : ")
- addressLabel = gui.newLabel(myGui, 57, 12, "---------")
- gui.newLabel(myGui, 44, 13, "Location : ")
- locationLabel = gui.newLabel(myGui, 57, 13, "unknown")
- gui.newLabel(myGui, 44, 14, "Status : ")
- statusLabel = gui.newLabel(myGui, 57, 14, "unknown")
- dialButton = gui.newButton(myGui, 44, 16, "dial", dialButtonCallback)
- disconnectButton = gui.newButton(myGui, 53, 16, "disconnect", disconnectButtonCallback)
- editButton = gui.newButton(myGui, 68, 16, "edit", editButtonCallback)
- newButton = gui.newButton(myGui, 44, 18, "new", newButtonCallback)
- deleteButton = gui.newButton(myGui, 53, 18, "delete", deleteButtonCallback)
- gui.newFrame(myGui, 1, 5, 30, 11)
- gui.newLabel(myGui, 2, 6, "Chevron 1 : ")
- gui.newLabel(myGui, 2, 7, "Chevron 2 : ")
- gui.newLabel(myGui, 2, 8, "Chevron 3 : ")
- gui.newLabel(myGui, 2, 9, "Chevron 4 : ")
- gui.newLabel(myGui, 2, 10, "Chevron 5 : ")
- gui.newLabel(myGui, 2, 11, "Chevron 6 : ")
- gui.newLabel(myGui, 2, 12, "Chevron 7 : ")
- gui.newLabel(myGui, 2, 13, "Chevron 8 : ")
- gui.newLabel(myGui, 2, 14, "Chevron 9 : ")
- chevron[1] = gui.newLabel(myGui, 14, 6, "open")
- chevron[2] = gui.newLabel(myGui, 14, 7, "open")
- chevron[3] = gui.newLabel(myGui, 14, 8, "open")
- chevron[4] = gui.newLabel(myGui, 14, 9, "open")
- chevron[5] = gui.newLabel(myGui, 14, 10, "open")
- chevron[6] = gui.newLabel(myGui, 14, 11, "open")
- chevron[7] = gui.newLabel(myGui, 14, 12, "open")
- chevron[8] = gui.newLabel(myGui, 14, 13, "open")
- chevron[9] = gui.newLabel(myGui, 14, 14, "open")
- gui.newLabel(myGui, 2, 17, "Lanteacraft version : " .. sg.getInterfaceVersion())
- gui.newLabel(myGui, 2, 19, "Last autodial addr. : ")
- loadActAddress()
- autodialLabel = gui.newLabel(myGui, 24, 19, getStartAddress())
- autodialButton = gui.newButton(myGui, 2, 21, "autodial", autodialCallback)
- refreshList()
- editGui = gui.newGui("center", 4, 34, 14, true)
- editCloseButton = gui.newButton(editGui, "center", 12, "close", editCloseCallback)
- gui.newLabel(editGui, 2, 1, "Address :")
- editAddressText = gui.newText(editGui, 12, 1, 10, "")
- gui.newLabel(editGui, 2, 3, "Location :")
- editLocationText = gui.newText(editGui, 12, 3, 20, "")
- editStatusList = gui.newList(editGui, 2, 5, 30, 6, {"unknown", "safe", "dangerous"}, nil, "Status")
- gui.clearScreen()
- gui.setTop(prgName .. " " .. version)
- sg.disconnect()
- event.listen("sgIncoming", eventIncomingCallback)
- event.listen("sgOutgoing", eventOutgoingCallback)
- event.listen("sgChevronEncode", eventChevronCallback)
- while true do
- refreshStatus(false)
- gui.runGui(myGui)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement