Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 04:41 AM.


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