Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Bank System using SQLite (https://forums.graalonline.com/forums/showthread.php?t=86173)

Stryke 06-03-2009 11:52 PM

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. :)

Inverness 06-04-2009 10:06 AM

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
)

Stryke 06-04-2009 07:13 PM

Quote:

Originally Posted by Inverness (Post 1496937)
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 (Post 1496937)
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 (Post 1496937)
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.

zokemon 06-04-2009 09:56 PM

You should make account PRIMARY KEY rather then UNIQUE to speed up indexing (makes SELECT go faster)

Riot 06-04-2009 10:10 PM

Quote:

Originally Posted by zokemon (Post 1497132)
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.

Inverness 06-05-2009 09:25 AM

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 (Post 1497132)
You should make account PRIMARY KEY rather then UNIQUE to speed up indexing (makes SELECT go faster)

You just got told, by Riot.


All times are GMT +2. The time now is 05:44 AM.

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