Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Kredit API by JLW222 (James222)
- --Build V 1.0
- --Array Database
- local name = {}
- local money = {}
- local password = {}
- local inTransactions = {}
- local outTransactions = {}
- local isBanned = {}
- --Enter a number from 0 to 100 (%)
- local transactionFee = 1
- --The server can hold money because of transaction fees
- local serverMoney = 0
- --The server's password. It is asked when using admin commands
- --CHANGE ME <=========================================================================
- local serverAdminPassword = "beta2"
- --The encryption's password. It is used to encrypt passwords and data
- --CHANGE ME <=========================================================================
- --local encryptionPassword = "68026802"
- --Defines is KREDITAPI should automatically read the accounts file when api is ran
- local autoRead = true
- --Function Variables. Don't modify these
- local database = ""
- local completeDatabase = {}
- local foundAccountC = false
- local foundAccountCk = false
- local foundAccountE = false
- local foundAccountCr = false
- local databases = {}
- local splitDatabases = {}
- --Functions
- --os.loadAPI("enc")
- --Writes data from accounts file to tables
- function applyDatabase()
- if database == "" then
- print("Database must be loaded first or is empty")
- return false
- else
- databases[1] = textutils.unserialize(database)
- splitDatabases = split(databases[1], "}")
- name = textutils.unserialize(splitDatabases[1])
- money = textutils.unserialize(splitDatabases[2])
- password = textutils.unserialize(splitDatabases[3])
- inTransactions = textutils.unserialize(splitDatabases[4])
- outTransactions = textutils.unserialize(splitDatabases[5])
- isBanned = textutils.unserialize(splitDatabases[6])
- end
- end
- --Save the database's data
- function saveDatabase()
- if fs.exists("KACCOUNTS") then
- local h = fs.open("KACCOUNTS", "w")
- completeDatabase[1] = textutils.serialize(name) .. textutils.serialize(money) .. textutils.serialize(password) .. textutils.serialize(inTransactions) .. textutils.serialize(outTransactions) .. textutils.serialize(isBanned)
- h.write(textutils.serialize(completeDatabase[1]))
- h.close()
- else
- loadDatabase()
- saveDatabase()
- end
- end
- --Loads the bank account file into the tables
- function loadDatabase()
- if fs.exists("KACCOUNTS") then
- local h = fs.open("KACCOUNTS", "r")
- database = h.readAll()
- h.close()
- print("Accounts database loaded")
- applyDatabase()
- else
- local h = fs.open("KACCOUNTS", "a")
- h.close()
- print("Accounts database created")
- end
- end
- --Lists all accounts names
- function listAccounts()
- local accounts = ""
- if count() == 0 then
- return "There are no accounts in this database"
- else
- for i = 1, count() do
- accounts = accounts .. " | " .. name[i]
- end
- return accounts
- end
- end
- --Used to create an account
- function createAccount(userName, userPassword)
- for i = 0, count() do
- if userName == name[i] then
- print("Account already exists")
- foundAccountCr = false
- return false
- end
- if i == count() then
- if foundAccountCr == false then
- -- encryptedPassword = enc.encrypt(userPassword, encryptionPassword)
- table.insert(name, userName)
- table.insert(money, 0)
- table.insert(password, userPassword)
- table.insert(inTransactions, "")
- table.insert(outTransactions, "")
- table.insert(isBanned, false)
- return true
- end
- end
- end
- end
- --Used to check account balance
- function check(userName, userPassword)
- if checkCredentials(userName, userPassword) == true then
- for i = 0, count() do
- if userName == name[i] then
- foundAccountCk = true
- return money[i]
- end
- if i == count() then
- if foundAccountCk == false then
- print("Account does not exist")
- foundAccountCk = false
- end
- end
- end
- end
- end
- --Used to give money to certain account
- function give(userName, userPassword, amount, receiverName, escapesFee)
- if type(amount) == "number" then
- if checkCredentials(userName, userPassword) == true then
- if (check(userName, userPassword) - amount) >= 0 then
- if checkUserExists(receiverName) == true then
- --Set the receiver's amount of money
- for i = 0, count() do
- if receiverName == name[i] then
- local moneyResult = money[i] + amount
- if escapesFee == true then
- table.insert(money, i, moneyResult)
- else
- table.insert(money, i, transac(moneyResult))
- end
- local osdate = os.time()
- local inTransac = inTransactions[i] .. "| From: " .. userName .. " | +" .. amount .. "$ | " .. osdate .. " |,"
- table.insert(inTransactions, i, inTransac)
- end
- end
- --Set the sender's amount of money
- for i = 0, count() do
- if userName == name[i] then
- local moneyResult = money[i] - amount
- table.insert(money, i, moneyResult)
- local osdate = os.time()
- local outTransac = outTransactions[i] .. "| To: " .. receiverName .. " | -" .. amount .. "$ | " .. osdate .. " |,"
- table.insert(outTransactions, i, outTransac)
- end
- end
- saveDatabase()
- return true
- else
- print("Receiver's account does not exist")
- return false
- end
- else
- return false
- end
- end
- else
- print("Amount provided is not numeric")
- return false
- end
- end
- -- check if banned
- function checkIfBanned(userName)
- for i = 0, count() do
- if userName == name[i] then
- return isBanned[i] == true
- end
- end
- end
- --Verify credentials
- function checkCredentials(userName, userPassword)
- -- local encryptedPassword = enc.decrypt(userPassword, encryptionPassword)
- for i = 0, count() do
- if userName == name[i] then
- if userPassword == password[i] then
- foundAccountC = true
- return true
- else
- foundAccountC = true
- end
- end
- if i == count() then
- if foundAccountC == false then
- print("Account does not exist")
- foundAccountC = false
- else
- print("Passwords do not match")
- foundAccountC = false
- return false
- end
- end
- end
- end
- --Used to check if a user exists. Does not require password
- function checkUserExists(userName)
- for i = 0, count() do
- if userName == name[i] then
- foundAccountE = true
- return true
- end
- if i == count() then
- if foundAccountE == false then
- print("Account does not exist")
- return false
- end
- end
- end
- end
- --Used to calculate amount after transactionFee
- function transac(amount)
- if transactionFee >= 101 then
- print("No fee applied: Transaction fee is over than 100!")
- return 0
- end
- local fee = (transactionFee / 100) * amount
- local finalAmount = amount - fee
- serverMoney = serverMoney + fee
- return finalAmount
- end
- --Used to count how many accounts there are
- function count()
- local count = 0
- for _ in pairs(name) do count = count + 1 end
- return count
- end
- --ADMIN FUNCTIONS, BE CAREFUL WITH THESE
- --Used to set the money of a user's account
- function setMoney(userName, amount, serverPassword)
- if serverPassword == serverAdminPassword then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- if type(amount) == "number" then
- table.insert(money, i, amount)
- saveDatabase()
- return true
- else
- print("Amount provided is not numeric")
- return false
- end
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Used to show money of all users
- function listMoney(serverPassword)
- if serverPassword == serverAdminPassword then
- local moneyList = ""
- if count() == 0 then
- return "There are no accounts in this database"
- else
- for i = 1, count() do
- moneyList = moneyList .. " | " .. name[i] .. ": " .. money[i] .. "$"
- end
- return moneyList
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- -- Used to ban a user
- function ban(userName)
- loadDatabase()
- for i = 0, count() do
- if userName == name[i] then
- table.insert(isBanned, i, true)
- saveDatabase()
- return true
- end
- end
- end
- -- Used to un ban a user
- function unban(userName)
- loadDatabase()
- for i = 0, count() do
- if userName == name[i] then
- table.insert(isBanned, i, false)
- saveDatabase()
- return true
- else
- return false
- end
- end
- end
- --Used to show money of a specific user
- function showMoney(userName, serverPassword)
- if serverPassword == serverAdminPassword then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- return name[i] .. " has " .. money[i] .. "$"
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Used to give money to a user instead having to manually calculate amount and use setMoney()
- function addMoney(userName, amount, serverPassword)
- if serverPassword == serverAdminPassword then
- if type(amount) == "number" then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- local moneyResult = money[i] + amount
- table.insert(money, i, moneyResult)
- saveDatabase()
- return true
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Amount provided is not numeric")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Used to remove money from a user instead having to manually calculate amount and use setMoney()
- function removeMoney(userName, amount, serverPassword)
- if serverPassword == serverAdminPassword then
- if type(amount) == "number" then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- local moneyResult = money[i] - amount
- table.insert(money, i, moneyResult)
- saveDatabase()
- return true
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Amount provided is not numeric")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Deletes the user's account
- function deleteAccount(userName, serverPassword)
- if serverPassword == serverAdminPassword then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- table.remove(diskID, i)
- table.remove(name, i)
- table.remove(money, i)
- table.remove(password, i)
- table.remove(inTransactions, i)
- table.remove(outTransactions, i)
- saveDatabase()
- end
- end
- return true
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Lists the user's in transactions
- function showInTransaction(userName, serverPassword)
- if serverPassword == serverAdminPassword then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- local transactions = ""
- for word in string.gmatch(inTransactions[i], '([^,]+)') do
- transactions = transactions .. "\n" .. word
- end
- return "Last In Transactions of " .. userName .. "\n" .. transactions
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Lists the user's out transactions
- function showOutTransaction(userName, serverPassword)
- if serverPassword == serverAdminPassword then
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- local transactions = ""
- for word in string.gmatch(outTransactions[i], '([^,]+)') do
- transactions = transactions .. "\n" .. word
- end
- return "Last Out Transactions of " .. userName .. "\n" .. transactions
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- -- print("Server passwords do not match")
- return false
- end
- end
- --change user password
- function changePassword(userName, oldPassword, newPassword)
- if (checkCredentials(userName, oldPassword) == true) then
- for i = 0, count() do
- if userName == name[i] then
- table.insert(password, i, newPassword)
- return true
- end
- end
- else
- return false
- end
- end
- --Sets the user's password
- function setPassword(userName, newPassword, serverPassword)
- if serverPassword == serverAdminPassword then
- -- encryptedPassword = enc.encrypt(newPassword, encryptionPassword)
- if checkUserExists(userName) == true then
- for i = 0, count() do
- if userName == name[i] then
- table.insert(password, i, userPassword)
- return true
- end
- end
- else
- print("Account does not exist")
- return false
- end
- else
- print("Server passwords do not match")
- return false
- end
- end
- --Other functions
- --Splits the string
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap .. "}")
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap .. "}")
- end
- return t
- end
- if autoRead == true then
- loadDatabase()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement