View difference between Paste ID: 5R8W7pkk and VHXZ1p3N
SHOW: | | - or go back to the newest paste.
1
-- **************************************************************** --
2
--  local variables
3
-- **************************************************************** --
4
5
local w,h = term.getSize() -- 51, 19
6
local select = 1
7
local verNum = 1.01
8
local m,s, timeUpdated = "00","00", false
9
local logs = {}
10
local firstTime = not fs.exists('STARGATE')
11
local gates = { }
12
13
-- **************************************************************** --
14
--  Download and load apis, files, and directories
15
-- **************************************************************** --
16
17
if not http then
18
	print("Stargate DHD requires http API")
19
	print("Set enableAPI_http to true in ComputerCraft.cfg")
20
	return
21
end
22
23
function Initialize()
24
	if not fs.exists("dropbox") then
25
		shell.run('pastebin get L9iRRjg6 dropbox')
26
		term.clear() -- **** need to fix GUI prob **** --
27
		table.insert(logs, 'Downloaded Dropbox Downloader')
28
		rollingLogs()
29
	end
30
	if not fs.exists('STARGATE') then
31
		fs.makeDir('STARGATE')
32
		table.insert(logs, 'Creating directory STARGATE')
33
		rollingLogs()
34
	end
35
	if not fs.exists('STARGATE/apis') then
36
		fs.makeDir('STARGATE/apis')
37
		table.insert(logs, 'Creating directory apis')
38
		rollingLogs()
39
	end	
40
	if not fs.exists('STARGATE/Settings') then
41
		fs.makeDir('STARGATE/Settings')
42
		table.insert(logs, 'Creating directory Settings')
43
		rollingLogs()
44
	end	
45
	if not fs.exists("STARGATE/apis/wrap") then
46
		shell.run("dropbox Simple%20Wrap.lua CC%20Porgrams 126735377 STARGATE/apis/wrap")
47
		table.insert(logs, 'Downloading api: Simple Wrap.lua from dropbox')
48
		rollingLogs()
49
	end
50
	if not fs.exists("STARGATE/apis/Downloader") then
51
		shell.run("dropbox Downloader.lua CC%20Porgrams 126735377 STARGATE/apis/Downloader")
52
		table.insert(logs, 'Downloading api: Downloader.lua from dropbox')
53
		rollingLogs()
54
	end
55
	os.loadAPI("STARGATE/apis/wrap")
56
	table.insert(logs, 'Loading api: wrap')
57
	rollingLogs()
58
	os.loadAPI("STARGATE/apis/Downloader")
59
	table.insert(logs, 'Loading api: Downloader')
60
	rollingLogs()
61
	if not fs.exists('STARGATE/Settings/Timing') then
62
		local f = fs.open('STARGATE/Settings/Timing', 'w')
63
			f.writeLine(14)
64
		f.close()
65
		table.insert(logs, 'Setting cumulative update time to 15 min')
66
		rollingLogs()		
67
	end
68
	if not fs.exists('STARGATE/Settings/onlineInfo') then
69
		local f = fs.open('STARGATE/Settings/onlineInfo', 'w')
70
			f.writeLine("Dropbox or Pastebin")
71
			f.writeLine("link or code")
72
		f.close()
73
		table.insert(logs, 'Setting online cumulative update location')
74
		rollingLogs()	
75
	end
76
	if not fs.exists('STARGATE/Settings/AddressList') then
77
		local f = fs.open('STARGATE/Settings/AddressList', 'w')
78
			f.writeLine("Stargate_Address_List = {")
79
			f.writeLine("}")
80
		f.close()
81
		table.insert(logs, 'Making blank Address List')
82
		rollingLogs()	
83
	end	
84
end
85
	
86
-- **************************************************************** --
87
--  Some Functions
88
-- **************************************************************** --
89
90
function dial(address)
91
	local gate = wrap.attach("Abstract Bus Adapter")
92
	if gate then
93
		gate.sendPacket("FFFF", "action: dial", "address: "..address)
94
	end
95
end
96
97
function disconnect()
98
	local gate = wrap.attach("Abstract Bus Adapter")
99
	if gate then
100
		gate.sendPacket("FFFF", "action: disconnect")
101
	end
102
end
103
104
function settings_Timing(newTime)
105
	if newTime then 
106
		local f = fs.open('STARGATE/Settings/Timing', 'w')
107
			f.writeLine(newTime)
108
		f.close()
109
		m, s, timeUpdated = newTime, "00", true
110
	else
111
		local f = fs.open('STARGATE/Settings/Timing', 'r')
112
			local Time = f.readLine()
113
		f.close()
114
		return math.floor(Time)
115
	end
116
end	
117
118
function settings_OnlineInfo(Dropbox_Pastebin, info)
119
	if info then
120
		local f = fs.open('STARGATE/Settings/onlineInfo', 'w')
121
			if Dropbox_Pastebin == "Dropbox" and string.find(info,"www.") then
122
				local str = strsplit("www.", info)
123
				info = str[1].."dl."..str[2]
124
			end		
125
			f.writeLine(Dropbox_Pastebin)
126
			f.writeLine(info)
127
		f.close()
128
	else
129
		local f = fs.open('STARGATE/Settings/onlineInfo', 'r')
130
			local Paste_Drop = f.readLine()
131
			local location = f.readLine()
132
		f.close()
133
		return Paste_Drop, location
134
	end	
135
end
136
137
function updateAdressList()
138
	os.unloadAPI("STARGATE/Settings/AddressList")
139
	local name, info = settings_OnlineInfo()
140
	Downloader.get(name, info)
141
	os.loadAPI("STARGATE/Settings/AddressList")
142
	if AddressList.Stargate_Address_List then
143
		gates = AddressList.Stargate_Address_List
144
	end
145
	updateGateMenu()
146
end
147
148
function updateSG_DHD()
149
	local file = http.get("https://dl.dropboxusercontent.com/u/126735377/CC%20Porgrams/Stargate%20DHD.lua")
150
	local program = file.readAll()
151
	file.close() 
152
	if not string.find(program,"local verNum = "..verNum) then
153
		term.clear()
154
		drawHeaderFooter()
155
		printCentered("O----------------O", 7, 1)
156
		printCentered("O----------------O", 9, 1)
157
		local update = {".   ","..  ","... ","...."}
158
		for i=1,#update do
159
			printCentered("|  updating"..update[i].."  |", 8, 1)
160
			sleep(1)
161
		end
162
		local dir = shell.getRunningProgram()
163
		fs.delete(dir)
164
		local f = fs.open(dir, "w")
165
			f.write(program)
166
		f.close()
167
		shell.run(dir)
168
	end
169
end
170
171
function strsplit( pat, str )
172
	local t = {}
173
	local fpat = "(.-)" .. pat
174
	local last_end = 1
175
	local s, e, cap = str:find(fpat, 1)
176
	while s do
177
		if s ~= 1 or cap ~= "" then
178
			table.insert(t,cap)
179
		end
180
		last_end = e+1
181
		s, e, cap = str:find(fpat, last_end)
182
	end
183
	if last_end <= #str then
184
		cap = str:sub(last_end)
185
		table.insert(t, cap)
186
	end
187
	return t
188
end
189
190
function rollingLogs()
191
	if firstTime then
192
		term.clear()
193
		drawHeaderFooter()
194
		printCentered("Doing some things...",8, 1)
195
		printCentered("O"..string.rep("-", w-6).."O",9, 1)
196
		for i=1, 3 do
197
			printCentered("|"..string.rep(" ", w-6).."|",9+i, 1)
198
		end
199
		printCentered("O"..string.rep("-", w-6).."O",13, 1)
200
		while #logs > 3 do
201
			table.remove(logs, 1)
202
		end
203
		for i=1, #logs do
204
			printCentered(logs[i], 9+i, 1)
205
		end
206
		sleep(1)
207
	end
208
end
209
210
-- **************************************************************** --
211
--  GUI 
212
-- **************************************************************** --
213
214
--Some draw functions to get us started
215
function printCentered(str, ypos, offset)
216
	if not offset then offset = 0 end
217
	term.setCursorPos(w/2 - #str/2+offset, ypos)
218
	term.write(str)
219
end
220
221
function printRight(str, ypos)
222
	term.setCursorPos(w - #str, ypos)
223
	term.write(str)
224
end
225
226
function printCollumRow(str, ypos, num, offset)
227
	if num == 1 then
228
		term.setCursorPos(1+offset,ypos)
229
		term.write(str)
230
	elseif num == 2 then
231
		term.setCursorPos(w/2 - #str/2, ypos)
232
		term.write(str)
233
	elseif num == 3 then
234
		term.setCursorPos((w - #str)-offset+1, ypos)
235
		term.write(str)
236
	end
237
end
238
239
--draw menu functions
240
function drawHeaderFooter()
241
	printCentered("O"..string.rep("-", w-2).."O", 1, 1)
242
	printCentered("O  Stargate D.H.D.  O", 1)
243
	printCentered("| Cumulative Update |", 2)
244
	printCentered("O-----< "..m..":"..s.." >-----O", 3)
245
	printCentered("O"..string.rep("-", w-2).."O", h-1, 1)
246
	for i=2, h-2 do
247
		term.setCursorPos(1,i)
248
		term.write("|")
249
		term.setCursorPos(w,i)
250
		term.write("|")		
251
	end
252
	term.setCursorPos(2,h)
253
	term.write("By: PaintPauller")	
254
	printRight("Version: "..verNum, h)
255
end
256
257
function drawMain()
258
	local toPrint = { "Dial Stargate", "Disconnect Stargate", "Settings", "Force Update"}
259
	local num = 1
260
	for i=6, #toPrint*4, 3 do
261
		printCentered(toPrint[num], i)
262
		if select == num then
263
			printCentered("O"..string.rep("-", #toPrint[num]).."O", i+1)
264
		end
265
		num = num + 1
266
	end
267
	printRight("Quit", h-2)
268
	if select == #toPrint+1 then
269
		printRight("> Quit <", h-2)
270
	end
271
end
272
273
function drawDiall()
274
	local collum = 1
275
	local row = 5
276
	for  i=1, #gates do
277
		if i>18 then break end
278
		printCollumRow(gates[i].name, row, collum, 2)
279
		if select == i then
280
			printCollumRow("O"..string.rep("-", #gates[i].name).."O", row+1, collum, 1)
281
		end		
282
		row = row + 2
283
		if row >= 16 then
284
			row = 5
285
			collum = collum + 1
286
		end 
287
	end
288
	printRight("Back", h-2)
289
	if select == #gates+1 or select == 19 then
290
		printRight("> Back <", h-2)
291
	end
292
end
293
294
function drawSettingsEditor()
295
	printCentered("O"..string.rep("-", 42).."O", h-6, 1)
296
	printCentered("|"..string.rep(" ", 42).."|", h-5, 1)
297
	printCentered("O"..string.rep("-", 42).."O", h-4, 1)
298
end
299
300
function drawSettings()
301
	local name, location = settings_OnlineInfo()
302
	if #location > 31 then
303
		location = string.sub(location, 1, 30).."..."
304
	end
305
	printCollumRow("Change Cumulative Update Timing", 5, 1, 3)
306
	printCollumRow("Change Online Location", 8, 1, 3)
307
	
308
	printCollumRow("Minutes: "..settings_Timing()+1, 6, 1, 5)
309
	printCollumRow(name, 9, 1, 5)
310
	printCollumRow("Location: "..location, 10, 1, 5)
311
	printRight("Back", h-2)
312
	settings_OnlineInfo(Dropbox_Pastebin, info)
313
	if select == 1 then
314
		printCollumRow("> Minutes: "..settings_Timing()+1, 6, 1, 3)
315
	elseif select == 2 then
316
		printCollumRow("> "..name, 9, 1, 3)
317
	elseif select == 3 then
318
		printCollumRow("> Location: "..location, 10, 1, 3)
319
	elseif select == 4 then	
320
		printRight("> Back <", h-2)
321
	end
322
end
323
324
-- Menu state
325
local menustate = "Main"
326
local menuOpt = {
327
	['Main'] = {
328
		options = { "Dial a Stargate", "Disconnect Stargate", "Settings", "Force Update", "Quit" },
329
		draw = drawMain
330
	},
331
	["Dial a Stargate"] = {
332
		options = { }, --filled in later
333
		draw = drawDiall
334
	},
335
	['Settings'] = {
336
		options = { "Minute", "DropBox_Pastebin", "Online Location", "Main" },
337
		draw = drawSettings
338
	},
339
}
340
341
function updateGateMenu()
342
	menuOpt["Dial a Stargate"].options = { }
343
	for i=1, #gates do
344
		if i>18 then break end
345
		table.insert(menuOpt["Dial a Stargate"].options, gates[i].name)
346
	end
347
	table.insert(menuOpt["Dial a Stargate"].options, 'Main')
348
end
349
updateGateMenu()
350
351
-- Run 
352
function timer()
353
	while true do
354
		updateAdressList()
355
		for mc=settings_Timing(), 0, -1 do
356
			for sc =60, 0, -1 do
357
				m = mc
358
				s = sc
359
				if s < 10 then s = tostring("0"..s) end
360
				if m < 10 then m = tostring("0"..m) end
361
				local old_x, old_y = term.getCursorPos()
362
				printCentered("O-----< "..m..":"..s.." >-----O", 3)
363
				term.setCursorPos(old_x, old_y)
364
				if not timeUpdated then sleep(1) end
365
			end
366
			if timeUpdated then sleep(.00001) end
367
		end
368
		timeUpdated = false
369
	end
370
end
371
372
function runMenu()
373
	while true do
374
		local acted = false
375
		term.clear()
376
		drawHeaderFooter()
377
		menuOpt[menustate].draw()
378
		
379
	--[[ -- Debug Start --
380
		term.setCursorPos(1,h)
381
		term.clearLine()
382
		printRight(menustate, h)
383
		term.setCursorPos(2,h)
384
		term.write(tostring(menuOpt[menustate].options[select])..": "..select)
385
	--]] -- Debug End --
386
	
387
		local id, key = os.pullEvent("key")
388
		-- 203: left, 205: right, 200: up, 208: down, 28: enter
389
		
390
		if key == 200 and select > 1 then 
391
			select = select-1
392
		elseif key == 200 and select == 1 then 
393
			select = #menuOpt[menustate].options
394
		elseif key == 208 and select < #menuOpt[menustate].options then 
395
			select = select+1
396
		elseif key == 208 and select == #menuOpt[menustate].options then
397
			select = 1
398
		elseif key == 203 and select == 19 and menustate == "Dial a Stargate" then
399
			select = 1
400
		elseif key == 203 and select < 7 and menustate == "Dial a Stargate" then
401
			select = select+12
402
			if select > #menuOpt[menustate].options then select = #menuOpt[menustate].options end
403
		elseif key == 203 and select > 6 and menustate == "Dial a Stargate" then
404
			select = select-6
405
		elseif key == 205 and select == 19 and menustate == "Dial a Stargate" then
406
			select = 1			
407
		elseif key == 205 and select < 13 and menustate == "Dial a Stargate" then
408
			select = select+6
409
			if select > #menuOpt[menustate].options then select = #menuOpt[menustate].options end
410
		elseif key == 205 and select > 12 and menustate == "Dial a Stargate" then
411
			select = select-12
412
		elseif key == 28 then
413
			if menuOpt[menustate].options[select] == "Quit" then break end
414
			if menuOpt[menustate].options[select] == "Force Update" then
415
				updateAdressList()
416
				acted = true
417
			end disconnect()
418
			if menuOpt[menustate].options[select] == "Disconnect Stargate" and not acted then
419
				disconnect()
420
				acted = true
421
			end 		
422
			for name, info in pairs(menuOpt) do
423
				if menuOpt[menustate].options[select] == name and not acted then
424
					menustate = menuOpt[menustate].options[select]
425
					select = 1
426
					acted = true
427
				end
428
			end
429
			if menustate == "Dial a Stargate" and not acted then
430
				dial(gates[select].address)
431
				sleep(1)
432
				menustate = "Main"
433
				acted = true
434
				
435
			end
436
			if menustate == "Settings" and not acted then
437
				if select == 1 then
438
					drawSettingsEditor()				
439
					term.setCursorPos(5,h-5)
440
					local command = tostring(read())
441
					local Ncommand = tonumber(command)
442
					if type(Ncommand) == 'number' and Ncommand <= 99 and Ncommand >= 1 then 
443
						term.setCursorPos(5,h-5)
444
						term.write('Updateing...')
445
						settings_Timing(math.floor(Ncommand-1))
446
						while timeUpdated do sleep(.00001) end
447
					else
448
						term.setCursorPos(5,h-5)
449
						term.write('Invalid number!')
450
						sleep(1)
451
					end
452
					acted = true
453
				elseif select == 2 then
454
					local name, location = settings_OnlineInfo()
455
					if name == "Dropbox" then 
456
						settings_OnlineInfo("Pastebin", location)
457
					else
458
						settings_OnlineInfo("Dropbox", location)
459
					end
460
					acted = true
461
				elseif select == 3 then
462
					local name, location = settings_OnlineInfo()
463
					drawSettingsEditor()
464
					term.setCursorPos(5,h-7)
465
					if name == "Dropbox" then
466
						term.write("Paste the Dropbox link below.")
467
					elseif name == "Pastebin" then
468
						term.write("Paste the Pastebin code below.")
469
					end
470
					term.setCursorPos(5,h-5)
471
					local command = tostring(read())
472
					drawSettings()
473
					drawSettingsEditor()
474
					term.setCursorPos(5,h-5)
475
					term.write('Updateing...')					
476
					settings_OnlineInfo(name, command)
477
					sleep(1)
478
					updateAdressList()
479
					acted = true
480
				end
481
			end
482
		end
483
	end
484
end
485
486
Initialize()
487
updateSG_DHD()
488
if settings_OnlineInfo() == "Dropbox or Pastebin" then
489
	menustate = "Settings"
490
	select = 2
491
end
492
parallel.waitForAny( timer, runMenu )
493
term.clear()
494
term.setCursorPos(1,1)