How to detect when Game Center turn based match has ended in iOS9?
The lifecycle of a turn based match looks like this:
- Create a match
- Invite others
- Others join
- Players take turns and pass the match object around (your game logic decides the order)
- Players send exchanges back and forth (optional)
- Players start leaving
- ----because they have been eliminated
- ----because they quit
- ----because they timed out
- Someone Wins
If you are not the active player, you are notified when steps 3, 4, 7, 8 and 9 happen by playerReceivedTurnEventForMatch
firing; however, as you can see from my answer here https://stackoverflow.com/a/34458493/1641444 playerReceivedTurnEventForMatch
fires for a lot of different conditions, and it doesn't tell you which one triggered it. You have to discern that based on the status of the players, which player is the active player, and whatever other information you track in the match object.
You are notified of #5 by playerReceivedExchangeRequest
firing (and Replies and Cancellation functions).
Your game logic will decide when to trigger #7. My preference is that the match object comes to the eliminated player, they are recognized as defeated, and calls participantQuitInTurnWithOutcome
Players decide when to trigger #8 by quitting, and the code calls either participantQuitInTurnWithOutcome
or participantQuitOutOfTurnWithOutcome
depending on their state.
Condition #9 is a real pain in the ass. Between limitations in the game design and outright bugs, timeouts can create several unrecoverable edge cases. Handling timeouts warrants its own full answer.
Finally, #10 is triggered by calling endMatchInTurnWithMatchData
. GKTurnBasedEventHandlerDelegate
and handleMatchEnded
were deprecated in IOS7. When using GKLocalPlayerListner
, you'll be notified of #10 by yet another occurrence of playerReceivedTurnEventForMatch
Edit--Clarifications based on followup questions:
Yeah, exactly. In your sendTurn function, when the game is over, don't call endTurnWithNextParticipant
. Instead, set the each participant's status to indicate who won and who lost, and then call endMatchInTurnWithMatchData
.
The other players will see playerReceivedTurnEventForMatch
fire. You can discern from the match status and the player status that the game is over and decide what actions to take for that recipient.
When does player(_:wantsToQuitMatch:) get called?
After several years of working with GKTurnBasedMatch
, I have Never seen wantsToQuitMatch
fire. Never. Not once.
The (massively overloaded) function, receivedTurnEventForMatch
is the only function that will fire when a player joins or leaves. (See my answer at https://stackoverflow.com/a/34458493/1641444 for more information about when each of the functions fire). It's a pain because receivedTurnEventForMatch
fires in many difference circumstances and it's left up to you to decipher which one triggered it.
This wasn't your question, but as you're just starting out with turn based matches, if you're not too heavily invested yet, I suggest you consider the new GKGameSession
announced at WWDC this year. GKTurnBasedMatch
has numerous constraints that can leave games stranded (can't be continued by any player). I'm just beginning to look into Game Sessions myself; however, at first glance, they give you much greater control plus a significantly larger match data object (512kB vs 64kB).
Related Topics
Choosing Units with Measurementformatter
Facebook App Requests Aren't Shown on iOS Devices
Xcode Duplicate Symbols for Architecture Error After Updating Cocoa Pods
Detect When a Unicode Character Cannot Be Displayed Correctly
Remote Image Size Without Downloading
Perform Action by Clicking on Some Word in Uitextview or Uilabel
How to Query Nearest Users in Firebase with Swift
What's Wrong Here: Instance Member Cannot Be Used on Type
Uipickerview as Inputview of Uitextfield
Cncopycurrentnetworkinfo with iOS 13
Apple Push Notification Service Server Certificate Update
How to Customise Uislider Height
Filter Array by First Letter of String Property
How to Get All Available Wifi Network Name Listings in iOS