Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local accounts = textutils.unserializeJSON(fs.open("bank_accounts/accounts.json", "r").readAll())
- local tanks = {peripheral.wrap("left"), peripheral.wrap("right")}
- local modem = peripheral.wrap("back")
- modem.callRemote("redrouter_2", "setOutput", "right", false)
- modem.callRemote("redrouter_2", "setOutput", "top", false)
- local function find_user(user)
- for ID, account in pairs(accounts) do
- if account.username == user then
- return ID
- end
- end
- return false
- end
- local function reset()
- term.clear()
- term.setCursorPos(1,1)
- end
- local function search(str,substr)
- return string.find(string.lower(str), substr)
- end
- local function get_tank()
- local exp = 0
- for _, tank in pairs(tanks) do
- if tank.tanks()[1] == nil then
- break;
- end
- exp = exp + tank.tanks()[1].amount
- end
- return exp
- end
- local function get_big_tank()
- local tank = modem.callRemote("create:fluid_tank_0", "tanks")
- if tank[1] == nil then
- return 0
- end
- return tank[1].amount
- end
- local function empty()
- modem.callRemote("redrouter_2", "setOutput", "right", true)
- reset()
- write("Emptying..")
- repeat
- os.sleep(1)
- until get_tank() == 0
- modem.callRemote("redrouter_2", "setOutput", "right", false)
- write("\nEmpty!")
- os.sleep(2)
- end
- local function shower(amount)
- modem.callRemote("redrouter_2", "setOutput", "top", true)
- local current = get_big_tank()
- local goal = get_big_tank() - amount
- repeat
- current = get_big_tank()
- print(current, goal)
- os.sleep(1)
- until current <= goal
- modem.callRemote("redrouter_2", "setOutput", "top", false)
- end
- local function select_action(account)
- reset()
- local username = accounts[account].username
- local experience = accounts[account].amount
- 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")
- local input = read()
- if search(input,"i") then
- reset()
- write("THE ACCOUNT \""..username.."\" HAS "..experience.." EXPERIENCE POINTS.")
- os.sleep(5)
- elseif search(input,"d") then
- reset()
- local function update_screen()
- local last = get_tank()
- reset()
- write("Press enter to deposit\nCurrent amount: "..last.." experience points")
- while true do
- local current = get_tank()
- if current ~= last then
- reset()
- write("Press enter to deposit\nCurrent amount: "..current.." experience points")
- end
- os.sleep(0.05)
- end
- end
- local function wait_for_enter()
- repeat
- local _, key = os.pullEvent("key")
- until key == keys.enter
- end
- parallel.waitForAny(update_screen, wait_for_enter)
- local to_add = get_tank()
- empty()
- reset()
- write(to_add.." experience points successfully deposited to \""..username.."\"")
- accounts[account].amount = accounts[account].amount + to_add
- os.sleep(5)
- elseif search(input, "w") then
- reset()
- write("Please Input how much experience you\'d like to withdraw: ")
- local amount = tonumber(io.read())
- if accounts[account].amount - amount < 0 then
- write("Nice try, you do not have the funds for such a withdrawal.")
- os.sleep(5)
- return
- end
- shower(amount)
- accounts[account].amount = accounts[account].amount - amount
- reset()
- write(amount.." experience points successfully withdrawn from \""..username.."\"")
- os.sleep(5)
- elseif search(input, "c") then
- else
- select_action(account)
- end
- end
- local function login(input, account, attempt)
- if input == accounts[account].password then
- select_action(account)
- elseif attempt ~= 3 then
- write("\nINCORRECT PASSWORD, TRY AGAIN ("..attempt.."/3): ")
- login(read("*"), account, attempt + 1)
- end
- end
- local function create(user)
- reset()
- write("Create a password: ")
- local pwd = read("*")
- table.insert(accounts, {username = user, password = pwd, amount = 0})
- reset()
- write("Account successfully created!")
- os.sleep(3)
- end
- local function main_loop()
- reset()
- write("NPS EXP BANKING SYSTEM.\n\n\nUSERNAME: ")
- local username = read("*")
- local account = find_user(username)
- if account then
- write("\nPASSWORD: ")
- login(read("*"), account, 1)
- else
- reset()
- write("There is no account with the username \""..username.."\" would you like to create an account with that username? (Y/N): ")
- local input = read()
- if search(input, "y") then
- create(username)
- elseif not search(input, "n") then
- reset()
- write("There is no account with the username \""..username.."\" would you like to create an account with that username? (Y/N)")
- end
- end
- local t = fs.open("bank_accounts/accounts.json", "w+")
- t.write(textutils.serialiseJSON(accounts))
- t.close()
- main_loop()
- end
- main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement