Advertisement
Guest User

card

a guest
Dec 15th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.25 KB | None | 0 0
  1. --First time setup/load settings
  2. if not fs.exists("settings") then
  3.   if not fs.exists("button") then
  4.     shell.run("pastebin", "get", "Kd7gQKLe", "button")
  5.     shell.run("pastebin", "get", "KrfYp2Jf", "splash")
  6.   end
  7.   data = {first = true, pin = " ", name = " "}
  8.   save = function()
  9.     file = fs.open("settings", "w")
  10.     file.write(textutils.serialise(data))
  11.     file.close()
  12.   end
  13.   save()
  14. else
  15.   save = function()
  16.     file = fs.open("settings", "w")
  17.     file.write(textutils.serialise(data))
  18.     file.close()
  19.   end
  20.   file = fs.open("settings", "r")
  21.   data = textutils.unserialise(file.readAll())
  22.   file.close()
  23. end
  24.  
  25. --Variable declaraction
  26. rednet.open("top")
  27. splash = paintutils.loadImage("splash")
  28. shell.run("button")
  29. size = {x = 26, y = 20}
  30. loggedIn = false
  31.  
  32. term.setBackgroundColor(colors.white)
  33. term.setTextColor(colors.black)
  34.  
  35. --Changes table button states
  36. function tState(_t, _s)
  37.   for _, button in pairs(_t) do
  38.     button:set("enabled", _s)
  39.   end
  40. end
  41.  
  42. --Create number pad buttons
  43. pinButtons = {}
  44. local i = 0
  45.   for _yy = 1, 3 do
  46.     for _xx = 0, 2 do
  47.     i = i + 1
  48.     pinButtons[i] = newButton((9 + (_xx * 3)), (11 + (_yy * 2)), 1, 1, tostring(i), "f", "0", tostring(i))
  49.   end
  50. end
  51. pinButtons[11] = newButton(12, 19, 1, 1, "0", "f", "0", 0)
  52. pinButtons[10] = newButton (9, 19, 1, 1, " ", "0", "d", "enter")
  53. pinButtons[12] = newButton (15, 19, 1, 1, " ", "0", "e", "back")
  54. tState(pinButtons, false)
  55.  
  56. --Create main menu buttons
  57. menuButtons = {}
  58. menuButtons[1] = newButton(3, 13, 12, 7, " $$ Send $$ ", colors.white, colors.blue, "send")
  59. menuButtons[2] = newButton(18, 13, 6, 3, "Update", colors.white, colors.green, "update")
  60. menuButtons[3] = newButton(18, 17, 6, 3, "Exit", colors.white, colors.red, "exit")
  61. tState(menuButtons, false)
  62.  
  63. --Create Confirm Buttons
  64. confirmButtons = {}
  65. confirmButtons[1] = newButton(3, 10, 9, 5, "Confirm", colors.white, colors.green, "confirm")
  66. confirmButtons[2] = newButton(14, 10, 9 , 5, "Cancel", colors.white, colors.red, "deny")
  67. tState(confirmButtons, false)
  68.  
  69. --Number pad gets numbers intill enter is pressed
  70. function pad(header)
  71.   local order = ""
  72.   local cTest = true
  73.   tState(pinButtons, true)
  74.   while cTest do
  75.     term.clear()
  76.     term.setCursorPos(math.floor((size.x / 2) - (#header / 2)), 4)
  77.     term.write(header)
  78.     term.setCursorPos(math.floor((size.x / 2) - (#order / 2)), 6)
  79.     term.write(order)
  80.     drawDummyButtons("t")
  81.     local _e, _m, mx, my = os.pullEvent("mouse_click")
  82.     local _a = checkDummy(mx, my)
  83.     if _a then
  84.       if _a == "enter" then
  85.         cTest = false
  86.         tState(pinButtons, false)
  87.         return order
  88.       elseif _a == "back" then
  89.         order = string.sub(order, 1, #order - 1)
  90.       else
  91.         order = order .. _a
  92.       end
  93.     end
  94.   end
  95.   tState(pinButtons, false)
  96. end
  97.  
  98. --Gets alphanumerical/enter/backspace
  99. function getKey()
  100.   while true do
  101.     local _e, character = os.pullEvent()
  102.     if _e == "char" then
  103.       return character
  104.     elseif _e == "key" then
  105.       if character == 28 then
  106.         return "enter"
  107.       elseif character == 14 then
  108.         return "back"
  109.       end
  110.     end
  111.   end
  112. end
  113.  
  114. --Opens text input field with header
  115. function getName(_t)
  116.   local _check = true
  117.   local name = ""
  118.   term.setBackgroundColor(colors.white)
  119.   term.setTextColor(colors.black)
  120.   while _check do
  121.     term.clear()
  122.     term.setCursorPos((13 - math.floor(#_t / 2)), 5)
  123.     term.write(_t)
  124.     term.setCursorPos(math.floor((size.x / 2) - (#name / 2)), 7)
  125.     term.write(name)
  126.     local char = getKey()
  127.     if char == "enter" then
  128.       _check = false
  129.       return name
  130.     elseif char == "back" then
  131.       if #name > 0 then
  132.         name = string.sub(name, 1, #name - 1)
  133.       end
  134.     else
  135.       name = name .. char
  136.     end
  137.   end
  138. end
  139.  
  140. --Gets balance
  141. function getBal()
  142.   local balMsg = {
  143.     command = "get balance",
  144.   }
  145.   rednet.broadcast(balMsg, "VCOB")
  146.   local _ID, bal = rednet.receive("VCOBR")
  147.   return bal
  148. end
  149.  
  150. --Main Loop
  151. while true do
  152.   --First time account creation
  153.   if data.first then
  154.     --Name input
  155.     data.name = getName("Enter your name:")
  156.     --Pin creation
  157.     term.clear()
  158.     local newPin = pad("Create a pin:")
  159.     data.pin = newPin
  160.     local newAccMsg = {
  161.       pin = data.pin,
  162.       name = data.name,
  163.       command = "create"
  164.     }
  165.     rednet.broadcast(newAccMsg, "VCOB")
  166.     id, NAC = rednet.receive("NAC")
  167.     if NAC == "success" then
  168.       data.first = false
  169.       save()
  170.       loggedIn = true
  171.       os.setComputerLabel(data.name .. "'s bank card")
  172.     end
  173.   else
  174.     if loggedIn then
  175.       --Main screen
  176.       term.setBackgroundColor(colors.white)
  177.       term.setTextColor(colors.black)
  178.       term.clear()
  179.       data.balance = getBal()
  180.       tState(menuButtons, true)
  181.       drawButtons("t")
  182.       paintutils.drawFilledBox(3, 9, 23, 11, colors.lightGray)
  183.       term.setTextColor(colors.black)
  184.       term.setCursorPos(4,10)
  185.       term.setBackgroundColor(colors.lightGray)
  186.       term.write("Balance:")
  187.       term.setCursorPos((22 - #tostring(data.balance)), 10)
  188.       term.write(tostring("$" .. data.balance))
  189.       term.setBackgroundColor(colors.white)
  190.       paintutils.drawBox(3, 3, 23, 6, colors.lightBlue)
  191.       paintutils.drawImage(splash, 3, 3)
  192.       paintutils.drawFilledBox(1,1,26,1, colors.black)
  193.       term.setTextColor(colors.white)
  194.       term.setCursorPos(((size.x / 2) - (#data.name / 2)),1)
  195.       term.write(data.name)
  196.       local _e, _m, mx, my = os.pullEvent("mouse_click")
  197.       local _m = checkButtons(mx, my)
  198.       if _m then
  199.         if _m == "update" then
  200.           --:-)
  201.         elseif _m == "exit" then
  202.           --Reboot device
  203.           term.setCursorPos(1,1)
  204.           term.setTextColor(colors.white)
  205.           shell.run("reboot")
  206.         elseif _m == "send" then
  207.           --Send money
  208.           tState(menuButtons, false)
  209.           local reciever = getName("Send to:")
  210.           term.clear()
  211.           local amount = tonumber(pad("Amount:"))
  212.           if amount > 0 then
  213.             term.clear()
  214.             tState(menuButtons, false)
  215.             tState(pinButtons, false)
  216.             tState(confirmButtons, true)
  217.             drawButtons("t")
  218.             local confMess = "Send $" .. tostring(amount) .. " too " .. reciever .. "?"
  219.             term.setCursorPos(math.floor((size.x / 2) - (#confMess / 2)), 5)
  220.             term.setBackgroundColor(colors.white)
  221.             term.setTextColor(colors.black)
  222.             term.write(confMess)
  223.             local _x, _y, mx, my = os.pullEvent("mouse_click")
  224.             local _m = checkButtons(mx, my)
  225.             if _m == "confirm" then
  226.               local sendMsg = {
  227.                 command = "send",
  228.                 recName = reciever,
  229.                 sum = amount,
  230.                 pin = data.pin
  231.               }
  232.               rednet.broadcast(sendMsg, "VCOB")
  233.             end
  234.             tState(confirmButtons, false)
  235.           end
  236.           tState(menuButtons, true)
  237.         end
  238.       end
  239.     else
  240.       --Log in
  241.       local answer = pad(" ")
  242.       if answer == data.pin then
  243.         loggedIn = true
  244.       else
  245.         term.setCursorPos(9, 9)
  246.         term.setTextColor(colors.red)
  247.         term.write("Incorrect")
  248.         term.setTextColor(colors.black)
  249.         sleep(2)
  250.       end
  251.     end
  252.   end
  253. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement