游戏中心的自动匹配和endTurnWithNextParticipants

我正在开发一款带有两个Game Center玩家的回合制游戏,我希望允许自动匹配。

我已经读到,邀请被发送给玩家,邀请玩家必须轮到他/她。 这意味着调用这个方法:

- (void)endTurnWithNextParticipants:(NSArray *)nextParticipants turnTimeout:(NSTimeInterval)timeout matchData:(NSData *)matchData completionHandler:(void (^)(NSError *error))completionHandler 

现在,我不明白的是“nextParticipants”数组的含义,以便在自动匹配模式下启动匹配,在我看来,这是通过设置参与者为零来完成的,例如:

  GKMatchRequest *request = [[GKMatchRequest alloc] init]; request.minPlayers = 2; request.maxPlayers = 2; request.playersToInvite = nil; request.inviteMessage = @"Let's play"; [GKTurnBasedMatch findMatchForRequest: request withCompletionHandler: ^(GKTurnBasedMatch *match, NSError *error) { NSLog(@"%@", match); }]; 

如果arrays是零,我不知道谁将参加比赛,我怎么可能通过轮到下一个球员? 如果我在下一个参与者的论点中使用零,当然我会得到一个“下一个参与者的错误列表”的错误。

苹果的文件似乎对此一言不发。

所以,我也不明白的是自动匹配实际上是如何工作的。 是否将无条件地匹配任何已经与自动匹配开始新比赛的球员? 我不能以某种方式select什么样的比赛我想自动匹配? (假设,例如,游戏允许有几个难度级别,我不希望与某个玩家在较低级别上进行自动匹配)。

编辑(根据xcodegirl的评论):

为了解决这个问题,通过添加一些在请求的playerGroup属性中编码想要的匹配types来扩展上面的代码就足够了:

 request.playerGroup = [Utils myEncodingAsNSUIntegerOfGameTypeGivenSomeParameters:...]; 

不过,糟糕的是,playerGroup似乎不是GKTurnBasedMatch的可用属性。 因此,如果您列出了您的匹配项,包括未决的自动匹配项,并希望显示您想要玩的游戏types的信息,则应以其他方式存储此信息。

经过一番尝试,似乎这个问题的第一部分的答案如下。 一旦比赛开始,即使没有人匹配自动匹配邀请,参与者arrays也会按照请求填充尽可能多的玩家(其中一个是邀请玩家),每个缺less的玩家是GKTurnBasedParticipant,其状态是GKTurnBasedParticipantStatusMatching。 因此,即使不等待受邀(自动匹配)玩家接受,也可以通过简单地创build邀请玩家放置在arrays末尾的下一个参与者arrays来让邀请玩家玩第一个回合。

 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 } ]; 

但是,我的问题的第二部分依然存在。 目前还不清楚如何有条件地进行自动匹配工作(比如:我愿意与想要与F1赛车比赛的人自动匹配,而不是与拉力赛车)。

关于第二个(未答复的)部分,我认为GKMatchRequest.playerAttributes的目的是实现你想要的。 您设置了一些标识所选比赛属性的值,游戏中心确保匹配的参与者对相同的游戏选项感兴趣。 所以如果你有一个赛车游戏,你可以在不同的天气条件下赛车,自行车和船只,你可能会有这样的:

 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 */ 

这将确保任何匹配的球员也想在雨中赛艇(谁没有?)。

至less,我认为你应该这样做。 尽pipe说实话,我至今没有能够在沙箱里自己做游戏中心回合制的配对工作。