Skip to content

Commit

Permalink
Merge pull request #19 from prakhar1989/leaderboards
Browse files Browse the repository at this point in the history
Leaderboards
  • Loading branch information
prakhar1989 committed Sep 7, 2014
2 parents e511769 + 1f62460 commit 4e31cf5
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 28 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/appcompat_v7_20_0_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/libraries/play_services_5_0_89.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .idea/libraries/support_v4_20_0_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
<orderEntry type="jdk" jdkName="Android API 19 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-20.0.0" level="project" />
<orderEntry type="library" exported="" name="play-services-5.0.89" level="project" />
<orderEntry type="library" exported="" name="support-v4-20.0.0" level="project" />
<orderEntry type="module" module-name="BaseGameUtils" exported="" />
</component>
</module>

11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ apply plugin: 'com.android.application'
android {
signingConfigs {
config {
keyAlias 'prakhar'
keyAlias 'prakhar srivastav'
keyPassword 'prakhar'
storeFile file('/Users/asrinivasan/Dropbox/android-play/keystore.jks')
storeFile file('/Users/asrinivasan/Documents/AndroidstudioProjects/keystore.jks')
storePassword 'prakhar'
}
}
Expand All @@ -15,8 +15,9 @@ android {
applicationId 'com.prakharme.prakharsriv.colorphun'
minSdkVersion 15
targetSdkVersion 19
versionCode 2
versionName '1.0.1'
versionCode 5
versionName '1.0.4'
signingConfig signingConfigs.config
}
buildTypes {
release {
Expand All @@ -35,4 +36,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile project(':BaseGameUtils')
compile 'com.google.android.gms:play-services:5.0.89'
}
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@
android:label="@string/title_activity_game_over"
android:screenOrientation="portrait" >
</activity>

<!-- FOR GOOGLE PLAY SERVICES -->
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/APP_ID" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,34 @@

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.GamesStatusCodes;
import com.google.android.gms.games.leaderboard.LeaderboardVariant;
import com.google.android.gms.games.leaderboard.Leaderboards;
import com.google.example.games.basegameutils.BaseGameActivity;

public class GameOverActivity extends Activity {
public class GameOverActivity extends BaseGameActivity {

private int points, best;
private int points, best, level;
private boolean newScore;
private boolean shown = false;
private TextView gameOverText, pointsBox, highScoreText;
private SharedPreferences sharedPreferences;

final int REQUEST_LEADERBOARD = 4000;
final int REQUEST_ACHIEVEMENTS = 5000;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -44,10 +56,22 @@ protected void onCreate(Bundle savedInstanceState) {
replayBtn.setTypeface(avenir_book);
highScoreText.setTypeface(avenir_black);

// disallow auto sign-in on this screen
getGameHelper().setMaxAutoSignInAttempts(0);

// set a simple game counter in shared pref
sharedPreferences = this.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPreferences.edit();
int timesPlayed = sharedPreferences.getInt("TIMESPLAYED", 0);
editor.putInt("TIMESPLAYED", timesPlayed + 1);
editor.apply();

// get data
Bundle bundle = getIntent().getExtras();
points = bundle.getInt("points");
int level = bundle.getInt("level");
level = bundle.getInt("level");
best = bundle.getInt("best");
newScore = bundle.getBoolean("newScore");

Expand All @@ -62,28 +86,23 @@ protected void onCreate(Bundle savedInstanceState) {
} else {
highScoreText.setVisibility(View.INVISIBLE);
}

}


@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus && !shown) {
shown = true;
ValueAnimator pointsAnim = getCounterAnimator(pointsBox, points);
pointsAnim.setDuration(1200);

ObjectAnimator anim = ObjectAnimator.ofFloat(gameOverText, "Y", 0, 130);
anim.setInterpolator(new BounceInterpolator());
anim.setDuration(600);

// animate high score text
if (newScore) {
ObjectAnimator highScoreAnim = ObjectAnimator.ofFloat(highScoreText, "alpha", 0f, 1f);
highScoreAnim.setDuration(600);
highScoreAnim.start();
}

anim.start();
pointsAnim.start();
}
}
Expand All @@ -105,4 +124,118 @@ public void playGame(View view) {
startActivity(new Intent(this, MainActivity.class));
finish();
}

public void showLeaderboard(View view) {
if (isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(),
getString(R.string.LEADERBOARD_ID)), REQUEST_LEADERBOARD);
} else {
showAlert(getString(R.string.signin_help_title), getString(R.string.signin_help));
}
}

public void showAchievements(View view) {
if (isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), REQUEST_ACHIEVEMENTS);
} else {
showAlert(getString(R.string.signin_help_title), getString(R.string.signin_help));
}

}

@Override
public void onSignInFailed() {
Log.e("SIGN IN", "ERROR Signin in game over");
}

@Override
public void onSignInSucceeded() {
// save scores on the cloud
pushAccomplishments();

// save achievements
setAchievements();

// fetching results from leaderboard and matching scores
PendingResult result = Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(),
getString(R.string.LEADERBOARD_ID), LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC);

result.setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(Leaderboards.LoadPlayerScoreResult result) {
// check if valid score
if (result != null
&& GamesStatusCodes.STATUS_OK == result.getStatus().getStatusCode()
&& result.getScore() != null) {

// assign score fetched as best score
updateHighScore((int) result.getScore().getRawScore());
}
}

});

}

void pushAccomplishments() {
if (!isSignedIn()) {
return;
}
if (best > 0) {
// submit score to play services
Games.Leaderboards.submitScore(getApiClient(),
getString(R.string.LEADERBOARD_ID) , best);
}
}

private void setAchievements() {
if (!isSignedIn()) {
return;
}

// standard achievements
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_NOVICE_ID));
Games.Achievements.increment(getApiClient(), getString(R.string.ACHIEVEMENT_LONGTIMER_ID), 1);

// points based
if (points <= 20) {
Games.Achievements.increment(getApiClient(), getString(R.string.ACHIEVEMENT_COLORBLIND_ID), 1);
}
if (points >= 100) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_CENTURION_ID));
}

// level based
if (level >= 3) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_CASUAL_ID));
}
if (level >= 4) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_DETAILEYE_ID));
}
if (level >= 5) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_TAPMASTER_ID));
}
if (level >= 6) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_GODLIKE_ID));
}
if (level > 6) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_IMPOSSIBLE_ID));
}

// game count based
int timesPlayed = sharedPreferences.getInt("TIMESPLAYED", 0);
if (timesPlayed <= 3 && points >= 50) {
Games.Achievements.unlock(getApiClient(), getString(R.string.ACHIEVEMENT_LUCK_ID));
}
}

// save high score in shared preferences file
private void updateHighScore(int score) {
if (score != best && score > 0) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("HIGHSCORE", score);
editor.apply();
}
}
}
Loading

0 comments on commit 4e31cf5

Please sign in to comment.