Jump to content
MakeWebGames

How to make a timer?


begood

Recommended Posts

Take the current timestamp (time()) and add seconds to that for how long you want them to wait. Then you have a timestamp of which then can do another crime. So check

 

if (time() > $storedTime)
{
#expired, time go go
}
else
{
#not expired yet, subtract time() from $storedTime to how many seconds left
}
Link to comment
Share on other sites

I try it but I can't see how it can work... Look:

(wt = waiting time to do another crime...)

$wt = 0;

if (time() > $wt)
{
I can make another crime.
   $wt = time()+10;
}
else
{
    I can't...
}

 

That's what I've think to do but obviously don't work... Can you explain with more details how to do that... :S

Thanks and sorry for lost of time...

Link to comment
Share on other sites

I try something else, look:

with:

 

if (time() > $this->player->wt)
{
    I can make another crime
    $query = $this->db->execute('UPDATE <ezrpg>players SET wt=? WHERE id=?', array(time() + 10, $this->player->id));
}
else
{
    I can't...
}

 

On db it gives me allways the same value: 838:59:59 and not 18:49:30 (if when i do the crime it was 18:49:20), as I expected.

And with:

 

if (time() > $this->player->wt)
{
    I can make another crime
    $query = $this->db->execute('UPDATE <ezrpg>players SET wt=? WHERE id=?', array($smarty.now + 10, $this->player->id));
}
else
{
    I can't...
}

 

On db it gives me allways 00:00:10 and not 18:49:30 (if when I do the crime it was 18:49:20), as I expected...

:s

Link to comment
Share on other sites

I don't know what $smarty.now is stored as but time() gives you the unix timestamp which is this large number of seconds since the unix epoch. Soo. You have to add number of seconds to it. I doubt you want just 10 seconds. So do this.

 

$storedTime = intval(time()) + intval(300);
//5 minutes added to timestamp

 

Then check

if (intval(time()) >= intval($storedTime))
{
// done waiting
}
Link to comment
Share on other sites

I've tested it now but it still don't working. I think the problem is when I do a crime it add to $storedTime - intval(time()) + intval(300), all right. But when it reloads the page $storedTime 'reset' to nothing so it allows the player to do how many crimes he wants... That's why I think to add the $storedTime to db but the value it stored in db it's all ways the same. :s

My code, right now is it:

 

<?php

defined('IN_EZRPG') or exit;



class Module_Crimes extends Base_Module

{



   public function start()

   {
if (intval(time()) >= intval($storedTime))
{

       switch($_GET['act'])

       {

         case 'um':

             $this->um();

             break;

       }
}
else
{
$msg = 'Time not over - can't do another crime right now...';

header('Location: index.php?msg=' . urlencode($msg));
}

   }





   private function render()

   {

       $this->tpl->assign('player', $this->player);

       $this->tpl->display('crimes.tpl');

   }



	// Crime um



   private function um()

   {
$storedTime = intval(time()) + intval(300);
$chance = rand(1,2);

if ($this->player->stamina < 5)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+5 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-5 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+3 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 5€ 3 pontos de experiência e perdeste 5% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }
}

?>
Edited by begood
Link to comment
Share on other sites

<?php

defined('IN_EZRPG') or exit;



class Module_Crimes extends Base_Module

{



   public function start()

   {

if (intval(time()) >= $this->player->tempo_crime)

{

       switch($_GET['act'])

       {

         case 'um':

             $this->um();

             break;

         case 'dois':

             $this->dois();

             break;

         case 'tres':

             $this->tres();

             break;

         case 'quatro':

             $this->quatro();

             break;

         case 'cinco':

             $this->cinco();

             break;

         case 'seis':

             $this->seis();

             break;

         case 'sete':

             $this->sete();

             break;

         default:

             $this->render();

             break;

       }

}

else

{

$msg = 'Cant do another crime...';

header('Location: index.php?msg=' . urlencode($msg));

}

   }





   private function render()

   {

       $this->tpl->assign('player', $this->player);

       $this->tpl->display('crimes.tpl');

   }



// Crime um



   private function um()

   {

$storedTime = intval(time()) + intval(300);

$chance = rand(1,2);

if ($this->player->stamina < 5)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+5 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-5 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+3 WHERE id=?', array($this->player->id));
$query = $this->db->execute('UPDATE <ezrpg>players SET tempo_crime=? WHERE id=?', array($storedTime, $this->player->id));

$msg = 'Ganhas-te 5€ 3 pontos de experiência e perdeste 5% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime dois



   private function dois()

   {

$chance = rand(1,2);

if ($this->player->stamina < 10)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+10 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-10 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+6 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 10€, 6 pontos de experiência e perdeste 10% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime tres



   private function tres()

   {

$chance = rand(1,2);

if ($this->player->stamina < 12)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+15 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-12 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+10 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 15€, 10 pontos de experiência e perdeste 12% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime quatro



   private function quatro()

   {

$chance = rand(1,2);

if ($this->player->stamina < 15)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+17 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-15 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+13 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 17€, 13 pontos de experiência e perdeste 15% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime cinco



   private function cinco()

   {

$chance = rand(1,2);

if ($this->player->stamina < 16)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+20 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-16 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+15 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 20€, 15 pontos de experiência e perdeste 16% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime seis



   private function seis()

   {

$chance = rand(1,2);

if ($this->player->stamina < 20)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+24 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-20 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+17 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 24€, 17 pontos de experiência e perdeste 20% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }



	// Crime sete



   private function sete()

   {

$chance = rand(1,2);

if ($this->player->stamina < 27)

{

$msg = 'Não tens stamina suficiente!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

if ($chance == 1)

{

$query = $this->db->execute('UPDATE <ezrpg>players SET money=money+35 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET stamina=stamina-27 WHERE id=?', array($this->player->id));

$query = $this->db->execute('UPDATE <ezrpg>players SET exp=exp+25 WHERE id=?', array($this->player->id));

$msg = 'Ganhas-te 35€, 25 pontos de experiência e perdeste 27% da tua stamina!';

header('Location: index.php?msg=' . urlencode($msg));

}

else

{

$msg = 'Escapaste por pouco, uma stora ia-te apanhando!! Infelizmente não conseguiste sacar nada...';

header('Location: index.php?msg=' . urlencode($msg));

}

}

   }

}

?>

 

That's my code right now... I don't know where is the error...

tempo_crime (is where the time is stored on db has his type defined to timestamp)

I'm making tests in private function um().

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...