Jump to content
MakeWebGames

Bank Module Tutorial, Part 2


Zeggy

Recommended Posts

Bank Module, Part 1

Now we have the basic module down, we'll move onto to the game logic - depositing and withdrawing money from the bank.

First, we'll edit our start() function so it is able to handle more than one operation:

    public function start()
   {
       requireLogin();

       if ($_GET['act'] == 'withdraw')
           $this->withdraw();
       else if ($_GET['act'] == 'deposit')
           $this->deposit();
       else
           $this->tpl->display('bank.tpl');
   }

 

Now, if the browser visits index.php?act=deposit, our bank class will call the deposit() method.

Next we need to write the withdraw and deposit functions. Both are fairly similar so I'll just go through the withdraw function only. You can try to implement the deposit function yourself!

First off, we decide that people should only reach the withdraw() function if they submitted the withdraw() form. We can do this by checking if $_POST['amount'] is set. Our bank form had a field called 'amount' so if the form was submitted it should contain some amount data.

 

    private function withdraw()
   {
       if (!isset($_POST['amount']))
       {
           header('Location: index.php?mod=Bank');
           exit;
       }

 

If there is no amount, then the player is redirected back to the Bank homepage as if nothing went wrong. Most likely, somebody visited a wrong URL so no harm done. We call exit right after the redirect so that no code after this will be processed.

In the next part of the code, we need to check that the submitted amount is valid. It must satisfy several conditions:

  • It must be a number
  • It must be positive (you cannot withdraw negative amounts)
  • It must not be more than what the player has in the bank (you cannot withdraw more than you own)

 

If the amount satisfies these conditions, then we can update the player database and change the player's amount of money on hand and in the bank.

 

        $msg = '';
       $amount = intval($_POST['amount']);
       if ($amount < 0 || $amount > $this->player->bank)
       {
           //No query, just error message
           $msg = 'You cannot withdraw that amount!';
       }
       else
       {
           //Update player database
           $query = $this->db->execute('UPDATE `<ezrpg>players` SET `money`=?, `bank`=? WHERE `id`=?', array($this->player->money + $amount, $this->player->bank - $amount, $this->player->id));

           $msg = 'You have withdrawn ' . $amount . ' money!';
       }

       //Redirect back to main bank page, including the message
       header('Location: index.php?mod=Bank&msg=' . urlencode($msg));
       exit;
   }

 

We use $msg to display an error message or success message to the user. We redirect the user back to the Bank homepage and pass $msg through the URL. $msg is automatically displayed at the top of the page for the user to see. This method is used because this will prevent a user from refreshing a page in order to withdraw more than once. In this case, refreshing a submitted form is harmless as you can't withdraw more than what you have, but in many situations, refreshing a form can cause problems (such as refreshing a registration form twice).

Now you can try to implement the deposit() function yourself! You can copy the code with withdraw() and just make the needed changes.

Some things to keep in mind:

Check that the player isn't depositing more than what they have on hand.

Edit the query so you are adding/subtracting the correct values.

Edit the success/error messages so they make sense!

 

For the full code of the bank module, you can download the free Bank Module.

There may be some extra code in there but it is mostly the same code used in the tutorial.

Coming soon: Part 3 - Coding install/uninstall functions for your module, so you can sell them or give them away and let your customers install them easily!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...