Advertisement
AssortedBrunoz

NSP Banking system

Sep 14th, 2024 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.52 KB | None | 0 0
  1. local accounts = textutils.unserializeJSON(fs.open("bank_accounts/accounts.json", "r").readAll())
  2. local tanks = {peripheral.wrap("left"), peripheral.wrap("right")}
  3. local modem = peripheral.wrap("back")
  4.  
  5. modem.callRemote("redrouter_2", "setOutput", "right", false)
  6. modem.callRemote("redrouter_2", "setOutput", "top", false)
  7.  
  8. local function find_user(user)
  9.     for ID, account in pairs(accounts) do
  10.         if account.username == user then
  11.             return ID
  12.         end
  13.     end
  14.  
  15.     return false
  16. end
  17.  
  18. local function reset()
  19.     term.clear()
  20.     term.setCursorPos(1,1)
  21. end
  22.  
  23. local function search(str,substr)
  24.     return string.find(string.lower(str), substr)
  25. end
  26.  
  27. local function get_tank()
  28.     local exp = 0
  29.  
  30.     for _, tank in pairs(tanks) do
  31.         if tank.tanks()[1] == nil then
  32.             break;
  33.         end
  34.  
  35.         exp = exp + tank.tanks()[1].amount
  36.  
  37.     end
  38.  
  39.     return exp
  40. end
  41.  
  42. local function get_big_tank()
  43.     local tank = modem.callRemote("create:fluid_tank_0", "tanks")
  44.    
  45.     if tank[1] == nil then
  46.         return 0
  47.     end
  48.  
  49.     return tank[1].amount
  50. end
  51.  
  52. local function empty()
  53.     modem.callRemote("redrouter_2", "setOutput", "right", true)
  54.  
  55.     reset()
  56.  
  57.     write("Emptying..")
  58.  
  59.     repeat
  60.         os.sleep(1)
  61.     until get_tank() == 0
  62.  
  63.     modem.callRemote("redrouter_2", "setOutput", "right", false)
  64.  
  65.     write("\nEmpty!")
  66.  
  67.     os.sleep(2)
  68. end
  69.  
  70. local function shower(amount)
  71.     modem.callRemote("redrouter_2", "setOutput", "top", true)
  72.  
  73.     local current = get_big_tank()
  74.     local goal = get_big_tank() - amount
  75.  
  76.     repeat
  77.         current = get_big_tank()
  78.         print(current, goal)
  79.         os.sleep(1)
  80.     until current <= goal
  81.  
  82.     modem.callRemote("redrouter_2", "setOutput", "top", false)
  83. end
  84.  
  85. local function select_action(account)
  86.     reset()
  87.  
  88.     local username = accounts[account].username
  89.     local experience = accounts[account].amount
  90.  
  91.     write("LOGGED IN AS: \""..username.."\"\nPlease input one of the following letters to select an action:\nW: Withdraw Experience\nD: Deposit Experience\nI: See account information\nC: Cancel\n")
  92.  
  93.     local input = read()
  94.  
  95.     if search(input,"i") then
  96.         reset()
  97.         write("THE ACCOUNT \""..username.."\" HAS "..experience.." EXPERIENCE POINTS.")
  98.  
  99.         os.sleep(5)
  100.     elseif search(input,"d") then
  101.         reset()
  102.  
  103.         local function update_screen()
  104.             local last = get_tank()
  105.  
  106.             reset()
  107.             write("Press enter to deposit\nCurrent amount: "..last.." experience points")
  108.  
  109.             while true do
  110.                 local current = get_tank()
  111.                
  112.                 if current ~= last then
  113.                     reset()
  114.                     write("Press enter to deposit\nCurrent amount: "..current.." experience points")
  115.                 end
  116.                
  117.                 os.sleep(0.05)
  118.             end
  119.         end
  120.  
  121.         local function wait_for_enter()
  122.             repeat
  123.                 local _, key = os.pullEvent("key")
  124.             until key == keys.enter
  125.         end
  126.  
  127.         parallel.waitForAny(update_screen, wait_for_enter)
  128.  
  129.         local to_add = get_tank()
  130.  
  131.         empty()
  132.  
  133.         reset()
  134.         write(to_add.." experience points successfully deposited to \""..username.."\"")
  135.  
  136.         accounts[account].amount = accounts[account].amount + to_add
  137.  
  138.         os.sleep(5)
  139.     elseif search(input, "w") then
  140.         reset()
  141.         write("Please Input how much experience you\'d like to withdraw: ")
  142.  
  143.         local amount = tonumber(io.read())
  144.  
  145.         if accounts[account].amount - amount < 0 then
  146.             write("Nice try, you do not have the funds for such a withdrawal.")
  147.             os.sleep(5)
  148.             return
  149.         end
  150.  
  151.         shower(amount)
  152.  
  153.         accounts[account].amount = accounts[account].amount - amount
  154.  
  155.         reset()
  156.         write(amount.." experience points successfully withdrawn from \""..username.."\"")
  157.    
  158.         os.sleep(5)
  159.     elseif search(input, "c") then
  160.     else
  161.         select_action(account)
  162.     end
  163. end
  164.  
  165. local function login(input, account, attempt)
  166.  
  167.     if input == accounts[account].password then
  168.         select_action(account)
  169.     elseif attempt ~= 3 then
  170.  
  171.         write("\nINCORRECT PASSWORD, TRY AGAIN ("..attempt.."/3): ")
  172.  
  173.         login(read("*"), account, attempt + 1)
  174.     end
  175.  
  176. end
  177.  
  178. local function create(user)
  179.     reset()
  180.     write("Create a password: ")
  181.     local pwd = read("*")
  182.  
  183.     table.insert(accounts, {username = user, password = pwd, amount = 0})
  184.  
  185.     reset()
  186.     write("Account successfully created!")
  187.  
  188.     os.sleep(3)
  189. end
  190.  
  191. local function main_loop()
  192.    
  193.     reset()
  194.  
  195.     write("NPS EXP BANKING SYSTEM.\n\n\nUSERNAME: ")
  196.  
  197.     local username = read("*")
  198.     local account = find_user(username)
  199.  
  200.     if account then
  201.         write("\nPASSWORD: ")
  202.  
  203.         login(read("*"), account, 1)
  204.     else
  205.         reset()
  206.         write("There is no account with the username \""..username.."\" would you like to create an account with that username? (Y/N): ")
  207.  
  208.         local input = read()
  209.  
  210.         if search(input, "y") then
  211.             create(username)
  212.         elseif not search(input, "n") then
  213.             reset()
  214.             write("There is no account with the username \""..username.."\" would you like to create an account with that username? (Y/N)")
  215.         end
  216.  
  217.     end
  218.  
  219.     local t = fs.open("bank_accounts/accounts.json", "w+")
  220.     t.write(textutils.serialiseJSON(accounts))
  221.     t.close()
  222.  
  223.     main_loop()
  224.  
  225. end
  226.  
  227. main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement