Jump to content
MakeWebGames

Bodyguard timer


Eurogangster

Recommended Posts

hello all i need your help to solve this issue. I created a function where users purchase with point bodyguard protection for 18 hours and this means they cant be killed.

I added the timer in the loggeding.php. Everything is going good until the timer is 00:00.

It roll over again and starts from the beginning and i cant find why

 

the  code of the timer is this :

 

new hook("userInformation", function ($user) {
        global $page;

    $currentTime = new DateTime();
    $bodyguardTime = new DateTime();
    $bodyguardTime->setTimestamp($user->getTimer("bodyguard"));
    $timeRemaining = $currentTime->diff($bodyguardTime);
    $timeRemaining = $timeRemaining ->format(" %h hours, %i minutes ");

        if (($timeRemaining) > 0) {
        $page->addToTemplate('bodygua_timer',$timeRemaining);
    } else {
    $messagebodyg = 'No Protection';
        $page->addToTemplate('bodygua_timer', $messagebodyg);
    }

    });

 

Link to comment
Share on other sites

a) $timeRemaining should be an integer - make it the number of seconds (%s) instead of a string of hours and minutes

b) Format the first occurence of addToTemplate to be the string you have in line 8

c) How is "bodyguard" timer updated - what is it updated with?

Link to comment
Share on other sites

When someone purchase the bodyguard protection : Can you rewrite my code to the correct one because my brain is out of order after many hours of trying.

 

 case "bodyguard":
			if($qty>1){
			$this->error('You can buy one Bodyguard Protection each time.');
			}
			else if($this->user->checktimer("bodyguard")){
                $this->user->set("US_points", $this->user->info->US_points - $cost);
			$this->user->updateTimer("bodyguard", time() + (1 * 64800));
			$this->user->updateTimer("bodyguardpurchase", time() );
                        $this->error('You paid ' . $cost . ' {_setting "pointsName"} for 18 hours of Bodyguard Protection', "success");
			}else{
			$this->error('You have already purchased Bodyguard Protection. You can buy one each time.');
			}
                    break

 

No one can help me with this ?

 

Link to comment
Share on other sites

@Eurogangster as @sniko (and I previously on Discord) said you're comparing a string to an integer, this check is going to create some weird results. Diff returns a string and shouldn't be used for comparison.

You should actually just compare between the two DateTime instances you already have declared, the diff call just gives you the output to display to your users.

Take the following example from PHP.net (https://www.php.net/manual/en/datetime.diff.php). This shows the 3 different types of comparisons you can do on DateTime

<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");

// Are the dates the same?
var_dump($date1 == $date2);
// Is date 1 before date 2?
var_dump($date1 < $date2);
// Is date 1 after date 2?
var_dump($date1 > $date2);
?>

In your case you're wanting to compare if the bodyguard timer is greater than the current time.

$currentTime = new DateTime();
$bodyguardTime = new DateTime();
$bodyguardTime->setTimestamp($user->getTimer("bodyguard"));

if ($bodyguardTime > $currentTime) {
    // Has a bodyguard
    $timeRemaining = $currentTime->diff($bodyguardTime);
    $timeRemaining = $timeRemaining ->format(" %h hours, %i minutes ");
    $page->addToTemplate('bodygua_timer',$timeRemaining);
}

 

Link to comment
Share on other sites

This is what i wrote and i tested it and works 100% perfect.

$timeb = time();
    $bodyguardTime = $this->getTimer("bodyguard");

    $timeRemaining =  $bodyguardTime - $timeb ;

        if ($bodyguardTime > time() ) {
$timeRemaining = gmdate("H:i:s", $timeRemaining);
        $page->addToTemplate('bodygua_timer',$timeRemaining);
    } else {

$messagebodyg = 'No Protection' ;
$page->addToTemplate('bodygua_timer',$messagebodyg);
}    
5 hours ago, Dave said:

Great! What was the problem you found?

 

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...