Skip to content

Commit

Permalink
added leaderboards
Browse files Browse the repository at this point in the history
fixed sign-out and back in error
  • Loading branch information
renaudbardet committed Oct 2, 2014
1 parent b21d429 commit 92e5507
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.freshplanet.ane.AirGooglePlayGames
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.StatusEvent;
import flash.external.ExtensionContext;
Expand Down Expand Up @@ -120,6 +121,12 @@ package com.freshplanet.ane.AirGooglePlayGames
return name;
}

public function getLeaderboard( leaderboardId:String ):void
{
if (AirGooglePlayGames.isSupported)
_context.call("getLeaderboard", leaderboardId );
}


// --------------------------------------------------------------------------------------//
// //
Expand All @@ -136,7 +143,7 @@ package com.freshplanet.ane.AirGooglePlayGames
private function onStatus( event : StatusEvent ) : void
{
trace("[AirGooglePlayGames]", event);
var e:AirGooglePlayGamesEvent;
var e:Event;
if (event.code == "ON_SIGN_IN_SUCCESS")
{
e = new AirGooglePlayGamesEvent(AirGooglePlayGamesEvent.ON_SIGN_IN_SUCCESS);
Expand All @@ -146,10 +153,20 @@ package com.freshplanet.ane.AirGooglePlayGames
} else if (event.code == "ON_SIGN_OUT_SUCCESS")
{
e = new AirGooglePlayGamesEvent(AirGooglePlayGamesEvent.ON_SIGN_OUT_SUCCESS);
} else if (event.code == "ON_LEADERBOARD_LOADED")
{
var jsonArray:Array = JSON.parse( event.level ) as Array;
if( jsonArray ) {
var leaderboard:GSLeaderboard = GSLeaderboard.fromJSONObject( jsonArray );
if( leaderboard )
e = new AirGooglePlayGamesLeaderboardEvent(AirGooglePlayGamesLeaderboardEvent.LEADERBOARD_LOADED, leaderboard);
}
} else if (event.code == "ON_LEADERBOARD_FAILED")
{
e = new Event(AirGooglePlayGamesLeaderboardEvent.LEADERBOARD_LOADING_FAILED );
}

if (e)
{
if (e) {
this.dispatchEvent(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.freshplanet.ane.AirGooglePlayGames
{
import flash.events.Event;

public class AirGooglePlayGamesLeaderboardEvent extends Event
{

public static const LEADERBOARD_LOADED:String = "AirGooglePlayGamesLeaderboardEvent.leaderboard_loaded";
public static const LEADERBOARD_LOADING_FAILED:String = "AirGooglePlayGamesLeaderboardEvent.leaderboard_loading_failed";

public var leaderboard:GSLeaderboard;

public function AirGooglePlayGamesLeaderboardEvent( type:String, leaderboard:GSLeaderboard )
{

super( type );

this.leaderboard = leaderboard;

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.freshplanet.ane.AirGooglePlayGames
{
public class GSLeaderboard
{

private var _scores:Vector.<GSScore>;
public function get scores():Vector.<GSScore> { return _scores.slice(); }

public function GSLeaderboard()
{

_scores = new <GSScore>[];

}

public static function fromJSONObject( jsonArray:Array ):GSLeaderboard {

var leaderboard:GSLeaderboard = new GSLeaderboard();

for each ( var scoreObject:Object in jsonArray ) {
leaderboard._scores.push( GSScore.fromJSONObject( scoreObject ) );
}

return leaderboard;

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.freshplanet.ane.AirGooglePlayGames
{
public class GSPlayer
{

private var _id:String;
public function get id():String { return _id; }
private var _displayName:String;
public function get displayName():String { return _displayName; }
private var _picture:String;
public function get picture():String { return _picture; }

public function GSPlayer( id:String, displayName:String, picture:String = null )
{

_id = id;
_displayName = displayName;
_picture = picture;

}

public static function fromJSONObject( jsonObject:Object ):GSPlayer {

if( jsonObject.id == null ) return null;

return new GSPlayer( jsonObject.id, jsonObject.displayName, jsonObject.picture );

}

}
}
33 changes: 33 additions & 0 deletions actionscript/src/com/freshplanet/ane/AirGooglePlayGames/GSScore.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.freshplanet.ane.AirGooglePlayGames
{
public class GSScore
{

private var _value:int;
public function get value():int { return _value; }
private var _rank:int;
public function get rank():int { return _rank; }
private var _player:GSPlayer;
public function get player():GSPlayer { return _player; }

public function GSScore( value:int, rank:int, player:GSPlayer )
{

_value = value;
_rank = rank;
_player = player;

}

public static function fromJSONObject( jsonObject:Object ):GSScore {

var player:GSPlayer = GSPlayer.fromJSONObject( jsonObject.player );

if( player == null ) return null;

return new GSScore( jsonObject.value, jsonObject.rank, player );

}

}
}
Binary file added android/libs/google-play-services.jar
Binary file not shown.
65 changes: 58 additions & 7 deletions android/src/com/freshplanet/googleplaygames/ExtensionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@

package com.freshplanet.googleplaygames;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.freshplanet.googleplaygames.functions.AirGooglePlayGamesGetActivePlayerName;
import com.freshplanet.googleplaygames.functions.AirGooglePlayGamesGetLeaderboardFunction;
import com.freshplanet.googleplaygames.functions.AirGooglePlayGamesReportAchievementFunction;
import com.freshplanet.googleplaygames.functions.AirGooglePlayGamesReportScoreFunction;
import com.freshplanet.googleplaygames.functions.AirGooglePlayGamesShowAchievementsFunction;
Expand All @@ -37,7 +33,19 @@
import com.freshplanet.googleplaygames.functions.AirGooglePlayStartAtLaunch;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesClient;
import com.google.android.gms.games.Player;
import com.google.android.gms.games.leaderboard.LeaderboardScore;
import com.google.android.gms.games.leaderboard.LeaderboardScoreBuffer;
import com.google.android.gms.games.leaderboard.LeaderboardVariant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtensionContext extends FREContext implements GameHelper.GameHelperListener
{
Expand All @@ -61,7 +69,8 @@ public Map<String, FREFunction> getFunctions()
functionMap.put("reportScore", new AirGooglePlayGamesReportScoreFunction());
functionMap.put("showStandardAchievements", new AirGooglePlayGamesShowAchievementsFunction());
functionMap.put("getActivePlayerName", new AirGooglePlayGamesGetActivePlayerName());
return functionMap;
functionMap.put("getLeaderboard", new AirGooglePlayGamesGetLeaderboardFunction());
return functionMap;
}

public void dispatchEvent(String eventName)
Expand Down Expand Up @@ -149,6 +158,48 @@ public void reportScore(String leaderboardId, int highScore)
Games.Leaderboards.submitScore(getApiClient(), leaderboardId, highScore);
}

public void getLeaderboard( String leaderboardId ) {

Games.Leaderboards.loadTopScores(
getApiClient(),
leaderboardId,
LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_SOCIAL,
25,
true
).setResultCallback(new ScoresLoadedListener());
}

public void onLeaderboardLoaded( LeaderboardScoreBuffer scores ) {
dispatchEvent( "ON_LEADERBOARD_LOADED", scoresToJsonString(scores) );
}
private String scoresToJsonString( LeaderboardScoreBuffer scores ) {

int scoresNb = scores.getCount();
JSONArray jsonScores = new JSONArray();
for ( int i = 0; i < scoresNb; ++i ) {
LeaderboardScore score = scores.get(i);
JSONObject jsonScore = new JSONObject();
try {
jsonScore.put("value", score.getRawScore());
jsonScore.put("rank", score.getRank());

Player player = score.getScoreHolder();
JSONObject jsonPlayer = new JSONObject();
jsonPlayer.put("id", player.getPlayerId());
jsonPlayer.put("displayName", player.getDisplayName());
jsonPlayer.put("picture", player.getIconImageUri());

jsonScore.put("player", jsonPlayer);

jsonScores.put( jsonScore );

} catch( JSONException e ) {}
}
return jsonScores.toString();

}

@Override
public void onSignInFailed() {
logEvent("onSignInFailed");
Expand Down
23 changes: 9 additions & 14 deletions android/src/com/freshplanet/googleplaygames/GameHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.freshplanet.googleplaygames;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
Expand All @@ -33,7 +31,6 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.Games.GamesOptions;
import com.google.android.gms.games.GamesActivityResultCodes;
Expand Down Expand Up @@ -373,12 +370,7 @@ public void onStart(Activity act, boolean connectOnStart) {
} else {
debugLog("Not attempting to connect becase mConnectOnStart=false");
debugLog("Instead, reporting a sign-in failure.");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
notifyListener(false);
}
}, 1000);
// mHandler.postDelayed( () -> { notifyListener(false);}, 1000 );
}
}

Expand Down Expand Up @@ -516,7 +508,8 @@ public void signOut() {
debugLog("Disconnecting client.");
mConnectOnStart = false;
mConnecting = false;
mGoogleApiClient.disconnect();
mConnectionResult = null;
mGoogleApiClient.disconnect();
}

/**
Expand Down Expand Up @@ -584,7 +577,7 @@ public void onActivityResult(int requestCode, int responseCode,
void notifyListener(boolean success) {
debugLog("Notifying LISTENER of sign-in "
+ (success ? "SUCCESS"
: mSignInFailureReason != null ? "FAILURE (error)"
: mSignInFailureReason != null ? "FAILURE ("+ mSignInFailureReason.toString() +")"
: "FAILURE (no error)"));
if (mListener != null) {
if (success) {
Expand Down Expand Up @@ -651,7 +644,8 @@ void connect() {
return;
}
debugLog("Starting connection.");
mConnecting = true;
mConnectionResult = null;
mConnecting = true;
mInvitation = null;
mTurnBasedMatch = null;
mGoogleApiClient.connect();
Expand Down Expand Up @@ -702,7 +696,8 @@ void succeedSignIn() {
mConnectOnStart = true;
mUserInitiatedSignIn = false;
mConnecting = false;
notifyListener(true);
mConnectionResult = null;
notifyListener(true);
}

private final String GAMEHELPER_SHARED_PREFS = "GAMEHELPER_SHARED_PREFS";
Expand Down Expand Up @@ -831,7 +826,7 @@ void resolveConnectionResult() {

public void disconnect() {
if (mGoogleApiClient.isConnected()) {
debugLog("Disconnecting client.");
debugLog("Disconnecting client.");
mGoogleApiClient.disconnect();
} else {
Log.w(TAG,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.freshplanet.googleplaygames;

import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.PageDirection;
import com.google.android.gms.games.leaderboard.LeaderboardScoreBuffer;
import com.google.android.gms.games.leaderboard.Leaderboards;

/**
* Created by renaud on 09/09/2014.
*/
class ScoresLoadedListener implements ResultCallback<Leaderboards.LoadScoresResult> {

private int currentBufferSize = 0;

public void ScoresLoadedListener() {}

public void onResult( Leaderboards.LoadScoresResult scoresResult ) {


LeaderboardScoreBuffer scores = scoresResult.getScores();

if( scores.getCount() == currentBufferSize ) {
Extension.context.onLeaderboardLoaded(scores);
}
else {
currentBufferSize = scores.getCount();
Games.Leaderboards.loadMoreScores( Extension.context.getApiClient(), scores, 25, PageDirection.NEXT ).setResultCallback( this );
}

}

}
Loading

0 comments on commit 92e5507

Please sign in to comment.