In the sandbox environment, I'm having trouble advancing a turnbased match to the next player.
Initial conditions:
- Player A and Player B on Device A and Device B, respectively.
- Both logged into the sandbox
- Both players can see each other's GC status message
- Player A creates a match and invites player B
- Player A ends the turn
In my "end turn" function I do the following:
NSLog(@"size = %ld", updatedMatchData.length);
//move the current player to the bottom of the list
NSMutableArray *nextPlayers = (NSMutableArray *)theMatch.participants;
NSLog(@"%@", [nextPlayers description]);
GKTurnBasedParticipant *firstGuy = nextPlayers[0];
[nextPlayers removeObjectAtIndex:0];
[nextPlayers addObject:firstGuy];
NSLog(@"------");
NSLog(@"%@", [nextPlayers description]);
//send the match to the servers
//"theMatch" was recorded in turnBasedMatchmakerViewController:didFindMatch
[theMatch endTurnWithNextParticipants:nextPlayers
turnTimeout:GKTurnTimeoutDefault
matchData:updatedMatchData
completionHandler:^(NSError *error)
{
if (error)
{
NSLog(@"WTF?");
}
}];
That produces the following log output:
size = 26926
(
"<GKTurnBasedParticipant 0x174018630 - playerID:G:1084583147 (local player) status:Active matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>",
"<GKTurnBasedParticipant 0x174018ba0 - playerID:G:12962188 status:Invited matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>"
)
------
(
"<GKTurnBasedParticipant 0x174018ba0 - playerID:G:12962188 status:Invited matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>",
"<GKTurnBasedParticipant 0x174018630 - playerID:G:1084583147 (local player) status:Active matchOutcome:None lastTurnDate:(null) timeoutDate:(null)>"
)
However, player B does not receive an invite or a turn. Player B's game center app shows no active games or turns. Player A's game center continues to show that he still has a turn pending. Each time I restart and re-execute the test, Player A racks up yet another pending turn.
Player A fires player:receivedTurnEventForMatch:didBecomeActive right after I end the turn, but didBecomeActive is set to NO.
So then I changed the timeout to 30 seconds. 30 seconds after playerA ends the turn, playerA fires didBecomeActive (no). PlayerB finally receives an invite prompt. Player B fires didBecomeActive, with a value of YES.
Why does my turn not advance immediately to player B after player A ends the turn? Why does player A seem to have another turn (which then times out and passes over to player B)?
Finally solved this. Do not attempt to edit the match object. Don't directly edit the matchData, or any other component of the match. Create a copy, do whatever you need to do to the copy and resubmit the copy.
My attempt to sort the players produced all sorts of erratic results until I created a completely separate participants array and sorted that. Then it worked as advertised.