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 10-09-2009, 01:07 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
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).
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.

Last edited by pooper200000; 10-10-2009 at 02:07 PM.. Reason: Modifying original post at thread creator's request.
Reply With Quote
  #2  
Old 10-09-2009, 01:32 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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.
__________________
Quote:
Reply With Quote
  #3  
Old 10-09-2009, 01:59 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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); 
__________________
Reply With Quote
  #4  
Old 10-09-2009, 09:04 PM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Quote:
Originally Posted by fowlplay4 View Post
post
Quote:
Originally Posted by cbk1994 View Post
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.
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.

Last edited by Switch; 10-09-2009 at 10:09 PM..
Reply With Quote
  #5  
Old 10-09-2009, 10:04 PM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Switch View Post
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
__________________
Quote:
Reply With Quote
  #6  
Old 10-09-2009, 10:55 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
lol, "You have never used the bank yet!"
__________________
Reply With Quote
  #7  
Old 10-09-2009, 11:12 PM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Quote:
Originally Posted by fowlplay4 View Post
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 View Post
lol, "You have never used the bank yet!"
Everyone uses the bank. It's only a matter of time...
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
  #8  
Old 10-09-2009, 11:27 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Switch View Post
Everyone uses the bank. It's only a matter of time...
Not quite what I meant, but ok
__________________
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 04:25 AM.


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