Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-03-2009, 11:52 PM
Stryke Stryke is offline
Scripter
Join Date: Apr 2008
Posts: 157
Stryke is an unknown quantity at this point
Bank System using SQLite

I just felt like contributing this bank system, because I haven't contributed anything to the public yet and I think it's about time.

Create a new weapon, it doesn't matter what it's named because it's going to be used just to create the table the bank system is going to use.

Put this in the script of the weapon and update it:

PHP Code:
function onCreated() {
  
requestsql("CREATE TABLE bankDB (
  account TEXT NOT NULL DEFAULT '',
  money INTEGER NOT NULL DEFAULT 0
  )"
false);

It's highly recommended to delete the weapon now, it has no further use.

Now you must create a DBNPC that's going to be used for the bank system.
Insert the script below:

PHP Code:
function onCreated() {
  
this.allowedScripts 
  {
    
this,
    
"atmclassnamehere"
  
};
  
  
this.echoToRC false;
  
this.echoTab "      ";
  
this.setTypes = {"add""subtract""set"};
}

public function 
BankAccountOpen(plechoToRC) {
  if (
DisableUnauthorized(plechoToRC) == true) {
    return;
  }
  
  
temp.req getsql(
  
"SELECT *
  FROM bankDB
  WHERE account = '" 
pl "'
  "
true);
  
  if (
req.rows.size() > 0) {
    echo(
"Bank System: The bank account for player '" pl "' already exists.");   
    
    
temp.callstack getcallstack();
    echo(
this.echoTab "Script: " GetScriptName());   
    
    echo(
this.echoTab this.echoTab "Query: BankAccountOpen(" pl "," echoToRC ")");
    return;
  }

  
getsql("INSERT INTO bankDB VALUES(
  NULL,
  '" 
pl "',
  0
  )"
false);
  
  if (
echoToRC == true || this.echoToRC == true) {
    echo(
"Bank System: A bank account was created for '" pl "'.");
  }
}

public function 
BankAccountClose(plechoToRC) {
  if (
DisableUnauthorized(plechoToRC) == true) {
    return;
  }
  
  
temp.req getsql(
  
"SELECT *
  FROM bankDB
  WHERE account = '" 
pl "'
  "
true);
  
  if (
req.rows.size() < 1) {
    echo(
"Bank System: The bank account for player '" pl "' does not exist.");   
    
    
temp.callstack getcallstack();
    echo(
this.echoTab "Script: " GetScriptName());   
    
    echo(
this.echoTab this.echoTab "Query: BankAccountClose(" pl "," echoToRC ")");
    return;
  }
  
  
getsql("DELETE FROM bankDB
  WHERE account = '" 
pl "'"
  
false);
  
  if (
echoToRC == true || this.echoToRC == true) {
    echo(
"Bank System: A bank account was closed for '" pl "'.");
  }
}

public function 
BankAccountSetMoney(plsetTypesetMoneyechoToRC) {
  if (
DisableUnauthorized(plechoToRC) == true) {
    return;
  }
  
  
temp.money 0;
 
  if (!(
setType in this.setTypes)) {
    echo(
"Bank System: An invalid set type was specified.");
    
    
temp.callstack getcallstack();
    echo(
this.echoTab "Script: " GetScriptName()); 
      
    echo(
this.echoTab this.echoTab "Query: BankAccountSetMoney(" pl "," setType "," setMoney "," echoToRC ")");
    return;
  }

  
temp.req getsql(
  
"SELECT *
  FROM bankDB
  WHERE account = '" 
pl "'
  "
true);

  if (
req.rows.size() < 1) {
    echo(
"Bank System: The bank account for player '" pl "' does not exist.");   
    
    
temp.callstack getcallstack();
    echo(
this.echoTab "Script: " GetScriptName());   
    
    echo(
this.echoTab this.echoTab "Query: BankAccountSetMoney(" pl "," setType "," setMoney "," echoToRC ")");
    return;
  }
  
  
temp.oldMoney req.rows[0].money;
  
  if (
setType == "add") {
    
money = (oldMoney) + setMoney;
  }
  else if (
setType == "subtract") {
    
money = (oldMoney) - setMoney;
  }
  else if (
setType == "set") {
    
money setMoney;
  }
  
  
getsql(
  
"UPDATE bankDB
  SET money = " 
money "
  WHERE account = '" 
pl "'"
  
false);
  
  if (
echoToRC == true || this.echoToRC == true) {
    echo(
"Bank System: The money was set on a bank account for player '" pl "'.");
    echo(
this.echoTab "Changes: $" oldMoney " to $" money ".");
  }
}

public function 
BankAccountViewMoney(plechoToRC) {
  if (
DisableUnauthorized(plechoToRC)) {
    return;
  }
  
  
temp.req getsql(
  
"SELECT *
  FROM bankDB
  WHERE account = '" 
pl "'
  "
true);
  
  if (
req.rows.size() < 1) {
    echo(
"Bank System: The bank account for player '" pl "' does not exist.");   
    
    
temp.callstack getcallstack();
    echo(
this.echoTab "Script: " GetScriptName());   
    
    echo(
this.echoTab this.echoTab "Query: BankAccountViewMoney(" pl "," echoToRC ")");
    return;
  }
  
  
temp.money req.rows[0].money;
  
  if (
echoToRC || this.echoToRC == true) {
    echo(
"Bank System: The player '" pl "' has $" money " in their bank account.");
  }
  
  return 
money;
}

public function 
BankAccountEchoRichest(top) {
  
temp.req getsql(
  
"SELECT *
  FROM bankDB
  ORDER BY account DESC, money DESC
  "
true);
  
  for (
temp.0temp.toptemp.i++) {
    if (
req.rows.size() < (1)) {
      echo((
1) @ ". None");
      continue;
    }
    echo((
1) @ ". " req.rows[i].account " (" req.rows[i].money ")");
  }
}

public function 
DisableUnauthorized(abcdefgh) {
  
temp.result "," "," "," "," e;

  
temp.callstack getcallstack(); 
  if (
callstack.size() < 2) {
    return 
true;
  }
  
  
temp.callstackobject GetScriptName();
  if (!(
callstackobject in this.allowedScripts)) {
    echo(
"Bank System: The script '" callstackobject "' tried to access the Bank System with command: " callstack[callstack.size() - 2] @ "(" result ").");
    return 
true;
  }
  
  return 
false;
}

public function 
GetScriptName() {
  
temp.callstack getcallstack();
  return (
callstack[callstack.size() - 2].scriptcallobject).name;
}

public function 
getsql(queryisreq) { 
  
temp.req requestsql(temp.querytemp.isreq); 
  
  if (
temp.req.error != "") { 
    echo(
"SQL Error: " temp.req.error); 
    echo(
"      Query: " temp.query); 
  } 
  
  if (
temp.isreq && !temp.req.completed && 
      !
waitfor(temp.req"onReceiveData"5)) {
    return 
null
  }
  
  return 
temp.req

Now for modifications.. Take a look at the top of the script.

See where it says this.allowedScripts={..}? You have to edit it so that certain scripts are allowed to use the Bank System.

If you want the bank system to echo to the RC everytime a command is used, you can set this.echoToRC to true.

Now let me explain how to use this bank system.
There are 5 commands:

PHP Code:
BankAccountOpen("accountname"echoToRC); 
This opens up a bank account for the specified account name.
The second parameter is whether or not to echo to RC. Set to true to echo to RC and false to not echo to RC.

PHP Code:
BankAccountClose("accountname"echoToRC); 
Closes an existing bank account for a specified account name.

PHP Code:
BankAccountSetMoney("accountname""add"numberechoToRC); 
Set's the amount of money in a bank account. The second parameter can be add, subtract, or set. The third parameter is the amount of money.

PHP Code:
BankAccountViewMoney("accountname"echoToRC); 
Returns the amount of money in a player's bank account.

PHP Code:
BankAccountEchoRichest(number); 
Echos the richest players, showing account name and amount of money in their bank account. It lists the top 1 to the number specified.

NOTE: If you want to call the bank system's functions from another script you have to put your DBNPC's name as the prefix. e.g. ("BankSystem").BankAccountEchoRichest();

If you have any questions or suggestions please post them.

Last edited by Stryke; 06-04-2009 at 07:17 PM..
Reply With Quote
  #2  
Old 06-04-2009, 10:06 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
Hmmm...

You know you can do "SET bank_money = (bank_money + 5)" in the update statement right?

Your account column should be UNIQUE, and I don't see why you have an id column. You also should not have bank_ in the column names, it's redundant.

CREATE TABLE IF NOT EXISTS bank (
account TEXT UNIQUE NOT NULL,
money INT NOT NULL DEFAULT 0
)
__________________
Reply With Quote
  #3  
Old 06-04-2009, 07:13 PM
Stryke Stryke is offline
Scripter
Join Date: Apr 2008
Posts: 157
Stryke is an unknown quantity at this point
Quote:
Originally Posted by Inverness View Post
You know you can do "SET bank_money = (bank_money + 5)" in the update statement right?
Yes, but there is no reason to do that in this script because it would just make the script longer.

Quote:
Originally Posted by Inverness View Post
Your account column should be UNIQUE, and I don't see why you have an id column.
I don't really know why I put it in either, it was never used in the script.

Quote:
Originally Posted by Inverness View Post
You also should not have bank_ in the column names, it's redundant.

CREATE TABLE IF NOT EXISTS bank (
account TEXT UNIQUE NOT NULL,
money INT NOT NULL DEFAULT 0
)
Yes, I think I'll change that actually.
Reply With Quote
  #4  
Old 06-04-2009, 09:56 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
You should make account PRIMARY KEY rather then UNIQUE to speed up indexing (makes SELECT go faster)
__________________
Do it with a DON!
Reply With Quote
  #5  
Old 06-04-2009, 10:10 PM
Riot Riot is offline
Delteria Management
Join Date: Nov 2003
Location: Seminole County, Florida
Posts: 280
Riot is on a distinguished road
Quote:
Originally Posted by zokemon View Post
You should make account PRIMARY KEY rather then UNIQUE to speed up indexing (makes SELECT go faster)
PRIMARY KEY is normally just adding a UNIQUE index, unless its an integer column.
Reply With Quote
  #6  
Old 06-05-2009, 09:25 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
And in case you didn't know, SQLite tables have a built-in rowid column. If you declare a column INTEGER PRIMARY KEY it becomes an alias for it. So if you don't have an alias for it, you can use:

SELECT rowid, * FROM table
Quote:
Originally Posted by zokemon View Post
You should make account PRIMARY KEY rather then UNIQUE to speed up indexing (makes SELECT go faster)
You just got told, by Riot.
__________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 09:10 AM.


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