1

I have created a 2 player turn based game. I want each player to have 24 hours to make their turn. If they don´t meet this time limit, I want to end the game.

.

I have tried using a NSTimer, with this the function below as the selector.

func timerCalled() {
                        for part in currentMatch!.participants!  {
                            (part ).matchOutcome = .Tied
                        }
                        currentMatch?.endMatchInTurnWithMatchData(data!, scores: nil, achievements: nil, completionHandler: { (error) -> Void in
                            if error != nil {
                                print(error)
                            }
                            self.statusLabel?.text = "Game has ended"
                        })
                    }

My game, however, crashes when this function is run, with this printed to the console:"The requested operation could not be completed because the session is in an invalid state". It crashes because I start the timer after the player has made his turn, so it is no longer his turn when "he" ends the game. I am sure it will work if I get the current player to end the game when the time is up, but how/when do I start the timer? When player 1 has made his turn, I have to start the timer with Player 2. How do I do this?

Nadim Baraky
  • 459
  • 5
  • 18
simiboy96
  • 207
  • 2
  • 5
  • 14

1 Answers1

1

Unfortunately, as of this writing, Game Center doesn't support what you want to do.

Your approach with Timers won't be effective because, as you've seen, you can't rely on player2 to take their turn and start the timer or to be active when the timer fires. And, as you've seen, player 1 can't force player 2's turn to end.

You can set the turnTimeout parameter when you call endTurnWithNextParticipants, basically saying, "I'm done with this turn and the next player has this length of time to complete their turn." However, per one of the WWDC videos, the last player in a turn will never timeout. So, in a two-player game (in any turn-based match, really), the last player always has a competitive advantage since they will never time out.

As a work around, I take the take the prior player's last turn date from the match data, add the timeout value to it, and check to see if the current time is beyond that. If it is, I kill the turn right there. However, player 2 may have waited days or even weeks to restart the game. Or they may never return. In which case, the turn based game is stranded, the prior players are stuck with no way to proceed unless/until the final player returns.

Thunk
  • 4,099
  • 7
  • 28
  • 47
  • 1
    possible workaround: what if the first player ends the turn providing two players for 'next participants' (2nd one, and then 1st one), so when 2nd player times out, the turn is passed to the 1st player. It works for me. So the idea is that 'next participants' array always has 2 players (infinite loop), so technically there is no "last player" – Anton Sep 26 '18 at 00:53
  • @Anton, I don't see that as a workaround, I see that as the solution. My reading of the documentation was that all participants should be included in endTurn(withNextParticipants...). I didn't see in the documentation something about not including a particular player... – Tyler A. Dec 26 '18 at 20:12