我正在使用Google Play服务排行榜上传用户的分数。有没有办法以编程方式检索用户的分数?
我的用例是我想运行乘数游戏,其中用户的级别根据他们的胜利/失败而上升和下降(有点像stackoverflow声誉)。我需要检索旧分数以上传新的修改分数。
我该怎么做?我没有在教程的任何地方看到这一点。
由于
答案 0 :(得分:1)
理论上,你可以做类似的事情:
gameHelper.getGamesClient().loadLeaderboardMetadata(this, false);
/**
* Called when the data is loaded
*/
@Override
public void onLeaderboardMetadataLoaded(int statusCode, LeaderboardBuffer leaderboards) {
// If the fragment is currently added to an activity
if (isAdded()) {
// There's been an error on leaderboards loading
if (statusCode != GamesClient.STATUS_OK) {
if (leaderboards != null) leaderboards.close();
// Error
return;
}
ArrayList<LeaderboardVariant> variants;
Iterator<Leaderboard> it = leaderboards.iterator();
while (it.hasNext()) {
Leaderboard leaderboard = it.next();
final String id = leaderboard.getLeaderboardId();
variants = leaderboard.getVariants();
// For each leaderboard
for (LeaderboardVariant variant: variants) {
if (variant.getCollection() == LeaderboardVariant.COLLECTION_PUBLIC &&
variant.getTimeSpan() == LeaderboardVariant.TIME_SPAN_ALL_TIME) {
int rawScore = (int) variant.getRawPlayerScore();
}
}
}
}
if (leaderboards != null) leaderboards.close();
}
...但实际情况是,您不能因为数据已过时,请参阅此处:Play games loadLeaderboardMetadata() returns outdated data
这可能是个错误,但因为没有提交游戏错误的地方......
无论如何,为什么不保留您提交的分数的本地副本?这不是更简单的方法吗?
[UPDATE] 您似乎可以使用loadCurrentPlayerLeaderboardScore而不是loadLeaderboardMetadata方法来实现您的目标。那个似乎没有返回过时的数据。检查here
答案 1 :(得分:0)
如果有人仍在寻找解决方案,根据最新的android版本,上面答案中描述的方法现已弃用。
最新的操作方法如下:
private LeaderboardsClient mLeaderboardsClient = Games.getLeaderboardsClient(this, googleSignInAccount);
private void updateLeaderboards(final String leaderboardId) {
mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(
leaderboardId,
LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC
).addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
if (leaderboardScoreAnnotatedData.get() == null)
mLeaderboardsClient.submitScore(leaderboardId, 1);
else {
long currentscore = leaderboardScoreAnnotatedData.get().getRawScore();
mLeaderboardsClient.submitScore(leaderboardId, currentscore + 1);
}
}
});
}