Meteor 实时游戏 - 根据得分匹配两名玩家?

Meteor realtime game - match two players according to their score?

我想构建一个随机匹配两名玩家的实时问答游戏(如果他们已登录,则根据他们的获胜率)。我已经阅读了Discover Meteor这本书,对框架有了基本的了解,但是我不知道如何实现匹配部分。有人知道怎么做吗?

一个实现可能如下:

用户以某种方式触发了 'looking for game' 事件,将 user.profile.lookingForGame 上的属性设置为 true。然后该事件调用服务器端 Meteor 方法,该方法查询所有其他在线用户寻找游戏。

从那里开始,这实际上取决于您希望如何处理用户 'match'。

要确定所有在线用户,请尝试使用用户状态包: https://github.com/mizzao/meteor-user-status

添加后,任何在线用户都将在 'online' 的配置文件对象中拥有一个属性。您可以使用它来查询所有在线用户。

如果你想匹配分数相近的用户,你可以这样做:mongodb - Find document with closest integer value

那些 Mongo 查询的 Meteor 代码非常相似,但有一些微妙的差异,有点棘手。在 Meteor 中,它看起来像这样:

  SP  // "selected player" = the User you want to match someone up with
 var score = SP.score; // selected player's score

  var queryLow = {score: {$lte:score},_id:{$ne:SP._id}};
  var queryHigh = {score:{$gte:score},_id:{$ne:SP._id}};

  // "L" is the player with the closest lower score
  var L=Players.findOne(queryLow,{sort:{score:-1},limit:1});

  // "H" is the player with the closest higher score
  var H=Players.findOne(queryHigh,{sort:{score:1},limit:1});

所以,现在您可以参考得分正好在 'selected player' 之上和之下的球员。在让它随机化方面,也许从像 "match me with the next available player who's score is closest" 这样的简单算法开始,然后如果它太可预测和无聊,你可以在算法中加入一些随机性。

你可以在这里查看上面的 Meteor 代码 http://meteorpad.com/pad/4umMP4iY8AkB9ct2d/ClosestScore 你可以 Fork 它并弄乱查询以查看它是如何工作的。 祝你好运!流星很棒,我很喜欢

如果您将包 peppelg:random-opponent-matcher 添加到您的应用程序中,您可以像这样匹配对手:

在服务器上,您需要有一个 RandomOpponentMatcher 的实例,如下所示:

new RandomOpponentMatcher('my-matcher', {name: 'fifo'}, function(user1, user2){
    // Create the match/game they should play.
})

您传递给 RandomOpponentMatcher 的函数将在两个用户匹配并互相对战时被调用。在其中,您可能想要创建用户应该相互对战的比赛(这个包只匹配对手,它不包含任何比赛功能 games/matches)。

在客户端,您还需要创建一个 RandomOpponentMatcher 的实例,但您只需将名称传递给它(与您在服务器上使用的名称相同):

myMatcher = new RandomOpponentMatcher('my-matcher')

那么当用户登录后,随机匹配哪个对手,只需要调用add方法即可。例如:

<template name="myTemplate">
    <button class="clickMatchesWithOpponent">Match me with someone!</button>
</template>
Template.myTemplate.events({
    'click .clickMatchesWithOpponent': function(event, template){
        myMatcher.add()
    }
})

当两个不同的登录用户单击该按钮时,将调用您在服务器上传递给 RandomOpponentMatcher 的函数。