Game Center的自动匹配和endTurnWithNextParticipants [英] Game Center's Auto-match and endTurnWithNextParticipants

查看:92
本文介绍了Game Center的自动匹配和endTurnWithNextParticipants的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a turn-based game with two Game Center players, and I want to allow auto-matching.

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

I've read that, for an invitation to be actually sent out to a player, the inviting player must end his/her turn. This means calling this method:

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

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

Now, what I don't understand is the meaning of the "nextParticipants" array in case the match is started in auto-match mode, which, as I read, is done by setting the participants to nil, e.g.:

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

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

If the array is nil, and I don't know who's going to join the match, how can I possibly pass the turn to the next player? If I use nil in the nextParticipants argument, of course I get an 'invalid list of nextParticipants' error.

Apple的doc似乎对此保持沉默。

Apple's doc seems to be silent about this.

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

So, what I also don't understand is how auto-matching actually works. Is it going to match any two players who have started a new match with auto-match, unconditionally? Can't I somehow select what kind of matches I want to be auto-matched with? (suppose, e.g., the game allows several difficulty levels, and I don't want to be auto-matched with someone playing at a lower level).

编辑(如每个xcodegirl的评论):

EDIT (as per xcodegirl's comment):

为了解决这最后一点,通过在请求的playerGroup属性中添加编码所需匹配类型的东西来扩展上面的代码就足够了:

To address this last point, it suffices to extend the above code by adding something that encodes the desired kind of match in the playerGroup property of the request:

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

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

The bad thing, though, is that the playerGroup does not seem to be an available property of GKTurnBasedMatch. So, if you are listing your matches, including the pending auto-matches, and want to display information regarding the kind of game you want to play, you should store this info in some other way.

推荐答案

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

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).

这篇关于Game Center的自动匹配和endTurnWithNextParticipants的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆