Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 05:45 PM.


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