Dynamic Pay-per-minute Balance Checking

Wrench starting from version 2014.05.02 comes with a new powerful dynamic balance checking feature that you can use in your pay-per-minute setup. This short tutorial will give you a brief overview on how to use this.

I assume that you are familiar with installing and setting up Wrench. If not, then first read my tutorial about setting up user authentication, where you'll find the necessary steps.

Set up your balance table

For storing the creadit balance of each user in your application, let's create a table in the database

CREATE TABLE wtb_balance (
   `user` varchar(64),
   `balance` int(11),
   `total` int(11)
);

INSERT INTO wtb_balance (user, balance, total) VALUES ('john', 5, 0);

This means that john has consumed 0 minutes so far and has a balance of 5 units initially. We are going to decrease the balance by 1 every time the monitor job runs, which will happen every 60 seconds. That means that 1 balance unit equals to 1 minute.

Configure Wrench

In your Application.xml add the following properties:

<!-- for setting up the database connection, please read the http://wowzatoolbox.com/user-authentication tutorial -->
<Property>
  <Name>wrench.monitor.period</Name>
  <Value>60</Value>
</Property>
<Property>
  <Name>wrench.ppm.update.sql</Name>
  <Value>update wtb_balance set total=:elapsedtime, balance = balance - 1 where user=:username</Value>
</Property>
<Property>
  <Name>wrench.ppm.insert.sql</Name>
  <Value>insert into wtb_balance (user, balance, total) values (:username, 100, 0)</Value>
 </Property>
<Property>
  <Name>wrench.ppm.disconnect.sql</Name>
  <Value>select 1 from wtb_balance where balance > 0 and user=:username</Value>
</Property>

Test

After you have set up all this, you should see that after the user passes all the authentication and authorization that you have configured ( see this tutorial for details), a new entry should appear for each connected player in the wtb_balance table with an initial balance of 100 units.

After 5 minutes, Wrench will have decreased the balance to 0 and so on the next monitor job execution the user will be dropped:

INFO server comment - Executing SQL select 1 from wtb_balance where balance > 0 and user=:username with params: {applica
tion=secret, username=john, session=1566a3fc8e6056220d8f3ad97f076a12, elapsedtime=240.545, stream0=mystream}
INFO server comment - Wrench Monitor finished in 468ms
INFO server comment - Performing monitor activity
INFO server comment - Executing SQL update wtb_balance set total=:elapsedtime, balance = balance - 1 where user=:usernam
e with params: {application=secret, username=john, session=1566a3fc8e6056220d8f3ad97f076a12, elapsedtime=300.615, str
eam0=mystream}
INFO server comment - Executing SQL select 1 from wtb_balance where balance > 0 and user=:username with params: {applica
tion=secret, username=john, session=1566a3fc8e6056220d8f3ad97f076a12, elapsedtime=300.615, stream0=mystream}
INFO server comment - Shutting down john because disconnect query did not return at least one row
INFO server comment - Wrench Monitor finished in 496ms
INFO stream stop mystream -
INFO stream destroy mystream -
INFO session disconnect 462642336 -
INFO server comment - User john disconnected, total connection time was 301.446s

The dynamic aspect of this solution comes when you have some means to increase the balance. Achieving this goes out of the scope of this tutorial, but basically you can integrate this function with your payment gateway, webshop. The purpose of this article was only to show you the basic idea of how to use Wrench's new dynamic pay-per-minute balance checking feature.

Feel free to leave any comments below if you liked this or have any questions.

Comments

can this be done with the webservice also?

Write new comment