簡體   English   中英

如何使用 Firebase 將新的真實玩家添加到排行榜?

[英]How do I add new, real players to a leaderboard using Firebase?

現在,我有一個排行榜,我的應用使用我在 Firebase 中生成的假玩家來顯示排行榜,如下所示:

在此處輸入圖片說明

我想知道如何將播放我的應用程序的每個新玩家的名稱添加到排行榜。

現在,我的代碼手動接收每個假玩家,而且我沒有自動接收 n 個玩家的方法

user2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value_temp = dataSnapshot.getValue(String.class);
                if(value_temp != null)
                    workshopParticipants.add(new LeaderPlayers(value_temp));
                Collections.sort(workshopParticipants);
                //Log.d(TAG, "Value is: " + mHigh);
            }



            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                //Log.w(TAG, "Failed to read value.", error.toException());
            }

        });
        user3 = database.getReference(Integer.toString(3));
        user3.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value_temp = dataSnapshot.getValue(String.class);
                Log.d(TAG, value_temp);
                if(value_temp != null)
                    workshopParticipants.add(new LeaderPlayers(value_temp));
                Collections.sort(workshopParticipants);
                //Log.d(TAG, "Value is: " + mHigh);
                for(int a = 0; a < workshopParticipants.size(); a++)
                {
                    players.add(workshopParticipants.get(a).getFullName());
                }
                for(int a = 0; a < players.size(); a++){
                    Log.d(TAG, "Players are: " + players.get(a));
                }
            }

這就是我手動添加假玩家的方式。

它目前適用於假玩家,但如果我有 N 個真實玩家玩我的游戲,那么我希望所有 N 個玩家也都出現在排行榜上。

如果我理解正確,您希望加載所有用戶而不必單獨加載每個用戶。 您可以通過將您的偵聽器附加到樹中的上一層,然后循環遍歷子節點來實現:

database.getReference().addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            Log.d(TAG, userSnapshot.getKey()); // "1", "2"...
            Log.d(TAG, userSnapshot.getValue(String.class)); "Christine 20", "Tom 64"...
        }
    }


    @Override
    public void onCancelled(DatabaseError error) {
        Log.w(TAG, "Failed to read value.", error.toException()); // Don't ignore errors
    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM