jakendrick3

Horyzon Bank ATM Client V2

Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local jenisis = require("jenisis")
  2. local component = require("component")
  3. local fs = require("filesystem")
  4. local textutils = require("serialization")
  5. local beep = require("computer")
  6. local event = require("event")
  7. local modem = component.getPrimary("modem")
  8. local gpu = component.getPrimary("gpu")
  9. local scrX, scrY = gpu.getResolution()
  10.  
  11. local function broadcast(packet)
  12.   local packet_ = textutils.serialize(packet)
  13.  
  14.   modem.broadcast(1024, packet_)
  15. end
  16.  
  17. local function receive()
  18.   event_ = {event.pull(5, "modem_message")}
  19.  
  20.   return textutils.unserialize(event_[6])
  21. end
  22.  
  23. currentaccount = {}
  24. currentaccount.accountName = "You shouldn't see this. Call Alex"
  25.  
  26. if fs.exists("/cache/bank.cache") then
  27.   file = io.open("/cache/bank.cache")
  28.   currentaccount = textutils.unserialize(file:read("*a"))
  29.   file:close()
  30.    
  31.   loggedIn = true
  32. end
  33.  
  34. local function getFunds()
  35.   if not loggedIn then
  36.     jenisis.errorMes("Please log in.")
  37.     return
  38.   end
  39.  
  40.   local packet = {
  41.     type = "getfunds",
  42.     target = currentaccount.accountName,
  43.     account = currentaccount
  44.   }
  45.  
  46.   broadcast(packet)
  47.   local response = receive()
  48.  
  49.   if response ~= false then
  50.     jenisis.errorMes("You have $"..response, "green")
  51.     return
  52.   else
  53.     jenisis.errorMes("Unexpected Error")
  54.   end
  55. end
  56.  
  57. local function transfer()
  58.   if not loggedIn then
  59.     jenisis.errorMes("Please log in.")
  60.     return
  61.   end
  62.  
  63.   local packet = {
  64.     account = currentaccount,
  65.     type = "transfer"
  66.   }
  67.  
  68.   packet.target = jenisis.inputBar("Whoms't ya sendin dollas 2 b?")
  69.   packet.amount = tonumber(jenisis.inputBar("Enter amount:"))
  70.  
  71.   broadcast(packet)
  72.   response = receive()
  73.  
  74.   if response then
  75.     jenisis.errorMes("Transfer Succesful.", "green")
  76.   else
  77.     jenisis.errorMes("Unexpected Error")
  78.   end
  79. end
  80.  
  81. local function newAccount()
  82.   local newacc = {}
  83.  
  84.   newacc.accountName = jenisis.inputBar("Enter new account name:")
  85.   newacc.accountKey = jenisis.inputBar("Enter Passkey:", true)
  86.   newacc.balance = 0
  87.   newacc.isFlagged = false
  88.   newacc.isFront = false
  89.   newacc.owners = {}
  90.  
  91.   packet = {
  92.     type = "newaccount",
  93.     account = newacc
  94.   }
  95.  
  96.   broadcast(packet)
  97.   result = receive()
  98.  
  99.   if result == true then
  100.     jenisis.errorMes("Account creation succesful.", "green")
  101.   else
  102.     jenisis.errorMes("Unexpected Error")
  103.   end
  104. end
  105.  
  106. local function doLog()
  107.   if loggedIn then
  108.     currentaccount.accountName = nil
  109.     currentaccount.accountKey = nil
  110.    
  111.     loggedIn = false
  112.  
  113.     fs.remove("/bank/bank.cache")
  114.     jenisis.errorMes("Logged Out", "green")
  115.     return
  116.   end
  117.  
  118.   currentaccount.accountName = jenisis.inputBar("Enter account:")
  119.   currentaccount.accountKey = jenisis.inputBar("Enter passkey:", true)
  120.  
  121.   local packet = {
  122.     type = "auth",
  123.     account = currentaccount
  124.   }
  125.  
  126.   broadcast(packet)
  127.   local result = receive()
  128.  
  129.   if result then
  130.     jenisis.errorMes("Logged In", "green")
  131.     loggedIn = true
  132.  
  133.     local file = io.open("/cache/bank.cache", "w")
  134.     file:write(textutils.serialize(currentaccount))
  135.     file:close()
  136.   else
  137.     jenisis.errorMes("Authentication Failed")
  138.     loggedIn = false
  139.   end
  140. end
  141.  
  142. local function centerText(line, text)
  143.   local length = string.len(text)
  144.  
  145.   local target = (scrX - length) / 2
  146.   term.setCursor(target, line)
  147.   term.write(text)
  148. end
  149.  
  150. local function displayInfo(table)
  151.   local amount = #table
  152.  
  153.   local lTarget = ((scrY - amount) / 2) - 1
  154.  
  155.   for i=1, amount do
  156.     centerText(lTarget + i, table[i])
  157.   end
  158. end
  159.  
  160. local function manageAccount()
  161.   if not loggedIn then
  162.     jenisis.errorMes("Please Log In")
  163.   end
  164.  
  165.   if jenisis.inputBar("Please re-enter password:", true) ~= currentaccount.accountKey then
  166.     return false
  167.   end
  168.  
  169.   manStatic = {
  170.     "Account Management Menu"
  171.   }
  172.  
  173.   manMenu = {
  174.     "Get Full Info",
  175.     "View Owners",
  176.     "Setup Multi-Ownership",
  177.     "Exit"
  178.   }
  179.  
  180.   while true do
  181.     cselected = jenisis.drawScreen(manStatic, manMenu)
  182.  
  183.     if cselected == 1 then
  184.       local packet = {
  185.         type = "getinfo",
  186.         account = currentaccount
  187.       }
  188.  
  189.       broadcast(packet)
  190.       local result = receive()
  191.  
  192.       term.clear()
  193.       local info = {
  194.         "Account Name: "..result.accountName,
  195.         "Account Key: "..result.accountKey,
  196.         "Account Balance: "..result.balance,
  197.         "Account Flag: "..tostring(result.isFlagged),
  198.         "Account Co-Owned: "..tostring(result.isFront)
  199.       }
  200.  
  201.       displayInfo(info)
  202.       event.pull("key_down")
  203.     elseif cselected == 2 then
  204.       term.clear()
  205.  
  206.       local packet = {
  207.         type = "getinfo",
  208.         account = currentaccount
  209.       }
  210.  
  211.       broadcast(packet)
  212.       currentaccount = receive()
  213.  
  214.       if not currentaccount.isFront then
  215.         centerText(scrY / 2, "Privatly Owned Account")
  216.       else
  217.         local ownersList = {}
  218.  
  219.         for k, v in pairs(currentaccount.owners) do
  220.           table.insert(ownersList, "Account "..k.." owns "..(v * 100).."%")
  221.         end
  222.  
  223.         displayInfo(ownersList)
  224.       end
  225.  
  226.       event.pull("key_down")
  227.     elseif cselected == 3 then
  228.       local tutorial = {
  229.         "A prompt will appear asking you to enter the new owners,",
  230.         "Followed by the % share they own. Ensure that the total",
  231.         "shares add up to 100% or you will be asked to restart.",
  232.         "Press any button to continue..."
  233.       }
  234.  
  235.       displayInfo(tutorial)
  236.       event.pull("key_down")
  237.       local done = nil
  238.       local total = 0
  239.       local newOwners = {}
  240.  
  241.       repeat
  242.         local name = jenisis.inputBar("Input the owner name:")
  243.         local amount = jenisis.inputBar("Input the % share:")
  244.  
  245.         local i, j = string.find(amount, "%%")
  246.  
  247.         if i ~= nil then
  248.           local amount = string.sub(0, string.len(amount) - 1)
  249.         end
  250.  
  251.         amount = tonumber(amount)
  252.         total = total + amount
  253.  
  254.         if total == 100 then
  255.           done = true
  256.           newOwners[name] = amount * 0.01
  257.         elseif total > 100 then
  258.           jenisis.errorMes("You have gone over 100. Restarting...")
  259.           total = 0
  260.         else
  261.           tutorial[4] = ("  Current counted shares at "..total.."%  ")
  262.           displayInfo(tutorial)
  263.           newOwners[name] = (amount * 0.01)
  264.         end
  265.       until done
  266.  
  267.       local temp = 0
  268.       for k, v in pairs(newOwners) do
  269.         temp = temp + v
  270.       end
  271.  
  272.       if temp ~= 1 then
  273.         jenisis.errorMes("Shares exeeded 100% ("..temp..")")
  274.         return
  275.       else
  276.         currentaccount.owners = newOwners
  277.       end
  278.  
  279.       local packet = {
  280.         type = "dofront",
  281.         account = currentaccount
  282.       }
  283.  
  284.       broadcast(packet)
  285.       local result = receive()
  286.  
  287.       if result then
  288.         jenisis.errorMes("Successfully set up ownership", "green")
  289.       else
  290.         jenisis.errorMes("Unexpected Error")
  291.       end
  292.     else
  293.       return
  294.     end
  295.   end
  296. end
  297.  
  298. while true do
  299.  
  300.   static = {
  301.     "Bank ATM by Horyzon",
  302.     ""
  303.   }
  304.  
  305.   menu = {
  306.     "Check Funds",
  307.     "Transfer",
  308.     "Account Management",
  309.     "New Account",
  310.     "",
  311.     "Exit"
  312.   }
  313.  
  314.   if loggedIn then
  315.     static[2] = ("Logged in as "..currentaccount.accountName)
  316.     menu[5] = "Log Out"
  317.   else
  318.     static[2] = "Please log in."
  319.     menu[5] = "Log In"
  320.   end
  321.  
  322.   result = jenisis.drawScreen(static, menu)
  323.  
  324.   if result == 1 then
  325.     getFunds()
  326.   elseif result == 2 then
  327.     transfer()
  328.   elseif result == 3 then
  329.     manageAccount()
  330.   elseif result == 4 then
  331.     newAccount()
  332.   elseif result == 5 then
  333.     doLog()
  334.   elseif result == 6 then
  335.     break
  336.   end
  337. end
Add Comment
Please, Sign In to add comment