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 09-22-2006, 04:52 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
Bank System

Bank system I made using dbnpc.

DBNPC Script (Name is BankDB):
PHP Code:
function onCreated()
{
  
this.interest 0.01;
  
this.hour 0;
  
Interest();
  
setTimer(3600);
}
public function 
CreateAccount(pl,ammount)
{
  if (
this.accounts.index(pl) = -1)
  {
    
this.accounts.add(pl);
    
this.("acc"@pl) = ammount;
    
findplayer(pl).rupees -= ammount;
    return 
true
  
}
  else return 
false;
}

public function 
CheckBalance(pl)
{
  if (
this.accounts.index(pl) => 0)
  {
    return 
this.("acc"@pl);
  }
  else return 
false;
}  

public function 
AddMoney(pl,ammount)
{
  if (
this.accounts.index(pl) => 0)
  {
    
findplayer(pl).rupees -= ammount;
    
this.("acc"@pl) += ammount;
    return 
this.("acc"@pl);
  }
  else return 
false;
}  

public function 
RemoveMoney(pl,ammount)
{
  if (
this.accounts.index(pl) => 0)
  {
    if (
this.("acc"@pl) => ammount)
    {
      
this.("acc"@pl) -= ammount;
      
findplayer(pl).rupees += ammount;
      return 
true;
    }
    else return 
"nm";
  }
  else return 
"na";
}

function 
onTimeout() 
{
/* Cheers to Kuji for this.hour idea */
  
this.hour ++;
  if (
this.hour => 24)
  {
    echo(
"Interest Added");
    
Interest();
    
this.hour 0;
  }
setTimer(3600);


public function 
Interest()
{
  for (
int this.accounts)
  {
    
this.("acc"@int) = int(this.("acc"@int) * 1.01);
  }

Manual interest for RC - add this to your Control-NPC
PHP Code:
 function onRCChat()
{
  if (
params[0] == "interest")
  {
    if (
player.account == "Whoever")
    {
      echo(
"Manual Interest Added!");
      
BankDB.Interest();
    }
  }

And finally some examples on how to use this system in a bank
PHP Code:
/* Do not clientside this script */
function onPlayerChats()
{
  
/*Create Account*/
  
if (player.chat.starts("/createaccount"))
  {
    
temp.result BankDB.CreateAccount(player.account,player.chat.substring(15,-1));
    if (
temp.result == true)
      
say2("Account Opened");

    else 
say2("You already have an account");
   }

  else if (
player.chat == "/balance")
  {
    
temp.result BankDB.CheckBalance(player.account);
    if(
temp.result != false)
      
say2("Current Balance" SPC temp.result);
    else 
say2("You do not have an account");
   }

   else if (
player.chat.starts("/withdraw"))
   {
     
temp.result BankDB.RemoveMoney(player.account,player.chat.substring(10,-1));
     if (
temp.result == "na")
       
say2("Open an account?");
     else if (
temp.result == "nm")
       
say2("Not enough money");
     else if (
temp.result ==  true)
       
say2("Withdrawn");
    }
   else if (
player.chat.starts("/deposit"))
   {
        if (
player.rupees player.chat.substring(9,-1))
        {
          
temp.result BankDB.AddMoney(player.account,player.chat.substring(9,-1));
          if (
temp.result != false)
            
say2("Money Deposited. Balance: " temp.result);
          else
            
say2("Open an account first");
        }
    }

This system can work on a global scale. One BankDB can serve any ammount of banks. I used a BankDB for a couple of reasons. First off - incredibly easy to add interest now. Secondly - I can use functions to determine who has the most money, total ammount of money etc. (Not included).
Reply With Quote
  #2  
Old 09-22-2006, 09:04 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Slightly different approach to how I would do it, but good none the less.
Reply With Quote
  #3  
Old 09-22-2006, 01:12 PM
KuJi KuJi is offline
Banned
Join Date: Apr 2004
Location: Staten Island, New York
Posts: 2,202
KuJi will become famous soon enough
Send a message via ICQ to KuJi Send a message via AIM to KuJi Send a message via MSN to KuJi Send a message via Yahoo to KuJi
Quote:
Originally Posted by Twinny View Post
public function CreateAccount(pl,ammount)
Take a look at the bolded twinny
Reply With Quote
  #4  
Old 09-23-2006, 01:21 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
So I can't spell for peanuts. Does this really matter? No? Good .
Reply With Quote
  #5  
Old 09-23-2006, 09:18 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Quote:
Originally Posted by Skyld View Post
Slightly different approach to how I would do it, but good none the less.
I agree :S
Good though
__________________
Reply With Quote
  #6  
Old 09-23-2006, 10:25 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
Ok: How would you two do it?
Reply With Quote
  #7  
Old 09-23-2006, 10:30 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Personally, I'd merge the Create account and Deposit into one. If the string inside the database doesn't exist, I'd then instantly create the bank account. I wouldn't use so many public functions, remember that they're public. I'd also convert your playerchats.
__________________
Reply With Quote
  #8  
Old 09-23-2006, 11:07 AM
KuJi KuJi is offline
Banned
Join Date: Apr 2004
Location: Staten Island, New York
Posts: 2,202
KuJi will become famous soon enough
Send a message via ICQ to KuJi Send a message via AIM to KuJi Send a message via MSN to KuJi Send a message via Yahoo to KuJi
Clientside -> Trigger to Server -> Use ChatBar -> Ftw =O?
Reply With Quote
  #9  
Old 09-23-2006, 11:32 AM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by Twinny View Post
Ok: How would you two do it?
Well, the way that my system works is that I have a collection of public functions in a class which is joined to the player.
PHP Code:
public function bankCreate();
public function 
bankDeposit();
public function 
bankWithdraw(); 
I also have other functions for checking if they have an account, checking the balance, etc.

I also have a DB NPC for storing the bank data, i.e.
PHP Code:
DB_Bank.("bank_" this.account) = ...; 
However, but it doesn't do any functioning itself.

Then, instead of having to pass the account name, I can just use the this. object to access the player so that you can just do this to deposit 30g:
PHP Code:
player.bankDeposit(30); 
It makes it:
  • Easier to type, and remember.
  • Easier for other people to pick up and use.
  • So that you don't have to pass the account name.
Reply With Quote
  #10  
Old 09-23-2006, 11:49 AM
KuJi KuJi is offline
Banned
Join Date: Apr 2004
Location: Staten Island, New York
Posts: 2,202
KuJi will become famous soon enough
Send a message via ICQ to KuJi Send a message via AIM to KuJi Send a message via MSN to KuJi Send a message via Yahoo to KuJi
Hmm.. my atms a bit diff =o.

acc_123456=KuJi,PASSWORD,BALANCE

I should probally MD5 the password, maybe I will redo the ATM sometime soon.. is kinda old.

Anyway, when you use your ATM Card it has the ATM # in it automatically... so yeah.
Reply With Quote
  #11  
Old 09-23-2006, 02:32 PM
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 xAndrewx View Post
I'd also convert your playerchats.
The playerchat stuff was just an example to show how to do it. Hopefully people will use something better eg a GUI.

That's a pretty good idea Skyld. May edit the code with a more user friendly version later . I'm still pretty sucky at scripting but anything that seems half decent to me i'll share in the hope of receiving advice
Reply With Quote
  #12  
Old 09-23-2006, 03:57 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Skyld's idea is good :]
__________________
Reply With Quote
  #13  
Old 09-23-2006, 08:30 PM
k0rupt_ed k0rupt_ed is offline
Lat.. Bleh
k0rupt_ed's Avatar
Join Date: Feb 2006
Location: Under your bed.
Posts: 514
k0rupt_ed is on a distinguished road
you people inspire me.
__________________


Reply With Quote
  #14  
Old 09-23-2006, 09:46 PM
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
To do what?
Reply With Quote
  #15  
Old 09-28-2006, 09:04 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
Update!
Bank System v 1.1
After receving advice from Skyld I have made a improved bank system using classes.

DBNPC named BankDB
PHP Code:
function onCreated()
{
  
this.interest 0.01;
  
this.hour 0;
  
Interest();
  
setTimer(3600);
}

function 
onTimeout()
{
  
this.hour ++;
  if (
this.hour => 24)
  {
    echo(
"Interest Added");
    
Interest();
    
this.hour 0;
  }
setTimer(3600);


public function 
Interest()
{
  for (
int this.accounts)
  {
    
this.("acc"@int) = int(this.("acc"@int) * 1.01);
  }

Class with bank functions
PHP Code:
public function bankCreate(amount)
{
  if (
BankDB.accounts.index(player.account) = -1)
  {
    if (
player.rupees => amount)
    {
      
BankDB.accounts.add(player.account);
      
BankDB.("acc"@player.account) = amount;
      
player.rupees -= amount;
      return 
True;
    }
    else return 
"nem";  
  }
  else return 
false;
}

public function 
bankDeposit(amount)
{
  if (
BankDB.accounts.index(player.account) => 0)
  {
    if (
player.rupees => amount)
    {
      
BankDB.("acc"@player.account) += amount;
      
player.rupees -= amount;
      return 
true;
    }
    else return 
"nem"//Not enough money
  
}
  else return 
"nba"//No bank account  
}

public function 
bankWithdraw(amount)
{
  if (
BankDB.accounts.index(player.account) => 0)
  {
    if (
BankDB.("acc"@player.account) => amount)
    {
      
BankDB.("acc"@player.account) -= amount;
      
player.rupees += amount;
      return 
true;
    }
    else return 
"nem";
  }
  else return 
"nba";   
}

public function 
bankBalance()
{
  if (
BankDB.accounts.index(player.account) => 0)
  {
    return 
BankDB.("acc"@player.account);
  }
  else return 
"nba";
}

public function 
bankTransfer(amount,pl)
{
/* Lot's of Tests */
  
if (BankDB.accounts.index(player.account) => 0)
  {
    if(
BankDB.accounts.index(pl) => 0)
    {
      if (
BankDB.("acc"@player.account) => amount)
      {
        
BankDB.("acc"@pl) += amount;
        
BankDB.("acc"@player.account) -= amount;
        return 
true;
      }
      else return 
"nem";
    }
    else return 
"pdne"//Player does not exist
  
}
  else return 
"nba";

Finally add in your Control-NPC
PHP Code:
function onActionPlayeronline()
{
  
player.join("classname");
}

function 
onRCChat()
{
  if (
params[0] == "interest")
  {
    if (
player.account == "Whoever")
    {
      echo(
"Manual Interest Added!");
      
BankDB.Interest();
    }
  }

Commands Available
PHP Code:
player.bankCreate(amount);
player.bankDeposit(amount);
player.bankWithdraw(amount);
player.bankBalance();
player.bankTransfer(amount,account); 
Simple enough to use. Call the commands serverside eg. player.bankDeposit(400);

May add a GUI script to use the Bank system next.
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 06:09 AM.


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