Page 1 of 1

How should I implement a custom timer for a multiplayer phase?

Posted: 10 November 2022, 18:00
by ShashaKrismas
Hello,
For the game I am developping, I need to implement a timer for a multiplayer phase that goes 3... 2... 1... or at least being able to set it to three seconds tops and then goes on the next state after the end of this counter.
I searched for "reflexion time" on the wiki but all I could see was a method that added reflexion time to a player.

What is the best way to implement the counter? Given that the players need to perform actions during this time I can't use sleep on the server side, unless there is a way to do it on two different threads.

//Shanly

Re: How should I implement a custom timer for a multiplayer phase?

Posted: 10 November 2022, 18:59
by Tisaac
You can't do that on the backend so you will need to do that in js with setTimeout for instance

Re: How should I implement a custom timer for a multiplayer phase?

Posted: 12 November 2022, 20:30
by abcde
With the help of Tisaac & Vincentt code of The Crew, Mission Deep Sea, I have implemented a timer in
Escape the Curse of the Temple also a realtime multiplayer game.
the JS code for the timer is

Code: Select all

		
		launchTimer: function() {
			if (this.timer_running) return;
			this.timer_running = true;
			this.timer = setTimeout(() => this.increaseTimer(), 1000);
		},
		pauseTimer: function() {
			if (this.timer_running) clearTimeout( this.timer );;
			this.timer_running = false;
		},
		refreshTimer: 	function( time_sent ) {
			let sec = parseInt(time_sent % 60);
			$('time').innerHTML = parseInt(time_sent / 60) + ':' + (sec < 10 ? '0' : '') + sec;
		},
		increaseTimer: function() {
			this.game_duration++;
			this.refreshTimer( this.game_duration);
			this.timer = setTimeout(() => this.increaseTimer(), 1000);
			if ( this.phaseChangeNeeded( this.game_duration )) {
				this.performActionNotStock( 12);
			}
		},
		


this.game_duration is from php and sent to js through gamesdata .

php code

Code: Select all

	function getGameDuration() {
		$time_start = self::getGameStateValue('game_start');
		return time() -  $time_start;
	}

Re: How should I implement a custom timer for a multiplayer phase?

Posted: 15 November 2022, 21:26
by ShashaKrismas
Thank you abcde for your code,
Could you provide example of implementation of the timer?
I added
php:
in getAllDatas():

Code: Select all

$result['game_duration'] = $this->getGameDuration();
in the gamestatelabels

Code: Select all

"game_start" => 20,
in js right at the beginning

Code: Select all

   this.game_duration=3000;
            this.timer = 3000;
            this.timer_running = false;
and

Code: Select all

this.launchTimer();
right after setupNotifications()


I get the error

Code: Select all

Uncaught TypeError: $(...) is null
    refreshTimer https://studio.boardgamearena.com:8084/data/themereleases/current/games/kinapatest/999999-9999/kinapatest.js?1668543769904:355
    increaseTimer https://studio.boardgamearena.com:8084/data/themereleases/current/games/kinapatest/999999-9999/kinapatest.js?1668543769904:360
    timer https://studio.boardgamearena.com:8084/data/themereleases/current/games/kinapatest/999999-9999/kinapatest.js?1668543769904:345

Re: How should I implement a custom timer for a multiplayer phase?

Posted: 15 November 2022, 21:44
by SwHawk
The error you get implies that you're trying to get an element by its id attribute, but the id doesn't exist on the webpage. The trace indicates function calls at lines 355, 360 and 345 in your gamename.js so you should inspect the contents of those lines to see if you haven't made a typo while trying to get an element