Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking Wins and Loses in Google Play Game Services

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.

like image 635
Goose Avatar asked Oct 31 '22 23:10

Goose


1 Answers

You might want to try using the Events provided by Google Play Game Services. Here's the steps for using it:

  1. Go to the Android Developer Console
  2. On the sidebar click on Game Services, choose your App, then Events
  3. Then create two new Events, one called 'Games Won' and the other 'Games Lost', then click to publish your changes
  4. Click the Get Resources link to see the resource ids of your new events, and add them to your App in the res/values directory
  5. 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.

  6. 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:

  • the wins and losses are stored in Google Play's cloud, and they'll be stored across all the user's devices
  • you can use the Events to create Quests (also described on Google Play), such as "Win 10 games this week", or "Go this week undefeated"
  • Google Play will give you stats on Events for everyone who plays your game, which can be very useful for analysing player activity and performance

And, of course, you can use the same approach to create Events for anything you want.

like image 132
Jaron Avatar answered Nov 08 '22 05:11

Jaron