Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- MCNET Bank Server Software
- Version 0.1
- ]]
- --[[
- Request Format
- local quest = {
- requestType (auth, trans, create),
- requestAction (deposit, withdraw),
- accountName,
- accountPin,
- additionalParameter (amount)
- }
- The server will respond with either
- true (fuck you) or false (fuck you too)
- ]]
- --Variables
- local trustedClients = {}
- --Functions
- function isTrustedClient(id)
- for i = 1, table.getn(trustedClients) do
- if (id == trustedClients[i]) then
- return true
- end
- end
- return false
- end
- function genAccountName(accountName)
- return "/accounts/" .. accountName
- end
- function getAccountInfo(accountName)
- if (fs.exists(accountName)) then
- local account = fs.open(genAccountName(accountName), "r")
- if (account == nil) then return nil end
- local pin = account.readLine()
- local balance = account.readLine()
- account.close()
- return pin, balance
- end
- return nil
- end
- function updateAccountInfo(accountName, pin, balance)
- if (fs.exists(accountName)) then
- local account = fs.open(genAccountName(accountName), "w")
- if (account == nil) then return false end
- account.writeLine(pin)
- account.writeLine(balance)
- account.flush()
- account.close()
- return true
- end
- return false
- end
- function verifyPin(accountName, userPin)
- pin, balance = getAccountInfo(accountName)
- return (pin == userPin)
- end
- function deposit(accountName, userPin, amount)
- if (verifyPin(accountName, userPin)) then
- pin, balance = getAccountInfo(genAccountName(accountName))
- updateAccountInfo(accountName, userPin, balance + amount)
- return true
- end
- return false
- end
- function withdraw(accountName, userPin, amount)
- pin, balance = getAccountInfo(accountName)
- if (balance < amount) then
- return false
- end
- return deposit(accountName, userPin, -1 * amount)
- end
- function createAccount(accountName, userPin)
- if (fs.exists(genAccountName(accountName))) then
- return false
- end
- local acnt = fs.open(genAccountName(accountName), "w")
- acnt.close();
- return updateAccountInfo(accountName, userPin, 0)
- end
- --The Actual Shit
- while true do
- id, strRequest = rednet.receive()
- request = textutils.unserialize(strRequest)
- if (not request == nil and isTrustedClient(id)) then
- if (table.getn(request) == 5) then
- if (request[1] == "auth") then
- if (verifyPin(request[3], request[4])) then
- rednet.send(id, "true")
- else
- rednet.send(id, "false")
- end
- end
- if (request[1] == "trans") then
- local accountName = request[3]
- local accountPin = request[4]
- if (not verifyPin(accountName, accountPin)) then
- rednet.send(id, "false")
- else
- if (request[2] == "deposit") then
- deposit(accountName, accountName, accountPin, request[5])
- end
- if (request[2] == "withdraw") then
- if (withdraw(accountName, accountPin, request[5])) then
- rednet.send(id, "true")
- end
- rednet.send(id, "false")
- end
- end
- end
- if (request[1] == "create") then
- local accountName = request[3]
- local accountPin = request[4]
- if (createAccount(accountName, accountPin)) then
- rednet.send(id, "true")
- else
- rednet.send(id, "false")
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement