6

我正在通过游戏中心实现具有多人模式的回合制游戏。我有 2 台设备(1 台 ipad,1 台 iphone)在沙盒模式下进行测试,它们运行良好,但最近它开始在自动匹配过程中遇到困难。在我从一个用户发送第一回合后,另一台设备不会立即识别该游戏,而是打开自己的新游戏。在它能够立即发现游戏在另一台设备上开始之前,匹配相当简单。而且我不记得更改任何与匹配相关的部分(NSCoding、、GKTurnBasedEventHandler委托GKTurnBasedMatchmakerViewControllerDelegate方法等)。

现在我从一个设备发送第一个回合,需要等待大约 1 分钟,以便其他设备可以成功连接到该游戏。连接发生后 endTurnWithMatchData 调用没有任何问题,它可以在 1-2 秒内发送和接收数据。但是,如果用户开始一个新游戏并且必须等待 1 分钟以便另一个用户可以连接到他的游戏,这将不是一个好的用户体验。有没有人在自动匹配过程中遇到过明显的滞后?我还没有实施邀请,所以我无法检查它。我用 NSKeyedArchiver 存档的比赛数据看起来很大,3396 字节,即使对于几乎没有数据的新游戏也是如此。这是我的代码的相关部分:

GameOptionsViewController

- (void)turnBasedMatchmakerViewControllerWasCancelled:(GKTurnBasedMatchmakerViewController *)viewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFailWithError:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)turnBasedMatchmakerViewController:(GKTurnBasedMatchmakerViewController *)viewController didFindMatch:(GKTurnBasedMatch *)match
{
    [self dismissViewControllerAnimated:NO completion:nil];
    self.gcMatch = match;
    [self performSegueWithIdentifier:@"GameMultiplayer" sender:self];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"GameMultiplayer"])
    {
        GameViewController *GameVC = (GameViewController *)segue.destinationViewController;

        [GameVC setGameMode:GAMEMODE_MULTIPLAYER_SAMEDEVICE];

        //Multiplayer game it is
        if(self.gcMatch != nil)
        {
            [GameVC setGameMode:GAMEMODE_MULTIPLAYER_GAMECENTER];
            GameVC.gcMatchDelegate = self;
            GameVC.gcMatch = self.gcMatch;
            NSLog(@"Game OVC Segue: Match ID | %@", self.gcMatch.matchID);

        }
    }
    else
    {
       ...
    }
}

GameViewController

//This method is called according to user actions
//It's the only method I use to send data to other participant
-(void) sendCurrentGameDataWithNewTurn:(BOOL) newTurn
{
    NSLog(@"Sending game data current participant : %@", gcMatch.currentParticipant.playerID);

    //Update match data if it is corrupted anyhow
    if (gcMatch.currentParticipant == nil)
    {
    [GKTurnBasedMatch loadMatchWithID:gcMatch.matchID withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
     {
         if (error != nil)
         {
             NSLog(@"Error :%@", error);
             return ;
         }
         [self sendCurrentGameDataWithNewTurn:newTurn];
     }];
}
else
{
    NSData *matchData = [NSKeyedArchiver archivedDataWithRootObject:game];

    //Game advances to new player, buttons are disabled
    if(newTurn)
    {
        NSLog(@"SENDING NEW TURN");

        NSUInteger currentIndex = [gcMatch.participants
                                   indexOfObject:gcMatch.currentParticipant];

        GKTurnBasedParticipant *nextParticipant;
        nextParticipant = [gcMatch.participants objectAtIndex:
                           ((currentIndex + 1) % [gcMatch.participants count])];

        [gcMatch endTurnWithNextParticipants:[NSArray arrayWithObject:nextParticipant] turnTimeout:GC_TURN_TIMEOUT matchData:matchData completionHandler:^(NSError *error) {
            NSLog(@"Sent");
            if (error) {
                NSLog(@"SNT - %@", error);
            }
        }];
    }
    else
    {
        NSLog(@"ONLY UPDATING DATA");
        [gcMatch saveCurrentTurnWithMatchData:matchData completionHandler:^(NSError *error) {
            NSLog(@"Sent");
            if (error) {
                NSLog(@"OUD - %@", error);
            }
        }];
    }
}

}

-(void) updateGameDataWithGCMatch
{
    //Update whole game data
    self.game = [NSKeyedUnarchiver unarchiveObjectWithData:self.gcMatch.matchData];

    //Update game ui
    ...
}

-(void) handleTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
    //Check if I got data for the currently active match that options vc forwarded me here, if not do some debug print and return
    if(![self.gcMatch.matchID isEqual:match.matchID])
    {
        //For debugging reasons I skip if i get info for any previous match (other player quit etc)
        NSLog(@"GCMatch matchID: %@ match matchID:  %@",self.gcMatch.matchID,match.matchID);
        return;
    }

    NSLog(@"Turn event handle");

    self.gcMatch = match;

    if([match.currentParticipant.playerID isEqualToString: [GKLocalPlayer localPlayer].playerID ])
    {
        //Disable field buttons
        [self setFieldButtonsEnabled:TRUE];
        [self turnChangeAnimationFromLeftToRight:FALSE];
    }

    [self updateGameDataWithGCMatch];
}
4

1 回答 1

0

至于你的问题:

我自己在 Game Center 上进行了很多匹配,并且经常遇到延迟,这已被证明不是由我的网站引起的,而是由苹果游戏中心服务器引起的。

至于额外的指导:

据我所知,您目前在设备上进行配对的方法是:

看看是否有比赛我可以连接 --> 如果是,请求游戏数据并连接到比赛 ELSE 开始你自己的比赛并广播比赛数据

根据我的经验,最好从 matchrequestbroadcasts 开始,等到找到第二个玩家,定义服务器设备(例如,通过较低的游戏中心名称校验和),然后在该设备上开始游戏。

于 2013-04-29T09:59:13.473 回答