Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset achievements/leaderboard from my Android application

Can I reset my achievements/leaderboard result from my Android application that is used with Google Play Game Services?

like image 957
PEK Avatar asked Jul 15 '13 16:07

PEK


People also ask

How do I delete Google Play achievements?

Deleting an achievement Once your achievement has been published, it cannot be deleted. You can only delete an achievement in a pre-published state by clicking the button labeled Delete at the bottom of the form for that achievement.

How can I see my Android achievements?

The best way to keep track of them is to use the Google Play Games app for Android. This app is the global window to gaming on mobile. This app will let you see all of the games you have played and view the achievements for them.

How do I unlock achievements in Google Play?

Define the achievements that you want your game to unlock or display, by following the instructions in the Google Play Console guide. Download and review the achievements code samples in the Android samples page. Familiarize yourself with the recommendations described in Quality Checklist.


1 Answers

Yes, this is possible from test accounts. It’s a bit tricky so here some example code.

Add this in your manifest:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />

And here is the actual code to be executed in the application. Added in the activity that is based on BaseGameActivity.

public void resetAchievements()
{
    if( isSignedIn() )
    {
        String accountName = getGamesClient().getCurrentAccountName();      
        String scopes = getScopes();

        new ResetterTask(this, accountName, scopes).execute((Void) null);
    }
}

private class ResetterTask extends AsyncTask<Void, Void, Void>
{
    public String mAccountName;
    public String mScope;
    public Context mContext;

    public ResetterTask(Context con, String name, String sc)
    {
        mContext = con;
        mAccountName = name;
        mScope = sc;
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            String accesstoken = GoogleAuthUtil.getToken(mContext, mAccountName, mScope);

            HttpClient client = new DefaultHttpClient();                
            //Reset leader board:
            /*String leaderboardid = "theleaderboardid";
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/leaderboards/"+
                        leaderboardid+
                        "/scores/reset?access_token="+accesstoken
                    );*/

            //Reset a single achievement like this:
            /*
            String acheivementid = "acheivementid";
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/achievements/"+
                        acheivementid+
                        "/reset?access_token="+accesstoken
                    );*/

            //This resets all achievements:
            HttpPost post = new HttpPost
                    (
                        "https://www.googleapis.com"+
                        "/games/v1management"+
                        "/achievements"+
                        "/reset?access_token="+accesstoken
                    );


            client.execute(post);
            Log.w(LogTag, "Reset achievements done.");
        }
        catch(Exception e)
        {
            Log.e(LogTag, "Failed to reset: " + e.getMessage(), e);
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        //Launch activity to refresh data on client.
        //NOTE: Incremental achievements will look like they are not reset.
        //However, next time you and some steps it will start from 0 and
        //gui will look ok.
        startActivityForResult(getGamesClient().getAchievementsIntent(), 0);
    }
}
like image 173
PEK Avatar answered Oct 15 '22 00:10

PEK