Guest User

gcapp.lua

a guest
Aug 8th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.08 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. local colorT = colors.white
  3. os.loadAPI("gcapi.lua")
  4. os.loadAPI("apis")
  5. local sides = rs.getSides()
  6. local chnl = 9584
  7. local modem, xd, yd
  8. local curr = "activity"
  9. local stop = false
  10. local open = false
  11. for i = 1, #sides do
  12.     if peripheral.isPresent(sides[i]) and peripheral.getType(sides[i]) == "modem" then
  13.         modem = peripheral.wrap(sides[i])
  14.         modem.open(chnl)
  15.         open = true
  16.     end
  17. end
  18. assert(open, "Attach a Wireless Modem.")
  19. local user, pass, sid, id, dEnable
  20. local x, y = term.getSize()
  21. local win = window.create(1, y/4, x, y)
  22. term.setBackgroundColor(colors.lightGray)
  23. term.clear()
  24. function bar()
  25.     paintutils.drawLine(1, 1, x, 1, colors.orange)
  26.     local s = "Glo"
  27.     but = {"Activity ", "Actions ", "X"}
  28.     butN = {"activity", "action", "logout"}
  29.     local prevLen = 0
  30.     term.setTextColor(colors.white)
  31.     for i = #but, 1, -1 do
  32.         apis.button.add(x-(prevLen+string.len(but[i])), 1, x-(prevLen-1), 1, butN[i])
  33.         term.setCursorPos(x-(prevLen+string.len(but[i])-1), 1)
  34.         term.setTextColor(colors.white)
  35.         term.write(but[i])
  36.         prevLen = prevLen+string.len(but[i])
  37.     end
  38.     term.setCursorPos(1, 1)
  39.     term.write(s)
  40.     term.setBackgroundColor(colors.lightGray)
  41.     term.setTextColor(colorT)
  42. end
  43. function checkDefault()
  44.     local x, y = term.getSize()
  45.     file = fs.open(".default", "r")
  46.     if not file then
  47.         local tfile = fs.open(".default", "w")
  48.         tfile.writeLine("false")
  49.         tfile.close()
  50.         denable = false
  51.     else
  52.         dEnable = file.readLine()
  53.         if dEnable == "true" then dEnable = true else dEnable = false end
  54.     end
  55.     if xd and yd then
  56.     local x0 = xd
  57.     local y0 = yd
  58.     apis.button.add(x0, y0, x0+4, y0, "default")
  59.         term.setTextColor(colors.yellow)
  60.         if dEnable == true then
  61.             paintutils.drawLine(x0, y0, x0+4, y0, colors.green)
  62.             term.setCursorPos(x0+1, y0)
  63.             term.write("on")
  64.         else
  65.             paintutils.drawLine(x0, y0, x0+4, y0, colors.red)
  66.             term.setCursorPos(x0+1, y0)
  67.             term.write("off")
  68.         end
  69.         term.setBackgroundColor(colors.lightGray)
  70.         term.setTextColor(colorT)
  71.     end
  72. end
  73. function toggleDefault()
  74.     local file = fs.open(".default", "w")
  75.     file.writeLine(not dEnable)
  76.     dEnable = not dEnable
  77.     file.close()
  78. end
  79. function sendDeposit()
  80.     repeat
  81.         while dEnable do
  82.             e,side,freq,rfreq,msg,dist = os.pullEvent("modem_message")
  83.             if type(dist) == "number" and dist <= 10 then
  84.                 modem.transmit(rfreq, freq, id)
  85.             end
  86.         end
  87.         sleep(.5)
  88.     until true == false    
  89. end
  90. function doTransfer()
  91.     tBPos = {}
  92.     local passt
  93.     local x, y = term.getSize()
  94.     apis.button.clear()
  95.     term.setBackgroundColor(colors.lightGray)
  96.     term.clear()
  97.     bar()
  98.     term.setTextColor(colors.gray)
  99.     local boxes = {{"toUser", "Username"}, {"amt", "Amount"}, {"desc", "Description"}}
  100.     for i = 1, #boxes do
  101.         local x0, y0 = 2, (y/4)*i
  102.         apis.button.add(x0, (y/4)*i, x-1, (y/4)*i, boxes[i][1])
  103.         paintutils.drawLine(x0, (y/4)*i, x-1, (y/4)*i, colors.white)
  104.         term.setCursorPos(x0, y0)
  105.         term.write(boxes[i][2])
  106.         tBPos[#tBPos+1] = {x0, (y/4)*i, x-1, (y/4)*i}
  107.     end            
  108.     term.setBackgroundColor(colors.red)
  109.     term.setTextColor(colors.white)
  110.     apis.button.add(2, y-1, x-2, y-1, "cancel")
  111.     paintutils.drawLine(2, y-1, x-2, y-1, colors.red)
  112.     local ca = "Cancel"
  113.     term.setCursorPos((x/2)-(string.len(ca)/2)-1, y-1)
  114.     term.write(ca)
  115.     term.setBackgroundColor(colors.white)
  116.     repeat
  117.         local name = apis.button.check()
  118.         if name then
  119.             for i = 1, #boxes do
  120.                 if name == boxes[i][1] then
  121.                     term.setTextColor(colors.black)
  122.                     paintutils.drawLine(tBPos[i][1], tBPos[i][2], tBPos[i][3], tBPos[i][4], colors.white)
  123.                     term.setCursorPos(tBPos[i][1], tBPos[i][2])
  124.                     local inp = read()
  125.                     boxes[i][3] = inp
  126.                 end                    
  127.             end
  128.             all = 0                    
  129.             for i = 1, #boxes do
  130.                 if boxes[i][3] then
  131.                     all = all+1
  132.                 end
  133.             end            
  134.             if all == #boxes then
  135.                 term.setBackgroundColor(colors.lightGray)
  136.                 term.setTextColor(colors.red)
  137.                 if not boxes[1][3] == "" then
  138.                     local s = "Insert Valid ID"
  139.                     apis.text.center(s, boxes[1][2]+1)
  140.                 elseif not tonumber(boxes[2][3]) and boxes[2][3] ~= "0" then
  141.                     local s = "Insert Valid Amount"
  142.                     apis.text.center(s, boxes[2][2]+1)
  143.                 elseif boxes[1][3] ~= "" and tonumber(boxes[2][3]) then
  144.                     passt = true
  145.                 end
  146.             end
  147.             for i = 1, #boxes do
  148.                 if boxes[i][3] == "" then
  149.                     paintutils.drawLine(tBPos[i][1], tBPos[i][2], tBPos[i][3], tBPos[i][4], colors.white)
  150.                     term.setCursorPos(tBPos[i][1], tBPos[i][2])
  151.                     term.setTextColor(colors.gray)
  152.                     term.write(boxes[i][2])
  153.                 end
  154.             end          
  155.         end
  156.     until name == "cancel" or passt == true or name == "exit"
  157.     if passt then
  158.         if boxes[1][3] then
  159.             suc, uid = gc.getAccountID(boxes[1][3])
  160.         else
  161.             suc, uid = gc.getNearestID()
  162.         end
  163.         local suc, res
  164.         if not pass then            
  165.             suc, res = gc.transferAID2AID(id, uid, tonumber(boxes[2][3]), boxes[3][3])
  166.         else
  167.             suc, res = gc.transferSID2AID(sid, uid, tonumber(boxes[2][3]), boxes[3][3])
  168.         end
  169.         term.setBackgroundColor(colors.lightGray)
  170.         term.clear()
  171.         if not suc then
  172.             apis.text.center("Transfer failed", y/2)
  173.             apis.text.center(res, (y/2)+1)
  174.         else
  175.             apis.text.center("Transfer succeeded", y/2)
  176.         end
  177.         sleep(1)
  178.     end
  179.     if name == "cancel" then
  180.         return "back"
  181.     elseif name == "exit" then
  182.         return "exit"
  183.     end
  184. end
  185.  
  186. function loginDisp()
  187.     local x, y = term.getSize()
  188.     local def = false
  189.     apis.button.clear()
  190.     local suc, id = gc.getAccountID(user)
  191.     if suc and not def then
  192.         term.setBackgroundColor(colors.lightGray)
  193.         term.clear()
  194.         repeat
  195.             while not stop do
  196.                 local suc, bal = gc.getBalance(id)
  197.                 term.setCursorPos(1, 3)
  198.                 term.write("Balance: ")
  199.                 term.setCursorPos(x/2, 3)
  200.                 if suc then
  201.                     term.write(bal.." GLO")
  202.                 else
  203.                    term.write("Not Found")
  204.                 end
  205.                 term.setCursorPos(1, 4)
  206.                 term.write("User ID:")
  207.                 term.setCursorPos(x/2, 4)
  208.                 term.write(id)
  209.                 term.setCursorPos(1, 4)
  210.                 term.write("Broadcast Deposit: ")
  211.                 xd, yd = term.getCursorPos()
  212.                 elseif not suc then
  213.                     apis.text.center("Failed to load user", y/2)
  214.                     apis.text.center(user, (y/2)+1)
  215.                     sleep(1)
  216.                     break
  217.                 end
  218.                 sleep(5)
  219.             end
  220.             sleep(5)
  221.         until true == false
  222.     end
  223. end
  224.  
  225. function actionDisp()
  226.     repeat
  227.         term.redirect(win)
  228.         local tBtns = {{"transfer", colors.orange, "Transfer"}, {"withdraw", colors.lightGray, "Withdraw (NYI)"}} --, {"logout", colors.red, "Log Out"}}
  229.         term.setBackgroundColor(colors.white)
  230.         checkDefault()
  231.         term.setTextColor(colors.yellow)
  232.         for i = 1, #tBtns do
  233.             local ys = {}
  234.             for j = -1+i, i do
  235.                 table.insert(ys, (y/(#tBtns*2))*(j+#tBtns))
  236.             end
  237.             if i ~= 2 then
  238.             apis.button.add(1, ys[1]+1, x, ys[2], tBtns[i][1])
  239.             end
  240.             paintutils.drawFilledBox(1, ys[1]+1, x, ys[2], tBtns[i][2])
  241.             apis.text.center(tBtns[i][3], ys[1]+2)
  242.         end
  243.         def = false
  244.         term.redirect(term.native())
  245.         name = apis.button.check()
  246.         if name == "transfer" then
  247.             name = doTransfer()
  248.         elseif name == "withdraw" then
  249.             checkDeposit()
  250.         elseif name == "default" then
  251.             toggleDefault()
  252.             checkDefault()
  253.             def = true
  254.         elseif name == "logout" then
  255.             user = nil
  256.             pass = nil
  257.             sid = nil
  258.             id = nil
  259.             term.setBackgroundColor(colors.lightGray)
  260.             term.clear()
  261.             loginScreen()
  262.         end
  263.     until name == "exit"
  264.     return true
  265. end
  266.  
  267. function activityDisp()
  268.     term.redirect(win)
  269.        
  270.     term.redirect(term.current())
  271. end
  272. function genDisp()
  273.    
  274. end
  275. function doLogin()
  276.     local x, y = term.getSize()
  277.     term.setBackgroundColor(colors.lightGray)
  278.     term.clear()
  279.     term.setTextColor(colors.yellow)
  280.     local s = "Logging in..."
  281.     apis.text.center(s, y/2)
  282.     local suc
  283.     suc, id = gc.getAccountID(user)
  284.     if type(pass) == "string" and pass then
  285.         suc, sid = gc.login(user, pass)
  286.     end
  287.     if suc then
  288.         term.clear()
  289.         apis.text.center("Logged in Successfully", y/2)
  290.         sleep(.5)
  291.        
  292.         ex = genDisp()
  293.         if ex then return "exit" end
  294.     else
  295.         term.clear()
  296.         apis.text.center("Login Failed:", y/2)
  297.         if pass then
  298.             apis.text.center(sid, (y/2)+1)
  299.         else
  300.             apis.text.center(id, (y/2)+1)
  301.         end
  302.         sleep(.5)
  303.         return "exit"
  304.     end
  305. end
  306. function loginScreen()
  307.     apis.button.clear()
  308.     local x, y = term.getSize()
  309.     term.setTextColor(colors.orange)
  310.     apis.text.center("Glo.Cash", (y/2)-3)
  311.     local x0, y0 = 2, (y/2)-1
  312.     term.setBackgroundColor(colors.white)
  313.     term.setTextColor(colors.gray)
  314.     apis.button.add(x0, y0, x-1, y0, "name")
  315.     paintutils.drawLine(x0, y0, x-1, y0, colors.white)
  316.     term.setCursorPos(x0, y0)
  317.     term.write("Username")
  318.     local x1, y1 = 2, (y/2)+1
  319.     apis.button.add(x1, y1, x-1, y1, "pass")
  320.     paintutils.drawLine(x1, y1, x-1, y1, colors.white)
  321.     term.setCursorPos(x1, y1)
  322.     term.write("Password (optional)")
  323.     paintutils.drawLine(2, (y/2)+3, x-1, (y/2)+3, colors.orange)
  324.     apis.button.add(2, (y/2)+3, x-1, (y/2)+3, "valid")
  325.     term.setTextColor(colors.white)
  326.     apis.text.center("Log In", (y/2)+3)
  327.     repeat
  328.         term.setTextColor(colors.black)
  329.         name = apis.button.check()
  330.         if name ~= nil and name ~= "exit" then
  331.             if name == "name" then
  332.                 paintutils.drawLine(x0, y0, x-1, y0, colors.white)
  333.                 term.setCursorPos(x0, y0)
  334.                 user = read()
  335.                 if user == "" then
  336.                     user = nil
  337.                     paintutils.drawLine(x0, y0, x-1, y0, colors.white)
  338.                     term.setCursorPos(x0, y0)
  339.                     term.setTextColor(colors.gray)
  340.                     term.write("Username")
  341.                 end
  342.             end
  343.             if name == "pass" then
  344.                 paintutils.drawLine(x1, y1, x-1, y1, colors.white)
  345.                 term.setCursorPos(x1, y1)
  346.                 pass = read("*")
  347.                 if pass == "" then
  348.                     pass = nil
  349.                     paintutils.drawLine(x1, y1, x-1, y1, colors.white)
  350.                     term.setCursorPos(x1, y1)
  351.                     term.setTextColor(colors.gray)
  352.                     term.write("Password (optional)")
  353.                 end
  354.             end
  355.             if name == "valid" then
  356.                 name = doLogin()
  357.             end
  358.         elseif name == "exit" then
  359.             break
  360.         end
  361.     until name ~= nil and name == "exit"
  362. end
  363. repeat
  364. term.setBackgroundColor(colors.lightGray)
  365. term.clear()
  366. parallel.waitForAny(loginScreen, sendDeposit)
  367. until true == false
Add Comment
Please, Sign In to add comment