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

feature(ios/android): update plugin code to player_id and player_name #15

Merged
merged 3 commits into from
Oct 3, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,44 @@ public CapacitorGameConnect(AppCompatActivity activity) {
* * Method to sign-in a user to Google Play Services
*
* @param call as PluginCall
* @param resultCallback as SignInCallback
*/
public void signIn(PluginCall call) {
public void signIn(PluginCall call, final SignInCallback resultCallback) {
Log.i(TAG, "SignIn method called");
GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(this.activity);

gamesSignInClient
.isAuthenticated()
.addOnCompleteListener(
isAuthenticatedTask -> {
boolean isAuthenticated = (isAuthenticatedTask.isSuccessful() && isAuthenticatedTask.getResult().isAuthenticated());
.isAuthenticated()
.addOnCompleteListener(
isAuthenticatedTask -> {
boolean isAuthenticated = (isAuthenticatedTask.isSuccessful() && isAuthenticatedTask.getResult().isAuthenticated());

if (isAuthenticated) {
Log.i(TAG, "User is already authenticated");
call.resolve();
} else {
gamesSignInClient
.signIn()
.addOnCompleteListener(
data -> {
Log.i(TAG, "Sign-in completed successful");
}
);
}
}
);
if (isAuthenticated) {
Log.i(TAG, "User is already authenticated");
resultCallback.success();
} else {
gamesSignInClient
.signIn()
.addOnCompleteListener(
data -> {
Log.i(TAG, "Sign-in completed successful");
resultCallback.success();
}
).addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}
}
).addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}

/**
* * Method to fetch the logged in Player
*
* @param resultCallback as PlayerResultCallback
*/
public void fetchUserInformation(final PlayerResultCallback resultCallback) {
PlayGames.getPlayersClient(this.activity).getCurrentPlayer().addOnSuccessListener(player -> {
resultCallback.success(player);
}).addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}

/**
Expand All @@ -59,16 +72,16 @@ public void showLeaderboard(PluginCall call, ActivityResultLauncher<Intent> star
Log.i(TAG, "showLeaderboard has been called");
var leaderboardID = call.getString("leaderboardID");
PlayGames
.getLeaderboardsClient(this.activity)
.getLeaderboardIntent(leaderboardID)
.addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityIntent.launch(intent);
}
}
);
.getLeaderboardsClient(this.activity)
.getLeaderboardIntent(leaderboardID)
.addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityIntent.launch(intent);
}
}
);
}

/**
Expand All @@ -91,16 +104,16 @@ public void submitScore(PluginCall call) {
public void showAchievements(ActivityResultLauncher<Intent> startActivityIntent) {
Log.i(TAG, "showAchievements has been called");
PlayGames
.getAchievementsClient(this.activity)
.getAchievementsIntent()
.addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityIntent.launch(intent);
}
}
);
.getAchievementsClient(this.activity)
.getAchievementsIntent()
.addOnSuccessListener(
new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
startActivityIntent.launch(intent);
}
}
);
}

/**
Expand All @@ -123,4 +136,4 @@ public void incrementAchievementProgress(PluginCall call) {
var pointsToIncrement = call.getInt("pointsToIncrement");
PlayGames.getAchievementsClient(this.activity).increment(achievementID, pointsToIncrement);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.openforge.capacitorgameconnect;

import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.google.android.gms.games.PlayGamesSdk;
import com.google.android.gms.games.Player;

@CapacitorPlugin(name = "CapacitorGameConnect")
public class CapacitorGameConnectPlugin extends Plugin {
Expand All @@ -36,8 +40,33 @@ public void onActivityResult(ActivityResult result) {

@PluginMethod
public void signIn(PluginCall call) {
implementation.signIn(call);
call.resolve();
implementation.signIn(call, new SignInCallback() {
@Override
public void success() {
implementation.fetchUserInformation(new PlayerResultCallback() {
@Override
public void success(Player player) {
String playerId = player.getPlayerId();
String playerName = player.getDisplayName();

JSObject ret = new JSObject();
ret.put("player_id", playerId);
ret.put("player_name", playerName);
call.resolve(ret);
}

@Override
public void error(String message) {
call.reject(message);
}
});
}

@Override
public void error(String message) {
call.reject(message);
}
});
}

@PluginMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.openforge.capacitorgameconnect;

import com.google.android.gms.games.Player;

public interface PlayerResultCallback {
void success(Player player);
void error(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.openforge.capacitorgameconnect;

public interface SignInCallback {
void success();
void error(String message);
}
3 changes: 1 addition & 2 deletions ios/Plugin/CapacitorGameConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import Capacitor
public func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true);
}

@objc func signIn(_ call: CAPPluginCall, _ viewController: UIViewController) {
let localPlayer = GKLocalPlayer.local

localPlayer.authenticateHandler = { gcAuthVC, error in
if localPlayer.isAuthenticated {
print("User is authenticated to Game Center!")
let result = [
"player_name": localPlayer.displayName,
"player_id": localPlayer.gamePlayerID
Expand Down
3 changes: 2 additions & 1 deletion ios/Plugin/CapacitorGameConnectPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Foundation
import Capacitor
import GameKit

/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitorjs.com/docs/plugins/ios
*/

@objc(CapacitorGameConnectPlugin)
public class CapacitorGameConnectPlugin: CAPPlugin {
private let implementation = CapacitorGameConnect()

@objc func signIn(_ call: CAPPluginCall) {
implementation.signIn(call, (self.bridge?.viewController)!)
call.resolve()
}

@objc func showLeaderboard(_ call: CAPPluginCall) {
Expand Down