I am implementing a Turn Base Multiplayer game using Google Play Services. There seems to be a major feature missing from it though: tracking wins and loses! Seems like a pretty important feature for any sort of competitive game.
Am I missing something? Is this already handled by Google in some way?
If not, I am wondering if anyone has implemented their own w/l tracking system, and how they approached it. I am worried about just saving it locally, as it might get out of sync with reality. I suppose I could use Google Cloud Storage, but I also worry that keeping track of which matches have been accounted for could be a little error prone (eg. counting wins/loses on the same match multiple times). Maybe using the Google Play Leaderboard system would be good, as you could compare with friends/etc.
You might want to try using the Events provided by Google Play Game Services. Here's the steps for using it:
When a game is won or lost, call the following code, where eventRef is your friendly resource name you added in the last step, either "events_games_won" or "events_games_lost":
int eventInt = activity.getApplicationContext().getResources().getIdentifier(eventRef, "string", "com.yourpackage.android");
String eventID = activity.getString(eventInt);
Games.Events.increment(apiClient, eventID, 1);
If you're using basegameutils, your apiClient instance can be obtained with aHelper.getApiClient(), otherwise follow these instructions to set it up.
To read the data you've saved, you can use a callback like this:
PendingResult<Events.LoadEventsResult> results = Games.Events.load(apiClient, true);
results.setResultCallback(new ResultCallback<Events.LoadEventsResult>() {
@Override
public void onResult(Events.LoadEventsResult result)
{
Events.LoadEventsResult r = (Events.LoadEventsResult)result;
EventBuffer eb = r.getEvents();
for (int i=0; i < eb.getCount(); i++) {
Event event = eb.get(i);
// do something, like cache the results for later
YourGameState.eventStats.put(event.getName(), (int)event.getValue());
}
eb.close();
listener.onResult();
}
});
The benefits of this approach over maintaining and managing counts locally are:
And, of course, you can use the same approach to create Events for anything you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With