Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------- Bank Server
- -------------------- Params
- local filePath = "disk/"
- local modemSide = "top"
- local text_account_start_description = "¡Bienvenido a Mermegold!"
- local text_error_invalidamount = "Monto invalido"
- local text_error_from = "Cuenta origen inválida"
- local text_error_to = "Cuenta destino inválida"
- local text_error_account = "Cuenta inexistente"
- local text_error_same = "No se puede usar la misma cuenta como destino y origen a la vez"
- local text_error_notenoughbalance = "Balance insuficiente"
- local text_success_transaction = "Transacción exitosa"
- local text_success_update = "Actualización exitosa"
- local text_success_account = "Cuenta creada con exito. Código: "
- --------------------
- -------------------- Variables
- local clientData = {}
- --------------------
- -------------------- Funciones
- local function loadClients()
- local f = fs.open(filePath.."clientList.txt", "r")
- if (f ~= nil) then
- local line = f.readLine()
- clientData = {}
- repeat
- local key = line
- clientData[key] = {}
- local cf = fs.open(filePath.."clientData/"..key.."/info.txt", "r")
- local cline = cf.readLine()
- repeat
- if (cline == "name") then
- cline = cf.readLine()
- clientData[key].name = cline
- elseif (cline == "balance") then
- cline = cf.readLine()
- clientData[key].balance = tonumber(cline)
- elseif (cline == "color") then
- cline = cf.readLine()
- clientData[key].color = tonumber(cline)
- end
- cline = cf.readLine()
- until cline == nil or cline == ""
- cf.close()
- line = f.readLine()
- until line == nil or line == ""
- f.close()
- end
- end
- local function getClientData(data)
- loadClients()
- return true, clientData
- end
- local function updateClientFile(key)
- local f = fs.open(filePath.."clientData/"..key.."/info.txt", "w")
- if (f == nil) then return false, text_error_account end
- f.writeLine("name")
- f.writeLine(clientData[key].name)
- f.writeLine("balance")
- f.writeLine(tostring(clientData[key].balance))
- f.writeLine("color")
- f.writeLine(tostring(clientData[key].color))
- f.close()
- return true, text_success_update
- end
- local function appendTransactionToLog(from, to, amount, balance, time, description)
- local f = fs.open(filePath.."clientData/"..from.."/log.txt", "a")
- f.writeLine("other")
- f.writeLine(to)
- f.writeLine("amount")
- f.writeLine(tostring(-amount))
- f.writeLine("balance")
- f.writeLine(tostring(balance))
- f.writeLine("time")
- f.writeLine(time)
- f.writeLine("description")
- f.writeLine(description)
- f.writeLine("")
- f.close()
- end
- local function getCurrentTime()
- return "--/--/---- --:--"
- end
- -- Move money from one account to another
- local function transaction(data) --from, to, amount, description
- local from = data.from
- local to = data.to
- local amount = tonumber(data.amount)
- local description = data.description
- if (amount == 0 or amount == nil) then
- return false, text_error_invalidamount
- end
- loadClients()
- if (from == to) then
- return false, text_error_same
- end
- if (not clientData[from]) then
- return false, text_error_from.." ("..from..")"
- end
- if (not clientData[to]) then
- return false, text_error_to.." ("..to..")"
- end
- local previousFromBalance = clientData[from].balance
- local newFromBalance = previousFromBalance-amount
- if (newFromBalance < 0) then
- return false, text_error_notenoughbalance
- end
- local previousToBalance = clientData[to].balance
- local newToBalance = previousToBalance+amount
- local time = getCurrentTime()
- clientData[from].balance = newFromBalance
- updateClientFile(from)
- clientData[to].balance = newToBalance
- updateClientFile(to)
- appendTransactionToLog(from, to, amount, newFromBalance, time,description)
- appendTransactionToLog(to, from, -amount, newToBalance, time, description)
- return true, text_success_transaction
- end
- local function deposit(data) --key, amount
- local key = data.key
- local amount = tonumber(data.amount)
- local description = "Depósito"
- if (amount == 0 or amount == nil) then
- return false, text_error_invalidamount
- end
- loadClients()
- if (clientData[key] == nil) then
- return false, text_error_from.." ("..key..")"
- end
- local previousBalance = clientData[key].balance
- local newBalance = previousBalance+amount
- local time = getCurrentTime()
- print(clientData[key].name.." ha depositado "..amount)
- clientData[key].balance = newBalance
- updateClientFile(key)
- appendTransactionToLog(key, key, amount, newBalance, time, description)
- return true, text_success_transaction
- end
- local function withdraw(data) --key, amount
- local key = data.key
- local amount = tonumber(data.amount)
- local description = "Retiro"
- if (amount == 0 or amount == nil) then
- return false, text_error_invalidamount
- end
- loadClients()
- if (key == nil or clientData[key] == nil) then
- return false, text_error_from.." ("..tostring(key)..")"
- end
- local previousBalance = clientData[key].balance
- local newBalance = previousBalance-amount
- if (newBalance < 0) then
- return false, text_error_notenoughbalance
- end
- local time = getCurrentTime()
- clientData[key].balance = newBalance
- updateClientFile(key)
- appendTransactionToLog(key, key, -amount, newBalance, time, description)
- return true, text_success_transaction
- end
- local function getTransactionLog(data) --key
- local key = data.key
- local log = {}
- local logCounter = 1
- local f = fs.open(filePath.."clientData/"..key.."/log.txt", "r")
- local line = f.readLine()
- while line ~= nil do
- local lline = line
- local other = ""
- local amount = 0
- local balance = 0
- local time = ""
- local description = ""
- while lline ~= "" and lline ~= nil do
- if (lline == "other") then
- other = f.readLine()
- elseif (lline == "amount") then
- amount = f.readLine()
- elseif (lline == "balance") then
- balance = f.readLine()
- elseif (lline == "time") then
- time = f.readLine()
- elseif (lline == "description") then
- description = f.readLine()
- end
- lline = f.readLine()
- end
- log[logCounter] = {
- ["other"] = other,
- ["amount"] = amount,
- ["balance"] = balance,
- ["time"] = time,
- ["description"] = description
- }
- logCounter = logCounter+1
- line = f.readLine()
- end
- return true, log
- end
- -- Create a new client, with its files and folders, given a name, a starting balance and a color
- local function newClient(data) --name, balance, color
- local name = data.name
- local color = data.color
- local bankKey = "1000"
- local firstFreeClientNumber = 0
- while firstFreeClientNumber < 9999 do
- firstFreeClientNumber = firstFreeClientNumber+1
- local free = true
- for k, v in pairs(clientData) do
- local clientNumber = tonumber(string.sub(k, 5, 8))
- if (clientNumber == firstFreeClientNumber) then
- free = false
- break
- end
- end
- if (free) then
- break
- end
- end
- local clientNumber = tostring(firstFreeClientNumber)
- clientNumber = string.rep("0", 4-string.len(clientNumber))..clientNumber
- local randomKey = ""
- for i=1, 8 do
- randomKey = randomKey..tostring(math.random(10)-1)
- end
- local key = bankKey..clientNumber..randomKey
- local f = fs.open(filePath.."clientList.txt", "a")
- f.writeLine(key)
- f.close()
- fs.makeDir(filePath.."clientData")
- fs.makeDir(filePath.."clientData/"..key)
- -- Create basic info
- clientData[key] = {
- ["name"] = name,
- ["balance"] = 0,
- ["color"] = color
- }
- updateClientFile(key)
- -- Create transaction log
- local f = fs.open(filePath.."clientData/"..key.."/log.txt", "w")
- f.close()
- return true, "Cuenta creada con exito"
- end
- local function deleteAccount(data) --key
- local key = data.key
- clientData[key] = nil
- local f = fs.open(filePath.."clientList.txt", "w")
- for k, v in pairs(clientData) do
- f.writeLine(k)
- end
- f.close()
- fs.delete(filePath.."clientData/"..key)
- return true, "Cuenta borrada"
- end
- local function getAPIVersion(data)
- local f = fs.open(filePath.."apiversion.txt", "r")
- if (f == nil) then
- return false, nil
- end
- local version = f.readLine()
- local pastebin = f.readLine()
- f.close()
- return true, {["version"] = version, ["pastebin"] = pastebin}
- end
- local function getATMVersion(data)
- local f = fs.open(filePath.."atmversion.txt", "r")
- if (f == nil) then
- return false, nil
- end
- local version = f.readLine()
- local pastebin = f.readLine()
- f.close()
- return true, {["version"] = version, ["pastebin"] = pastebin}
- end
- local function processRequest(func, sender, data)
- local success, response = func(data)
- local message = {
- ["success"] = success,
- ["response"] = response
- }
- --print("Responding.")
- rednet.send(sender, message, "mermegold")
- end
- ---------------- Start
- loadClients()
- rednet.open(modemSide)
- while true do
- local sender, message = rednet.receive("mermegold")
- --term.write("#"..sender.." requested "..message.action..". ")
- if (message.action == "getClientData") then --------------- Get Client Data
- processRequest(getClientData, sender, message)
- elseif (message.action == "getTransactionLog") then ------- Get Transaction Log
- processRequest(getTransactionLog, sender, message)
- elseif (message.action == "transaction") then ------------- Make a transaction
- processRequest(transaction, sender, message)
- elseif (message.action == "deposit") then ----------------- Make a deposit
- processRequest(deposit, sender, message)
- elseif (message.action == "withdraw") then ------------- Make a withdrawal
- processRequest(withdraw, sender, message)
- elseif (message.action == "new") then --------------------- Create an account
- processRequest(newClient, sender, message)
- elseif (message.action == "delete") then ------------------ Delete an account
- processRequest(deleteAccount, sender, message)
- elseif (message.action == "getAPIVersion") then ----------- Get API Version
- processRequest(getAPIVersion, sender, message)
- elseif (message.action == "getATMVersion") then ----------- Get ATM Version
- processRequest(getATMVersion, sender, message)
- else ------------------------------------------------------ Not a valid message
- --print("No valid response.")
- local message = {
- ["success"] = false,
- ["response"] = "Invalid request"
- }
- rednet.send(sender, message, "mermegold")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement