|
Registered User
|
Join Date: Jul 2006
Posts: 1,241
|
|
Banking Database
Just whipped up a Banking Database system for Terra, but I decided to start fresh and to try to go for something more advanced, so I guess I'll just leave this here. Hopefully it will help someone with their loadvars/savevars. All functions are at the top of the script, if you wanted to you can make a client ATM or something and just use these functions save accounts and stuff.
Save this in an NPC called 'Banking' or something, and don't forget to give your (npcserver) rights to mud/bank/*.txt
PHP Code:
/*************************************** * * * BANKING * * by RW for Terra * * * ****************************************/ /* Functions and Parameters:
CreateAccount(account_owner, initial_money, start_rate, password) -- To create a new account Deposit(account_id, money_ammount, password) -- For Deposits Withdraw(account_id, money_ammount, password) -- For withdrawls NextAvailableID() -- Returns the next available ID number. ChangePassword(account_id, requester, oldpassword, newpassword) -- To change the account password *CheckAccess(accountid, password) -- Checks to see if the password matches the specified account ID's password sendMessage(topic, message) -- Sends an NC message in format 'Banking (topic): message' Error(id) -- Returns error message for specified error id *DumpAccountsList() -- Dumps all accounts information to the RC (Only works on debug mode)
Features to come: DeleteAccount(accountid, password, confirmation_code) -- When function is called with no confirmation code, one is generated. With a confirmation code the account is deleted. *ApplyRates -- Applys the specified yearly rate for each account, if no rate exists the default is applied. (Default: 1.5%)
(* signifies a security risk)
*/
function onCreated() CreateVariables();
function CreateVariables(){ this.debug_mode = false; // Output all actions to RC this.yearly_rate = 1.5; // Default yearly rate this.database_location = "mud/bank/main_database.txt"; // Database text file sendMessage("System", "The script of the database was updated."); }
public function Deposit(accountid, ammount, password){ if (!CheckAccess(temp.accountid, temp.password)) return Error(); if (temp.ammount < 15 || temp.ammount > 500000) return Error(1, temp.accountid, temp.ammount); temp.deposit.loadVars(this.database_location); if (!temp.deposit.("account_" @ temp.accountid)) return Error(3, temp.accountid); temp.deposit.("account_" @ temp.accountid)[1] += temp.ammount; temp.deposit.saveVars(this.database_location, 0); sendMessage("Deposit", format(_("%f was deposited into account_%i"), temp.ammount, temp.accountid)); }
public function Withdraw(accountid, ammount, password){ if (!CheckAccess(temp.accountid, temp.password)) return Error(); if (temp.ammount < 15 || temp.ammount > 500000) return Error(2, temp.accountid, temp.ammount); temp.withdraw.loadVars(this.database_location); if (!temp.withdraw.("account_" @ temp.accountid)) return Error(3, temp.accountid); temp.withdraw.("account_" @ temp.accountid)[1] -= temp.ammount; temp.withdraw.saveVars(this.database_location, 0); sendMessage("Withdrawl", format(_("%f was withdrawn from account_%i"), temp.ammount, temp.accountid)); }
public function CreateAccount(accountowner, start_ammount, start_rate, password){ temp.conditionsok = ((temp.start_ammount > 50 || temp.start_ammount < 500000) && (temp.accountowner && temp.start_ammount && temp.start_rate) == true ? true:false); if (!temp.conditionsok) return Error(); temp.create.loadVars(this.database_location); temp.accountid = NextAvailableID(); if (!temp.start_rate) temp.start_rate = this.yearly_rate; if (temp.create.("account_" @ temp.accountid)) temp.accountid = (NextAvailableID() + 1); temp.create.("account_" @ temp.accountid) = new [4]; temp.create.("account_" @ temp.accountid)[0] = temp.accountowner; temp.create.("account_" @ temp.accountid)[1] = temp.start_ammount; temp.create.("account_" @ temp.accountid)[2] = temp.start_rate; temp.create.("account_" @ temp.accountid)[3] = temp.password; temp.create.saveVars(this.database_location, 0); sendMessage("New Account", format(_("account_%i was created, with a initial deposit of %f and a start rate of %f"), temp.accountid, temp.start_ammount, temp.start_rate)); }
public function DumpAccountsList(){ temp.dump.loadVars(this.database_location); sendMessage("Accounts Dump", format(_("Account, Money, Interest, Password"))); for (temp.i: temp.dump.getdynamicvarnames()) sendMessage("Accounts Dump", temp.dump.(@temp.i)); }
public function NextAvailableID(){ temp.id.loadVars(this.database_location); for (temp.i: temp.id.getdynamicvarnames()) temp.nextavailable = temp.i.substring(("account_").length()); return (temp.nextavailable + 1); }
public function CheckAccess(accountid, password){ temp.check.loadVars(this.database_location); temp.allowed = false; if (lowercase(temp.check.("account_" @ temp.accountid)[3]) == lowercase(temp.password)) temp.allowed = true; return temp.allowed; }
public function ChangePassword(accountid, requester, oldpassword, newpassword){ if (!CheckAccess(temp.accountid, temp.oldpassword)) return Error(); temp.change.loadVars(this.database_location); if (lowercase(temp.change.("account_" @ temp.accountid)[0]) == lowercase(temp.requester)){ temp.change.("account_" @ temp.accountid)[3] = temp.newpassword; } temp.change.saveVars(this.database_location, 0); }
public function ApplyRates(){ /* scheduleevent((60 * 60 * 24 * 31 * 12), "ApplyRates", ""); // Apply rates every year */ }
function onApplyRates() ApplyRates();
public function sendMessage(topic, message){ if (this.debug_mode) echo(format(_("Banking (%s): %s"), temp.topic, temp.message)); }
public function Error(error_id, accountid, ammount){ switch (temp.error_id){ case 1: temp.message = format(_("$%f is an invalid ammount for a deposit, called by AccountID: %i."), temp.ammount, temp.accountid); break; case 2: temp.message = format(_("$%f is an invalid ammount for a withdrawl, called by AccountID: %i."), temp.ammount, temp.accountid); break; case 3: temp.message = format(_("AccountID: %i doesn't exist"), temp.accountid); break; default: temp.message = "Unknown error occured."; break; } temp.message @= " Error Code |" SPC temp.error_id; sendMessage("Error", temp.message); }
|
Last edited by Rapidwolve; 04-04-2007 at 12:35 AM..
|