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" applicationId "com.pixelmintdrop"
minSdk 30 minSdk 30
targetSdk 35 targetSdk 35
versionCode 3 versionCode 5
versionName "0.2" versionName "0.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View file

@ -76,10 +76,8 @@ class HighScoresActivity : AppCompatActivity() {
finish() finish()
} }
// Set up leaderboard button // Hide the leaderboard button
binding.leaderboardButton?.setOnClickListener { binding.leaderboardButton?.visibility = View.GONE
showGlobalLeaderboard()
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e("HighScoresActivity", "Error in onCreate", e) Log.e("HighScoresActivity", "Error in onCreate", e)
// Show an error message if necessary, or finish gracefully // Show an error message if necessary, or finish gracefully
@ -118,7 +116,6 @@ class HighScoresActivity : AppCompatActivity() {
// Apply theme to buttons // Apply theme to buttons
binding.backButton.setTextColor(textColor) binding.backButton.setTextColor(textColor)
binding.leaderboardButton?.setTextColor(textColor)
// Update adapter theme // Update adapter theme
highScoreAdapter.applyTheme(themeId) 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] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})")
Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})") 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) // End the session (updates lifetime stats)
statsManager.endSession() statsManager.endSession()
@ -1077,6 +1081,10 @@ class MainActivity : AppCompatActivity(),
Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})") Log.d(TAG, "[GameOverDebug] Triples: $lastSessionTriples (from manager: ${statsManager.getSessionTriples()})")
Log.d(TAG, "[GameOverDebug] Quads: $lastSessionQuads (from manager: ${statsManager.getSessionQuads()})") 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) // End the session (updates lifetime stats)
statsManager.endSession() statsManager.endSession()
@ -2243,20 +2251,24 @@ class MainActivity : AppCompatActivity(),
* Handles sign-in to Google Play Games Services * Handles sign-in to Google Play Games Services
*/ */
private fun signInToPlayGames() { private fun signInToPlayGames() {
Log.d(TAG, "Attempting to sign into Google Play Games")
gamesSignInClient.isAuthenticated.addOnCompleteListener { task -> gamesSignInClient.isAuthenticated.addOnCompleteListener { task ->
val isAuthenticated = task.isSuccessful && task.result.isAuthenticated val isAuthenticated = task.isSuccessful && task.result.isAuthenticated
if (!isAuthenticated) { if (!isAuthenticated) {
// Silent sign-in failed, offer to sign in explicitly // Silent sign-in failed, but don't prompt right away
Log.d(TAG, "Not authenticated with Play Games, will prompt user later") 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 { } else {
Log.d(TAG, "Already authenticated with Play Games") Log.d(TAG, "Already authenticated with Play Games")
// If we were already authenticated, make sure scores are synced // If we were already authenticated, make sure scores are synced
val lastHighScore = highScoreManager.getHighScores().maxByOrNull { it.score } val lastHighScore = highScoreManager.getHighScores().maxByOrNull { it.score }
lastHighScore?.let { 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) 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) // Submit score to leaderboard (requires an activity for potential sign-in)
fun submitScore(score: Long, activity: Activity? = null) { fun submitScore(score: Long, activity: Activity? = null) {
val account = GoogleSignIn.getLastSignedInAccount(context) if (activity == null) {
if (account == null) { Log.w(TAG, "Activity context is required for score submission.")
Log.w(TAG, "Not signed in, score will not be submitted to Google Play Games")
return return
} }
try { Log.d(TAG, "Attempting to submit score: $score")
val leaderboardsClient = PlayGames.getLeaderboardsClient(activity ?: return)
leaderboardsClient.submitScore(LEADERBOARD_ID, score) // Always force a sign-in to ensure we have a valid account
Log.d(TAG, "Score $score submitted to leaderboard $LEADERBOARD_ID") val signInClient = PlayGames.getGamesSignInClient(activity)
} catch (e: Exception) { signInClient.signIn().addOnCompleteListener { signInTask ->
Log.e(TAG, "Error submitting score to leaderboard", e) 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,7 +111,43 @@ class GooglePlayGamesManager(private val context: Context) {
fun showLeaderboard(activity: Activity, listener: LeaderboardIntentListener) { fun showLeaderboard(activity: Activity, listener: LeaderboardIntentListener) {
Log.d(TAG, "showLeaderboard called in GooglePlayGamesManager") 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 { try {
val leaderboardsClient = PlayGames.getLeaderboardsClient(activity) val leaderboardsClient = PlayGames.getLeaderboardsClient(activity)
Log.d(TAG, "LeaderboardsClient obtained, attempting to get leaderboard intent for ID: $LEADERBOARD_ID") Log.d(TAG, "LeaderboardsClient obtained, attempting to get leaderboard intent for ID: $LEADERBOARD_ID")
@ -89,12 +155,6 @@ class GooglePlayGamesManager(private val context: Context) {
.getLeaderboardIntent(LEADERBOARD_ID) .getLeaderboardIntent(LEADERBOARD_ID)
.addOnSuccessListener { intent -> .addOnSuccessListener { intent ->
Log.d(TAG, "Successfully obtained leaderboard intent. Calling listener.") 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)
// }
listener.onLeaderboardIntentSuccess(intent) listener.onLeaderboardIntentSuccess(intent)
} }
.addOnFailureListener { e -> .addOnFailureListener { e ->
@ -102,7 +162,7 @@ class GooglePlayGamesManager(private val context: Context) {
listener.onLeaderboardIntentFailure(e) listener.onLeaderboardIntentFailure(e)
} }
} catch (e: Exception) { } 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) listener.onLeaderboardIntentFailure(e)
} }
} }

View file

@ -452,20 +452,6 @@
android:textStyle="bold" android:textStyle="bold"
android:fontFamily="sans-serif" android:fontFamily="sans-serif"
android:textAllCaps="false" /> 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> </LinearLayout>
</ScrollView> </ScrollView>
</LinearLayout> </LinearLayout>

View file

@ -420,19 +420,6 @@
android:fontFamily="sans-serif" android:fontFamily="sans-serif"
android:textAllCaps="false" /> 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 <Button
android:id="@+id/statsButton" android:id="@+id/statsButton"
android:layout_width="200dp" android:layout_width="200dp"