Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require( "component" )
- local term = require( "term" )
- local os = require( "os" )
- local filesystem = require( "filesystem" )
- local math = require( "math" )
- local serialization = require( "serialization" )
- local tableToString = serialization.serialize
- local stringToTable = serialization.unserialize
- local encData = component.cipher.encrypt
- local decData = component.cipher.decrypt
- local string = require( "string" )
- bankDB = {}
- local playersAccounts=-1
- local enableEncryption=true
- local dbFileName="acBank.db"
- local dbFilePath="/cBank/"
- local bankAPI = {}
- function bankAPI.returnUUID( playerName )
- component.command_block.setCommand( "/uuid "..playerName )
- _, uuid = component.command_block.executeCommand()
- uuid = string.sub(uuid, 19) --Cut the first 19 chars
- return uuid
- end
- function bankAPI.saveDB(crypted)
- if not crypted then
- enableEncryption=false
- else
- enableEncryption=true
- end
- prepareDatabaseSave(dbFileName)
- end
- function prepareDatabaseSave(fileName)
- if filesystem.exists(dbFilePath..fileName) then
- local file=io.open(dbFilePath..fileName,"w")
- if bankDB.TheRealKANi then
- tmp=tableToString(bankDB)
- if enableEncryption then
- file:write(encData(tmp))
- else
- file:write(tmp)
- end
- file:close()
- else
- tmp=tableToString(bankDB)
- if enableEncryption then
- file:write(encData(tmp))
- else
- file:write(tableToString(bankDB))
- end
- file:close()
- print("Saving Blank Account file"..fileName)
- end
- else
- local file=io.open(dbFilePath..fileName,"a")
- if enableEncryption then
- file:write(encData(tmp))
- else
- file:write(tableToString(tmp))
- end
- file:close()
- end
- end
- function bankAPI.loadDB()
- prepareDatabaseLoad(dbFileName)
- end
- function prepareDatabaseLoad(fileName)
- if filesystem.exists(dbFilePath..fileName) then
- local file=io.open(dbFilePath..fileName,"r")
- if file:read(1) == "{" then
- bankDB=stringToTable("{"..file:read())
- elseif not enableEncryption then
- file:close()
- local file=io.open(dbFilePath..fileName,"r")
- bankDB=stringToTable(decData(file:read()))
- print("Encrypted DB Found")
- if bankDB.TheRealKANi then
- if bankDB.TheRealKANi.name=="TheRealKANi" then
- print("DB Decrypted")
- print("Remember to enable encryption.!")
- enableEncryption=true
- else
- print("Cannot Decrypt DB.!")
- end
- else
- print("Can't decrypt DB")
- print("Possible Corruption")
- end
- else
- file:close()
- local file=io.open(dbFilePath..fileName,"r")
- bankDB=stringToTable(decData(file:read()))
- end
- else
- print("Cannot Find DB named: '"..dbFileName.."'")
- print("in path: "..dbFilePath)
- print("Creating "..dbFileName)
- bankAPI.addPlayersToDB()
- print(tostring(playersAccounts).." Players Accounts Added")
- bankAPI.saveDB(enableEncryption)
- end
- --return bankDB
- end
- function bankAPI.addPlayersToDB()
- for k,v in ipairs(component.debug.getPlayers()) do
- if bankAPI.debug then
- print("Parseing Players")
- end
- parsePlayer(v,0,tostring(math.floor(math.random(1,9999))),true)
- end
- end
- function parsePlayer(playerName,accountAmount,password,returnUUID)
- if bankAPI.debug then
- print("Running Function")
- end
- bankDB[tostring(playerName)]={["coinsStored"]=tonumber(accountAmount),["password"]=tostring(password),["uuid"]=bankAPI.returnUUID(playerName),["name"]=playerName}
- for k in pairs(bankDB) do
- playersAccounts=playersAccounts+1
- end
- end
- return bankAPI
Add Comment
Please, Sign In to add comment