Skip to content

Commit

Permalink
Add playerCentered option to loadLeaderboards.
Browse files Browse the repository at this point in the history
  • Loading branch information
theLee3 committed Aug 8, 2024
1 parent 0e8d4bb commit fd6f920
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ class GamesServicesPlugin : FlutterPlugin,
}
Method.LoadLeaderboardScores -> {
val leaderboardID = call.argument<String>("leaderboardID") ?: ""
val playerCentered = call.argument<Boolean>("playerCentered") ?: false
val span = call.argument<Int>("span") ?: 0
val leaderboardCollection = call.argument<Int>("leaderboardCollection") ?: 0
val maxResults = call.argument<Int>("maxResults") ?: 0
leaderboards?.loadLeaderboardScores(activity, leaderboardID, span, leaderboardCollection, maxResults, result)
leaderboards?.loadLeaderboardScores(activity, leaderboardID, playerCentered, span, leaderboardCollection, maxResults, result)
}
Method.SubmitScore -> {
val leaderboardID = call.argument<String>("leaderboardID") ?: ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) :

// used to retry [loadLeaderboardScore] after being granted friends list access
private var leaderboardID: String? = null;
private var playerCentered: Boolean? = null;
private var span: Int? = null;
private var leaderboardCollection: Int? = null;
private var maxResults: Int? = null;
Expand All @@ -48,6 +49,7 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) :
loadLeaderboardScores(
activityPluginBinding.activity,
leaderboardID!!,
playerCentered!!,
span!!,
leaderboardCollection!!,
maxResults!!,
Expand All @@ -61,6 +63,7 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) :
)
}
leaderboardID = null
playerCentered = null
span = null
leaderboardCollection = null
maxResults = null
Expand Down Expand Up @@ -97,13 +100,15 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) :
fun loadLeaderboardScores(
activity: Activity?,
leaderboardID: String,
playerCentered: Boolean,
span: Int,
leaderboardCollection: Int,
maxResults: Int,
result: MethodChannel.Result
) {
activity ?: return
leaderboardsClient.loadTopScores(leaderboardID, span, leaderboardCollection, maxResults)
(if (playerCentered) leaderboardsClient.loadPlayerCenteredScores(leaderboardID, span, leaderboardCollection, maxResults)
else leaderboardsClient.loadTopScores(leaderboardID, span, leaderboardCollection, maxResults))
.addOnSuccessListener { annotatedData ->
val data = annotatedData.get()

Expand Down Expand Up @@ -153,6 +158,7 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) :
// handle friends list CONSENT_REQUIRED error to trigger permission request dialog
if (it is FriendsResolutionRequiredException) {
this.leaderboardID = leaderboardID
this.playerCentered = playerCentered
this.span = span
this.leaderboardCollection = leaderboardCollection
this.maxResults = maxResults
Expand Down
17 changes: 15 additions & 2 deletions games_services/darwin/Classes/Leaderboards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Leaderboards: BaseGamesServices {
}
}

func loadLeaderboardScores(leaderboardID: String, span: Int, leaderboardCollection: Int, maxResults: Int, result: @escaping FlutterResult) {
func loadLeaderboardScores(leaderboardID: String, playerCentered: Bool, span: Int, leaderboardCollection: Int, maxResults: Int, result: @escaping FlutterResult) {
if #available(iOS 14.0, *) {
Task {
do {
Expand All @@ -106,9 +106,22 @@ class Leaderboards: BaseGamesServices {
result(PluginError.failedToLoadLeaderboardScores.flutterError())
return
}
var startLocation = 1
if (playerCentered) {
let response = try await leaderboard.loadEntries(for: [currentPlayer],
timeScope: GKLeaderboard.TimeScope(rawValue: span) ?? .allTime)

let (localPlayerEntry, _) = response
if let localPlayerEntry {
startLocation = localPlayerEntry.rank - maxResults / 2
if (startLocation < 1) {
startLocation = 1
}
}
}
let (_, scores, _) = try await leaderboard.loadEntries(for: GKLeaderboard.PlayerScope(rawValue: leaderboardCollection) ?? .global,
timeScope: GKLeaderboard.TimeScope(rawValue: span) ?? .allTime,
range: NSRange(location: 1, length: maxResults))
range: NSRange(location: startLocation, length: maxResults))
var items = [LeaderboardScoreData]()
for item in scores {
#if os(macOS)
Expand Down
2 changes: 2 additions & 0 deletions games_services/darwin/Classes/SwiftGamesServicesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ public class SwiftGamesServicesPlugin: NSObject, FlutterPlugin {
result: result)
case .loadLeaderboardScores:
let leaderboardID = (arguments?["leaderboardID"] as? String) ?? ""
let playerCentered = (arguments?["playerCentered"] as? Bool) ?? false
let span = (arguments?["span"] as? Int) ?? 0
let leaderboardCollection = (arguments?["leaderboardCollection"] as? Int) ?? 0
let maxResults = (arguments?["maxResults"] as? Int) ?? 10
leaderboards.loadLeaderboardScores(leaderboardID: leaderboardID,
playerCentered: playerCentered,
span: span,
leaderboardCollection: leaderboardCollection,
maxResults: maxResults,
Expand Down
13 changes: 8 additions & 5 deletions games_services/lib/src/leaderboards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ abstract class Leaderboards {
static Future<List<LeaderboardScoreData>?> loadLeaderboardScores(
{iOSLeaderboardID = "",
androidLeaderboardID = "",
bool playerCentered = false,
required PlayerScope scope,
required TimeScope timeScope,
required int maxResults}) async {
final response = await GamesServicesPlatform.instance.loadLeaderboardScores(
androidLeaderboardID: androidLeaderboardID,
iOSLeaderboardID: iOSLeaderboardID,
playerCentered: playerCentered,
scope: scope,
timeScope: timeScope,
maxResults: maxResults);
Expand All @@ -42,11 +44,12 @@ abstract class Leaderboards {
androidLeaderboardID = "",
required PlayerScope scope,
required TimeScope timeScope}) async {
final String? response = await GamesServicesPlatform.instance.getPlayerScoreObject(
androidLeaderboardID: androidLeaderboardID,
iOSLeaderboardID: iOSLeaderboardID,
scope: scope,
timeScope: timeScope);
final String? response = await GamesServicesPlatform.instance
.getPlayerScoreObject(
androidLeaderboardID: androidLeaderboardID,
iOSLeaderboardID: iOSLeaderboardID,
scope: scope,
timeScope: timeScope);

return LeaderboardScoreData.fromJson(json.decode(response ?? ""));
}
Expand Down
25 changes: 8 additions & 17 deletions games_services/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ packages:
games_services_platform_interface:
dependency: "direct main"
description:
name: games_services_platform_interface
sha256: be5a69656d0b76dd03a7d3d3db5c3eec5d85c51d44c862d83761576fa6cb4f13
url: "https://pub.dev"
source: hosted
path: "../games_services_platform_interface"
relative: true
source: path
version: "4.0.1"
lints:
dependency: transitive
Expand All @@ -50,18 +49,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.15.0"
plugin_platform_interface:
dependency: transitive
description:
Expand All @@ -83,14 +82,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=3.2.0-194.0.dev <4.0.0"
dart: ">=3.3.0-0 <4.0.0"
flutter: ">=1.22.0"
4 changes: 2 additions & 2 deletions games_services/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ dependencies:
flutter:
sdk: flutter

games_services_platform_interface: ^4.0.1
#path: ../games_services_platform_interface/
games_services_platform_interface: # ^4.0.1
path: ../games_services_platform_interface/
#uncomment in time of development.
# git:
# url: https://github.com/Abedalkareem/games_services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ abstract class GamesServicesPlatform extends PlatformInterface {
Future<String?> loadLeaderboardScores(
{iOSLeaderboardID = "",
androidLeaderboardID = "",
bool playerCentered = false,
required PlayerScope scope,
required TimeScope timeScope,
required int maxResults}) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class MethodChannelGamesServices extends GamesServicesPlatform {

@override
Future<String?> submitScore({required Score score}) async {
return await _channel.invokeMethod(
"submitScore", {"leaderboardID": score.leaderboardID, "value": score.value, "token": score.token});
return await _channel.invokeMethod("submitScore", {
"leaderboardID": score.leaderboardID,
"value": score.value,
"token": score.token
});
}

@override
Expand Down Expand Up @@ -64,12 +67,14 @@ class MethodChannelGamesServices extends GamesServicesPlatform {
Future<String?> loadLeaderboardScores(
{iOSLeaderboardID = "",
androidLeaderboardID = "",
bool playerCentered = false,
required PlayerScope scope,
required TimeScope timeScope,
required int maxResults}) async {
return await _channel.invokeMethod("loadLeaderboardScores", {
"leaderboardID":
Device.isPlatformAndroid ? androidLeaderboardID : iOSLeaderboardID,
"playerCentered": playerCentered,
"leaderboardCollection": scope.value,
"span": timeScope.value,
"maxResults": maxResults
Expand All @@ -92,7 +97,8 @@ class MethodChannelGamesServices extends GamesServicesPlatform {
required PlayerScope scope,
required TimeScope timeScope}) async {
return await _channel.invokeMethod("getPlayerScoreObject", {
"leaderboardID": Device.isPlatformAndroid ? androidLeaderboardID : iOSLeaderboardID,
"leaderboardID":
Device.isPlatformAndroid ? androidLeaderboardID : iOSLeaderboardID,
"leaderboardCollection": scope.value,
"span": timeScope.value
});
Expand Down

0 comments on commit fd6f920

Please sign in to comment.