简体   繁体   English

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

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

Right now, I have a leaderboard that my app displays using fake players that I have generated in Firebase, like so:现在,我有一个排行榜,我的应用使用我在 Firebase 中生成的假玩家来显示排行榜,如下所示:

在此处输入图片说明

I want to know how I can add the name of every new player that plays my app to the leaderboard.我想知道如何将播放我的应用程序的每个新玩家的名称添加到排行榜。

Right now, my code manually takes in each fake player, and I don't have an automated way of taking n number of players in现在,我的代码手动接收每个假玩家,而且我没有自动接收 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));
                }
            }

This is how I manually add the fake players.这就是我手动添加假玩家的方式。

It currently works for the fake players, but if I have for example, N real players play my game, then I want all N of them to appear on the leaderboard as well.它目前适用于假玩家,但如果我有 N 个真实玩家玩我的游戏,那么我希望所有 N 个玩家也都出现在排行榜上。

If I understand you correctly, you want to load all users without have to load each of them individually.如果我理解正确,您希望加载所有用户而不必单独加载每个用户。 You can do that by attaching your listener one level higher in the tree, and then looping over the child nodes:您可以通过将您的侦听器附加到树中的上一层,然后循环遍历子节点来实现:

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