火力:如何匹配的对手在一场比赛?火力、对手

2023-09-10 23:42:35 作者:人走茶凉

我实行社会棋局。每个用户都可以创建一个新的游戏,他们会等到系统会找到一个对手对于他们。

I'm implementing a social chess game. Every user can create a new game, and they'll wait until the system will find an opponent for them.

当用户创建一个游戏,他们指定约束条件:颜色,他们想打,而对手的最小象棋等级

When user creates a game, they specify constraints: color they'd like to play, and opponent's minimal chess rating.

反对者可以匹配或不匹配例如,下面的两个对手将匹配:

Opponents can either match or not match. For example, the following two opponents will match:

// User 1 with rating 1700              // User 2 with rating 1800
// creates this game                    // creates this game
game: {                                 game: { 
  color: 'white',                         minRating: 1650
  minRating: 1600                       }
}                                       // User did not specify a preferred color,
                                        // meaning they do not care which color to play

因此​​,如果用户1是系统的第一个用户,并创建了他们的比赛,他们将等待。一旦用户2创建自己的游戏,就应该立即用​​用户1相匹配。

So, if User 1 is the first user in the system, and created their game, they'll wait. Once User 2 creates their game, they should be matched immediately with User 1.

在另一侧,下面的两个对手将不匹配,因为它们既要玩白色。在这种情况下,应该等到别人与创建一个游戏的色彩:黑(或不指定颜色),而 minRating ,将符合要求。

On the other side, the following two opponents won't match, because they both want to play white. In this case, both should wait until someone else creates a game with color: 'black' (or color not specified), and minRating that would match the requirements.

// User 1 with rating 1700              // User 2 with rating 1800
// creates this game                    // creates this game
game: {                                 game: { 
  color: 'white',                         color: 'white'  
  minRating: 1600                         minRating: 1650
}                                       }

我的相关场景,成千上万的用户创建,同时新游戏的关注。我如何确保我匹配的对手不会产生死锁?即我怎么$当用户1,用户2和用户3正在努力寻找对手在同一时刻p $ pvent的情景,和他们匹配算法返回用户99.我如何从这种情况中恢复,分配用户99其中只有一个?

My concerns related to scenarios where thousands of users creates new games at the same time. How do I make sure that I match opponents without creating deadlocks? i.e. how do I prevent scenarios when User 1, User 2, and User 3 are trying to find an opponent at the same time, and their matching algorithms return User 99. How do I recover from this scenario, assigning User 99 to only one of them?

你会如何使用火力地堡的力量来实现这样的配套体系?

How would you use the power of Firebase to implement such a matching system?

推荐答案

这是特别是如果你想匹配多个领域的NoSQL环境具有挑战性的任务。

It is a challenging task in nosql environment especially if you want to match multiple fields

在你的情况我会设置一个简单的指数颜色和颜色中我会存储参考与优先级设置为minRating游戏。 这样,你可以通过prefered颜色minRating优先查询游戏。

in your case I would setup a simple index by color and within the color I would store the reference to the game with priority set to minRating. That way you can query the games by the prefered color with the priority of minRating.

indexes: {
  color:{
     white:{
        REF_WITH_PRIORITY_TO_RATING: true
     },
     black:{
        REF_WITH_PRIORITY_TO_RATING: true
     }
  }
}

如果你想获得的信息每当比赛打开游戏:

if you want to get info whenever the match opens the game:

ref = new(Firebase)('URL');
query =ref.child('color_index/white/').startAt(minPriority);
query.on('child_added',function(snapshot){
  //here is your new game matching the filter
});

不过,这会变得更加复杂,如果你引入多个字段筛选的游戏,例如 dropRate 的timeZone gamesPlayed等......在这种情况下,您可以嵌套索引更深层次的:

This however it would get more complex if you introduce multiple fields for filtering the games for example dropRate, timeZone, 'gamesPlayed' etc... In this case you can nest the indexes deeper:

indexes: {
  GMT0: {
    color:{
       white:{
          REF_WITH_PRIORITY_TO_RATING: true
       },
       black:{
          REF_WITH_PRIORITY_TO_RATING: true
       },
  }
  GMT1: {
       // etc
  }
}