Firebase:如何在游戏中匹配对手? [英] Firebase: How to match opponents in a game?
问题描述
我正在实施一个社交国际象棋游戏.每个用户都可以创建一个新游戏,他们会等待系统为他们找到对手.
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.
另一方面,以下两个对手不会匹配,因为他们都想下白棋.在这种情况下,两者都应该等到其他人使用 color: 'black'
(或未指定颜色)和 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 同时尝试寻找对手时,我如何防止出现这种情况,并且他们的匹配算法返回用户 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?
您将如何利用 Firebase 的强大功能来实现这样一个匹配系统?
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.这样您就可以通过首选颜色和 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 colour 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
}
}
这篇关于Firebase:如何在游戏中匹配对手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!