Cron Job Question [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Cron Job Question [Resolved]

Post 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
Last edited by Epiales on Sun Sep 01, 2013 11:44 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job Question

Post 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");
   
 }
 }
 ?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Cron Job Question

Post by Epiales »

Update: For whatever reason, after rewriting code, it works fine now. Although I still get the undefines, it works now. YAY
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”