【问题标题】:How to compare points from different Uid in firebase如何在firebase中比较来自不同UID的积分
【发布时间】:2018-06-14 09:28:35
【问题描述】:

是否可以比较来自不同uid的点,一个是接收者uid,另一个是发送者uid。我想检索这些点并比较这些点,以便我可以决定第 1 天、第 2 天和第 3 天的获胜者。 使用云功能或仅在 Android Studio 上怎么可能。 请帮帮我。

【问题讨论】:

  • 但是如何在发送方和接收方检索这些点,因为它们具有不同的 uid,所以如何根据它们的 ui'd 检索并比较它们。
  • 我已经浏览了该链接,但与我的问题没有任何关系,我们只能将数据检索并保存到各自的 uid 中,但从不同的 uid 检索没有任何意义。据我说,这可以在云功能中完成,但不知道如何处理。
  • 您是否存储了挑战 ID -LExD...TXqP
  • @AlexMamo -LExD...TXqP 它是每个新挑战的 pushId。我想要比较不同uid的点数。
  • 从所有 uid 中说?对吗?

标签: java android firebase firebase-realtime-database


【解决方案1】:

要解决这个问题,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference challengeRef = rootRef.child("Challenge");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            int senderPointsDayOne = ds.child(senderId).child("points").child("Day1").getValue(Integer.class);
            int senderPointsDayTwo = ds.child(senderId).child("points").child("Day2").getValue(Integer.class);
            int senderPointsDayThree = ds.child(senderId).child("points").child("Day3").getValue(Integer.class);

            int receiverPointsDayOne = ds.child(receiverId).child("points").child("Day1").getValue(Integer.class);
            int receiverPointsDayTwo = ds.child(receiverId).child("points").child("Day2").getValue(Integer.class);
            int receiverPointsDayThree = ds.child(receiverId).child("points").child("Day3").getValue(Integer.class);

            int senderTotal = senderPointsDayOne + senderPointsDayTwo + senderPointsDayThree;
            int receiverTotal = receiverPointsDayOne + receiverPointsDayTwo + receiverPointsDayThree;

            if(senderTotal > receiverTotal) {
                //sender is the winner
            } else if (senderTotal < receiverTotal) {
                //receiver is the winner
            } else {
                //it's even
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
challengeRef.addListenerForSingleValueEvent(valueEventListener);

其中senderId是发送者的id,receiverId是接收者的id。此代码为您提供了所有日子的赢家。如果你想要每天获胜,只需更改逻辑并比较相应日期的结果即可。

编辑: 作为结论,如果没有senderIdreceiverId 用于每个播放,您将无法查询您的数据库,因此我建议您存储它们。最佳做法是将匹配项存储在名为 matches 的新节点中。您的数据库结构应如下所示:

Firebase-root
   |
   --- matches
         |
         --- matchId
               |
               --- senderId: true
               |
               --- receiverId: true

其中matchId实际上是从另一个节点-LExD...TXqP推送的id。在这种情况下,您需要先查询您的数据库以获取对手(senderIdreceiverId),然后按照上述说明获取结果。所以基本上你需要查询你的数据库两次。

【讨论】:

  • 您的比较逻辑很好,但如果假设我是发送者,那么我只能从我的 uid 中检索点数,发送者如何从接收者 uid 中检索点数,接收者也是如此。这是我最关心的问题。
  • 但是我已经在挑战节点中存储了uid,我是否必须创建另一个节点来仅存储用户的uid?
  • 是的。为了使用这些 id,您首先需要拥有它们,以便能够在您的参考中使用它们。因此,查询您的数据库以获取作为键的 senderIdreceiverId 并在上述代码中使用这些值。所以这是关于嵌套侦听器的。试试看,让我知道。
  • 是的,我已经解决了它,但是一些类转换异常即将到来,所以我正在解决那部分,但是是的,我能够通过创建另一个名为 match 的节点来检索发送者和接收者的 uid。所以谢谢这个解决方案对我有意义。
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 2019-06-02
  • 2016-04-30
  • 2021-04-10
  • 2020-06-15
  • 1970-01-01
  • 2018-08-25
  • 2020-11-09
相关资源
最近更新 更多