Game Center's Auto-match and endTurnWithNextParticipants

戏子无情 提交于 2019-12-05 10:53:30

After some attempts, it seems that the answer to the first part of this question is as follows. As soon as the match is started, even if no one has matched the automatch invitation, the array of participants is populated with as many players as requested (one of which is the inviting player), and every missing player is a GKTurnBasedParticipant whose status is GKTurnBasedParticipantStatusMatching. So, the inviting player could play the first turn even without waiting for the invited (auto-match) players to accept, by simply creating an array of next participants where the inviting player is placed at the end of the array.

NSMutableArray *nextParticipants = [NSMutableArray new];
for (GKTurnBasedParticipant *participant in match.participants) {
    if ([participant.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
        [nextParticipants addObject:participant];
    } else {
        [nextParticipants insertObject:participant atIndex:0];
    }
}

NSData *matchData = [@"some data" dataUsingEncoding:NSUTF8StringEncoding];

// Send new game state to Game Center & pass turn to next participant
[self.invitation.match endTurnWithNextParticipants: nextParticipants
                                       turnTimeout: GKTurnTimeoutDefault
                                         matchData: matchData
                                 completionHandler: ^(NSError *error) {
                                       // do something like refreshing UI
                                 } ];

Yet, the second part of my question still stands. It's unclear to me how to make auto-matching work conditionally (like: I'm willing to auto-match with someone who wants to race with Formula 1 cars, but not with Rally cars).

Regarding the second (unanswered) part, I think the purpose of the GKMatchRequest.playerAttributes is to achieve what you want. You set some value identifying the properties of the selected match and game centre ensures that matched participants are interested in the same game options. SO if you had a racing game where you can race cars, bikes and boats in different weather conditions, you might have this:

typedef enum GameType {
    GameTypeRaceCars = 0;
    GameTypeMotorbikes = 1;
    GameTypeBoats = 2;
};
typedef enum GameOptionWeather {
    GameOptionWeatherSunny = 0;
    GameOptionWeatherRainy = 1;
};
GKMatchRequest *request = /* boilerplate */
// bit 0-1 = GameType.
// bit 3   = Weather option.
request.playerAttributes = (uint32_t)GameTypeBoats;
request.playerAttributes |= (uint32_t)(GameOptionWeatherRainy) << 2;

/* boilerplate to create the match with the request */

That would ensure that any matched player also wants to race boats in the rain (who doesn't?).

At least, I think that's how you're supposed to do it. Though to be honest i have failed so far to get Game Center turn based matchmaking working in the sandbox myself.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!