简体   繁体   English

具有Firebase云功能的排行榜

[英]Leaderboard with firebase cloud functions

My current database structure looks like this, 我当前的数据库结构如下所示: 在此处输入图片说明

Which basically has a mobile_users table and and a top 100 table which will be the leaderboard. 它基本上有一个mobile_users表和一个排在前100位的表。

Im trying to figure out how to write a cloud function that executes every minute that updates/populates the top100 table with the userid, earned_points from mobile_users and sort it by earned_points. 我试图弄清楚如何编写一个执行每分钟执行一次的云函数,该函数使用用户名,mobile_users中的early_points更新/填充top100表并按early_points对其进行排序。

Should i add a rank field on this table or is there a way to order the table from asc/desc order based on mobile_users? 我应该在此表上添加等级字段,还是有一种方法可以基于mobile_users从asc / desc顺序对表进行排序?

My current function looks like this 我目前的功能看起来像这样

exports.dbRefOnWriteEvent = functions.database.ref('/mobile_user/{userId}/{earned_points}').onWrite(event => {
    var ref = admin.database().ref("/top100");
    ref.orderByChild("earned_points").once("value", function(dataSnapshot) {
        var i = 0;
        dataSnapshot.forEach(function(childSnapshot) {
          var r = (dataSnapshot.numChildren() - i);
          childSnapshot.ref.update({rank: r},function(error) {
              if (error != null)
                console.log("update error: " + error);
          });
          i++;
        });
    });
});

I have yet to figure out how to tell the cloud function to execute every minute. 我还没有弄清楚如何告诉云功能每分钟执行一次。 I am having trouble structuring these type of queries. 我在构造这些类型的查询时遇到了麻烦。

My function is also failing to populate the the top100 table with those 3 current users. 我的功能还没有用这3个当前用户填充top100表。 I would appreciate if someone could point me in the right direction. 如果有人能指出我正确的方向,我将不胜感激。

Create a http request function that will do your business. 创建一个http请求函数来完成您的业务。

Then use cron-job to call your http firebase function every minute : cron-job 然后每分钟使用cron-job来调用您的http firebase函数: cron-job

Maybe you can have two root nodes in your database. 也许您的数据库中可以有两个根节点。 One like the above and a second node that is called leaderboard. 类似于上面的一个,第二个节点称为页首横幅。

That second node can be an array where the index reflects the rank and the name reflects the score. 第二个节点可以是一个数组,其中索引反映排名,名称反映得分。

Leaderboard
           |-- [0] 
                |-- score: 5000
                |-- uid: 4zzdawqeasdasq2w1
           |---[1]
                |-- score: 4990
                |-- uid: 889asdas1891sadaw

Then when you get a new score, you update the user's node and then also update the leaderboard. 然后,当您获得新分数时,您将更新用户的节点,然后还更新排行榜。 Then you just grab the uid and look up the name from the user's node. 然后,您只需获取uid,并从用户节点中查找名称。

Like the other posters said, use a HTTP Firebase Cloud Function and a chron job. 就像其他张贴者所说的那样,请使用HTTP Firebase Cloud Function和chron作业。

However I would recommend you use a chron job just to keep the cloud function alive (look up cold start firebase functions), but also make a fetch request to trigger the cloud function from the front-end everytime a user plays the game and generates a score. 不过,我建议您使用chron作业,以保持云功能保持活动状态(查找冷启动firebase功能),而且每次用户玩游戏并生成游戏时都会发出获取请求以从前端触发云功能得分。 Otherwise if you get 10 plays per minute and it only updates once a minute, that will not be great user experience for the players who expect a real time leaderboard. 否则,如果您每分钟获得10次播放,并且每分钟更新一次,那么对于那些希望获得实时排行榜的玩家来说,这并不是很好的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM