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

Game development with Board Game Arena Studio
Post Reply
User avatar
ShashaKrismas
Posts: 15
Joined: 21 April 2013, 10:30

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

Post 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
User avatar
Tisaac
Posts: 2344
Joined: 26 August 2014, 21:28

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

Post by Tisaac »

You can't do that on the backend so you will need to do that in js with setTimeout for instance
User avatar
abcde
Posts: 22
Joined: 24 January 2014, 18:30

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

Post 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;
	}
User avatar
ShashaKrismas
Posts: 15
Joined: 21 April 2013, 10:30

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

Post 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
User avatar
SwHawk
Posts: 133
Joined: 23 August 2015, 16:45

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

Post 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
Post Reply

Return to “Developers”