Wolvan

Wolvan's Super Controller - Host

Mar 30th, 2012
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. os.pullEvent=os.pullEventRaw
  2. width, height = term.getSize()
  3. -- Start of persistent variables
  4. --Copyright (C) 2012 Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
  5. function exists(name)
  6.     if fs.exists('.vars/'..name..'.txt') then
  7.         return true
  8.     end
  9. end
  10.  
  11. function getType(name)
  12.     if exists(name) then
  13.         local file = io.open('.vars/'..name..'.txt', 'r')
  14.         local line = file:read('*l')
  15.         file:close()
  16.         return line
  17.     else
  18.         print('var.getType(): Variable does not exist')
  19.         error()
  20.     end
  21. end
  22.  
  23. function getValue(name)
  24.     if exists(name) then
  25.         if getType(name) ~= 'table' then
  26.             local file = io.open('.vars/'..name..'.txt', 'r')
  27.             local line = file:read('*l')
  28.             line = file:read('*l')
  29.             file:close()
  30.             if getType(name) == 'number' then
  31.                 return tonumber(line)
  32.             elseif getType(name) == 'string' then
  33.                 return line
  34.             elseif getType(name) == 'boolean' then
  35.                 if line == 'true' then
  36.                     return true
  37.                 elseif line == 'false' then
  38.                     return false
  39.                 end
  40.             elseif getType(name) == 'nil' then
  41.                 return nil
  42.             end
  43.         else
  44.             local file = io.open('.vars/'..name..'.txt', 'r')
  45.             local line = file:read('*l')
  46.             local lines = {}
  47.             local returnTable = {}
  48.             local i = 1
  49.             line = file:read('*l')
  50.             while line ~= nil do
  51.                 lines[i] = line
  52.                 line = file:read('*l')
  53.                 i = i + 1
  54.             end
  55.  
  56.             for _i = 1, #lines do
  57.                 local pos = string.find(lines[_i], '_')
  58.                 local _type = string.sub(lines[_i], 1, pos - 1)
  59.                 local value = string.sub(lines[_i], pos + 1)
  60.                 if _type == 'number' then
  61.                     returnTable[_i] = tonumber(value)
  62.                 elseif _type == 'string' then
  63.                     returnTable[_i] = value
  64.                 elseif _type == 'boolean' then
  65.                     if value == 'true' then
  66.                         returnTable[_i] = true
  67.                     elseif value == 'false' then
  68.                         returnTable[_i] = false
  69.                     end
  70.                 elseif _type == 'nil' then
  71.                     returnTable[_i] = nil
  72.                 elseif _type == 'table' then
  73.                     print('var.getValue(): You cannot have a table inside a table')
  74.                 end
  75.             end
  76.  
  77.             return returnTable
  78.         end
  79.     else
  80.         print('var.getValue(): Variable does not exist')
  81.         error()
  82.     end
  83. end
  84.  
  85. function setType(name, _type)
  86.     if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  87.         print('var.setType(): Unsupported type, or the variable already exists')
  88.         error()
  89.     else
  90.         local file = io.open('.vars/'..name..'.txt', 'w')
  91.         file:write(_type)
  92.         file:close()
  93.     end
  94. end
  95.  
  96. function setValue(name, value)
  97.     if exists(name) then
  98.         if tostring(type(value)) ~= getType(name) then
  99.             print('var.setValue(): Wrong type: '..type(value))
  100.             error()
  101.         end
  102.         if getType(name) ~= 'table' then
  103.             local file = io.open('.vars/'..name..'.txt', 'r')
  104.             local firstLine = file:read('*l')
  105.             file:close()
  106.             local file = io.open('.vars/'..name..'.txt', 'w')
  107.             file:write(firstLine..'\n'..tostring(value))
  108.             file:close()
  109.         else
  110.             local file = io.open('.vars/'..name..'.txt', 'r')
  111.             local firstLine = file:read('*l')
  112.             file:close()
  113.             local file = io.open('.vars/'..name..'.txt', 'w')
  114.             file:write(firstLine..'\n')
  115.             for i = 1, #value do
  116.                 file:write(type(value[i])..'_'..tostring(value[i])..' \n')
  117.             end
  118.             file:close()
  119.         end
  120.     else
  121.         print('var.setValue(): Variable does not exist')
  122.         error()
  123.     end
  124. end
  125.  
  126. function create(name, _type)
  127.     if not fs.exists('.vars') then fs.makeDir('.vars') end
  128.     if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  129.         print('var.create(): Unsupported type, or the variable already exists')
  130.         error()
  131.     else
  132.         local file = io.open('.vars/'..name..'.txt', 'w')
  133.         file:write(_type)
  134.         file:close()
  135.     end
  136. end
  137.  
  138. function delete(name)
  139.     if exists(name) then
  140.         fs.delete('.vars/'..name..'.txt')
  141.     else
  142.         print('var.delete(): Variable does not exist')
  143.         error()
  144.     end
  145. end
  146. -- End of persistant variables
  147. local int = 1
  148. function newi(inp)
  149. if inp == "reset" then
  150. int = 1
  151. elseif inp == "more" then
  152. int = int + 1
  153. if int > 5 then
  154. int = 1
  155. end
  156. elseif inp == "less" then
  157. int = int - 1
  158. if int < 1 then
  159. int = 5
  160. end
  161. end
  162. draw()
  163. end
  164. function fc()
  165. shell.run("clear")
  166. print("Where is the Redstone-Output for the PCs located?")
  167. write("PCs: ")
  168. pcside = read()
  169. print("Where is the Redstone-Output for the Lamps located?")
  170. write("Lamps: ")
  171. lside = read()
  172. print("On logout?")
  173. print("[1]Turn off lamps and disable PCs")
  174. print("[2]PCs and lamps stay the way they are")
  175. write("On logout: ")
  176. onlogout = read()
  177. if onlogout == "1" then onlogout = 1
  178. elseif onlogout == "2" then onlogout = 2
  179. else onlogout = 1 end
  180. print("Set a terminal Login")
  181. write("User: ")
  182. user = read()
  183. write("Password: ")
  184. pass1 = read("*")
  185. write("Confirm: ")
  186. pass2 = read("*")
  187. if pass1 == pass2 then
  188. shell.run("clear")
  189. print("Are these options correct?")
  190. print("")
  191. print("PC-Side: "..pcside)
  192. print("Lamp-Side: "..lside)
  193. write("On Logout: ")
  194. if onlogout == 1 then
  195. print("Turn off everything")
  196. elseif onlogout == 2 then
  197. print("Keep state of devices")
  198. end
  199. print("Username: "..user)
  200. print("")
  201. write("y/n: ")
  202. confirm = read()
  203. if confirm == "Y" or confirm == "y" then
  204. if exists ("pass") then delete("pass") end
  205. if exists ("fconf") then delete("fconf") end
  206. if exists ("pcside") then delete("pcside") end
  207. if exists ("user") then delete("user") end
  208. if exists ("lside") then delete("lside") end
  209. if exists ("logout") then delete("logout") end
  210. print("Saving config")
  211. create("logout", "number")
  212. create("pass", "string")
  213. create("fconf", "string")
  214. create("pcside", "string")
  215. create("user", "string")
  216. create("lside", "string")
  217. setValue("logout", onlogout)
  218. setValue("pcside", pcside)
  219. setValue("lside", lside)
  220. setValue("user", user)
  221. setValue("pass", pass1)
  222. setValue("fconf", "true")
  223. os.reboot()
  224. else
  225. os.reboot()
  226. end
  227. else
  228. print("Passwords do not match")
  229. sleep(2)
  230. os.reboot()
  231. end
  232. end
  233. function draw()
  234. shell.run("clear")
  235. print("What do you want to do?")
  236. print("")
  237. if int == 1 then
  238. print("<Unlock computers>")
  239. print(" Change lights")
  240. print(" Reconfigure")
  241. print(" Show Current State")
  242. print(" Logout")
  243. elseif int == 2 then
  244. print(" Unlock computers")
  245. print("<Change lights>")
  246. print(" Reconfigure")
  247. print(" Show Current State")
  248. print(" Logout")
  249. elseif int == 3 then
  250. print(" Unlock computers")
  251. print(" Change lights")
  252. print("<Reconfigure>")
  253. print(" Show Current State")
  254. print(" Logout")
  255. elseif int == 4 then
  256. print(" Unlock computers")
  257. print(" Change lights")
  258. print(" Reconfigure")
  259. print("<Show Current State>")
  260. print(" Logout")
  261. elseif int == 5 then
  262. print(" Unlock computers")
  263. print(" Change lights")
  264. print(" Reconfigure")
  265. print(" Show Current State")
  266. print("<Logout>")
  267. end
  268. print("")
  269. print("")
  270. print("[UP][DOWN] - Navigate")
  271. print("[ENTER] - Select")
  272. print("[S] - Shutdown the terminal")
  273. listen()
  274. end
  275. function listen()
  276. while true do
  277. event, key = os.pullEventRaw('key')
  278. if key == 200 then
  279. newi('less')
  280. elseif key == 208 then
  281. newi('more')
  282. elseif key == 31 then
  283. os.shutdown()
  284. elseif key == 28 then
  285. domenuentry()
  286. end
  287. end
  288. end
  289. pc = false
  290. function pct()
  291. shell.run("clear")
  292. if pc == false then
  293. print("Activate Computers...")
  294. rs.setOutput(getValue("pcside"), true)
  295. pc = true
  296. print("Booting PCs enabled")
  297. elseif pc == true then
  298. print("Deactivate Computers...")
  299. rs.setOutput(getValue("pcside"), false)
  300. pc = false
  301. print("Booting PCs disabled")
  302. end
  303. sleep(2)
  304. newi("reset")
  305. end
  306. light = false
  307. function tl()
  308. shell.run("clear")
  309. if light == false then
  310. print("Turning on the lights...")
  311. rs.setOutput(getValue("lside"), true)
  312. light = true
  313. elseif light == true then
  314. print("Turning off the Lights...")
  315. rs.setOutput(getValue("lside"), false)
  316. light = false
  317. end
  318. print("Light toggled")
  319. sleep(2)
  320. newi("reset")
  321. end
  322. function show()
  323. shell.run("clear")
  324. print("PC-Side: "..getValue("pcside"))
  325. print("Lamp-Side: "..getValue("lside"))
  326. print("Lamps on: "..tostring(light))
  327. print("PCs active: "..tostring(pc))
  328. write("On logout: ")
  329. if getValue("logout") == 1 then
  330. print("Turn everything off")
  331. elseif getValue("logout") == 2 then
  332. print("Keep State")
  333. end
  334. print("Username: "..getValue("user"))
  335. shell.run("time")
  336. print("Press any key to continiue...")
  337. while true do
  338. event, key = os.pullEventRaw('key')
  339. if key == 1 then
  340. else
  341. newi("reset")
  342. end
  343. end
  344. end
  345. function logout()
  346. if getValue("logout") == 1 then
  347. os.reboot()
  348. elseif getValue("logout") == 2 then
  349. login()
  350. end
  351. end
  352. function domenuentry()
  353. if int == 1 then pct()
  354. elseif int == 2 then tl()
  355. elseif int == 3 then fc()
  356. elseif int == 4 then show()
  357. elseif int == 5 then logout()
  358. end
  359. end
  360. function login()
  361. shell.run("clear")
  362. if exists("fconf") then
  363. term.setCursorPos(width/2 - 12, height/2 - 2)
  364. print("+----------------------+")
  365. term.setCursorPos(width/2 - 12, height/2 - 1)
  366. print("|Username:             |")
  367. term.setCursorPos(width/2 - 12, height/2)
  368. print("|Password:             |")
  369. term.setCursorPos(width/2 - 12, height/2 + 1)
  370. print("+----------------------+")
  371. term.setCursorPos(width/2 - 1, height/2 - 1)
  372. user = read()
  373. term.setCursorPos(width/2 + 11, height/2 - 1)
  374. write("|")
  375. term.setCursorPos(width/2 - 1, height/2)
  376. pass2 = read("*")
  377. term.setCursorPos(width/2 + 11, height/2)
  378. write("|")
  379. if user == getValue("user") then
  380. if pass2 == getValue("pass") then
  381. newi("reset")
  382. else
  383. print("Wrong password")
  384. sleep(2)
  385. os.reboot()
  386. end
  387. else
  388. print("Wrong username")
  389. sleep(2)
  390. os.reboot()
  391. end
  392. else
  393. fc()
  394. end
  395. end
  396. login()
Advertisement
Add Comment
Please, Sign In to add comment