Graal Forums

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

Switch 10-09-2009 01:07 AM

SQLite Bank System
 
I know there's already one in the Code Gallery, but I feel like mine's much more simple.
Also note that this is my first script in any type of SQL, so it could probably be improved a lot.

First, create the table. To do this, put the following in a new Weapon NPC:
PHP Code:

function onCreated() {
  
requestsql("CREATE TABLE bank (account TEXT UNIQUE NOT NULL, money INT NOT NULL DEFAULT 0"false);


(Thanks to Invernest for the column-constraints)
Once that has been done you should delete the weapon since it doesn't server a purpose anymore.

Now create a new class (for multiple locations) or a level NPC (if you want only one bank) and input the following:
PHP Code:

function onPlayerChats() {
  if (
player.chat.tokenize()[0] == "/deposit") {
    
temp.amount int(player.chat.tokenize()[1]);
    
    if (
player.rupees temp.amount) {
      
say2("You don't have enough Gralat!");
      return;
    }
    if (
temp.amount <= 0) {
      
say2("You can only deposit an amount
over 0!"
);
      return;
    }
    
    
temp.req requestsql("SELECT * FROM bank WHERE account = '" @player.account"'"true);
    if (
temp.req.rows.size() <= 0) {
      
requestsql("INSERT INTO bank VALUES ('" @player.account"', " @temp.amount")"false); //creates a new row for a player that hasn't used the bank
    
}
    else {
      
requestsql("UPDATE bank SET money = (money + " @temp.amount") WHERE account = '" @player.account"'"false); //adds the money
    
}
    
player.rupees -= temp.amount;
    
say2("Deposited " @temp.amount" Gralat in the
bank."
);
  }
  
  if (
player.chat.tokenize()[0] == "/withdraw") {
    
temp.amount int(player.chat.tokenize()[1]);
    
    if (
temp.amount <= 0) {
      
say2("You can only withdraw an amount
over 0!"
);
      return;
    }
    
    
temp.req requestsql("SELECT * FROM bank WHERE account = '" @player.account"'"true);
    
    if (
temp.req.rows.size() <= 0) {
      
say2("You have never used the bank yet!");
      return;
    }
    if (
temp.req.rows[0].money temp.amount) {
      
say2("You don't have enough Gralat
in the bank!"
);
      return;
    }
    
    
requestsql("UPDATE bank SET money = (money - " @temp.amount") WHERE account = '" @player.account"'"false); //takes out the money
    
player.rupees += temp.amount;
    
say2("Withdrew " @temp.amount" Gralat from the
bank."
);
  }
  
  if (
player.chat == "/balance") {
    
temp.req requestsql("SELECT * FROM bank WHERE account = '" @player.account"'"true);
    
    if (
temp.req.rows.size() <= 0) {
      
say2("You have never used the bank yet!");
      return;
    }
    
    
say2("You currently have a balance
of " 
@temp.req.rows[0].money" Gralat in the bank.");
  }


If you need to, change any text that says "Gralat" into whatever you call your money.

Now you should be good to go!
Remember to remind people in a sign or something of the commands "/deposit #" (to put money in the bank), "/withdraw #" (to take money out of the bank), and "/balance" (to check what's in the bank).

fowlplay4 10-09-2009 01:32 AM

Consider doing the following for balance:

NPC Code:

SELECT money
FROM bank
WHERE account = player.account



And use the same where clause in your other select queries, instead of cycling through an array every time.

As well as adding functions for each feature (Depositing, Withdrawing, Balance), so it's at least re-usable instead of keeping it nested in the onPlayerChats function.

Other than that, good work.

cbk1994 10-09-2009 01:59 AM

It's not horrible, but you definitely need to use a more efficient SELECT statement, something like:

PHP Code:

temp.req requestSQL("SELECT * FROM bank WHERE account = '" player.account "'"true);

if (
req.rows.size() <= 0) {
  return 
printf("Sorry, you haven't registered a bank account!");
}

temp.balance req.rows[0][1];

printf("My balance is $%d"balance); 


Switch 10-09-2009 09:04 PM

Quote:

Originally Posted by fowlplay4 (Post 1528496)
post

Quote:

Originally Posted by cbk1994 (Post 1528510)
post

Thanks for the tip, I was thinking about doing that but was a bit dizzy and wanted to finish so just did what already made sense to me. Added that and fixed the checks to go with it.

fowlplay4 10-09-2009 10:04 PM

Quote:

Originally Posted by Switch (Post 1528671)
Thanks for the tip :)
Wouldn't temp.req.rows[0].money also work, though?

Yeah that should work fine, then you don't have to worry about static index values getting thrown off.

You should still use functions for your bank operations. :P

cbk1994 10-09-2009 10:55 PM

lol, "You have never used the bank yet!"

Switch 10-09-2009 11:12 PM

Quote:

Originally Posted by fowlplay4 (Post 1528694)
You should still use functions for your bank operations. :P

Maybe if there needs to be more flexibility with it, but there really doesn't.

Quote:

Originally Posted by cbk1994 (Post 1528725)
lol, "You have never used the bank yet!"

Everyone uses the bank. It's only a matter of time...

cbk1994 10-09-2009 11:27 PM

Quote:

Originally Posted by Switch (Post 1528741)
Everyone uses the bank. It's only a matter of time...

Not quite what I meant, but ok :fro:


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

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