Browser MMO Video #36
Posted: Sat Dec 24, 2011 12:03 am
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);
}
}
}
?>
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>";
}
}
?>
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".
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.