Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function listPer(pname) --Returns list of peripherals matching pname
- local pers = peripheral.getNames()
- local perList = {}
- for i,v in pairs(pers) do
- if string.find(v, pname) then
- table.insert(perList, v)
- end
- end
- return perList
- end
- stargates = listPer("stargate")
- --monitors = listPer("monitor")
- --static variables(you can change them if needed)
- sg = peripheral.wrap(stargates[1])
- sg.disconnect()
- --mon = peripheral.wrap(monitors[1])
- sgd = "sgDatabase"
- gatelog = "gatelog.txt"
- t = 20
- --Log Gates
- function glog(text)
- if fs.exists(gatelog) == false then
- fileg = fs.open(gatelog, "w")
- fileg.write("--StarGate Logs--")
- fileg.close()
- end
- local fileg = fs.open(gatelog, "a")
- fileg.write("\n".. "Day:".. os.day() .."|".. textutils.formatTime(os.time()) .."|".. text)
- fileg.close()
- end
- --making my life easier
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- --sgDatabase
- function dWrite()
- local dbs = textutils.serialize(db)
- local file = fs.open(sgd, "w")
- file.write(dbs)
- file.close()
- end
- function dRead()
- local file = fs.open(sgd, "r")
- db = textutils.unserialize(file.readAll())
- return db
- end
- --checking if address is available
- function check()
- if sg.isValidAddress(addr) == true then
- print("Valid address")
- sleep(1)
- dial()
- elseif sg.isBusy(addr) == true then
- print("Address is busy")
- sleep(2)
- else
- print("Address not valid")
- sleep(2)
- end
- end
- --dials gate
- function dial()
- sg.connect(addr)
- clear()
- repeat
- writeC("Gate is dialling...", 9)
- sleep(1)
- clear()
- until not sg.isDialing()
- for i = t, 1, -1 do
- writeC("Closing gate in ".. i .." seconds", 9)
- sleep(1)
- clear()
- end
- writeC("Stargate disconnecting from ".. addr, 9)
- sg.disconnect()
- sleep(1)
- shell.run("startup")
- clear()
- end
- --incoming wormhole
- function iCheck()
- while true do
- local _,iAddr = os.pullEvent("sgIncoming")
- if db[iAddr] then
- glog(iAddr.." ".. db[iAddr].name)
- else
- glog(iAddr.." Unknown")
- end
- end
- end
- --GUI
- x,y = term.getSize()
- function writeC(text, line)
- term.setCursorPos((x/2) - (#text/2), line)
- write(text)
- local _,cy = term.getCursorPos()
- term.setCursorPos(1,cy + 1)
- end
- function header(text)
- term.setBackgroundColor(colors.gray)
- --term.setTextColor(colors.blue)
- for i = 1, 2 do
- term.setCursorPos(1,i)
- write(string.rep(" ",x))
- end
- writeC(text,1)
- term.setCursorPos(1,y)
- write(string.rep(" ",x))
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,4)
- end
- local function tc(tcolor,bcolor)
- if term.isColor() then
- if tcolor then
- term.setTextColor(colors[tcolor])
- end
- if bcolor then
- term.setBackgroundColor(colors[bcolor])
- end
- end
- end
- function selection()
- term.setCursorPos(1, (csel - mod) + (ind - 1))
- tc("yellow") term.write("[")
- tc("white") term.write(tempfo[csel])
- tc("yellow") term.write("]")
- term.setCursorPos(1,y)
- tc("white","gray")
- term.write("Page: ".. page + 1 .. "/" .. tpages .. "|Csel="..csel.."|MOD="..mod.."|AVA="..ava.."|"..csel / (ava + 1))
- tc("white","black")
- end
- function sp_drawgates()
- tempfo = {} rawName = {}
- c = 1
- for gid, data in pairs(db) do
- table.insert(tempfo, gid .." ".. data.name)
- table.insert(rawName, gid)
- c = c + 1
- end
- tpages = math.ceil(c / ava)
- mod = page * ava
- for i = 1, ava do
- term.setCursorPos(2, i + ind - 1)
- term.write(tempfo[i + mod])
- end
- end
- csel = 1
- page = 0
- ind = 3
- ava = y - ind
- --ava = 16
- function sp_guiloop()
- clear()
- header("Address Book")
- sp_drawgates()
- selection()
- e,key = os.pullEvent("key")
- if key == keys.q then
- finished = true
- end
- if key == keys.down then
- csel = csel + 1
- end
- if key == keys.up then
- csel = csel - 1
- end
- if key == keys.enter then
- clear()
- addr = rawName[csel]
- check()
- end
- if csel < 1 then
- csel = 1
- end
- if csel > #tempfo then
- csel = #tempfo
- end
- page = math.floor((csel - 1) / ava)
- end
- guit = {
- ["main"] = {
- draw = function()
- header("Main Menu")
- print("[1] Direct Dial")
- print("[2] Destinations")
- print("[3] Gate Log")
- end,
- options = {"direct_dial","destin","log"}
- },
- ["direct_dial"] = {
- run = function()
- header("Direct Dial")
- print("Where would you like to go?\n")
- write("Gate Address: ")
- addr = read()
- check()
- end,
- },
- ["destin"] = {
- draw = function()
- header("Destinations")
- print("[1] Main Menu")
- print("")
- print("[2] Address Book")
- print("")
- print("[3] Add")
- print("[4] Remove")
- end,
- options = {"main","destin_dial","destin_add","destin_remove"}
- },
- ["destin_dial"] = {},
- ["destin_add"] = {
- run = function()
- header("Destinations - Add")
- repeat
- write("Gate Serial: ")
- gser = string.upper(read())
- until sg.isValidAddress(gser) == true
- repeat
- print("\nDestination Name: ")
- gname = read()
- until #gname > 3
- db[gser] = {
- name = gname,
- }
- dWrite()
- end
- },
- ["destin_remove"] = {
- run = function()
- header("Destinations - Remove")
- write("Gate Serial to remove: ")
- gser = string.upper(read())
- if db[gser] then
- db[gser] = nil
- dWrite()
- print("\nGate Address Remove.")
- else
- print("\nGate Address not found.")
- end
- end,
- },
- ["log"] = {
- run = function()
- shell.run("edit", gatelog)
- end,
- },
- }
- function runGui()
- state = "main"
- while true do
- if state == "destin_dial" then
- finished = false
- repeat
- sp_guiloop()
- until finished == true
- state = oldState
- end
- clear()
- if guit[state].draw then
- oldState = state
- guit[state].draw()
- e,key = os.pullEvent("char")
- if tonumber(key) then
- key = tonumber(key)
- if key > 0 and key <= #guit[state].options then
- state = guit[state].options[key]
- end
- end
- else
- guit[state].run()
- state = oldState
- end
- end
- end
- if not fs.exists(sgd) then
- db = {}
- dWrite()
- end
- db = dRead()
- parallel.waitForAll(runGui,iCheck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement