The online racing simulator
Countdown
Hello,
I created a command to start a countdown.
I can't count in minutes and seconds, I only have seconds.
Is it possible to display in minutes and seconds?

My code :


CASE "!5":
openGlobalButton( "countdown_button",78,60,42,20,13,300,16,"^3Race starts in %cpt% seconds");
BREAK;

As you can see, i put 300 for 300sec = 5 min, but it doesn't work.

Thanks in advance
Attached images
5.png
It's not possible with that function, but of course you could easily construct the correct code.

<?php 
CASE "!5":
$race_start_time_sec=300;
$race_start_minutes = $race_start_time_sec /60;

openGlobalButton( "countdown_button",78,60,42,20,13,-1,16,"^3Race starts in" . $race_start_minutes. " minutes", race_start_timer);
BREAK;
?>
Then you need to create an event called "race_start_timer".
In that event you lower $race_start_time_sec by one.
Then you need to calculated the minutes and minutes again, update the button and call the same event again.

Am doing this just from my head, so the code isn't tested.
Ok thanks, with your code i have this : (see picture)

I'm thinking, I'll get there Wink
Attached images
1.png
Shouldn't you run this to start the countdown? -> %cpt%
To calculate minutes from seconds you have to use a modulo function and not a simple division.

minutes = seconds%60
When i put a $ before "race_start_time_sec /60;" i see now "Race starts in 5 minutes" but the countdown doesn't start.

Rane_nbg, where i have to put "seconds%60" ?
From your questions, I can see that you don't understand the basics of programming and simple math.

I can not give you the finished working code, but I can steer you in the right direction. Read again this part that Yisc wrote above:

Then you need to create an event called "race_start_timer".
In that event you lower $race_start_time_sec by one.
Then you need to calculate the seconds and minutes again, update the button and call the same event again.

Your counter does not start because you didn't make one. You only display a message with initial settings, nothing is going on there, how do you expect a number to increment? It's not magic. I'm not trying to be rude, just pushing you a bit to think with your own head rather than asking for a finished solution to copy paste.
In order to create a countdown timer, you have to make a timer of some sort, a function that can read system time, I don't know how this is done in PHP.

Then you can make an event or a function that gets called when each second passes and increment your time variable. After that, you show your button with the message. After a certain preset time has passed you should no longer call the timer function.
Look, everyone has their own level and their own field, I'm just trying to tinker with something, I'm learning.
That's perfectly fine, everyone starts from somewhere. Until you have some more meaningful question that shows you did some homework or at least googled it first before posting here, it's gonna be like this.
Quote from rane_nbg :In order to create a countdown timer, you have to make a timer of some sort, a function that can read system time, I don't know how this is done in PHP.

Then you can make an event or a function that gets called when each second passes and increment your time variable. After that, you show your button with the message. After a certain preset time has passed you should no longer call the timer function.

First, the question is not about PHP but about Lapper.
I wrote my code between PHP-tags to present them properly on this forum, that's all/
Second, if you are not helping but only trying to wind someone up or even worse, make them drop their question and efforts because of not getting answers, you are not helping and it would be better not to answer.
I find this and i adapt for lapper, i try tonight.


CASE "!5":
// Durée du compte à rebours en secondes (5 minutes)
$duration = 5 * 60;

// Date et heure de fin du compte à rebours
$_SESSION['countdown_end_time'] = time() + $duration;
$remaining_seconds = $duration;

// Conversion des secondes en minutes et secondes
$minutes = floor($remaining_seconds / 60);
$seconds = $remaining_seconds % 60;

// Affichage du compte à rebours
openGlobalButton( "countdown_button",78,60,42,20,13,300,16, "^3Race starts in : $minutes minutes, $seconds secondes");
}
BREAK;


Thanks Yisc
Let us know if you got it to work, as it might help others at some point as well.

*edit*

I see that the code uses PHP functions which are not in Lapper.
So I think this won't work.
I will try to write the code as soon as I get home (in about 1,5 hour from now) and then post it here.
Ok, you have some progress there now.

I believe you have made a few mistakes. You are setting the variable $remaining_seconds to a fixed value which is 5*60 and then using that same variable that will stay fixed to calculate minutes and seconds. I hope you see what is the issue there. It's better to do something like this:
before a case 5 is called, make a new variable $time0 = time(), then do
$_SESSION['countdown_end_time'] = time() - $time0
$remaining_seconds = $duration - $_SESSION['countdown_end_time']

The math you used for seconds and minutes will result in a message that shows minutes=5, seconds=5. You have to calculate like this:
$minutes = $remaining_seconds % 60
$seconds = $remaining_seconds;

I think this should be it, assuming function time() returns the system time in seconds.
I wrote like this :


CASE "!5":

// Durée du compte à rebours en secondes (5 minutes)
$duration = 5 * 60;

// Date et heure de fin du compte à rebours
$time0 = time();
$_SESSION['countdown_end_time'] = time() - $duration;
$remaining_seconds = $duration-$_SESSION['countdown_end_time'];

// Conversion des secondes en minutes et secondes
$minutes = $remaining_seconds % 60;
$seconds = $remaining_seconds;

// Affichage du compte à rebours
openGlobalButton( "countdown_button",78,60,42,20,13,300,16, "^3Race starts in : $minutes minutes, $seconds secondes");

BREAK;

Maybe it works and it's my line here that's wrong:

openGlobalButton( "countdown_button",78,60,42,20,13,300,16, "^3Race starts in: $minutes minutes, $seconds seconds");

The code you found uses PHP functions (time, $_SESSION) that are not available in Lapper, so this will never work.
I will write the code when I get home, which is about half an hour from now.
Really, thank you
** Best answer **
Right, I had to get up to speed again, but it's done.
Here's all the code you need.

First the CASE:


CASE "!5":
Globalvar $Seconds;
Globalvar $DisplaySeconds;
Globalvar $Minutes;
Globalvar $start_race_seconds;

$Seconds = 0;
$DisplaySeconds = 0;
$Minutes = 0;
$start_race_seconds = 300;

calc_race_start_time( 0,0 );
BREAK;

Then the sub-event to calculate the time:


Sub calc_race_start_time( $KeyFlags,$id )
$Minutes = round(($start_race_seconds/60),0);
$Time = (round(($start_race_seconds/60),2)-$Minutes);
$Seconds = round(($Time*60),0);

IF ( $Seconds < 0 )
THEN # when seconds are negative
$Minutes = $Minutes-1;
$Seconds = 60 + $Seconds;
ENDIF

$DisplaySeconds=$Seconds;

Timer( $KeyFlags );#Goto Countdowntimer
EndSub

Then the actual button to display the time:


Sub Timer( $KeyFlags )
### Display button ###
openGlobalButton( "countdown_button",78,60,82,20,13,-1,16,"^3Race starts in " . $Minutes . " minutes and " . $DisplaySeconds . " seconds" );
### End ###

### Reduce seconds by 1 ###
### If seconds are below 0, reduce minutes by 1 and set seconds back to 59 ###
### If seconds are below 10, add a leading 0 to the displayed value ####
$Seconds = $Seconds-1;

IF ( $Seconds < 0 )
THEN
$Minutes = $Minutes-1;
$Seconds = 59;
ENDIF

IF ( $Seconds < 10 )
THEN
$DisplaySeconds="0".$Seconds;
ELSE
$DisplaySeconds=$Seconds;
ENDIF
### End ###

### If minutes and seconds are greater then or equal to 0, use the 1 second loop, else close button ###
IF ( $Minutes >= 0 && $Seconds >=0 )
THEN
HostDelayedCommand( 1, Timer );
ELSE
closeGlobalButton( "countdown_button" );
ENDIF
### End ###
EndSub

Wouaw Wink
Tonight is race night, I'm going to try to test it first. Otherwise tomorrow.
Thanks a lot

I will let you know quickly
I may have done something wrong but it doesn't work.
I'll try again tomorrow Smile
Quote from neron59 :I may have done something wrong but it doesn't work.
I'll try again tomorrow Smile

Probably, as I know for sure (100%) that it works. Smile
In the code he posted there were some text lines that were not commented out, pay attention to those, comment out or remove them.
Really Thank you !
It runs perfect, I learned something else Thumbs up

I hadn't placed some things in the right place

FGED GREDG RDFGDR GSFDG