2

I'm writing a turn based iOS game using gamecenter and am having issues finding everyone currently seated in a partially filled game. How should I walk through my match's participants array and pull out the seated players? There will be between [1,8] players in the game at any one time and I am trying to populate a lobby with those that are filled.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
Logan Shire
  • 5,013
  • 4
  • 29
  • 37
  • I have to say, I have no idea why you are being voted down/voted to close. The question seems reasonable to me, sadly I don't have quite enough experience with turn-based Gamecenter to answer. You may want to add some sample code or something. – Kendall Helmstetter Gelner May 10 '13 at 04:36
  • Thanks. I have no idea why they would downvote this. It is really frustrating. I can't seem to reliably be able to determine who's really in my games. – Logan Shire May 10 '13 at 04:39

2 Answers2

2

Here are two convenience methods I wrote for a 'GKTurnBasedMatch' category:

@implementation GKTurnBasedMatch (Convenience)

- (NSArray *) remainingPlayingParticipants;
{
    NSMutableArray *participants = [NSMutableArray array];

    // start searching at the current player
    NSUInteger currentIndex = [self.participants indexOfObject:self.currentParticipant];

    for (int i = 0; i < [self.participants count]; i++)
    {
        GKTurnBasedParticipant *part = [self.participants objectAtIndex:(currentIndex + 1 + i)
                                                                        % self.participants.count];
        if (part.matchOutcome == GKTurnBasedMatchOutcomeNone)
        {
            [participants addObject:part];
        }
    }

    return [NSArray arrayWithArray:participants];
}


- (NSArray *) remainingPlayingAndMatchedOpponents;
{
    NSMutableArray *participants = [NSMutableArray array];
    GKTurnBasedParticipant *localParticipant = [self localParticipant];

    // start searching at the current player
    NSUInteger currentIndex = [self.participants indexOfObject:self.currentParticipant];

    for (int i = 0; i < [self.participants count]; i++)
    {
        GKTurnBasedParticipant *part = [self.participants objectAtIndex:(currentIndex + 1 + i)
                                        % self.participants.count];
        if (part.matchOutcome == GKTurnBasedMatchOutcomeNone
            && part.status == GKTurnBasedParticipantStatusActive
            && part != localParticipant)
        {
            [participants addObject:part];
        }
    }

    return [NSArray arrayWithArray:participants];
}
@end
NSSplendid
  • 1,957
  • 1
  • 13
  • 14
1

Edit:

Follow the tutorial in these two links and you should be all set..

Part 1 - Part 2

In short, every GKTurnBasedMatch object has an NSArray property called participants filled with GKTurnBasedMatchParticipant objects. You can loop through this array and see every participant in the game. It's smart to look at status property of these instances (of kind GKTurnBasedParticipantStatus) to see if a participant is actively seated or declined etc.. Hope this helps.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
  • OOOPS - I just reread your question and figured that I answered a completely different question in my head - will edit in a few hours – Kaan Dedeoglu May 10 '13 at 05:10