Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local jenisis = require("jenisis")
- local component = require("component")
- local fs = require("filesystem")
- local textutils = require("serialization")
- local beep = require("computer")
- local event = require("event")
- local modem = component.getPrimary("modem")
- local gpu = component.getPrimary("gpu")
- local scrX, scrY = gpu.getResolution()
- local function broadcast(packet)
- local packet_ = textutils.serialize(packet)
- modem.broadcast(1024, packet_)
- end
- local function receive()
- event_ = {event.pull(5, "modem_message")}
- return textutils.unserialize(event_[6])
- end
- currentaccount = {}
- currentaccount.accountName = "You shouldn't see this. Call Alex"
- if fs.exists("/cache/bank.cache") then
- file = io.open("/cache/bank.cache")
- currentaccount = textutils.unserialize(file:read("*a"))
- file:close()
- loggedIn = true
- end
- local function getFunds()
- if not loggedIn then
- jenisis.errorMes("Please log in.")
- return
- end
- local packet = {
- type = "getfunds",
- target = currentaccount.accountName,
- account = currentaccount
- }
- broadcast(packet)
- local response = receive()
- if response ~= false then
- jenisis.errorMes("You have $"..response, "green")
- return
- else
- jenisis.errorMes("Unexpected Error")
- end
- end
- local function transfer()
- if not loggedIn then
- jenisis.errorMes("Please log in.")
- return
- end
- local packet = {
- account = currentaccount,
- type = "transfer"
- }
- packet.target = jenisis.inputBar("Whoms't ya sendin dollas 2 b?")
- packet.amount = tonumber(jenisis.inputBar("Enter amount:"))
- broadcast(packet)
- response = receive()
- if response then
- jenisis.errorMes("Transfer Succesful.", "green")
- else
- jenisis.errorMes("Unexpected Error")
- end
- end
- local function newAccount()
- local newacc = {}
- newacc.accountName = jenisis.inputBar("Enter new account name:")
- newacc.accountKey = jenisis.inputBar("Enter Passkey:", true)
- newacc.balance = 0
- newacc.isFlagged = false
- newacc.isFront = false
- newacc.owners = {}
- packet = {
- type = "newaccount",
- account = newacc
- }
- broadcast(packet)
- result = receive()
- if result == true then
- jenisis.errorMes("Account creation succesful.", "green")
- else
- jenisis.errorMes("Unexpected Error")
- end
- end
- local function doLog()
- if loggedIn then
- currentaccount.accountName = nil
- currentaccount.accountKey = nil
- loggedIn = false
- fs.remove("/bank/bank.cache")
- jenisis.errorMes("Logged Out", "green")
- return
- end
- currentaccount.accountName = jenisis.inputBar("Enter account:")
- currentaccount.accountKey = jenisis.inputBar("Enter passkey:", true)
- local packet = {
- type = "auth",
- account = currentaccount
- }
- broadcast(packet)
- local result = receive()
- if result then
- jenisis.errorMes("Logged In", "green")
- loggedIn = true
- local file = io.open("/cache/bank.cache", "w")
- file:write(textutils.serialize(currentaccount))
- file:close()
- else
- jenisis.errorMes("Authentication Failed")
- loggedIn = false
- end
- end
- local function centerText(line, text)
- local length = string.len(text)
- local target = (scrX - length) / 2
- term.setCursor(target, line)
- term.write(text)
- end
- local function displayInfo(table)
- local amount = #table
- local lTarget = ((scrY - amount) / 2) - 1
- for i=1, amount do
- centerText(lTarget + i, table[i])
- end
- end
- local function manageAccount()
- if not loggedIn then
- jenisis.errorMes("Please Log In")
- end
- if jenisis.inputBar("Please re-enter password:", true) ~= currentaccount.accountKey then
- return false
- end
- manStatic = {
- "Account Management Menu"
- }
- manMenu = {
- "Get Full Info",
- "View Owners",
- "Setup Multi-Ownership",
- "Exit"
- }
- while true do
- cselected = jenisis.drawScreen(manStatic, manMenu)
- if cselected == 1 then
- local packet = {
- type = "getinfo",
- account = currentaccount
- }
- broadcast(packet)
- local result = receive()
- term.clear()
- local info = {
- "Account Name: "..result.accountName,
- "Account Key: "..result.accountKey,
- "Account Balance: "..result.balance,
- "Account Flag: "..tostring(result.isFlagged),
- "Account Co-Owned: "..tostring(result.isFront)
- }
- displayInfo(info)
- event.pull("key_down")
- elseif cselected == 2 then
- term.clear()
- local packet = {
- type = "getinfo",
- account = currentaccount
- }
- broadcast(packet)
- currentaccount = receive()
- if not currentaccount.isFront then
- centerText(scrY / 2, "Privatly Owned Account")
- else
- local ownersList = {}
- for k, v in pairs(currentaccount.owners) do
- table.insert(ownersList, "Account "..k.." owns "..(v * 100).."%")
- end
- displayInfo(ownersList)
- end
- event.pull("key_down")
- elseif cselected == 3 then
- local tutorial = {
- "A prompt will appear asking you to enter the new owners,",
- "Followed by the % share they own. Ensure that the total",
- "shares add up to 100% or you will be asked to restart.",
- "Press any button to continue..."
- }
- displayInfo(tutorial)
- event.pull("key_down")
- local done = nil
- local total = 0
- local newOwners = {}
- repeat
- local name = jenisis.inputBar("Input the owner name:")
- local amount = jenisis.inputBar("Input the % share:")
- local i, j = string.find(amount, "%%")
- if i ~= nil then
- local amount = string.sub(0, string.len(amount) - 1)
- end
- amount = tonumber(amount)
- total = total + amount
- if total == 100 then
- done = true
- newOwners[name] = amount * 0.01
- elseif total > 100 then
- jenisis.errorMes("You have gone over 100. Restarting...")
- total = 0
- else
- tutorial[4] = (" Current counted shares at "..total.."% ")
- displayInfo(tutorial)
- newOwners[name] = (amount * 0.01)
- end
- until done
- local temp = 0
- for k, v in pairs(newOwners) do
- temp = temp + v
- end
- if temp ~= 1 then
- jenisis.errorMes("Shares exeeded 100% ("..temp..")")
- return
- else
- currentaccount.owners = newOwners
- end
- local packet = {
- type = "dofront",
- account = currentaccount
- }
- broadcast(packet)
- local result = receive()
- if result then
- jenisis.errorMes("Successfully set up ownership", "green")
- else
- jenisis.errorMes("Unexpected Error")
- end
- else
- return
- end
- end
- end
- while true do
- static = {
- "Bank ATM by Horyzon",
- ""
- }
- menu = {
- "Check Funds",
- "Transfer",
- "Account Management",
- "New Account",
- "",
- "Exit"
- }
- if loggedIn then
- static[2] = ("Logged in as "..currentaccount.accountName)
- menu[5] = "Log Out"
- else
- static[2] = "Please log in."
- menu[5] = "Log In"
- end
- result = jenisis.drawScreen(static, menu)
- if result == 1 then
- getFunds()
- elseif result == 2 then
- transfer()
- elseif result == 3 then
- manageAccount()
- elseif result == 4 then
- newAccount()
- elseif result == 5 then
- doLog()
- elseif result == 6 then
- break
- end
- end
Add Comment
Please, Sign In to add comment