Reputation: 207
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?
Upvotes: 1
Views: 141
Reputation: 4187
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.
Upvotes: 1