如果玩家在 Firestore 中的分数相等,如何按剩余时间为他们排序?

How to order by time remaining for gamers if their scores are equal in Firestore?

我正在制作一个游戏应用程序,我想按降序列出用户的分数。到目前为止,我可以列出排行榜中降序排列的分数,但如果用户分数相等怎么办?然后我希望谁能在更短的时间内完成游戏,就可以进入该列表。我想列出这样的用户:if scores of the user is equal, which user has more time left he will go up

这是我的代码:

   val query: Query = FirebaseFirestore.getInstance()
        .collection("Scores")
        .orderBy("score",Query.Direction.DESCENDING)
        .limit(50)

    val options: FirestoreRecyclerOptions<LeaderModel> = FirestoreRecyclerOptions.Builder<LeaderModel>()
        .setQuery(query, LeaderModel::class.java)
        .build()


    adapter = FBRecyclerAdapter(options)
    binding?.recyclerViewLeaderBoard?.adapter = adapter

如有任何帮助,我们将不胜感激。谢谢!

but what if the users scores are equal?

如果两个用户的分数相同,你应该实施一种机制来区分他们。

Then I want whoever completed the game in less time to go up in that list.

要实现这一点,您应该像这样添加一个新的 .orderBy() 调用:

val query: Query = FirebaseFirestore.getInstance()
    .collection("Scores")
    .orderBy("score", Query.Direction.DESCENDING)
    .orderBy("time", Query.Direction.DESCENDING) //Newly added
    .limit(50)

这意味着您应该在“分数”集合中存在的每个文档中添加一个新的 属性,可以保存用户完成游戏的时间。

Firestore--root
  |
  --- Scores (collection)
       |
       --- $scoreId (document)
             |
             --- score: 100
             |
             --- time: 30 //Time in seconds

为了让它工作,不要忘记创建一个索引,正如我在下面的回答中所解释的那样 post:

所以您的索引应该在以下字段上:

score Descending time Descending