1

Let me first describe you what I'm trying to do with one example:

- Players join a game (it's a web turn-based app/game with firebase/angular)
- When they are ready they have 30 seconds to choose a character
- Then player 1 has 30 sec to end his turn
- Then player 2 has 30 sec to end his turn
- ...

It's a little turn-based game and I'm having difficulties to end a turn. I thought about using cron or setInterval on a cloud function but setInterval isn't recommended (costly) and cron isn't exactly what I'm looking for (I don't want to trigger something everyday...). I saw this informations on two similars topics:

setInterval on firebase instead of using cron && Cloud Functions for Firebase trigger on time?

What should be the right way to do that (end a turn based on a time to increment the "state" of the game) ?

Emeric
  • 6,315
  • 2
  • 41
  • 54

1 Answers1

0

You can use setInterval in client-side js. Running setInterval in the browser is the correct way to perform some action after a pre-determined interval of time in javascript. setInterval does not work on cloud functions, but you don't need them there.

Psuedo code goes like:

  1. Start player's turn by saving the current time
  2. setInterval to start time + interval
  3. 30 seconds later your setInterval callback executes where you can update the game state
  4. Rinse and repeat
Ananth
  • 848
  • 11
  • 26
  • I already thought about doing that but as you said: it's client-side, and it's not the client's job to do that. But I think I'll use this solution if there is no way to do that on server side, thank's for your answer ! – Emeric Nov 05 '18 at 10:42
  • You can execute this logic on the client, but verify it in a cloud function or whatever backend you have. When the game state changes, have a function watching that data, which checks that each player's turn is only as long as the interval. This can help identify if something's not right with your clients. – Ananth Nov 05 '18 at 10:44