Game Center的自动匹配和endTurnWithNextParticipants

Mai*_*aux 5 ios game-center

我正在开发一个有两个游戏中心玩家的回合制游戏,我想允许自动匹配.

我已经读到了,为了实际发送给玩家的邀请,邀请玩家必须结束他/她的回合.这意味着调用此方法:

- (void)endTurnWithNextParticipants:(NSArray *)nextParticipants turnTimeout:(NSTimeInterval)timeout matchData:(NSData *)matchData completionHandler:(void (^)(NSError *error))completionHandler
Run Code Online (Sandbox Code Playgroud)

现在,我不明白的是"nextParticipants"数组的含义,以防匹配在自动匹配模式下启动,正如我所读,通过将参与者设置为nil来完成,例如:

 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);
                 }];
Run Code Online (Sandbox Code Playgroud)

如果阵列为零,我不知道谁将参加比赛,我怎么可能将转牌传给下一位球员?如果我在nextParticipants参数中使用nil,当然我得到一个'nextParticipants的无效列表'错误.

Apple的文档似乎对此保持沉默.

所以,我也不明白的是自动匹配实际上是如何工作的.它会无条件地匹配任何已经开始与自动比赛进行新比赛的两名球员吗?我不能以某种方式选择我想要自动匹配的匹配项吗?(假设,例如,游戏允许几个难度级别,并且我不希望与在较低级别玩的人自动匹配).

编辑(根据xcodegirl的评论):

为了解决这一点,只需通过在请求的playerGroup属性中添加编码所需匹配类型的内容来扩展上述代码即可:

request.playerGroup = [Utils myEncodingAsNSUIntegerOfGameTypeGivenSomeParameters:...];
Run Code Online (Sandbox Code Playgroud)

但不好的是,playerGroup似乎不是GKTurnBasedMatch的可用属性.因此,如果您列出了您的匹配项,包括待处理的自动匹配项,并希望显示有关您要玩的游戏类型的信息,则应以其他方式存储此信息.

Mai*_*aux 6

经过一些尝试,似乎这个问题的第一部分的答案如下.一旦比赛开始,即使没有人匹配自动匹配邀请,参与者阵列也会填充所需数量的玩家(其中一个是邀请玩家),每个缺少的玩家都是GKTurnBasedParticipant,其状态为GKTurnBasedParticipantStatusMatching.因此,邀请玩家可以在没有等待受邀(自动匹配)玩家接受的情况下进行第一回合,只需创建一个下一个参与者的阵列,其中邀请玩家被放置在阵列的末尾.

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
                                 } ];
Run Code Online (Sandbox Code Playgroud)

然而,我的问题的第二部分仍然存在.我不清楚如何有条件地进行自动匹配工作(例如:我愿意与想要参加一级方程式赛车的人进行自动匹配,而不是与拉力赛车进行比赛).