View Single Post
  #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