Firebase game matching algorithm -
i'm trying create player matching algorithm. expected work that:
algorithm:
- player creates game request specifying
bet,gametype. - server subscribes following query list:
{ 'gamerequests': { '1': { createdat: 1454777718074, uid: 123, bet: 10, gametype: 'nhl' }, '2': { createdat: 1454777718075, uid: 123, bet: 20, gametype: 'nhl' }, '3': { createdat: 1454777718076, uid: 321, bet: 10, gametype: 'nhl' }, } } i requests keys '1', '3'.
- now delete children keys
'1','3', create game them.
what have:
so far i'm doing loading whole gamerequests branch.
randomgamesref.on('value', ongamerequestsupdate) function ongamerequestsupdate (snapshot) { const gamerequests = snapshot.val() const pairs = _.chain(gamerequests) // transform array .values() // group sport , bet size .groupby((req) => req.gametype + '+' + req.bet) // map groups pairs .map((group) => { return _.chain(group) .uniqby('createdby') .take(2) .value() }) .filter((pairs) => pairs.length === 2) .value() // having pairs can delete them // database , create new game } but loading memory every time doesn't seem idea.
question: how implemented in firebase?
in nosql end modeling data way want access in code. instead of reading game requests , grouping them gametype , bet in code, consider storing them under key combines gametype , bet:
{ 'gamerequests': { 'nhl_10': { '1': { createdat: 1454777718074, uid: 123, }, '3': { createdat: 1454777718076, uid: 321, } }, 'nhl_20': { '2': { createdat: 1454777718075, uid: 123, } } } } now code becomes:
function ongamerequestsupdate (snapshot) { snapshot.foreach(function(gametypesnapshot) { if (gametypesnapshot.numchildren() >= 2) { // there @ least 1 request pair game type } }); }; i keep recommending people read this article, since explains nosql data modeling techniques such one.
Comments
Post a Comment