Domanda

I have implemented a custom matchmaker as a direct drop-in replacement to GKMatchmakerViewController, shown only when running in iOS6+. It is working perfectly, but there is one part of the UI in GKMatchmakerViewController that I can't seem to figure out.

When initiating a 2-player (request.minPlayers = 2, request.maxPlayers = 2) auto-match, GKMatchmakerViewController is able to update its UI with the display name and photo of the player that was found before they have changed to a connected state.

I use the following code to initiate an auto-match. A connection is made and the game starts and everything's fine.

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
    if (error != nil) {
        // ...the error handling code...
    } else if (match != nil) {
        NSLog(@"An auto-match has been found: %@", match);

        if (match.expectedPlayerCount == 0) {
            [[GKMatchmaker sharedMatchmaker] finishMatchmakingForMatch:match];
        } else {
            NSLog(@"player IDs: %@", match.playerIDs);
        }
    }
}];

However, I'm not able to get the playerID until they have changed to GKPlayerStateConnected via:

- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state;

When the match is found, that player is in GKPlayerStateUnknown. However, the match NSLog() in the code shows the playerID (it is definitely NOT the localPlayer's ID; the real number has been redacted):

An auto-match has been found: <GKMatch 0x210b2c90 expected count: 1 seqnum: 0
    G:1234567890:unknown
reinvitedPlayers:(
)>

The playerIDs array for the match (the second NSLog()) is empty as soon as the match is created, which makes sense because no connections have officially been made yet:

player IDs: (
)

I finally get to the questions (thank you for your patience):

1a) Where is the ID of the player in the unknown state coming from?

1b) It's in the match obviously but where exactly is it being stored? The only thing I see playerID-related is the array which is empty.

2) Is there some other (legal) way to obtain that playerID? i.e. before they change to a connected state

È stato utile?

Soluzione

You had the playerID in your findMatchForRequest completion handler:

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:matchRequest withCompletionHandler:^(GKMatch *match, NSError *error) {
  // -- removed error checking code for short --
  puts( "PLAYER ID's:" ) ;
  for( NSString* ns in match )
      puts( [ ns UTF8String ] ) ; // FORMAT: G:37145177499. DO NOT FUDGE WITH THE STRING.

    [GKPlayer loadPlayersForIdentifiers:theMatch.playerIDs withCompletionHandler:^( NSArray *players, NSError *nsError ) {
      puts( "The REMOTE player aliases are:" ) ;
      if( !nsError )
        for( GKPlayer* p in players )
          puts( [p.alias UTF8String ] ) ;
   } ] ;
}];

You can then retrieve the player's alias etc from GKPlayer loadPlayersForIdentifiers:withCompletionHandler:

Altri suggerimenti

Ok, answering my own question. It can be done thus:

NSLog(@"An auto-match has been found: %@", match);

NSString * matchDescription = [match description];
NSLog(@"%@", matchDescription);   // displays the same thing as NSLog(@"%@", match)

NSRange gRange = [matchDescription rangeOfString:@"G:"];
if (gRange.location != NSNotFound) {
    NSRange endSearchRange;
    endSearchRange.location = gRange.location + 2;  // skip the G:
    endSearchRange.length = matchDescription.length - endSearchRange.location;
    NSRange endRange = [matchDescription rangeOfString:@":" options:NSLiteralSearch range:endSearchRange];

    if (endRange.location != NSNotFound) {
        NSUInteger idSpan = endRange.location - gRange.location;
        gRange.length = idSpan;
        NSString * opponentPlayerID = [matchDescription substringWithRange:gRange];
        NSLog(@"%@", opponentPlayerID);    // G:1234567890

        // update the UI with the opponent's info
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top