Mis*_*hko 5 algorithm nosql firebase firebase-realtime-database
我正在实施社交象棋游戏.每个用户都可以创建一个新游戏,他们会等到系统找到他们的对手.
当用户创建游戏时,他们会指定约束:他们想要玩的颜色,以及对手的最低国际象棋等级.
对手可以匹配也可以不匹配.例如,以下两个对手将匹配:
// 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
Run Code Online (Sandbox Code Playgroud)
因此,如果用户1是系统中的第一个用户,并创建了他们的游戏,他们将等待.用户2创建游戏后,应立即与用户1匹配.
另一方面,以下两个对手将无法比赛,因为他们都想打白棋.在这种情况下,两者都应该等到其他人用color: 'black'(或未指定颜色)创建游戏,并且minRating符合要求.
// 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
} }
Run Code Online (Sandbox Code Playgroud)
我的担忧涉及数千名用户同时创建新游戏的场景.如何在不造成死锁的情况下确保匹配对手?即,当用户1,用户2和用户3同时尝试查找对手时,如何阻止方案,并且他们的匹配算法返回用户99.如何从此方案中恢复,将用户99分配给其中一个他们?
您如何使用Firebase的强大功能来实现这样的匹配系统?
起点的明显选择是颜色,因为这是一项独家要求.其他看起来更像加权结果,所以那些可以简单地增加或减少重量.
利用最小/最大范围的优先级,并将每个范围保持在单独的"索引"中.然后抓住每个匹配并创建一个联合.考虑这个结构:
/matches
/matches/colors/white/$user_id
/matches/ranking/$user_id (with a priority equal to ranking)
/matches/timezones/$user_id (with a priority of the GMT relationship)
Run Code Online (Sandbox Code Playgroud)
现在要查询,我只需抓住每个类别中的匹配并按匹配数排名.我可以从颜色开始,因为这可能不是可选或相对评级:
var rootRef = new Firebase('.../matches');
var VALUE = {
"rank": 10, "timezone": 5, "color": 0
}
var matches = []; // a list of ids sorted by weight
var weights = {}; // an index of ids to weights
var colorRef = rootRef.child('colors/black');
colorRef.on('child_added', addMatch);
colorRef.child('colors/black').on('child_removed', removeMatch);
var rankRef = rootRef.child('ranking').startAt(minRank).endAt(maxRank);
rankRef.on('child_added', addWeight.bind(null, VALUE['rank']));
rankRef.on('child_removed', removeWeight.bind(null, VALUE['rank']));
var tzRef = ref.child('timezone').startAt(minTz).endAt(maxTz);
tzRef.on('child_added', addWeight.bind(null, VALUE['timezone']));
tzRef.on('child_removed', removeWeight.bind(null, VALUE['timezone']));
function addMatch(snap) {
var key = snap.name();
weights[key] = VALUE['color'];
matches.push(key);
matches.sort(sortcmp);
}
function removeMatch(snap) {
var key = snap.name();
var i = matches.indexOf(key);
if( i > -1 ) { matches.splice(i, 1); }
delete weights[key];
}
function addWeight(amt, snap) {
var key = snap.name();
if( weights.hasOwnProperty(key) ) {
weights[key] += amt;
matches.sort(sortcmp);
}
}
function removeWeight(amt, snap) {
var key = snap.name();
if( weights.hasOwnProperty(key) ) {
weights[key] -= amt;
matches.sort(sortcmp);
}
}
function sortcmp(a,b) {
var x = weights[a];
var y = weights[b];
if( x === y ) { return 0; }
return x > y? 1 : -1;
}
Run Code Online (Sandbox Code Playgroud)
好的,现在我已经给出了每个人在这个用例中要求的内容 - 如何创建一个基本的where子句.但是,这里适当的答案是搜索应该由搜索引擎执行.这不是简单的条件.这是对最佳匹配的加权搜索,因为颜色等字段不是可选的或只是最佳匹配,而其他 - 排名可能 - 是两个方向上最接近的匹配,而有些只是影响匹配的质量.
查看手电筒以获得简单的ElasticSearch集成.通过这种方法,您应该能够利用ES的强大加权工具,动态排序以及执行正确匹配算法所需的一切.
关于死锁.在你每秒有数百笔交易(即数十万用户竞争比赛)之前,我不会过多关注.拆分我们将写入的路径以接受连接并执行事务以确保只有一个人成功获取它.将其与读取数据分开,以便锁定该路径不会减慢处理速度.将事务保持在最小尺寸(如果可能,单个字段).
在 NoSQL 环境中这是一项具有挑战性的任务,尤其是当您想要匹配多个字段时
在您的情况下,我将按颜色设置一个简单的索引,并在颜色内存储对游戏的引用,并将优先级设置为 minRating。这样您就可以按首选颜色以 minRating 优先级查询游戏。
indexes: {
color:{
white:{
REF_WITH_PRIORITY_TO_RATING: true
},
black:{
REF_WITH_PRIORITY_TO_RATING: true
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想在比赛开始时获取信息:
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
});
Run Code Online (Sandbox Code Playgroud)
然而,如果您引入多个字段来过滤游戏,例如dropRate、timeZone、 'gamesPlayed' 等,情况会变得更加复杂......在这种情况下,您可以将索引嵌套得更深:
indexes: {
GMT0: {
color:{
white:{
REF_WITH_PRIORITY_TO_RATING: true
},
black:{
REF_WITH_PRIORITY_TO_RATING: true
},
}
GMT1: {
// etc
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9188 次 |
| 最近记录: |