Enhance: Update Google Play Games integration and versioning

- Incremented version code to 5 and version name to 0.4 in build.gradle.
- Removed Google Play sign-in buttons from both portrait and landscape layouts.
- Updated HighScoresActivity to hide the leaderboard button.
- Improved score submission process in GooglePlayGamesManager with forced sign-in and user feedback.
- Enhanced MainActivity to submit scores to Google Play Games upon game over.
This commit is contained in:
cmclark00 2025-04-02 19:52:02 -04:00
parent 808dc79396
commit 14205b2b0a
6 changed files with 97 additions and 55 deletions

View file

@ -11,8 +11,8 @@ android {
applicationId "com.pixelmintdrop"
minSdk 30
targetSdk 35
versionCode 3
versionName "0.2"
versionCode 5
versionName "0.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View file

@ -76,10 +76,8 @@ class HighScoresActivity : AppCompatActivity() {
finish()
}
// Set up leaderboard button
binding.leaderboardButton?.setOnClickListener {
showGlobalLeaderboard()
}
// Hide the leaderboard button
binding.leaderboardButton?.visibility = View.GONE
} catch (e: Exception) {
Log.e("HighScoresActivity", "Error in onCreate", e)
// Show an error message if necessary, or finish gracefully
@ -118,7 +116,6 @@ class HighScoresActivity : AppCompatActivity() {
// Apply theme to buttons
binding.backButton.setTextColor(textColor)
binding.leaderboardButton?.setTextColor(textColor)
// Update adapter theme
highScoreAdapter.applyTheme(themeId)

View file

@ -473,6 +473,10 @@ class MainActivity : AppCompatActivity(),
Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})")
Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})")
// *** Submit score to Google Play Games ***
Log.d(TAG, "Attempting to submit end-game score to Play Games: $lastSessionScore")
highScoreManager.getGooglePlayGamesManager().submitScore(lastSessionScore.toLong(), this@MainActivity)
// End the session (updates lifetime stats)
statsManager.endSession()
@ -1077,6 +1081,10 @@ class MainActivity : AppCompatActivity(),
Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})")
Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})")
// *** Submit score to Google Play Games ***
Log.d(TAG, "Attempting to submit end-game score to Play Games: $lastSessionScore")
highScoreManager.getGooglePlayGamesManager().submitScore(lastSessionScore.toLong(), this@MainActivity)
// End the session (updates lifetime stats)
statsManager.endSession()
@ -2243,20 +2251,24 @@ class MainActivity : AppCompatActivity(),
* Handles sign-in to Google Play Games Services
*/
private fun signInToPlayGames() {
Log.d(TAG, "Attempting to sign into Google Play Games")
gamesSignInClient.isAuthenticated.addOnCompleteListener { task ->
val isAuthenticated = task.isSuccessful && task.result.isAuthenticated
if (!isAuthenticated) {
// Silent sign-in failed, offer to sign in explicitly
Log.d(TAG, "Not authenticated with Play Games, will prompt user later")
// Silent sign-in failed, but don't prompt right away
Log.d(TAG, "Not authenticated with Play Games, will prompt for sign-in when needed")
// We don't show UI for this immediately, we'll add a button to the pause menu
// Optional: Add a sign-in button to your menu or UI for manual sign-in
// We'll attempt sign-in when accessing leaderboards or submitting scores
} else {
Log.d(TAG, "Already authenticated with Play Games")
// If we were already authenticated, make sure scores are synced
val lastHighScore = highScoreManager.getHighScores().maxByOrNull { it.score }
lastHighScore?.let {
// Submit the high score if we're already authenticated
Log.d(TAG, "User is signed in, submitting last high score: ${it.score}")
highScoreManager.getGooglePlayGamesManager().submitScore(it.score.toLong(), this)
}
}

View file

@ -62,18 +62,48 @@ class GooglePlayGamesManager(private val context: Context) {
// Submit score to leaderboard (requires an activity for potential sign-in)
fun submitScore(score: Long, activity: Activity? = null) {
val account = GoogleSignIn.getLastSignedInAccount(context)
if (account == null) {
Log.w(TAG, "Not signed in, score will not be submitted to Google Play Games")
if (activity == null) {
Log.w(TAG, "Activity context is required for score submission.")
return
}
Log.d(TAG, "Attempting to submit score: $score")
try {
val leaderboardsClient = PlayGames.getLeaderboardsClient(activity ?: return)
leaderboardsClient.submitScore(LEADERBOARD_ID, score)
Log.d(TAG, "Score $score submitted to leaderboard $LEADERBOARD_ID")
} catch (e: Exception) {
Log.e(TAG, "Error submitting score to leaderboard", e)
// Always force a sign-in to ensure we have a valid account
val signInClient = PlayGames.getGamesSignInClient(activity)
signInClient.signIn().addOnCompleteListener { signInTask ->
if (signInTask.isSuccessful) {
Log.d(TAG, "Sign-in successful, retrieving account for score submission")
// After sign-in, get the account and submit score
try {
// Use PlayGames API directly to get client and submit score
val leaderboardsClient = PlayGames.getLeaderboardsClient(activity)
leaderboardsClient.submitScore(LEADERBOARD_ID, score)
Log.d(TAG, "Successfully submitted score $score to leaderboard $LEADERBOARD_ID")
// Show confirmation to user
android.widget.Toast.makeText(
activity,
"Score submitted to leaderboard",
android.widget.Toast.LENGTH_SHORT
).show()
} catch (e: Exception) {
Log.e(TAG, "Error submitting score after sign-in", e)
android.widget.Toast.makeText(
activity,
"Failed to submit score to leaderboard",
android.widget.Toast.LENGTH_SHORT
).show()
}
} else {
Log.e(TAG, "Sign-in failed, unable to submit score", signInTask.exception)
android.widget.Toast.makeText(
activity,
"Sign-in required to submit score to leaderboard",
android.widget.Toast.LENGTH_SHORT
).show()
}
}
}
@ -81,20 +111,50 @@ class GooglePlayGamesManager(private val context: Context) {
fun showLeaderboard(activity: Activity, listener: LeaderboardIntentListener) {
Log.d(TAG, "showLeaderboard called in GooglePlayGamesManager")
Log.d(TAG, "Attempting to get LeaderboardsClient (assuming user is signed in)")
// First check if already authenticated
PlayGames.getGamesSignInClient(activity).isAuthenticated.addOnCompleteListener { authTask ->
val isAuthenticated = authTask.isSuccessful && authTask.result.isAuthenticated
if (isAuthenticated) {
// Already authenticated, get leaderboard
getLeaderboardIntent(activity, listener)
} else {
// Need to authenticate first
Log.d(TAG, "User not authenticated for leaderboard, attempting sign-in")
PlayGames.getGamesSignInClient(activity).signIn().addOnCompleteListener { signInTask ->
if (signInTask.isSuccessful) {
// Successfully signed in, now get leaderboard
Log.d(TAG, "Sign-in successful, getting leaderboard")
getLeaderboardIntent(activity, listener)
} else {
// Failed to sign in
Log.e(TAG, "Failed to sign in for leaderboard access", signInTask.exception)
listener.onLeaderboardIntentFailure(
Exception("Sign-in required to view leaderboard", signInTask.exception)
)
// Inform user
android.widget.Toast.makeText(
activity,
"Sign-in required to view leaderboard",
android.widget.Toast.LENGTH_SHORT
).show()
}
}
}
}
}
// Private helper to get leaderboard intent
private fun getLeaderboardIntent(activity: Activity, listener: LeaderboardIntentListener) {
Log.d(TAG, "Attempting to get LeaderboardsClient")
try {
val leaderboardsClient = PlayGames.getLeaderboardsClient(activity)
Log.d(TAG, "LeaderboardsClient obtained, attempting to get leaderboard intent for ID: $LEADERBOARD_ID")
leaderboardsClient
.getLeaderboardIntent(LEADERBOARD_ID)
.addOnSuccessListener { intent ->
Log.d(TAG, "Successfully obtained leaderboard intent. Calling listener.")
// try { // Removed - Activity start is now handled by MainActivity
// activity.startActivity(intent)
// Log.d(TAG, "Leaderboard activity started successfully.")
// } catch (e: Exception) {
// Log.e(TAG, "Error starting leaderboard activity", e)
// }
Log.d(TAG, "Successfully obtained leaderboard intent. Calling listener.")
listener.onLeaderboardIntentSuccess(intent)
}
.addOnFailureListener { e ->
@ -102,7 +162,7 @@ class GooglePlayGamesManager(private val context: Context) {
listener.onLeaderboardIntentFailure(e)
}
} catch (e: Exception) {
Log.e(TAG, "Error obtaining LeaderboardsClient or getting leaderboard intent", e) // Updated log message
Log.e(TAG, "Error obtaining LeaderboardsClient or getting leaderboard intent", e)
listener.onLeaderboardIntentFailure(e)
}
}

View file

@ -452,20 +452,6 @@
android:textStyle="bold"
android:fontFamily="sans-serif"
android:textAllCaps="false" />
<!-- Google Play Sign In Button -->
<Button
android:id="@+id/googlePlaySignInButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@color/transparent"
android:text="Sign in to Google Play"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="sans-serif"
android:textAllCaps="false" />
</LinearLayout>
</ScrollView>
</LinearLayout>

View file

@ -420,19 +420,6 @@
android:fontFamily="sans-serif"
android:textAllCaps="false" />
<Button
android:id="@+id/googlePlaySignInButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/transparent"
android:text="Sign in to Google Play"
android:textColor="@color/white"
android:textSize="24sp"
android:textStyle="bold"
android:fontFamily="sans-serif"
android:textAllCaps="false" />
<Button
android:id="@+id/statsButton"
android:layout_width="200dp"