Page 1 of 1

Cron Job Question [Resolved]

Posted: Sun Sep 01, 2013 10:24 am
by Epiales
I have a new cronjob setup that seems to work very well. It's setup within the game pages, so I don't have to automatically set a cronjob through the server. It will also accumulate the turns and health(hp) while they are offline. It is working, but when I manually run it, I get a few undefines, and I have no idea what to define them to. Here is the code:

Code: Select all

<?php
$currenttime = time();

////////////////////static variables////////////////////
//// Everything is done in seconds////
$hptime = 30;
$healedhealth = 2;
$turntime = 60;
$healedturns = 5;

//////////////////////////////////////////////////////

if($stats['crontime'] < 1)
{
$updateplayer="update stats set crontime='$currenttime' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

}
else
{
$timepassed = $currenttime - $stats['crontime'];

   if($timepassed > $hptime)
   {
   $intervals = $timepassed / $hptime;
   $intervals = (int)$intervals;
   $healedhealth = $intervals * $healedhealth;
    $totalhealth = $stats['hpoints'] + $healedhealth;
   if($totalhealth > $stats['maxhp']){$totalhealth = $stats['maxhp'];}
   
   $updateplayer="update stats set crontime='$currenttime',hpoints='$totalhealth' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");
   
   }
  }
if($stats['crontime'] < 1)
{
$updateplayer="update stats set crontime='$currenttime' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

}
else
{
$timepassed = $currenttime - $stats['crontime'];

   if($timepassed > $turntime)
   {
   $intervals = $timepassed / $turntime;
   $intervals = (int)$intervals;
   $healedturns = $intervals * $healedturns;
    $totalturns = $stats['turns'] + $healedturns;
   if($totalturns > $stats['maxturns']){$totalturns = $stats['maxturns'];}
   
   $updateplayer="update stats set crontime='$currenttime',turns='$totalturns' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player"); 
 }
 }
 ?>
As you can see, it's setup to even update their information if they are not playing as well. But the two undefined I get are:

Notice: Undefined variable: stats in C:\xampp\htdocs\flash\runcron.php on line 13

Notice: Undefined variable: id in C:\xampp\htdocs\flash\runcron.php on line 15
Could not update player

Now I can probably define the id as $id = $stats['id']; and that would remove the 2nd one, but also add another one because stats is not defined.

The reason this is important to me, is 1, I hate errors and want them fixed, but 2... It's resetting my turns to zero for whatever reason. All I can think is that it's something to do with this particular coding. Can't go around having turns getting set back to zero now can we? lol. Any ideas? Thanks

Re: Cron Job Question

Posted: Sun Sep 01, 2013 11:13 am
by Epiales
Rewrote it, but only hp is increasing... and if I give the player turns, those turns disappear upon refresh... Should be no reason why it's doing that. :cry:

Code: Select all

<?php
$currenttime = time();

////////////////////static variables////////////////////
//// Everything is done in seconds////
$hptime = 10;
$healedhealth = 2;
$turntime = 10;
$healedturns = 5;

//////////////////////////////////////////////////////

if($stats['crontime'] < 1)
{
$updateplayer="update stats set crontime='$currenttime' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

}
else
{
$timepassed = $currenttime - $stats['crontime'];

   if($timepassed > $hptime)
   {
   $intervals = $timepassed / $hptime;
   $intervals = (int)$intervals;
   $healedhealth = $intervals * $healedhealth;
    $totalhealth = $stats['hpoints'] + $healedhealth;
   if($totalhealth > $stats['maxhp']){$totalhealth = $stats['maxhp'];}
  } 

$timepassed = $currenttime - $stats['crontime'];
   if($timepassed > $turntime)
   {
   $intervals = $timepassed / $turntime;
   $intervals = (int)$intervals;
   $healedturns = $intervals * $healedturns;
    $totalturns = $stats['turns'] + $healedturns;
   if($totalturns > $stats['maxturns']){$totalturns = $stats['maxturns'];}   
   
   
   $updateplayer="update stats set crontime='$currenttime',hpoints='$totalhealth', turns='$totalturns' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");
   
 }
 }
 ?>

Re: Cron Job Question

Posted: Sun Sep 01, 2013 11:20 am
by Epiales
Update: For whatever reason, after rewriting code, it works fine now. Although I still get the undefines, it works now. YAY