2

在我的回合制比赛中,当一名不按顺序的玩家退出比赛时,我无法干净利落地结束比赛。我相信这会阻止我重新开始比赛。

这是我观察到的。当我从玩家 1 设备的 GKTurnBasedMatchmakerViewController 中查看该游戏的已完成游戏状态时,显示玩家 1 的比赛结果为 Quit,玩家 2 的比赛结果为“轮到此玩家”。但是,如果我从玩家 2 设备的 GKTurnBasedMatchmakerViewController 中查看该游戏的已完成游戏状态,则显示玩家 1 的比赛结果是 Quit,而玩家 2 的比赛结果是“赢”。但是,如果我从玩家 1 设备中的 GKTurnBasedMatchmakerViewController 中单击“查看游戏”,则玩家 2 的比赛结果会切换为“获胜”。如何正确结束此游戏,以便玩家 2 的比赛结果为“获胜” “从玩家 1 的角度来看?

在我的场景中,玩家 1 开始比赛并轮到他。然后玩家 2 开始他的回合。在此期间,玩家 1 退出比赛。那么代码序列如下:

玩家 1:

[currentMatch participantQuitOutOfTurnWithOutcome:GKTurnBasedMatchOutcomeQuit withCompletionHandler:nil];

玩家 2 - 在收到对 handleTurnEventForMatch:didBecomeActive 的调用后:

        // For each participant, set their matchOutcome relative to the local player who has just won.
        for (GKTurnBasedParticipant *part in [currentMatch.participants) {
            if ([part.playerID isEqualToString:[GKLocalPlayer localPlayer].playerID]) {
                part.matchOutcome = GKTurnBasedMatchOutcomeWon;
            }
            else {
                part.matchOutcome = GKTurnBasedMatchOutcomeQuit;

            }
        }
        [currentMatch endMatchInTurnWithMatchData: data
                             completionHandler: ^(NSError *error) {
                                if (error) {
                                    NSLog(@"%@", error);
                                }
                                else {
                                    NSLog(@"After win: match ended successfully");
                                }];

玩家 1 收到提示,询问他们是否想要重赛。如果用户回答是肯定的,则发送以下内容:

[currentMatch rematchWithCompletionHandler:^(GKTurnBasedMatch *reMatch, NSError *error) {
    if (error) {
        NSLog(@"In rematchWithCompletionHandler. Error creating rematch: %@", error.localizedDescription);
    } else {
        NSLog(@"Success");
    }];

不幸的是,由于玩家 2 的状态不正确,上述重赛尝试失败。上面的结果错误消息是:

The requested operation could not be completed because the match request is invalid.

以下堆栈溢出问题表明这是由于上述错误的匹配数据:Trouble Using the new rematchWithCompletionHandler method from Game Center

4

1 回答 1

0

为了解决这个问题,我不得不以编程方式加载比赛数据以强制玩家 2 的 matchOutcome 切换到“Won”状态。因此,当用户确认重新匹配提示时,我不会立即调用 rematchWithCompletionHandler,而是在他们确认重新匹配提示时执行以下操作:

[GKTurnBasedMatch loadMatchWithID:currentMatch.matchID withCompletionHandler:
  ^(GKTurnBasedMatch *match, NSError *error) {
    if (error) {
        NSLog(@"In requestRematch, failed to receive match data: %@", error.localizedDescription);
    }
    else {
         // Received the match data. Now request a rematch
         [match rematchWithCompletionHandler:
            ^(GKTurnBasedMatch *reMatch, NSError *error) {
              if (error) {
                  NSLog(@"In requestRematch: rematchWithCompletionHandler. Error creating rematch: %@", error.localizedDescription);
              }

              else
              {
                  NSLog(@"In requestRematch: Rematch granted with new matchID=%@. currentMatch was: %@", match.matchID, currentMatch.matchID);

                  // Do something with the reMatch data
              }

          }];
     }
}];
于 2014-06-26T19:48:00.297 回答