Page 1 of 1

timer in php for realtime game

Posted: 20 October 2021, 08:10
by developgame
( this question has been edited to be more clear)
Hi Everyone
Another Newbie question again.
I am developing Escape, the curse of the temple. A multiplayer simultaneous play 10 minute real time race against the clock game.

A timer is needed for certain actions during these 10 minutes
at 3.1 minutes, 6.1 minutes and 9.1 minutes - players are alerted
at 4 minutes and 7 minutes - if players are not in the home chamber, they lose a die.
10 minutes - all lose if everyone has not escaped.


Currently whenever a player takes an action, I have php check the duration since the game start time and only then are players alerted of which phase they are in.

The problem with my method is that players are only alerted of the current game phases after a player takes an actions.
I do not know how to alert them that it is now pass the 4 and 7 minute mark. that is is safe to leave the home tile without a player having to take an action first.

How would you set up a timer for this game?
What would you advice?

Thanks for any input, advice, insight, suggestions!!!!

Re: timer in php for realtime game

Posted: 20 October 2021, 09:41
by vincentt
Hi,
You can add in the JS, a timeout that will trigger an action that will trigger notification from the PHP to notify everyone? :)

Not sure I am clear clear ^^

Cheers

Re: timer in php for realtime game

Posted: 20 October 2021, 11:02
by tchobello
hello

can't you write first phase name in action bar ?

something like 'SEARCH phase : YOU must select...'

Re: timer in php for realtime game

Posted: 20 October 2021, 15:41
by developgame
tchobello wrote: 20 October 2021, 11:02 hello

can't you write first phase name in action bar ?

something like 'SEARCH phase : YOU must select...'
Thanks for your response tchobello! Really appreciate your help.

I think my original question was not very well stated.
So stated better,
I was wondering how to set a timer, or something like a timer, that would be on in the background, while the players takes their actions. at 3.2 minutes into the game, the players are warned of they need to return to the home chamber. 4 minutes into the game, any players not in the home chamber will lose a die. this again happens 6.2 minutes into the game. 7 minutes into the game, another die is lost if not in home chamber. 9.2 minutes again the warning. 10 minutes game is over and players lose if they have not all escaped.

Re: timer in php for realtime game

Posted: 20 October 2021, 15:59
by developgame
vincentt wrote: 20 October 2021, 09:41 Hi,
You can add in the JS, a timeout that will trigger an action that will trigger notification from the PHP to notify everyone? :)

Not sure I am clear clear ^^

Cheers
Thanks for your response vincentt! Really appreciate your help.

How would you program JS to have the time out to trigger php at 3.2 minutes, 4 minutes, 6.2 minutes, 7 minutes, 9.2 minutes and 10 minutes into the game?

Re: timer in php for realtime game

Posted: 20 October 2021, 16:35
by vincentt
I guess a timer in JS
This is what we did in The Crew. You can adapt this to trigger an action and not do anything if you already did something

Code: Select all

    /**
     * setupTimer: put the timer on the table if needed
     */
    setupTimer() {
      $('game_play_area').classList.toggle('realTime', this.gamedatas.realTime);
      if (this.gamedatas.realTime) {
        this.refreshTimer();

        let mission = this.getMission();
        if (this.gamedatas.timer < mission.timer) {
          this.launchTimer();
        }
      }
    },

    /**
     * refreshTimer: update html content of timer
     */
    refreshTimer() {
      let sec = parseInt(this.gamedatas.timer % 60);
      $('realtime-timer').innerHTML = parseInt(this.gamedatas.timer / 60) + ':' + (sec < 10 ? '0' : '') + sec;
    },

    /**
     * launchTimer
     */
    launchTimer() {
      if (this._isTimerRunning) return;
      this._isTimerRunning = true;
      setTimeout(() => this.decreaseTimer(), 1000);
    },

    /**
     * stopTimer
     */
    stopTimer() {
      this._isTimerRunning = false;
    },

    /**
     * decreaseTimer : main js loop to decrease timer
     */
    decreaseTimer() {
      if (!this._isTimerRunning) return;
      if (this.gamedatas.timer <= 0) {
        this.takeAction('actTimerIsOver', { lock: false }, false);
        return;
      }

      this.gamedatas.timer--;
      this.refreshTimer();
      setTimeout(() => this.decreaseTimer(), 1000);
    },

Re: timer in php for realtime game

Posted: 28 October 2021, 01:23
by developgame
vincentt wrote: 20 October 2021, 16:35 I guess a timer in JS
This is what we did in The Crew. You can adapt this to trigger an action and not do anything if you already did something

Code: Select all

    /**
     * setupTimer: put the timer on the table if needed
     */
    setupTimer() {
      $('game_play_area').classList.toggle('realTime', this.gamedatas.realTime);
      if (this.gamedatas.realTime) {
        this.refreshTimer();

        let mission = this.getMission();
        if (this.gamedatas.timer < mission.timer) {
          this.launchTimer();
        }
      }
    },

    /**
     * refreshTimer: update html content of timer
     */
    refreshTimer() {
      let sec = parseInt(this.gamedatas.timer % 60);
      $('realtime-timer').innerHTML = parseInt(this.gamedatas.timer / 60) + ':' + (sec < 10 ? '0' : '') + sec;
    },

    /**
     * launchTimer
     */
    launchTimer() {
      if (this._isTimerRunning) return;
      this._isTimerRunning = true;
      setTimeout(() => this.decreaseTimer(), 1000);
    },

    /**
     * stopTimer
     */
    stopTimer() {
      this._isTimerRunning = false;
    },

    /**
     * decreaseTimer : main js loop to decrease timer
     */
    decreaseTimer() {
      if (!this._isTimerRunning) return;
      if (this.gamedatas.timer <= 0) {
        this.takeAction('actTimerIsOver', { lock: false }, false);
        return;
      }

      this.gamedatas.timer--;
      this.refreshTimer();
      setTimeout(() => this.decreaseTimer(), 1000);
    },
Thanks a bunch, vincentt!

Really appreciate you sharing your code with me. I will study it and learn much from it! :D