PDA

View Full Version : Banking Database


Rapidwolve
04-04-2007, 01:03 AM
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

/***************************************
* *
* 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);
}

Gambet
04-04-2007, 01:13 AM
It's alright.


My bank system that I released a while back dominated all other bank systems though. It was hot.

Rapidwolve
04-04-2007, 01:16 AM
It's alright.


My bank system that I released a while back dominated all other bank systems though. It was hot.

I see :oo:, anyways this is just a database yours was a whole system, from clientside to serverside.

killerogue
04-04-2007, 01:20 AM
Oi, nice work Rapid.

Check your PMs in a few.

Twinny
04-04-2007, 01:43 AM
It's alright.


My bank system that I released a while back dominated all other bank systems though. It was hot.

Your bank system was narrow in the ways it could be used :whatever:

Kristi
04-04-2007, 03:15 AM
Your bank system was narrow in the ways it could be used :whatever:
Not to mention a fairly sizable security risk.

Gambet
04-04-2007, 03:47 AM
Your bank system was narrow in the ways it could be used :whatever:


You don't make any sense.


It does what it's supposed to, and it's easily customizable.


Not to mention a fairly sizable security risk.


Yeah, except implying that Graalians are stupid isn't a solid basis on a security risk when players are allowed to choose their own password.


I provide the tools, the rest is out of my hands. Besides, I provided the system, it could be altered by any server that wishes for it to be another method where players don't pick their own passwords.


We don't need to argue about this AGAIN.

Chandler
04-04-2007, 09:06 AM
Here we go again, Gambet.

Kristi
04-04-2007, 06:58 PM
We don't need to argue about this AGAIN.
When you bring up an opinion such as "oh your system is decent but mine was the hottest thing ever" (that is a paraphrase, not a direct quote) which claims superiority, you can surely expect someone who disagrees to reply. If you do not want to start such a discussion, do not provide the bomb to start the war :)

Gambet
04-04-2007, 09:21 PM
When you bring up an opinion such as "oh your system is decent but mine was the hottest thing ever" (that is a paraphrase, not a direct quote) which claims superiority, you can surely expect someone who disagrees to reply. If you do not want to start such a discussion, do not provide the bomb to start the war :)


Meh, I didn't mean it so negatively.



I'm actually glad that people still release code to the public, no matter what it is, mostly any code can be used as a reference to learn about different ways of using different functions, such as in RW's example here with savevars and loadvars.

Inverness
04-05-2007, 01:48 AM
I'm actually glad that people still release code to the public, no matter what it is, mostly any code can be used as a reference to learn about different ways of using different functions, such as in RW's example here with savevars and loadvars.
You've inspired me to hurry up and polish my dialog system for release :D

Chompy
04-05-2007, 02:36 AM
You've inspired me to hurry up and polish my dialog system for release :D

WOOT! Take off :]
That dialog system owns! :o