Le Mot Juiced
Le Mot Juiced

Reputation: 3849

Programmatically find list of existing turn-based matches in Game Center?

Game Center documentation says

If you display the standard matchmaking user interface, then the player sees existing matches as well.

It does not explain if there is any way to display existing matches in a programatically-generated user interface.

It this possible?

Upvotes: 2

Views: 236

Answers (2)

Le Mot Juiced
Le Mot Juiced

Reputation: 3849

I never figured out how to do this (the strategy offered by Thunk seems good though), but in case anyone is trying to achieve the same goals, this is what I ended up doing.

My ultimate purpose was that, if two players were searching for a match at around the same time, I wanted them to be sure to connect to each other instead of having them each given different empty matches.

I'd thought the only way to do this was to get a list of open matches, but it's a little easier than that.

The key is that players searching for matches are only connected to existing matches if everyone in those matches has already taken their turn. Game Center wants you to be able to immediately take your turn as soon as you're connected to a match, so it will never serve you a match that is waiting for someone else to play.

So I made any player that is given an empty match immediately end their turn. This makes the match immediately available to be filled by any other player.

It works pretty well. Not all the time, but often enough.

Of course after the players are connected I have to straighten out whose turn it really is, which is a whole other thing, but it's not all that difficult in the end.

Upvotes: 0

Thunk
Thunk

Reputation: 4177

There's no API to find a list of all existing matches on the server, or even to find all matches that are awaiting players. There are only two related things you can do through the GameKit APIs:

1.Use the following to find a single match that meets the requirements in a match request:

[GKTurnBasedMatch findMatchForRequest:request withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error) 
{

};

2.Load all the matches the player has previously joined, and thus already personally aware of.

[GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error)
 {

 }];

As an alternative, you could consider using Apple's CloudKit (or any other back-end cloud storage) in addition to Game Center to persist a list of matches. Each time a player creates a new match, you could save a record to the public database with the match ID and other relevant details as you see fit, and maintain your own list of all existing sessions. You would also need to remove those records when matches end.

Upvotes: 1

Related Questions