Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SignIn result: add token and email #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

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

25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ if Engine.has_singleton("GodotPlayGamesServices"):

# Initialize plugin by calling init method and passing to it a boolean to enable/disable displaying game pop-ups

var show_popups := true
play_games_services.init(show_popups)
var show_popups := true
var request_email := true
var request_profile := true
#The client id must be created in [the google console](https://console.cloud.google.com/apis/credentials), an OAuth 2.0 Client credentials of a Web application type
var request_token := "Client ID"

play_games_services.init(show_popups, request_email, request_profile, request_token)

# For enabling saved games functionality use below initialization instead
# play_games_services.initWithSavedGames(show_popups, "SavedGamesName")
# play_games_services.initWithSavedGames(show_popups, "SavedGamesName", request_email, request_profile, request_token)

# Connect callbacks (Use only those that you need)
play_games_services.connect("_on_sign_in_success", self, "_on_sign_in_success") # account_id: String
Expand Down Expand Up @@ -92,9 +98,16 @@ After what plugin was initialized you can use supported features
play_games_services.signIn()

# Callbacks:
func _on_sign_in_success(account_id: String) -> void:
pass

func _on_sign_in_success(userProfile_json: String) -> void:
var userProfile = parse_json(userProfile_json)

# The returned JSON contains an object of userProfile info.
# Use the following keys to access the fields
userProfile["displayName"] # The user's display name
userProfile["email"] # The user's email
userProfile["token"] # User token for backend use
userProfile["id"] # The user's id

func _on_sign_in_failed(error_code: int) -> void:
pass

Expand Down
Binary file added app/.DS_Store
Binary file not shown.
22 changes: 17 additions & 5 deletions app/src/main/java/io/cgisca/godot/gpgs/ConnectionController.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package io.cgisca.godot.gpgs

import android.app.Activity
import android.util.Log
import android.util.Pair
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import io.cgisca.godot.gpgs.signin.UserProfile

class ConnectionController(
private val activity: Activity,
private var signInOptions: GoogleSignInOptions
) {

fun isConnected(): Pair<Boolean, String> {
fun isConnected(): Pair<Boolean, UserProfile> {
val userProfile = UserProfile(null, null, null, null)
val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(activity)
var accId = ""
?: return Pair(false, userProfile)

googleSignInAccount?.id?.let {
accId = it
if (!googleSignInAccount.isExpired) {
Log.i("godot","Sign in data is valid")
userProfile.let {
it.displayName = googleSignInAccount.displayName
it.email = googleSignInAccount.email
it.token = googleSignInAccount.idToken
it.id = googleSignInAccount.id
}
return Pair(GoogleSignIn.hasPermissions(googleSignInAccount, *signInOptions.scopeArray), userProfile)
}else{
Log.i("godot","Sign in data has expired")
return Pair(false, userProfile)
}
return Pair(GoogleSignIn.hasPermissions(googleSignInAccount, *signInOptions.scopeArray), accId)
}
}
31 changes: 20 additions & 11 deletions app/src/main/java/io/cgisca/godot/gpgs/PlayGameServicesGodot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.drive.Drive
import com.google.android.gms.games.SnapshotsClient
import com.google.android.gms.games.snapshot.SnapshotMetadata
import com.google.gson.Gson
import io.cgisca.godot.gpgs.accountinfo.PlayerInfoController
import io.cgisca.godot.gpgs.accountinfo.PlayerInfoListener
import io.cgisca.godot.gpgs.achievements.AchievementsController
Expand All @@ -21,6 +22,7 @@ import io.cgisca.godot.gpgs.savedgames.SavedGamesController
import io.cgisca.godot.gpgs.savedgames.SavedGamesListener
import io.cgisca.godot.gpgs.signin.SignInController
import io.cgisca.godot.gpgs.signin.SignInListener
import io.cgisca.godot.gpgs.signin.UserProfile
import io.cgisca.godot.gpgs.stats.PlayerStatsController
import io.cgisca.godot.gpgs.stats.PlayerStatsListener
import org.godotengine.godot.Godot
Expand Down Expand Up @@ -164,22 +166,29 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
}
}

fun init(enablePopups: Boolean) {
initialize(false, enablePopups, "DefaultGame")
fun init(enablePopups: Boolean, requestEmail: Boolean, requestProfile: Boolean, requestToken: String) {
initialize(false, enablePopups, "DefaultGame", requestEmail, requestProfile, requestToken)
}

fun initWithSavedGames(enablePopups: Boolean, saveGameName: String) {
initialize(true, enablePopups, saveGameName)
fun initWithSavedGames(enablePopups: Boolean, saveGameName: String, requestEmail: Boolean, requestProfile: Boolean, requestToken: String) {
initialize(true, enablePopups, saveGameName, requestEmail, requestProfile, requestToken)
}

private fun initialize(enableSaveGamesFunctionality: Boolean, enablePopups: Boolean, saveGameName: String) {
private fun initialize(enableSaveGamesFunctionality: Boolean, enablePopups: Boolean, saveGameName: String,
requestEmail: Boolean, requestProfile: Boolean, requestToken: String) {
this.saveGameName = saveGameName
val signInOptions = if (enableSaveGamesFunctionality) {
val signInOptions = run {
val signInOptionsBuilder = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
signInOptionsBuilder.requestScopes(Drive.SCOPE_APPFOLDER).requestId()
if (enableSaveGamesFunctionality)
signInOptionsBuilder.requestScopes(Drive.SCOPE_APPFOLDER).requestId()
if (requestToken.isNotEmpty()) {
signInOptionsBuilder.requestIdToken(requestToken)
}
if (requestEmail)
signInOptionsBuilder.requestEmail()
if (requestProfile)
signInOptionsBuilder.requestProfile()
signInOptionsBuilder.build()
} else {
GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN
}

connectionController = ConnectionController(godot as Activity, signInOptions)
Expand Down Expand Up @@ -404,8 +413,8 @@ class PlayGameServicesGodot(godot: Godot) : GodotPlugin(godot), AchievementsList
emitSignal(SIGNAL_SAVED_GAME_CREATE_SNAPSHOT.name, currentSaveName)
}

override fun onSignedInSuccessfully(accountId: String) {
emitSignal(SIGNAL_SIGN_IN_SUCCESSFUL.name, accountId)
override fun onSignedInSuccessfully(userProfile: UserProfile) {
emitSignal(SIGNAL_SIGN_IN_SUCCESSFUL.name, Gson().toJson(userProfile))
}

override fun onSignInFailed(statusCode: Int) {
Expand Down
31 changes: 22 additions & 9 deletions app/src/main/java/io/cgisca/godot/gpgs/signin/SignInController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.cgisca.godot.gpgs.signin

import android.app.Activity
import android.util.Log
import android.util.Pair
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
Expand All @@ -25,22 +26,29 @@ class SignInController(
}

fun signIn(googleSignInClient: GoogleSignInClient) {
val connection: Pair<Boolean, String> = connectionController.isConnected()
val userProfile = UserProfile(null, null, null, null)
val connection: Pair<Boolean, UserProfile> = connectionController.isConnected()
if (connection.first) {
Log.i("godot","Using cached signin data")
signInListener.onSignedInSuccessfully(connection.second)
enablePopUps()
} else {
Log.i("godot","Using new signin data")
googleSignInClient
.silentSignIn()
.addOnCompleteListener(activity) { task ->
if (task.isSuccessful) {
val googleSignInAccount = task.result
var accId = ""
googleSignInAccount?.id?.let {
accId = it
if (googleSignInAccount != null) {
userProfile.let {
it.displayName = googleSignInAccount.displayName
it.email = googleSignInAccount.email
it.token = googleSignInAccount.idToken
it.id = googleSignInAccount.id
}
}

signInListener.onSignedInSuccessfully(accId)
signInListener.onSignedInSuccessfully(userProfile)
enablePopUps()
} else {
val intent = googleSignInClient.signInIntent
Expand All @@ -51,14 +59,19 @@ class SignInController(
}

fun onSignInActivityResult(googleSignInResult: GoogleSignInResult?) {
val userProfile = UserProfile(null, null, null, null)
if (googleSignInResult != null && googleSignInResult.isSuccess) {
val googleSignInAccount = googleSignInResult.signInAccount
var accId = ""
googleSignInAccount?.id?.let {
accId = it
if (googleSignInAccount != null) {
userProfile.let {
it.displayName = googleSignInAccount.displayName
it.email = googleSignInAccount.email
it.token = googleSignInAccount.idToken
it.id = googleSignInAccount.id
}
}
enablePopUps()
signInListener.onSignedInSuccessfully(accId)
signInListener.onSignedInSuccessfully(userProfile)
} else {
var statusCode = Int.MIN_VALUE
googleSignInResult?.status?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.cgisca.godot.gpgs.signin

interface SignInListener {
fun onSignedInSuccessfully(accountId: String)
fun onSignedInSuccessfully(userProfile: UserProfile)
fun onSignInFailed(statusCode: Int)
fun onSignOutSuccess()
fun onSignOutFailed()
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/io/cgisca/godot/gpgs/signin/UserProfile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.cgisca.godot.gpgs.signin

data class UserProfile(var displayName: String?, var email: String?, var token: String?, var id: String?) {
}
11 changes: 7 additions & 4 deletions demo/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func _ready():
play_games_services.connect("_on_player_info_loaded", self, "_on_player_info_loaded")
play_games_services.connect("_on_player_info_loading_failed", self, "_on_player_info_loading_failed")

play_games_services.init(true)
# play_games_services.initWithSavedGames(true, "SAVE_GAME_NAME") # Use this init if you want saved games feature to be enabled
play_games_services.init(true,false,false,"")
# play_games_services.init(true,true,false,"") # Use this init if you want to get email of the player
# play_games_services.init(true,true,true,"") # Use this init if you want to get email and profile data of the player
# play_games_services.initWithSavedGames(true, "SAVE_GAME_NAME",false, false, "") # Use this init if you want saved games feature to be enabled


# Sign-in/sign-out methods
Expand Down Expand Up @@ -127,8 +129,9 @@ func load_player_info() -> void:

# CALLBACKS
# Sign-in / sign-out callbacks
func _on_sign_in_success(account_id: String) -> void:
print("Sign in success %s"%account_id)
func _on_sign_in_success(userProfile_json: String) -> void:
var userProfile = parse_json(userProfile_json)
print("Sign in success ",userProfile)

func _on_sign_in_failed(error_code: int) -> void:
print("Sign in failed %s"%error_code)
Expand Down