Bank Tutorial

Post all your tuts or request for tuts here.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Bank Tutorial

Post by Ravinos »

:)
countvoldermort
Posts: 165
Joined: Tue Oct 13, 2009 4:22 pm

Re: Bank Tutorial

Post by countvoldermort »

you will neen a database thing for this wont you??? if you do, can you send me it?(in SQL)
Im dumb;)
raymondnl
Posts: 2
Joined: Sat Feb 06, 2010 9:16 pm

Re: Bank Tutorial

Post by raymondnl »

countvoldermort wrote:you will neen a database thing for this wont you??? if you do, can you send me it?(in SQL)
if you look at the code where it says

Code: Select all

$playerinfo3['bank']
this means that you need to add the bank field to your players table this worked for me i made it bank int(11) just like the gold field
hope this helps others who did not find this yet

you could add this field to the reguser.php file only most people wont need or you want to give the player a starting amount in the bank
tehbeasthax
Posts: 15
Joined: Thu Feb 25, 2010 6:22 pm

Re: Bank Tutorial

Post by tehbeasthax »

can i make the balance auto-refresh becuz when i deposit i have to hit refresh at the top of the page to see my current balance.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Bank Tutorial

Post by Ravinos »

I have updated the code to make sure that it refreshes the bank balance when you do a deposit or withdraw.

I moved the if(isset) section above the forms to ensure proper updating. I also add two new variables that make $playerinfo3['bank'] read to the new balance by adding $playerinfo3['bank'] = $newbankd; to the deposit section and $playerinfo3['bank'] = $newbankw; to the widthraw section. What this now does is instead of pulling the bank info from the original player query it will pull the modified numbers from the form instead.

I hope it helps. I'm horrible at tutorials :mrgreen:

Code: Select all

<?php
include_once 'connect.php';
session_start();
   ?>
<?php
include 'session.php';?>
<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
?>
<?php
if (isset($_POST['deposit']))
{
   if ($_POST['deposit'] > $playerinfo3['gold'] || $_POST['deposit'] < 0)
   {
      echo "You are trying to deposit too much. Try a smaller amount.";
   }
   else
   {
      $newbankd = ($playerinfo3['bank'] + $_POST['deposit']);
      $newgoldd = ($playerinfo3['gold'] - $_POST['deposit']);      
      $bankdeposit = "update players set bank='$newbankd', gold='$newgoldd' where name='$player'";
      mysql_query($bankdeposit) or die("could not update bank");  
	  $playerinfo3['bank'] = $newbankd;

      echo "You deposited your gold into the bank!";
   }
}
else if (isset($_POST['withdraw']))
{
   if ($_POST['withdraw'] > $playerinfo3['bank'] || $_POST['withdraw'] < 0)
   {
      echo "You do not have enough in your account.";
   }
   else
   {
      $newbankw = ($playerinfo3['bank'] - $_POST['withdraw']);
      $newgoldw = ($playerinfo3['gold'] + $_POST['withdraw']);      
      $bankwithdraw = "update players set bank='$newbankw', gold='$newgoldw' where name='$player'";
      mysql_query($bankwithdraw) or die("could not withdraw bank");
	  $playerinfo3['bank'] = $newbankw;

      echo "You have withdrawn your gold!";
    }
}
?>
<center>
<table width="90%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>Gold:</td>
    <td><?php echo "Your balance is:  " . $playerinfo3['bank'] . "<br>";?></td>
  </tr>
  <tr>
    <td>Deposit</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="deposit" value="">
<input type="submit" name="bank_action" value="Deposit">
</form>
</fieldset>
    </td>
  </tr>
  <tr>
    <td>Withrawl</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="withdraw" value="">
<input type="submit" name="bank_action" value="Withdraw">
</form>
</fieldset>
    </td>
  </tr>
</table>
</center>
</div>
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Bank Tutorial

Post by Jackolantern »

There is also a ghetto way to stop browser refreshes from doing the same action over again. This can work for many different things:

1. Add a $_GET variable to the URL of the page that you don't want to act when refreshed. It doesn't matter that users can see it, as it is just a random number.
2. Add a session variable, and call it whatever you want. It will store a random 5-digit code.
3. Every time the player actually performs the action that you don't want repeated on refresh, use a rand(1, 35000) to generate a random number between 1 and 35000. Don't store it in the session variable yet.
4. Send the code in a $_GET variable attached to the URL when the player actually performs the action, and you either go to a new page, or reload the current page to process the activity.
5. Wrap the contents of the script action in an IF statement that compares the $_GET variable passed through the URL to the session variable. Only perform the action if the $_GET code is different from the session variable. If they were different, perform the action, then store the contents of the $_GET code in the session variable.

This way, if the player is on the processing page and they hit refresh, the player has not performed the action again so the code will not be regenerated. It will be the same as the session variable, and the action will not occur again. If the player does perform the action again, a new code will be generating and the action can occur one more time until the player performs the action again.

(Note: Obviously this is not a secure method since the player could just type in a different code into the URL if they wanted to, so it is best used to protect players from accidentally refreshing and doing something they don't want to do, such as buying another item from a shop, or moving unintentionally on a map. It should not be used to prevent players from getting double rewards, or anything like that since it is easily circumvented.)
The indelible lord of tl;dr
tehbeasthax
Posts: 15
Joined: Thu Feb 25, 2010 6:22 pm

Re: Bank Tutorial

Post by tehbeasthax »

okay so how would i go about making this so it can deposit more than just gold like possibley items they had in their inventory because i made my max inventory to hold 35 things
jpoisson
Posts: 245
Joined: Sun Aug 02, 2009 5:12 pm

Re: Bank Tutorial

Post by jpoisson »

tehbeasthax wrote:okay so how would i go about making this so it can deposit more than just gold like possibley items they had in their inventory because i made my max inventory to hold 35 things
by reading this i see you haven't watched halls videos on inventory and items. pretty much a vault where you store your items is a lot like your inventory if their was any difference.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Bank Tutorial

Post by Ravinos »

This may get you pointed in the right direction. For my game I have both house storage as well as vault storage. The vault storage goes with the bank as well as a guild vault(work in progress) at the bank.

Basics this is if you are still using the same table setup as halls' tutorial:
For storage, add a new field to playerweapons, playerarmor, and inventory named "storage" make it a tiny integer of 1. This tiny integer will be your toggle switch. I have mine set as 0="not storred" 1="bank" 2="house"

Then you make a new page called "bankstorage" this page will be the one that displays your inventory with basically the same setup as the equip page. But instead of equipping the item you will want the option that will store the item in your bank by flipping the "storage" toggle in playerweapons to 1 instead of 0.

That is a basic concept idea of the banking with items system I made. My system is of course more deeply involved with numerous safeguards such as not being able to store equipped items, sell items from your bank, or even trade them unless they have the toggle set back to "0". Which means they are in your inventory instead of storred. With this system there is no need to physically move items from table to table and its as easy as adding one new column to your tables and changing a few query.

I hope it helped a bit. For any actual code snippets I'd have to rewrite them to look like Halls' again. My setup and table structure is totally different than the tutorials now. I may do a Bank part 2 tutorial now when I get time. This will include storing items in depth.
jpoisson
Posts: 245
Joined: Sun Aug 02, 2009 5:12 pm

Re: Bank Tutorial

Post by jpoisson »

I guess you don't have to do the transfer of items from on table to another but it is a lot more flexible then by just adding a new field.
Post Reply

Return to “Tutorials”