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

feat/fetch-leaderboards-score #18

Merged
merged 2 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ Before use the `Achievement Methods` of the plugin, you need to setup your Achie
* [`showAchievements()`](#showachievements)
* [`unlockAchievement(...)`](#unlockachievement)
* [`incrementAchievementProgress(...)`](#incrementachievementprogress)
* [`getUserTotalScore(...)`](#getusertotalscore)
* [Interfaces](#interfaces)

</docgen-index>

Expand Down Expand Up @@ -252,6 +254,33 @@ incrementAchievementProgress(options: { achievementID: string; pointsToIncrement

--------------------


### getUserTotalScore(...)

```typescript
getUserTotalScore(options: { leaderboardID: string; }) => Promise<PlayerScore>
```

* Method to get total player score from a leaderboard

| Param | Type | Description |
| ------------- | --------------------------------------- | ----------- |
| **`options`** | <code>{ leaderboardID: string; }</code> | : string } |

**Returns:** <code>Promise&lt;<a href="#playerscore">PlayerScore</a>&gt;</code>

--------------------


### Interfaces


#### PlayerScore

| Prop | Type |
| ------------------ | ------------------- |
| **`player_score`** | <code>number</code> |

</docgen-api>

# Testing Limitations
Expand Down
1 change: 1 addition & 0 deletions android/.idea/gradle.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import android.content.Intent;
import android.util.Log;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.google.android.gms.games.AnnotatedData;
import com.google.android.gms.games.GamesSignInClient;
import com.google.android.gms.games.PlayGames;
import com.google.android.gms.games.leaderboard.LeaderboardScore;
import com.google.android.gms.games.leaderboard.LeaderboardVariant;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;

public class CapacitorGameConnect {
Expand Down Expand Up @@ -45,10 +51,12 @@ public void signIn(PluginCall call, final SignInCallback resultCallback) {
Log.i(TAG, "Sign-in completed successful");
resultCallback.success();
}
).addOnFailureListener(e -> resultCallback.error(e.getMessage()));
)
.addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}
}
).addOnFailureListener(e -> resultCallback.error(e.getMessage()));
)
.addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}

/**
Expand All @@ -57,9 +65,15 @@ public void signIn(PluginCall call, final SignInCallback resultCallback) {
* @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()));
PlayGames
.getPlayersClient(this.activity)
.getCurrentPlayer()
.addOnSuccessListener(
player -> {
resultCallback.success(player);
}
)
.addOnFailureListener(e -> resultCallback.error(e.getMessage()));
}

/**
Expand Down Expand Up @@ -136,4 +150,38 @@ public void incrementAchievementProgress(PluginCall call) {
var pointsToIncrement = call.getInt("pointsToIncrement");
PlayGames.getAchievementsClient(this.activity).increment(achievementID, pointsToIncrement);
}

/**
* * Method to get the total player score from a leaderboard
*
*/
public void getUserTotalScore(PluginCall call) {
Log.i(TAG, "getUserTotalScore has been called");
var leaderboardID = call.getString("leaderboardID");
var leaderboardScore = PlayGames
.getLeaderboardsClient(this.activity)
.loadCurrentPlayerLeaderboardScore(leaderboardID, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC);
leaderboardScore
.addOnSuccessListener(
new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
if (leaderboardScore != null) {
long totalScore = leaderboardScore.getResult().get().getRawScore();
JSObject result = new JSObject();
result.put("player_score", totalScore);
call.resolve(result);
}
}
}
)
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
call.reject("Error getting player score" + e.getMessage());
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ public void incrementAchievementProgress(PluginCall call) {
implementation.incrementAchievementProgress(call);
call.resolve();
}

@PluginMethod
public void getUserTotalScore(PluginCall call) {
implementation.getUserTotalScore(call);
}
}
Binary file not shown.
30 changes: 30 additions & 0 deletions ios/Plugin/CapacitorGameConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,34 @@ import Capacitor
call.resolve(result as PluginCallResultData)
}
}

@objc func getUserTotalScore(_ call: CAPPluginCall) {
guard GKLocalPlayer.local.isAuthenticated else {
print("Player is not authenticated")
call.reject("Player is not authenticated")
return
}

let leaderboardID = String(call.getString("leaderboardID") ?? "") // * Property to get the leaderboard ID
let leaderboard = GKLeaderboard() // * LeaderBoard functions
leaderboard.identifier = leaderboardID // * LeaderBoard we are going to use for
leaderboard.playerScope = .global // * Section to use
leaderboard.timeScope = .allTime // * Time to search for

leaderboard.loadScores { (scores, error) in
if let error = error {
call.reject("Error loading leaderboard score: \(error.localizedDescription)")
} else if let scores = scores {
for score in scores {
if score.player.gamePlayerID == GKLocalPlayer.local.gamePlayerID {
let result = [
"player_score": score.value
]
call.resolve(result)
break
}
}
}
}
}
}
1 change: 1 addition & 0 deletions ios/Plugin/CapacitorGameConnectPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
CAP_PLUGIN_METHOD(submitScore, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(unlockAchievement, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(incrementAchievementProgress, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getUserTotalScore, CAPPluginReturnPromise);
)
4 changes: 4 additions & 0 deletions ios/Plugin/CapacitorGameConnectPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public class CapacitorGameConnectPlugin: CAPPlugin {
implementation.incrementAchievementProgress(call)
call.resolve()
}

@objc func getUserTotalScore(_ call: CAPPluginCall) {
implementation.getUserTotalScore(call)
}
}
4 changes: 2 additions & 2 deletions package-lock.json

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

9 changes: 9 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { PlayerScore } from './interfaces/player-score.interface';

export interface CapacitorGameConnectPlugin {
/**
* * Method to sign-in a user
Expand Down Expand Up @@ -44,4 +46,11 @@ export interface CapacitorGameConnectPlugin {
achievementID: string;
pointsToIncrement: number;
}): Promise<void>;

/**
* * Method to get total player score from a leaderboard
*
* @param options { leaderboardID: string }
*/
getUserTotalScore(options: { leaderboardID: string }): Promise<PlayerScore>;
}
3 changes: 3 additions & 0 deletions src/interfaces/player-score.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PlayerScore {
player_score: number;
}
4 changes: 4 additions & 0 deletions src/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface User {
player_id: string;
player_name: string;
}
20 changes: 15 additions & 5 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { WebPlugin } from '@capacitor/core';

import type { CapacitorGameConnectPlugin } from './definitions';

export interface User {
player_id: string;
player_name: string;
}
import type { PlayerScore } from './interfaces/player-score.interface';
import type { User } from './interfaces/user.interface';

export class CapacitorGameConnectWeb
extends WebPlugin
Expand Down Expand Up @@ -77,4 +74,17 @@ export class CapacitorGameConnectWeb
);
return Promise.resolve();
}

/**
* * Function to get the total player score from a specific leaderboard
*
* @param options { leaderboardID: string }
* @returns Promise<PlayerScore>
*/
async getUserTotalScore(options: {
leaderboardID: string;
}): Promise<PlayerScore> {
console.info('getUserTotalScore function has been called', options);
return Promise.resolve({} as PlayerScore);
}
}