Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-04-2007, 12:03 AM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
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(accountidammountpassword){
  if (!
CheckAccess(temp.accountidtemp.password)) return Error();
  if (
temp.ammount 15 || temp.ammount 500000) return Error(1temp.accountidtemp.ammount);
  
temp.deposit.loadVars(this.database_location);
  if (!
temp.deposit.("account_" temp.accountid)) return Error(3temp.accountid);
  
temp.deposit.("account_" temp.accountid)[1] += temp.ammount;
  
temp.deposit.saveVars(this.database_location0);
  
sendMessage("Deposit"format(_("%f was deposited into account_%i"), temp.ammounttemp.accountid));
}

public function 
Withdraw(accountidammountpassword){
  if (!
CheckAccess(temp.accountidtemp.password)) return Error();
  if (
temp.ammount 15 || temp.ammount 500000) return Error(2temp.accountidtemp.ammount);
  
temp.withdraw.loadVars(this.database_location);
  if (!
temp.withdraw.("account_" temp.accountid)) return Error(3temp.accountid);
  
temp.withdraw.("account_" temp.accountid)[1] -= temp.ammount;
  
temp.withdraw.saveVars(this.database_location0);
  
sendMessage("Withdrawl"format(_("%f was withdrawn from account_%i"), temp.ammounttemp.accountid));
}

public function 
CreateAccount(accountownerstart_ammountstart_ratepassword){
  
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_ratetemp.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_location0);
  
sendMessage("New Account"format(_("account_%i was created, with a initial deposit of %f and a start rate of %f"), temp.accountidtemp.start_ammounttemp.start_rate));
}

public function 
DumpAccountsList(){
  
temp.dump.loadVars(this.database_location);
  
sendMessage("Accounts Dump"format(_("Account, Money, Interest, Password")));
    for (
temp.itemp.dump.getdynamicvarnames())
      
sendMessage("Accounts Dump"temp.dump.(@temp.i));
}

public function 
NextAvailableID(){
  
temp.id.loadVars(this.database_location);
    for (
temp.itemp.id.getdynamicvarnames())
      
temp.nextavailable temp.i.substring(("account_").length());
  return (
temp.nextavailable 1);
}

public function 
CheckAccess(accountidpassword){
  
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(accountidrequesteroldpasswordnewpassword){
  if (!
CheckAccess(temp.accountidtemp.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_location0);
}

public function 
ApplyRates(){
/*
scheduleevent((60 * 60 * 24 * 31 * 12), "ApplyRates", ""); // Apply rates every year
*/
}

function 
onApplyRates() ApplyRates();

public function 
sendMessage(topicmessage){
  if (
this.debug_mode)
    echo(
format(_("Banking (%s): %s"), temp.topictemp.message));
}

public function 
Error(error_idaccountidammount){
  switch (
temp.error_id){
    case 
1:
    
temp.message 
    
format(_("$%f is an invalid ammount for a deposit, called by AccountID: %i."), temp.ammounttemp.accountid);
    break;
    
    case 
2:
    
temp.message 
    
format(_("$%f is an invalid ammount for a withdrawl, called by AccountID: %i."), temp.ammounttemp.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..
Reply With Quote
  #2  
Old 04-04-2007, 12:13 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
It's alright.


My bank system that I released a while back dominated all other bank systems though. It was hot.
Reply With Quote
  #3  
Old 04-04-2007, 12:16 AM
Rapidwolve Rapidwolve is offline
Registered User
Join Date: Jul 2006
Posts: 1,241
Rapidwolve is an unknown quantity at this point
Quote:
Originally Posted by Gambet View Post
It's alright.


My bank system that I released a while back dominated all other bank systems though. It was hot.
I see , anyways this is just a database yours was a whole system, from clientside to serverside.
Reply With Quote
  #4  
Old 04-04-2007, 12:20 AM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Oi, nice work Rapid.

Check your PMs in a few.
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.
Reply With Quote
  #5  
Old 04-04-2007, 12:43 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Gambet View Post
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
Reply With Quote
  #6  
Old 04-04-2007, 02:15 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by Twinny View Post
Your bank system was narrow in the ways it could be used
Not to mention a fairly sizable security risk.
__________________
Reply With Quote
  #7  
Old 04-04-2007, 02:47 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Twinny View Post
Your bank system was narrow in the ways it could be used

You don't make any sense.


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


Quote:
Originally Posted by Kristi View Post
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.
Reply With Quote
  #8  
Old 04-04-2007, 08:06 AM
Chandler Chandler is offline
Banned
Join Date: Jan 2007
Posts: 656
Chandler will become famous soon enough
Here we go again, Gambet.
Reply With Quote
  #9  
Old 04-04-2007, 05:58 PM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by Gambet View Post
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
__________________
Reply With Quote
  #10  
Old 04-04-2007, 08:21 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Kristi View Post
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.
Reply With Quote
  #11  
Old 04-05-2007, 12:48 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Gambet View Post
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
__________________
Reply With Quote
  #12  
Old 04-05-2007, 01:36 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by Inverness View Post
You've inspired me to hurry up and polish my dialog system for release
WOOT! Take off :]
That dialog system owns! :o
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.