smigger22

Untitled

Feb 14th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.11 KB | None | 0 0
  1. x, y = term.getSize()
  2. local buttons = {}
  3. local log = {}
  4. local Users = {}
  5. serverID = nil
  6. serverName = nil
  7. serverPass = nil
  8. page = 1
  9. if not fs.exists("db") then -- Checks for db
  10. downloadDB = http.get("http://pastebin.com/raw.php?i=SfXFSh0a")
  11. downloadDBView = downloadDB.readAll()
  12. downloadDB.close()
  13.  
  14. writeDB = fs.open("db", "w")
  15. writeDB.write(downloadDBView)
  16. writeDB.close()
  17.  
  18. os.loadAPI("db")
  19. elseif fs.exists("db") then
  20. os.loadAPI("db")
  21. end
  22.  
  23. -- Drawing functions
  24. function centered(str, height)
  25. term.setCursorPos(x / 2 - #str / 2, height)
  26. term.write(str)
  27. end
  28.  
  29. function indented(str, indent, height)
  30. term.setCursorPos(indent, height)
  31. term.write(str)
  32. end
  33.  
  34. function left(str, height)
  35. term.setCursorPos(1, height)
  36. term.write(str)
  37. end
  38.  
  39. function right(str, height)
  40. term.setCursorPos(x - #str, height)
  41. term.write(str)
  42. end
  43.  
  44. function clearLine(colour, height)
  45. term.setCursorPos(1, height)
  46. term.setBackgroundColour(colours[colour])
  47. term.clearLine()
  48. end
  49.  
  50. function drawBorder(xMin, yMin, xMax, yMax, colour)
  51. paintutils.drawBox(xMin, yMin, xMax, yMax, colours[colour])
  52. end
  53.  
  54. function drawBox(xMin, yMin, xMax, yMax, colour)
  55. paintutils.drawFilledBox(xMin, yMin, xMax, yMax, colours[colour])
  56. end
  57.  
  58. -- Button API
  59. function enableButton(buttonName)
  60. for k,v in pairs(buttons) do
  61. if v["buttonName"] == buttonName then
  62. buttons[k]["Enabled"] = true
  63. return true
  64. end
  65. end
  66. return false
  67. end
  68.  
  69. function disableButton(buttonName)
  70. for k,v in pairs(buttons) do
  71. if v["buttonName"] == buttonName then
  72. buttons[k]["Enabled"] = false
  73. return true
  74. end
  75. end
  76. return false
  77. end
  78.  
  79. function buttonNameToNumber(buttonName)
  80. for k,v in pairs(buttons) do
  81. if v["buttonName"] == buttonName then
  82. return k
  83. end
  84. end
  85. return false
  86. end
  87.  
  88. function checkButtonClick()
  89. while true do
  90. event, button, xPos, yPos = os.pullEvent("mouse_click")
  91. if event == "mouse_click" then
  92. for k,v in pairs(buttons) do
  93. if xPos >= v["xPosStart"] and xPos <= v["xPosStop"] and yPos >= v["yPos"] and yPos <= v["yPos"]+2 and v["Enabled"] == true then
  94. if v["Function"] then
  95. if v["Function"] == "connect" and page == 1 then
  96. if type(tonumber(serverID)) == "number" then
  97. if db.getConnection(tonumber(serverID), serverName, serverPass) == "Connection" then
  98. term.setBackgroundColour(colours.cyan)
  99. term.setTextColour(colours.green)
  100. centered("Login successful. Redirecting..",6)
  101. sleep(1.5)
  102. page = 2
  103. pageLayout()
  104. elseif db.getConnection(tonumber(serverID), serverName, serverPass) == "Wrong_Details" then
  105. term.setBackgroundColour(colours.cyan)
  106. term.setTextColour(colours.red)
  107. centered("Wrong Username/Password!",6)
  108. sleep(2)
  109. page = 1
  110. main()
  111. elseif db.getConnection(tonumber(serverID), serverName, serverPass) == "No_Connection" then
  112. term.setBackgroundColour(colours.cyan)
  113. term.setTextColour(colours.red)
  114. centered("No connection!",6)
  115. sleep(2)
  116. page = 1
  117. main()
  118. end
  119. else
  120. term.setBackgroundColour(colours.cyan)
  121. term.setTextColour(colours.red)
  122. centered("Not a correct server ID!",6)
  123. sleep(2)
  124. page = 1
  125. main()
  126. end
  127. elseif v["Function"] == "addUser" and page == 2 then
  128. drawBorder(5, 5, x - 5, y - 5, "blue")
  129. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  130. indented("Username: ", 7, 7)
  131. indented("Password: ", 7, 9)
  132. term.setCursorPos(17, 7)
  133. username = read()
  134. term.setCursorPos(17, 9)
  135. password = read("*")
  136. if db.addUser(tonumber(serverID), serverName, serverPass, username, password) == true then
  137. term.setTextColour(colours.green)
  138. centered("Success.", 10)
  139. sleep(2)
  140. pageLayout()
  141. elseif db.addUser(tonumber(serverID), serverName, serverPass, username, password) == false then
  142. term.setTextColour(colours.red)
  143. centered("Failed.", 10)
  144. sleep(2)
  145. pageLayout()
  146. end
  147. elseif v["Function"] == "editValue" and page == 2 then
  148. drawBorder(5, 5, x - 5, y - 5, "blue")
  149. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  150. indented("Username: ", 7, 7)
  151. indented("Value: ", 7, 9)
  152. indented("New Value: ", 7, 11)
  153. term.setCursorPos(17, 7)
  154. username = read()
  155. term.setCursorPos(14, 9)
  156. value = read()
  157. term.setCursorPos(18, 11)
  158. nValue = read()
  159. if db.editValue(tonumber(serverID), serverName, serverPass, username, value, nValue) == true then
  160. term.setTextColour(colours.green)
  161. centered("Success.", 10)
  162. sleep(2)
  163. pageLayout()
  164. elseif db.editValue(tonumber(serverID), serverName, serverPass, username, value, nValue) == false then
  165. term.setTextColour(colours.red)
  166. centered("Failed.", 10)
  167. sleep(2)
  168. pageLayout()
  169. end
  170. elseif v["Function"] == "addValue" and page == 2 then
  171. drawBorder(5, 5, x - 5, y - 5, "blue")
  172. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  173. indented("Username: ", 7, 7)
  174. indented("Value: ", 7, 9)
  175. indented("New Value: ", 7, 11)
  176. term.setCursorPos(17, 7)
  177. username = read()
  178. term.setCursorPos(14, 9)
  179. value = read()
  180. term.setCursorPos(18, 11)
  181. nValue = read()
  182. if db.addValue(tonumber(serverID), serverName, serverPass, username, value, nValue) == true then
  183. term.setTextColour(colours.green)
  184. centered("Success.", 10)
  185. sleep(2)
  186. pageLayout()
  187. elseif db.addValue(tonumber(serverID), serverName, serverPass, username, value, nValue) == false then
  188. term.setTextColour(colours.red)
  189. centered("Failed.", 10)
  190. sleep(2)
  191. pageLayout()
  192. end
  193. elseif v["Function"] == "editUser" and page == 2 then
  194. drawBorder(5, 5, x - 5, y - 5, "blue")
  195. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  196. indented("Username: ", 7, 7)
  197. indented("Password: ", 7, 9)
  198. term.setCursorPos(17, 7)
  199. username = read()
  200. term.setCursorPos(17, 9)
  201. password = read("*")
  202. if db.editUser(tonumber(serverID), serverName, serverPass, username, password) == true then
  203. term.setTextColour(colours.green)
  204. centered("Success.", 10)
  205. sleep(2)
  206. pageLayout()
  207. elseif db.editUser(tonumber(serverID), serverName, serverPass, username, password) == false then
  208. term.setTextColour(colours.red)
  209. centered("Failed.", 10)
  210. sleep(2)
  211. pageLayout()
  212. end
  213. elseif v["Function"] == "stopServer" and page == 2 then
  214. if db.stopServer(tonumber(serverID), serverName, serverPass) == true then
  215. drawBorder(5, 5, x - 5, y - 5, "blue")
  216. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  217. term.setTextColour(colours.green)
  218. centered("Success.", 10)
  219. disableButton("stopServer")
  220. sleep(2)
  221. term.setTextColour(colours.red)
  222. centered("Server closed. Aborting.", 10)
  223. sleep(2)
  224. page = 1
  225. begin()
  226. end
  227. elseif v["Function"] == "getValue" and page == 2 then
  228. drawBorder(5, 5, x - 5, y - 5, "blue")
  229. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  230. indented("Username: ", 7, 7)
  231. indented("Value: ", 7, 9)
  232. term.setCursorPos(17, 7)
  233. username = read()
  234. term.setCursorPos(14, 9)
  235. value = read()
  236. if db.getValue(tonumber(serverID), serverName, serverPass, username, value) == "No_Value" then
  237. centered("User has no value.", 10)
  238. sleep(2)
  239. pageLayout()
  240. else
  241. centered("Value: "..db.getValue(tonumber(serverID), serverPass, serverPass, username, value), 10)
  242. sleep(2)
  243. pageLayout()
  244. end
  245. elseif v["Function"] == "pingServer" and page == 2 then
  246. drawBorder(5, 5, x - 5, y - 5, "blue")
  247. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  248. if db.getConnection(tonumber(serverID), serverName, serverPass) == "Connection" then
  249. term.setTextColour(colours.green)
  250. centered("Success", 10)
  251. sleep(2)
  252. pageLayout()
  253. elseif db.getConnection(tonumber(serverID), serverName, serverPass) == "No_Connection" then
  254. term.setTextColour(colours.red)
  255. centered("Failed.", 10)
  256. centered("Server closed. Aborting.", 11)
  257. sleep(2)
  258. page = 1
  259. begin()
  260. end
  261. elseif v["Function"] == "leave" and page == 2 then
  262. serverID = nil
  263. serverName = nil
  264. serverPass = nil
  265. page = 1
  266. begin()
  267. elseif v["Function"] == "removeUser" and page == 2 then
  268. drawBorder(5, 5, x - 5, y - 5, "blue")
  269. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  270. indented("Username: ", 7, 7)
  271. username = read()
  272. if db.removeUser(tonumber(serverID), serverName, serverPass, username) == true then
  273. term.setTextColour(colours.green)
  274. centered("Success.", 10)
  275. sleep(2)
  276. pageLayout()
  277. elseif db.removeUser(tonumber(serverID), serverName, serverPass, username) == false then
  278. term.setTextColour(colours.red)
  279. centered("Failed.", 10)
  280. sleep(2)
  281. pageLayout()
  282. end
  283. elseif v["Function"] == "checkUser" and page == 2 then
  284. drawBorder(5, 5, x - 5, y - 5, "blue")
  285. drawBox(6, 6, x - 6, y - 6, "lightBlue")
  286. indented("Username: ", 7, 7)
  287. indented("Password: ", 7, 9)
  288. term.setCursorPos(17, 7)
  289. username = read()
  290. term.setCursorPos(17, 9)
  291. password = read("*")
  292. if db.checkUser(tonumber(serverID), serverName, serverPass, username, password) == true then
  293. term.setTextColour(colours.green)
  294. centered("Success.", 10)
  295. sleep(2)
  296. pageLayout()
  297. elseif db.checkUser(tonumber(serverID), serverName, serverPass, username, password) == false then
  298. term.setTextColour(colours.red)
  299. centered("Failed.", 10)
  300. sleep(2)
  301. pageLayout()
  302. end
  303. elseif v["Function"] == "log" and page == 2 then
  304. term.setBackgroundColour(colours.black)
  305. term.clear()
  306. term.setTextColour(colours.white)
  307. Log = db.getAllLogs(tonumber(serverID), serverName, serverPass)
  308. local yPos = 1
  309. term.setCursorPos(1, yPos)
  310. for k, v in pairs(Log) do
  311. if v[2] == "Info" then
  312. term.setTextColour(colours.blue)
  313. term.write("[ "..v[2].." ] ")
  314. term.setTextColour(colours.lightGrey)
  315. term.write(v[1])
  316. yPos = yPos + 1
  317. term.setCursorPos(1, yPos)
  318. elseif v[2] == "Success" then
  319. term.setTextColour(colours.green)
  320. term.write("[ "..v[2].. " ] ")
  321. term.setTextColour(colours.lightGrey)
  322. term.write(v[1])
  323. yPos = yPos + 1
  324. term.setCursorPos(1, yPos)
  325. elseif v[2] == "Warning" or v[2] == "Error" or v[2] == "Alert" then
  326. term.setTextColour(colours.red)
  327. term.write("[ "..v[2].." ] ")
  328. term.setTextColour(colours.lightGrey)
  329. term.write(v[1])
  330. yPos = yPos + 1
  331. term.setCursorPos(1, yPos)
  332. end
  333. end
  334. event, key = os.pullEvent("key")
  335. pageLayout()
  336. elseif v["Function"] == "listUsers" and page == 2 then
  337. term.setBackgroundColour(colours.black)
  338. term.clear()
  339. term.setTextColour(colours.white)
  340. Users = db.getAllUsers(tonumber(serverID), serverName, serverPass)
  341. term.setCursorPos(1, 1)
  342. for k, v in pairs(Users) do
  343. print(v["name"])
  344. end
  345. event, key = os.pullEvent("key")
  346. pageLayout()
  347. end
  348. end
  349. end
  350. end
  351. end
  352. end
  353. end
  354.  
  355. function removeButton(buttonName)
  356. table.remove(buttons, buttonNameToNumber(buttonName))
  357. end
  358.  
  359. function drawButton(xPos,yPos,bgColor,txtColor,length,txt,buttonName,functionName)
  360. term.setBackgroundColour(colours[bgColor])
  361. term.setTextColour(colours[txtColor])
  362. local X = xPos + math.floor((length/2)-(string.len(txt)/2))
  363. if not buttonName then
  364. buttonName = "Test"..tostring(#buttons+1)
  365. end
  366. for i = yPos,yPos+2 do
  367. if i ~= yPos+1 then
  368. term.setCursorPos(xPos,i)
  369. term.write(string.rep(" ", length))
  370. else
  371. term.setCursorPos(xPos,i)
  372. term.write(string.rep(" ", length))
  373. term.setCursorPos(X,i)
  374. buttons[#buttons+1] = {["Function"] = functionName, ["buttonName"] = buttonName, ["xPosStart"] = xPos, ["xPosStop"] = xPos+length-1, ["yPos"] = yPos, ["Enabled"] = true}
  375. term.write(txt)
  376. end
  377. end
  378. end
  379.  
  380. -- Main Code
  381.  
  382. function printHeader(page)
  383. if page == 1 then
  384. clearLine("blue", 1)
  385. clearLine("blue", 2)
  386. clearLine("blue", 3)
  387. term.setTextColour(colours.lightBlue)
  388. indented("EpiClient", 3, 2)
  389. elseif page == 2 then
  390. clearLine("blue", 1)
  391. clearLine("blue", 2)
  392. clearLine("blue", 3)
  393. term.setTextColour(colours.lightBlue)
  394. indented("EpiClient", 3, 2)
  395. amount = db.amountUsers(tonumber(serverID), serverName, serverPass)
  396. amount = tonumber(amount)
  397. term.setBackgroundColour(colours.blue)
  398. if amount == 1 then
  399. term.setCursorPos(45, 2)
  400. write(amount.." User")
  401. elseif amount ~= 1 then
  402. term.setCursorPos(44, 2)
  403. write(amount.." Users")
  404. end
  405. end
  406. end
  407.  
  408.  
  409. function pageLayout()
  410. if page == 1 then
  411. term.setBackgroundColour(colours.cyan)
  412. term.clear()
  413. printHeader(1)
  414. elseif page == 2 then
  415. term.setBackgroundColour(colours.cyan)
  416. term.clear()
  417. printHeader(2)
  418. drawButton(3, 6, "blue", "lightBlue", 10, "Add User", "addUser", "addUser")
  419. drawButton(14, 6, "blue", "lightBlue", 12, "Edit Value", "editValue", "editValue")
  420. drawButton(27, 6, "blue", "lightBlue", 11, "Add Value", "addValue", "addValue")
  421. drawButton(39, 6, "blue", "lightBlue", 11, "Edit User", "editUser", "editUser")
  422. drawButton(3, 10, "blue", "lightBlue", 13, "Stop Server", "stopServer", "stopServer")
  423. drawButton(17, 10, "blue", "lightBlue", 11, "Get Value", "getValue", "getValue")
  424. drawButton(29, 10, "blue", "lightBlue", 13, "Ping Server", "pingServer", "pingServer")
  425. drawButton(43, 10, "blue", "lightBlue", 7, "Leave", "leave", "leave")
  426. drawButton(3, 14, "blue", "lightBlue", 7, "Users", "listUsers", "listUsers")
  427. drawButton(11, 14, "blue", "lightBlue", 10, "View Log", "log", "log")
  428. drawButton(22, 14, "blue", "lightBlue", 13, "Remove User", "removeUser", "removeUser")
  429. drawButton(36, 14, "blue", "lightBlue", 14, "Check a User", "checkUser", "checkUser")
  430. term.setBackgroundColour(colours.cyan)
  431. term.setTextColour(colours.blue)
  432. indented("This client was made by smigger22 and the data-", 3, 17)
  433. centered("base was designed by jasperdekiller.", 18)
  434. checkButtonClick()
  435. end
  436. end
  437.  
  438. function main()
  439. pageLayout()
  440. term.setBackgroundColour(colours.cyan)
  441. indented("ID: ", 7, 7)
  442. indented("Username: ", 7, 9)
  443. indented("Password: ", 7, 11)
  444. term.setCursorPos(11, 7)
  445. serverID = read()
  446. term.setCursorPos(17, 9)
  447. serverName = read()
  448. term.setCursorPos(17, 11)
  449. serverPass = read("*")
  450. drawButton(16, 14, "blue", "lightBlue", 17, "Connect", "connect", "connect")
  451. end
  452.  
  453. function begin()
  454. parallel.waitForAll(main, checkButtonClick)
  455. end
  456.  
  457. rednet.open("top")
  458. begin()
Advertisement
Add Comment
Please, Sign In to add comment