Advertisement
dexman545

Dialing Computer v.2

Jan 22nd, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. local function listPer(pname) --Returns list of peripherals matching pname
  2. local pers = peripheral.getNames()
  3. local perList = {}
  4. for i,v in pairs(pers) do
  5. if string.find(v, pname) then
  6. table.insert(perList, v)
  7. end
  8. end
  9. return perList
  10. end
  11.  
  12. stargates = listPer("stargate")
  13. --monitors = listPer("monitor")
  14.  
  15.  
  16. --static variables(you can change them if needed)
  17. sg = peripheral.wrap(stargates[1])
  18. sg.disconnect()
  19. --mon = peripheral.wrap(monitors[1])
  20. sgd = "sgDatabase"
  21. gatelog = "gatelog.txt"
  22. t = 20
  23.  
  24. --Log Gates
  25. function glog(text)
  26. if fs.exists(gatelog) == false then
  27. fileg = fs.open(gatelog, "w")
  28. fileg.write("--StarGate Logs--")
  29. fileg.close()
  30. end
  31. local fileg = fs.open(gatelog, "a")
  32. fileg.write("\n".. "Day:".. os.day() .."|".. textutils.formatTime(os.time()) .."|".. text)
  33. fileg.close()
  34. end
  35.  
  36. --making my life easier
  37. function clear()
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. end
  41.  
  42. --sgDatabase
  43. function dWrite()
  44. local dbs = textutils.serialize(db)
  45. local file = fs.open(sgd, "w")
  46. file.write(dbs)
  47. file.close()
  48. end
  49. function dRead()
  50. local file = fs.open(sgd, "r")
  51. db = textutils.unserialize(file.readAll())
  52. return db
  53. end
  54.  
  55. --checking if address is available
  56. function check()
  57. if sg.isValidAddress(addr) == true then
  58. print("Valid address")
  59. sleep(1)
  60. dial()
  61. elseif sg.isBusy(addr) == true then
  62. print("Address is busy")
  63. sleep(2)
  64. else
  65. print("Address not valid")
  66. sleep(2)
  67. end
  68. end
  69.  
  70. --dials gate
  71. function dial()
  72. sg.connect(addr)
  73. clear()
  74. repeat
  75. writeC("Gate is dialling...", 9)
  76. sleep(1)
  77. clear()
  78. until not sg.isDialing()
  79. for i = t, 1, -1 do
  80. writeC("Closing gate in ".. i .." seconds", 9)
  81. sleep(1)
  82. clear()
  83. end
  84. writeC("Stargate disconnecting from ".. addr, 9)
  85. sg.disconnect()
  86. sleep(1)
  87. shell.run("startup")
  88. clear()
  89. end
  90.  
  91. --incoming wormhole
  92. function iCheck()
  93. while true do
  94. local _,iAddr = os.pullEvent("sgIncoming")
  95. if db[iAddr] then
  96. glog(iAddr.." ".. db[iAddr].name)
  97. else
  98. glog(iAddr.." Unknown")
  99. end
  100. end
  101. end
  102.  
  103. --GUI
  104. x,y = term.getSize()
  105. function writeC(text, line)
  106. term.setCursorPos((x/2) - (#text/2), line)
  107. write(text)
  108. local _,cy = term.getCursorPos()
  109. term.setCursorPos(1,cy + 1)
  110. end
  111. function header(text)
  112. term.setBackgroundColor(colors.gray)
  113. --term.setTextColor(colors.blue)
  114. for i = 1, 2 do
  115. term.setCursorPos(1,i)
  116. write(string.rep(" ",x))
  117. end
  118. writeC(text,1)
  119. term.setCursorPos(1,y)
  120. write(string.rep(" ",x))
  121. term.setBackgroundColor(colors.black)
  122. term.setTextColor(colors.white)
  123. term.setCursorPos(1,4)
  124. end
  125.  
  126. local function tc(tcolor,bcolor)
  127. if term.isColor() then
  128. if tcolor then
  129. term.setTextColor(colors[tcolor])
  130. end
  131. if bcolor then
  132. term.setBackgroundColor(colors[bcolor])
  133. end
  134. end
  135. end
  136. function selection()
  137. term.setCursorPos(1, (csel - mod) + (ind - 1))
  138. tc("yellow") term.write("[")
  139. tc("white") term.write(tempfo[csel])
  140. tc("yellow") term.write("]")
  141. term.setCursorPos(1,y)
  142. tc("white","gray")
  143. term.write("Page: ".. page + 1 .. "/" .. tpages .. "|Csel="..csel.."|MOD="..mod.."|AVA="..ava.."|"..csel / (ava + 1))
  144. tc("white","black")
  145. end
  146.  
  147. function sp_drawgates()
  148. tempfo = {} rawName = {}
  149. c = 1
  150. for gid, data in pairs(db) do
  151. table.insert(tempfo, gid .." ".. data.name)
  152. table.insert(rawName, gid)
  153. c = c + 1
  154. end
  155.  
  156. tpages = math.ceil(c / ava)
  157. mod = page * ava
  158.  
  159. for i = 1, ava do
  160. term.setCursorPos(2, i + ind - 1)
  161. term.write(tempfo[i + mod])
  162. end
  163. end
  164. csel = 1
  165. page = 0
  166. ind = 3
  167. ava = y - ind
  168. --ava = 16
  169. function sp_guiloop()
  170. clear()
  171. header("Address Book")
  172. sp_drawgates()
  173. selection()
  174.  
  175. e,key = os.pullEvent("key")
  176.  
  177. if key == keys.q then
  178. finished = true
  179. end
  180.  
  181. if key == keys.down then
  182. csel = csel + 1
  183. end
  184. if key == keys.up then
  185. csel = csel - 1
  186. end
  187. if key == keys.enter then
  188. clear()
  189. addr = rawName[csel]
  190. check()
  191. end
  192. if csel < 1 then
  193. csel = 1
  194. end
  195. if csel > #tempfo then
  196. csel = #tempfo
  197. end
  198.  
  199. page = math.floor((csel - 1) / ava)
  200. end
  201.  
  202. guit = {
  203. ["main"] = {
  204. draw = function()
  205. header("Main Menu")
  206. print("[1] Direct Dial")
  207. print("[2] Destinations")
  208. print("[3] Gate Log")
  209. end,
  210. options = {"direct_dial","destin","log"}
  211. },
  212. ["direct_dial"] = {
  213. run = function()
  214. header("Direct Dial")
  215. print("Where would you like to go?\n")
  216. write("Gate Address: ")
  217. addr = read()
  218. check()
  219. end,
  220. },
  221.  
  222. ["destin"] = {
  223. draw = function()
  224. header("Destinations")
  225. print("[1] Main Menu")
  226. print("")
  227. print("[2] Address Book")
  228. print("")
  229. print("[3] Add")
  230. print("[4] Remove")
  231. end,
  232. options = {"main","destin_dial","destin_add","destin_remove"}
  233. },
  234. ["destin_dial"] = {},
  235. ["destin_add"] = {
  236. run = function()
  237. header("Destinations - Add")
  238. repeat
  239. write("Gate Serial: ")
  240. gser = string.upper(read())
  241. until sg.isValidAddress(gser) == true
  242.  
  243. repeat
  244. print("\nDestination Name: ")
  245. gname = read()
  246. until #gname > 3
  247.  
  248. db[gser] = {
  249. name = gname,
  250. }
  251. dWrite()
  252.  
  253. end
  254. },
  255. ["destin_remove"] = {
  256. run = function()
  257. header("Destinations - Remove")
  258. write("Gate Serial to remove: ")
  259. gser = string.upper(read())
  260. if db[gser] then
  261. db[gser] = nil
  262. dWrite()
  263. print("\nGate Address Remove.")
  264. else
  265. print("\nGate Address not found.")
  266. end
  267. end,
  268. },
  269. ["log"] = {
  270. run = function()
  271. shell.run("edit", gatelog)
  272. end,
  273. },
  274. }
  275. function runGui()
  276. state = "main"
  277. while true do
  278. if state == "destin_dial" then
  279. finished = false
  280. repeat
  281. sp_guiloop()
  282. until finished == true
  283. state = oldState
  284. end
  285.  
  286. clear()
  287. if guit[state].draw then
  288. oldState = state
  289. guit[state].draw()
  290. e,key = os.pullEvent("char")
  291.  
  292. if tonumber(key) then
  293. key = tonumber(key)
  294. if key > 0 and key <= #guit[state].options then
  295. state = guit[state].options[key]
  296. end
  297. end
  298. else
  299. guit[state].run()
  300. state = oldState
  301. end
  302. end
  303. end
  304.  
  305. if not fs.exists(sgd) then
  306. db = {}
  307. dWrite()
  308. end
  309. db = dRead()
  310.  
  311. parallel.waitForAll(runGui,iCheck)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement