Welcome to the tutorial part of BBGScripting. Here there are references to how to build Browser Based Games (BBG's). Some are local and others are external. They should be a good starting point or build point upon what has already been developed. We hope they might even inspire new ways of developing BBG's.

If these are helpful; feel free to send us a review. We wish the best to everyone who uses them and hope they are of great benefit. Here is a tutorial for now but more will come.

How to Setup the Turn for a Real Time Game

Introduction
When setting up a Real Time Game, strategy or RPG, the key is to have a script run every few minutes to make the actions occur. These actions could consist of taking away food, adding points, giving more turns, and/or numerious other actions. In this tutorial we are going to show how to setup the script to do this action and give some examples of actions use in games.
Prerequisite
  • PHP
  • MySQL
  • Cron

Create the Turn/Action File
Before we can have the file run we have to make the file.
  1. First we need to setup a MySQL database and create the appropriate tables. I also included some test data.
    1. Action Table - Actions to occur just once or continuously. Once as in buy an item; Continuous as in each turn.
    2. Item Table - Items which are in the game.
    3. Member Table - Members of the Game.
    4. Member Item Table - Member to Item matching. Who had what and how much.
    CREATE TABLE IF NOT EXISTS action (
      action_id int(7) unsigned NOT NULL auto_increment,
      item_id int(7) unsigned NOT NULL,
      action_type enum('C','S') NOT NULL COMMENT 'C=Continuous,S=Single or once',
      action_name varchar(20) NOT NULL,
      action_execute varchar(100) NOT NULL,
      PRIMARY KEY  (action_id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
    
    INSERT INTO action (action_id, item_id, action_type, action_name, action_execute) VALUES
    (1, 2, 'C', 'SUBTRACT', '1');
    
    CREATE TABLE IF NOT EXISTS item (
      item_id int(7) unsigned NOT NULL auto_increment,
      item_name varchar(50) NOT NULL,
      item_description varchar(250) NOT NULL,
      item_start int(7) unsigned NOT NULL,
      item_max bigint(21) NOT NULL,
      PRIMARY KEY  (item_id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    INSERT INTO item (item_id, item_name, item_description, item_start, item_max) VALUES
    (1, 'Food', 'Food for Feed to keep up Health', 10, 1000),
    (2, 'Health', 'Health is how a player is doing.', 100, 100);
    
    CREATE TABLE IF NOT EXISTS member (
      member_id int(7) unsigned NOT NULL auto_increment,
      member_user varchar(50) NOT NULL,
      member_pass varchar(32) NOT NULL,
      member_email varchar(100) NOT NULL,
      member_confirm varchar(32) NOT NULL,
      PRIMARY KEY  (member_id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    INSERT INTO member (member_id, member_user, member_pass, member_email, member_confirm) VALUES
    (1, 'uset', '', '', 'true'),
    (2, 'demo', '', '', 'true');
    
    CREATE TABLE IF NOT EXISTS member_item (
      member_item_id int(7) unsigned NOT NULL auto_increment,
      member_id int(7) unsigned NOT NULL,
      item_id int(7) unsigned NOT NULL,
      member_item_quantity bigint(21) NOT NULL,
      PRIMARY KEY  (member_item_id)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
    
    INSERT INTO member_item (member_item_id, member_id, item_id, member_item_quantity) VALUES
    (1, 1, 1, 100),
    (2, 1, 2, 100),
    (3, 2, 1, 100),
    (4, 2, 2, 100);
    
  2. Make a PHP file on the server.(For now make it public but when done move it out of public reach. Example below public_html folder. The reason is if someone finds it, they can make the script run.)
  3. Create a MySQL Connection. Plus turn on error reporting.
  4. // Database Varables
    define('DB_HOST','localhost');
    define('DB_NAME','');
    define('DB_USER','');
    define('DB_PASS','');
    
    // Display All Errors
    error_reporting(E_ALL);
    // Connection to MySQL; if can not connect Error and Stop
    $db_res_conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_errno().': '.mysql_error());
    // Select Database; if does not exist then Stop
    $db_res = mysql_select_db(DB_NAME,$db_res_conn) or die(mysql_errno().': '.mysql_error());
    
  5. Grab all the items so they can be referenced.
  6. // Grab the full list of items
    $item_arr = item_list();
    
  7. Create the loop for making continuous actions happen.
  8. // Run Actions which are Continuous (turns)
    $sql = "SELECT * FROM action WHERE action_type='C'";
    $r2 = mysql_query($sql);
    while($action = mysql_fetch_assoc($r2))
    {
    	if($action['action_name'] == 'SUBTRACT'){
    		// subtract 1 of a specific item from all members
    		$sqli = "";
    		$sqli.= "UPDATE member_item SET ";
    		$sqli.= "member_item_quantity=member_item_quantity-{$action['action_execute']}";
    		$sqli.= " WHERE item_id='{$action['item_id']}'";
    		mysql_query($sqli);
    		unset($sqli);
    	}
    }
    
  9. Thats it; as for the basics of the code. More actions can be added like someone builds a farm; it can then add food to thier account. We will add more to this tutorial as we grow giving more things that can be done.
  10. Move the file out of public view or in my case below public_html folder.
  11. Setup a cron. In cPanel you go to cron jobs then standard. Then select how often you want it to run and put the below for the command. (This is how it works in cPanel. Will work on finding out other control panels in the future.)
  12. php /home/"user"/cron.php