question

App Lover avatar image
App Lover asked

Retrieve current value of a leaderboard

Hi I wish to retrieve current value of a leader board ID. How long does it normally take to retrieve a value? Is my below code right? public static Object[] getLeaderboard(final String leaderboardID) { LeaderboardsClient lbClient = agsGameClient.getLeaderboardsClient(); Object[] userdata = null; AGResponseHandle lbh = lbClient.getLeaderboards(userdata); lbh.setCallback(new AGResponseCallback () { @Override public void onComplete(GetLeaderboardsResponse getLeaderboardsResponse) { List leaderboards = getLeaderboardsResponse.getLeaderboards(); System.out.println("wrl leaderboards = " + leaderboards); for (Leaderboard currentLeaderboard : leaderboards) { if (currentLeaderboard.getId().equals(leaderboardID)) { leaderboard = currentLeaderboard; break; } } } }); while (lbh.getResponse() == null) { ; } System.out.println("wrl leaderboard=" + leaderboard); return userdata; } Message was edited by: App Lover Message was edited by: App Lover
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
I waited for hours but it still did not retrieve the value. Am I doing anything wrong?
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi App Lover, Thank you for your post. Have you created leader board draft in your app in distribution portal as mentioned in the below url? https://developer.amazon.com/sdk/gamecircle/documentation/leaderboards.html#Section2 If yes, are you able to submit the score in a particular leaderboard? While submitting the score you are supposed to get callback in the same way like getLeaderboards?
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
I have done everything needed for a game center app. All is working fine now. Since the above code was not returning even after several hours, I just saved the value on the device in a file and then sent the score of the leader board. Next time I am using the device-stored-value to do my calculations instead of trying to fetch the value from amazon. But this is just a temporary solution I did. Can you tell me why the above code is not returning? I guess you might already have access to my draft app "My First Game". In that you can check all my settings. As per my understanding I have done everything needed that's why everything (leaderboard, achievement) is working fine but with the above mentioned issue. Yes, am able to submit the score and it is working fine. Here is the code for that: public static void submitScore(final String leaderBoardID, final long newScore) { AGResponseHandle handle = agsGameClient.getLeaderboardsClient().submitScore(leaderBoardID, newScore); handle.setCallback(new AGResponseCallback () { @Override public void onComplete(SubmitScoreResponse result) { if (result.isError()) { System.out.println(" wrl error submitting score " + leaderBoardID + ":" + newScore + ":" + result); } else { System.out.println(" wrl success submitting score = " + leaderBoardID + ":" + newScore); } } }); } For retrieving value from Amazon I even tried below code. Still no luck long ct = System.currentTimeMillis(); LeaderboardsClient lbClient = agsGameClient.getLeaderboardsClient(); Object[] userData = null; AGResponseHandle lbh = lbClient.getLocalPlayerScore(leaderboardID, null, userData); while (lbh.getResponse() == null) { System.out.println("wrl waiting"); } GetPlayerScoreResponse a = lbh.getResponse(); System.out.println(a.getScoreValue());
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi App Lover, Have you made sure that when you are calling getLeaderboard method, agsGameClient is initialized properly? I mean you have got callback on onServiceReady of AmazonGamesCallback instance before you are making call to getLeaderboard ? If your code looks like below, agsGameClient = AmazonGamesClient.initialize( getApplication(), agsGameCallback, agsGameFeatures); getLeaderboard(); You would end up by receiving a null in GetLeaderboardsResponse instance in onComplete callback of AGResponseCallback. You should call to getLeaderboard() like below, @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); // Initialize Amazon GameCircle. agsGameClient = AmazonGamesClient.initialize(getApplication(),agsGameCallback,agsGameFeatures); } // Create a callback to handle initialization result codes. AmazonGamesCallback agsGameCallback = new AmazonGamesCallback() { @Override public void onServiceReady() { getLeaderboard(); } }; Could you please confirm, you are doing this or not?
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
I am initializing the gamesClient only once in the app(At the launch of the app). From then on I am using the static property "agsClient" to make all sorts of calls. All calls are working fine (submission of leaderboard, achievement) except retrieval calls. I will try the way you have mentioned also now and let you know.
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
Tried the way you suggested. Still the same.Here is my method. Calling this method from OnServiceReady of the callback. All it does it printing "waiting". private static void getLeaderboardScore(final String leaderboardID) { Leaderboard leaderboard = null; long ct = System.currentTimeMillis(); LeaderboardsClient lbClient = agsGameClient.getLeaderboardsClient(); Object[] userData = null; AGResponseHandle lbh = lbClient.getLocalPlayerScore(leaderboardID, null, userData); while (lbh.getResponse() == null) { System.out.println("wrl waiting"); } GetPlayerScoreResponse a = lbh.getResponse(); System.out.println("wrl score value=" + a.getScoreValue()); System.out.println("wrl time taken to get leaderboard value=" + (System.currentTimeMillis() - ct) / 1000 + " MilliSeconds"); }
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi App Lover, I can see that you are making a synchronized block (while loop) after calling getLocalPlayerScore(). You are waiting there to receive the GetPlayerScoreResponse instance from lbh.getResponse(). I think the right way to do it is set a AGResponseCallback object for getLocalPlayerScore and receive the value in onComplete() of that object. Please refer below link where we have shown how to retrieve a Time Filtered High Score. https://developer.amazon.com/sdk/gamecircle/documentation/advanced-api-features.html#Section2 So for your case the code should look like this, AGResponseHandle lbh = lbClient.getLocalPlayerScore(leaderboardID, null, userData); lbh.setCallback(new AGResponseCallback () { @Override public void onComplete(GetPlayerScoreResponse result) { if (result.isError()) { // Handle getLocalPlayerScore error } else { System.out.println("wrl score value=" + result.getScoreValue()); } } }); Does it make sense?
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
Getting an error with your suggested code( note am calling with a leaderboard ID, null, null: 06-10 22:13:10.236: I/AGC_AmazonGamesStatusHandler(22968): Game received status update of: SERVICE_CONNECTED 06-10 22:13:10.236: I/System.out(22968): wrl service ready. Connected to Game Center 06-10 22:13:10.236: D/LB_LeaderboardsServiceProxy(22968): Request Local Player Score called on leaderboardId Points Asynchronously with handle. 06-10 22:13:10.329: D/GC_BaseReplyMessengerFactory(22968): Constructing a ServiceHandleHandler for the outgoing asynchronous event 06-10 22:13:14.033: D/AndroidRuntime(22968): Shutting down VM 06-10 22:13:14.033: W/dalvikvm(22968): threadid=1: thread exiting with uncaught exception (group=0x40aff1f8) 06-10 22:13:14.033: E/AndroidRuntime(22968): FATAL EXCEPTION: main 06-10 22:13:14.033: E/AndroidRuntime(22968): java.lang.NullPointerException 06-10 22:13:14.033: E/AndroidRuntime(22968): at com.amazon.ags.client.leaderboards.LeaderboardsServiceProxy.createRequestBundleWithFilter(LeaderboardsServiceProxy.java:266) 06-10 22:13:14.033: E/AndroidRuntime(22968): at com.amazon.ags.client.leaderboards.LeaderboardsServiceProxy.access$200(LeaderboardsServiceProxy.java:38) 06-10 22:13:14.033: E/AndroidRuntime(22968): at com.amazon.ags.client.leaderboards.LeaderboardsServiceProxy$4.run(LeaderboardsServiceProxy.java:148) 06-10 22:13:14.033: E/AndroidRuntime(22968): at android.os.Handler.handleCallback(Handler.java:605) 06-10 22:13:14.033: E/AndroidRuntime(22968): at android.os.Handler.dispatchMessage(Handler.java:92) 06-10 22:13:14.033: E/AndroidRuntime(22968): at android.os.Looper.loop(Looper.java:137) 06-10 22:13:14.033: E/AndroidRuntime(22968): at android.app.ActivityThread.main(ActivityThread.java:4561) 06-10 22:13:14.033: E/AndroidRuntime(22968): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 22:13:14.033: E/AndroidRuntime(22968): at java.lang.reflect.Method.invoke(Method.java:511) 06-10 22:13:14.033: E/AndroidRuntime(22968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-10 22:13:14.033: E/AndroidRuntime(22968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-10 22:13:14.033: E/AndroidRuntime(22968): at dalvik.system.NativeStart.main(Native Method)
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi App Lover, Thank you for your patience. The 2nd param could not be null. Below is taken from API reference doc. AGResponseHandle getLocalPlayerScore(java.lang.String leaderboardId, LeaderboardFilter filter, java.lang.Object... userData) Requests the current users top RankedScore for the leaderboard. Returns a handle that receives the response Parameters: leaderboardId - Leaderboard ID to get scores from. [b]filter - Filter to apply to the request.[/b] userData - Optional user data. Delivered to the user as an array of objects. Returns: Returns the handle to poll for completion or to set a callback on. You should find the doc in SDK package. Apps-SDK/GameCircle/documentation/API-Reference/com/amazon/ags/api/leaderboards/LeaderboardsClient.html Below filters are available. FRIENDS_ALL_TIME GLOBAL_ALL_TIME GLOBAL_DAY GLOBAL_WEEK You should use GLOBAL_ALL_TIME for your case.
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

App Lover avatar image
App Lover answered
Thank you so much. That works!
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.