Skip to content

Commit

Permalink
async insertPlayerRating
Browse files Browse the repository at this point in the history
  • Loading branch information
howardchung committed Dec 4, 2023
1 parent 516d674 commit 8602d97
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 118 deletions.
104 changes: 19 additions & 85 deletions store/queries.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -592,91 +592,26 @@ function bulkIndexPlayer(bulkActions, cb) {
);
}
}
function insertPlayerRating(db, row, cb) {
async.series(
{
pr(cb) {
if (
row.match_id &&
(row.solo_competitive_rank || row.competitive_rank)
) {
db('player_ratings')
.insert({
account_id: row.account_id,
match_id: row.match_id,
time: row.time,
solo_competitive_rank: row.solo_competitive_rank,
competitive_rank: row.competitive_rank,
})
.asCallback(cb);
} else {
cb();
}
},
scr(cb) {
if (row.solo_competitive_rank) {
upsert(
db,
'solo_competitive_rank',
{
account_id: row.account_id,
rating: row.solo_competitive_rank,
},
{ account_id: row.account_id },
cb
);
} else {
cb();
}
},
cr(cb) {
if (row.competitive_rank) {
upsert(
db,
'competitive_rank',
{
account_id: row.account_id,
rating: row.competitive_rank,
},
{ account_id: row.account_id },
cb
);
} else {
cb();
}
},
rt(cb) {
if (row.rank_tier) {
upsert(
db,
'rank_tier',
{ account_id: row.account_id, rating: row.rank_tier },
{ account_id: row.account_id },
cb
);
} else {
cb();
}
},
lr(cb) {
if (row.leaderboard_rank) {
upsert(
db,
'leaderboard_rank',
{
account_id: row.account_id,
rating: row.leaderboard_rank,
},
{ account_id: row.account_id },
cb
);
} else {
cb();
}
export async function insertPlayerRating(row) {
if (row.rank_tier) {
await upsertPromise(
db,
'rank_tier',
{ account_id: row.account_id, rating: row.rank_tier },
{ account_id: row.account_id }
);
}
if (row.leaderboard_rank) {
upsertPromise(
db,
'leaderboard_rank',
{
account_id: row.account_id,
rating: row.leaderboard_rank,
},
},
cb
);
{ account_id: row.account_id }
);
}
}
function writeCache(accountId, cache, cb) {
return async.each(
Expand Down Expand Up @@ -1436,7 +1371,6 @@ export default {
insertPlayerPromise,
bulkIndexPlayer,
insertMatchPromise,
insertPlayerRating,
getHeroRankings,
getHeroItemPopularity,
getHeroBenchmarks,
Expand Down
68 changes: 35 additions & 33 deletions svc/mmr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@
import queue from '../store/queue.mjs';
import db from '../store/db.mjs';
import redis from '../store/redis.mjs';
import queries from '../store/queries.mjs';
import { insertPlayerRating, insertPlayerPromise } from '../store/queries.mjs';
import config from '../config.js';
import { getData, redisCount, getRetrieverArr } from '../util/utility.mjs';
const { insertPlayer, insertPlayerRating } = queries;
import {
getDataPromise,
redisCount,
getRetrieverArr,
} from '../util/utility.mjs';
const retrieverArr = getRetrieverArr();
function processMmr(job, cb) {
// Don't always do the job
if (Math.random() < 0) {
return cb();
}
async function processMmr(job, cb) {
const accountId = job.account_id;
const urls = retrieverArr.map(
(r) => `http://${r}?key=${config.RETRIEVER_SECRET}&account_id=${accountId}`
);
return getData({ url: urls }, (err, data) => {
if (err) {
return cb(err);
}
try {
const data = await getDataPromise({ url: urls });
redisCount(redis, 'retriever_player');
const player = {
account_id: job.account_id || null,
plus: Boolean(data.is_plus_subscriber),
};
return insertPlayer(db, player, false, () => {
if (
data.solo_competitive_rank ||
data.competitive_rank ||
data.rank_tier ||
data.leaderboard_rank
) {
data.account_id = job.account_id || null;
data.match_id = job.match_id || null;
data.solo_competitive_rank = data.solo_competitive_rank || null; // 0 MMR is not a valid value
data.competitive_rank = data.competitive_rank || null;
data.time = new Date();
return insertPlayerRating(db, data, cb);
}
return cb();
});
});
// NOTE: This leads to a massive number of updates on the player table
// Only write it sometimes, unless we're in dev mode
if (config.NODE_ENV === 'development' || Math.random() < 0.05) {
const player = {
account_id: job.account_id || null,
plus: Boolean(data.is_plus_subscriber),
};
await insertPlayerPromise(db, player);
}
if (
data.solo_competitive_rank ||
data.competitive_rank ||
data.rank_tier ||
data.leaderboard_rank
) {
data.account_id = job.account_id || null;
data.match_id = job.match_id || null;
data.solo_competitive_rank = data.solo_competitive_rank || null; // 0 MMR is not a valid value
data.competitive_rank = data.competitive_rank || null;
data.time = new Date();
await insertPlayerRating(data);
}
cb();
} catch (e) {
cb(e);
}
}
queue.runQueue(
'mmrQueue',
Expand Down

0 comments on commit 8602d97

Please sign in to comment.