Browser MMO Video #36
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Browser MMO Video #36
I came up with a working solution for running multiple cron jobs.
1. Create a new table in the Tutorial database called crontimes with 2 fields.

2. Here's the source code...
Basically what I am doing is pulling all the time from crontimes and assigning them to separate variables. Then I wrap the cron jobs in two gated conditional checks. The first asynchronously checks to see if player's health is less than player's maxhealth, and player's spoint is less than maxspoints. If these conditions are met then another asynchronous check is performed to see if the timestamp from the crontimes table is greater than the interval time (e.g. $healingtime, $manatime). If these conditions are met then the health and mana are updated asynchronously based on $healingtime (7 seconds) and $manatime (3 seconds).
With a dedicated crontimes table you can easily add many more things as needed.
Hope this helps someone!
1. Create a new table in the Tutorial database called crontimes with 2 fields.

2. Here's the source code...
Code: Select all
<?PHP
/*!
* runcron.php
* 2013-3-11
* Ryan Allen
*
* This file is used to run automated stuff.
*
*/
$currenttime = time(); // current unix formatted time
$healingtime = 10; // Passive healing over time in seconds
$healedhealth = 1; // Passive amount of health restored
$manatime = 3; // Passive mana over time in seconds
$restoredmana = 1; // Passive amount of mana restored
// This function updates a certain field with currenttime
function updateCrontime($field, $currenttime) {
$updatecrontime = "UPDATE crontimes SET $field = '$currenttime'";
mysql_query($updatecrontime) or die("Could not set current time for crontimes because: " . mysql_error());
}
// Grab and sets cron data from database
$croninfo = "SELECT * FROM crontimes";
$croninfo2 = mysql_query($croninfo) or die("Could not grab all crontimes because: " . mysql_error());
$croninfo3 = mysql_fetch_array($croninfo2);
$hptime = $croninfo3['time1'];
$mtime = $croninfo3['time2'];
// If crontimes table is empty add currenttime to all fields
if ($hptime < 1 || $mtime < 1) {
$updatecrontime = "INSERT INTO crontimes (time1, time2) values ('$currenttime', '$currenttime')";
mysql_query($updatecrontime) or die("Could not set current time for crontimes because: " . mysql_error());
}
else {
// Difference between currenttime and collective crontimes
$htimepassed = $currenttime - $croninfo3['time1']; // Health
$mtimepassed = $currenttime - $croninfo3['time2']; // Mana
// Passive Health
if ($playerinfo3['health'] < $playerinfo3['maxhealth']) {
if ($htimepassed > $healingtime) {
$intervals = $htimepassed / $healingtime;
$intervals = (int)$intervals;
$healedhealth = $intervals * $healedhealth;
$totalhealth = $playerinfo3['health'] + $healedhealth;
if ($totalhealth > $playerinfo3['maxhealth']) {
$totalhealth = $playerinfo3['maxhealth'];
}
$updateplayer = "UPDATE players SET health = '$totalhealth' WHERE email = '$email'";
mysql_query($updateplayer) or die("Could not set current health for player because: " . mysql_error());
updateCrontime('time1', $currenttime);
}
}
// Passive Mana
if ($playerinfo3['spoints'] < $playerinfo3['maxspoints']) {
if ($mtimepassed > $manatime) {
$intervals = $mtimepassed / $manatime;
$intervals = (int)$intervals;
$restoredmana = $intervals * $restoredmana;
$totalmana = $playerinfo3['spoints'] + $restoredmana;
if ($totalmana > $playerinfo3['maxspoints']) {
$totalmana = $playerinfo3['maxspoints'];
}
$updateplayer = "UPDATE players SET spoints = '$totalmana' WHERE email = '$email'";
mysql_query($updateplayer) or die("Could not set current mana for player because: " . mysql_error());
updateCrontime('time2', $currenttime);
}
}
}
?>
With a dedicated crontimes table you can easily add many more things as needed.
Hope this helps someone!
Last edited by Kxmode on Tue Mar 12, 2013 5:36 am, edited 2 times in total.
Re: Browser MMO Video #36
If you wanted to periodically award gold...


Cool beans! 


Code: Select all
<?PHP
/*!
* runcron.php
* 2013-3-11
* Ryan Allen
*
* This file is used to run automated stuff.
*
*/
$currenttime = time(); // current unix formatted time
$healingtime = 10; // Passive healing over time in seconds
$healedhealth = 1; // Passive amount of health restored
$manatime = 3; // Passive mana over time in seconds
$restoredmana = 1; // Passive amount of mana restored
$goldtime = 7; // Passive gold over time in seconds
$awardedgold = 1; // Passive amount of gold given
// This function updates a certain field with currenttime
function updateCrontime($field, $currenttime) {
$updatecrontime = "UPDATE crontimes SET $field = '$currenttime'";
mysql_query($updatecrontime) or die("Could not set current time for crontimes because: " . mysql_error());
}
// Grab and sets cron data from database
$croninfo = "SELECT * FROM crontimes";
$croninfo2 = mysql_query($croninfo) or die("Could not grab all crontimes because: " . mysql_error());
$croninfo3 = mysql_fetch_array($croninfo2);
$hptime = $croninfo3['time1'];
$mtime = $croninfo3['time2'];
$gtime = $croninfo3['time3'];
// If crontimes table is empty add currenttime to all fields
if ($hptime < 1 || $mtime < 1 || $gtime < 1) {
$updatecrontime = "INSERT INTO crontimes (time1, time2, time3) values ('$currenttime', '$currenttime', '$currenttime')";
mysql_query($updatecrontime) or die("Could not set current time for crontimes because: " . mysql_error());
}
else {
// Difference between currenttime and collective crontimes
$htimepassed = $currenttime - $croninfo3['time1']; // Health
$mtimepassed = $currenttime - $croninfo3['time2']; // Mana
$gtimepassed = $currenttime - $croninfo3['time3']; // Gold
// Passive Health
if ($playerinfo3['health'] < $playerinfo3['maxhealth']) {
if ($htimepassed > $healingtime) {
$intervals = $htimepassed / $healingtime;
$intervals = (int)$intervals;
$healedhealth = $intervals * $healedhealth;
$totalhealth = $playerinfo3['health'] + $healedhealth;
if ($totalhealth > $playerinfo3['maxhealth']) {
$totalhealth = $playerinfo3['maxhealth'];
}
$updateplayer = "UPDATE players SET health = '$totalhealth' WHERE email = '$email'";
mysql_query($updateplayer) or die("Could not set current health for player because: " . mysql_error());
updateCrontime('time1', $currenttime);
echo "Health increased<br>";
}
}
// Passive Mana
if ($playerinfo3['spoints'] < $playerinfo3['maxspoints']) {
if ($mtimepassed > $manatime) {
$intervals = $mtimepassed / $manatime;
$intervals = (int)$intervals;
$restoredmana = $intervals * $restoredmana;
$totalmana = $playerinfo3['spoints'] + $restoredmana;
if ($totalmana > $playerinfo3['maxspoints']) {
$totalmana = $playerinfo3['maxspoints'];
}
$updateplayer = "UPDATE players SET spoints = '$totalmana' WHERE email = '$email'";
mysql_query($updateplayer) or die("Could not set current mana for player because: " . mysql_error());
updateCrontime('time2', $currenttime);
echo "Mana increased<br>";
}
}
// Passive Gold
if ($gtimepassed > $goldtime) {
$intervals = $gtimepassed / $goldtime;
$intervals = (int)$intervals;
$awardedgold = $intervals * $awardedgold;
$totalgold = $playerinfo3['gold'] + $awardedgold;
$updateplayer = "UPDATE players SET gold = '$totalgold' WHERE email = '$email'";
mysql_query($updateplayer) or die("Could not set current gold for player because: " . mysql_error());
updateCrontime('time3', $currenttime);
echo "Gold increased<br>";
}
}
?>

- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Browser MMO Video #36
Very nice!
When you say "asynchronous" you mean that these are the single scripts to run as cron jobs, as they will run as the player is playing? Otherwise, I am not sure what you mean by "asynchronous".
When you say "asynchronous" you mean that these are the single scripts to run as cron jobs, as they will run as the player is playing? Otherwise, I am not sure what you mean by "asynchronous".
The indelible lord of tl;dr
Re: Browser MMO Video #36
Thanks!Jackolantern wrote:Very nice!
When you say "asynchronous" you mean that these are the single scripts to run as cron jobs, as they will run as the player is playing? Otherwise, I am not sure what you mean by "asynchronous".
Yes the code will run as the player is playing. In order to ensure the code runs constantly it needs to be included inside all the game's main files. By asynchronous I mean the health, mana, and gold update at different intervals independent of each other.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Browser MMO Video #36
Oh, I gotcha! Maybe I have been staring at too much async node and C# code recently
Very nice solution, though

Very nice solution, though

The indelible lord of tl;dr
Re: Browser MMO Video #36
Hi Halls,
In the thread Before Watching the videos!! you wrote:
First, can you skip the admin section and move into something new like say an auction house? Video #36 should provide viewers with the enough basics to expand the admin for all the sections they want to administrate in their game. Basically they need to INSERT (add), DELETE (remove), UPDATE (edit) and SELECT (grab) based on access level. If they don't understand how to do this by now then it's probably not worth your time to demonstrate step-by-step.
Second, may I suggest not getting too hung up on CSS and "layout" in these videos. I'm not going to use your CSS and HTML code. So when you stop working on the php code to deal with CSS, layout and positioning it really slows down the videos.
Thank you very much for your time and effort!
In the thread Before Watching the videos!! you wrote:
May I suggest a couple things?I am building this game and making these videos in the simplest possible form. These videos are here for you to learn from. Not to copy and make a static carbon copy of the game. You take the game and add what features you want and learn from trying. There is no better way to learn than to try and if I build the entire game from start to finish and add every feature you will never get a chance to try and the video series would be 300 videos and never get done.
First, can you skip the admin section and move into something new like say an auction house? Video #36 should provide viewers with the enough basics to expand the admin for all the sections they want to administrate in their game. Basically they need to INSERT (add), DELETE (remove), UPDATE (edit) and SELECT (grab) based on access level. If they don't understand how to do this by now then it's probably not worth your time to demonstrate step-by-step.
Second, may I suggest not getting too hung up on CSS and "layout" in these videos. I'm not going to use your CSS and HTML code. So when you stop working on the php code to deal with CSS, layout and positioning it really slows down the videos.
Thank you very much for your time and effort!

- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Browser MMO Video #36
Thanks for the suggestions! I am actually planning a part two to all this that will be advanced subjects and complete the series