Skip to content

Commit

Permalink
normalize BG hero card IDs for skins
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastientromp committed Jul 30, 2021
1 parent 170357b commit d9e3bfb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"dist/**/*"
],
"dependencies": {
"@firestone-hs/hs-replay-xml-parser": "0.0.55",
"@firestone-hs/reference-data": "^0.1.136",
"@firestone-hs/hs-replay-xml-parser": "0.0.78",
"@firestone-hs/reference-data": "^0.1.140",
"@types/elementtree": "^0.1.0",
"aws-sdk": "^2.524.0",
"elementtree": "^0.1.7",
Expand Down
41 changes: 28 additions & 13 deletions src/build-battlegrounds-hero-stats.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { AllCardsService } from '@firestone-hs/reference-data';
import { ServerlessMysql } from 'serverless-mysql';
import { gzipSync } from 'zlib';
import { BgsGlobalHeroStat, BgsHeroTier } from './bgs-global-stats';
import { getConnection as getConnectionStats } from './db/rds';
import { getConnection as getConnectionBgs } from './db/rds-bgs';
import { S3 } from './db/s3';
import { loadStats } from './retrieve-bgs-global-stats';
import { http } from './utils/util-functions';
import { http, normalizeHeroCardId } from './utils/util-functions';

const s3 = new S3();
const allCards = new AllCardsService();

// This example demonstrates a NodeJS 8.10 async handler[1], however of course you could use
// the more traditional callback-style handler.
// [1]: https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/
export default async (event): Promise<any> => {
await allCards.initializeCardsDb();
const lastBattlegroundsPatch = await getLastBattlegroundsPatch();

const mysql = await getConnectionStats();
const mysqlBgs = await getConnectionBgs();

const lastBattlegroundsPatch = await getLastBattlegroundsPatch();

await updateAggregatedStats(mysqlBgs, mysql, lastBattlegroundsPatch);
await updateLastPeriodStats(mysqlBgs, mysql, lastBattlegroundsPatch);
await updateAggregatedStats(mysqlBgs, mysql, lastBattlegroundsPatch, allCards);
await updateLastPeriodStats(mysqlBgs, mysql, lastBattlegroundsPatch, allCards);

const stats = await loadStats(mysql, mysqlBgs);
const stringResults = JSON.stringify(stats);
Expand All @@ -45,19 +48,29 @@ const getLastBattlegroundsPatch = async (): Promise<number> => {
return structuredPatch.currentBattlegroundsMetaPatch;
};

const updateAggregatedStats = async (mysqlBgs: ServerlessMysql, mysqlStats: ServerlessMysql, buildNumber: number) => {
const updateAggregatedStats = async (
mysqlBgs: ServerlessMysql,
mysqlStats: ServerlessMysql,
buildNumber: number,
allCards: AllCardsService,
) => {
// This won't be fully accurate, as not all update will be installed simulatenously, but it's good enough
const now = Date.now();
const earliestStartDate = new Date(now - 10 * 24 * 60 * 60 * 1000).toISOString();
await updateStats(mysqlBgs, mysqlStats, earliestStartDate, buildNumber, false);
await updateStats(mysqlBgs, mysqlStats, earliestStartDate, buildNumber, false, allCards);
};

// TODO: remove this lastperiod stuff, add a new column with the last update Date, and do a query on that last update date
const updateLastPeriodStats = async (mysqlBgs: ServerlessMysql, mysqlStats: ServerlessMysql, buildNumber: number) => {
const updateLastPeriodStats = async (
mysqlBgs: ServerlessMysql,
mysqlStats: ServerlessMysql,
buildNumber: number,
allCards: AllCardsService,
) => {
// Get all the reviews from the last day
const now = Date.now();
const earliestStartDate = new Date(now - 24 * 60 * 60 * 1000).toISOString();
await updateStats(mysqlBgs, mysqlStats, earliestStartDate, buildNumber, true);
await updateStats(mysqlBgs, mysqlStats, earliestStartDate, buildNumber, true, allCards);
};

const updateStats = async (
Expand All @@ -66,6 +79,7 @@ const updateStats = async (
creationDate: string,
buildNumber: number,
insertCreationDate: boolean,
allCards: AllCardsService,
) => {
const allHeroesQuery = `
SELECT distinct playerCardId
Expand All @@ -77,8 +91,8 @@ const updateStats = async (
`;
const allHeroesResult: readonly any[] = await mysqlStats.query(allHeroesQuery);
const allHeroes: readonly string[] = allHeroesResult
.map(result => result.playerCardId)
.filter(playerCardId => playerCardId !== 'TB_BaconShop_HERO_59t');
.filter(playerCardId => playerCardId !== 'TB_BaconShop_HERO_59t')
.map(result => normalizeHeroCardId(result.playerCardId, allCards));

const heroStatsQuery = `
SELECT playerCardId, additionalResult, count(*) as count, max(creationDate) as lastPlayedDate
Expand All @@ -91,12 +105,13 @@ const updateStats = async (
GROUP BY playerCardId, additionalResult
`;
const heroStatsResults: readonly any[] = ((await mysqlStats.query(heroStatsQuery)) as any[])
.filter(result => result.playerCardId !== 'TB_BaconShop_HERO_59t')
.map(result => ({
...result,
playerCardId: normalizeHeroCardId(result.playerCardId, allCards),
additionalResult: parseInt(result.additionalResult),
}))
.filter(result => result.additionalResult > 0)
.filter(result => result.playerCardId !== 'TB_BaconShop_HERO_59t');
.filter(result => result.additionalResult > 0);

const stats: BgsGlobalHeroStat[] = allHeroes.map(heroCardId => buildHeroInfo(heroCardId, heroStatsResults));

Expand Down
32 changes: 30 additions & 2 deletions src/utils/util-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AllCardsService } from '@firestone-hs/reference-data';
import { ReferenceCard } from '@firestone-hs/reference-data/lib/models/reference-cards/reference-card';
import { AllCardsService, ReferenceCard } from '@firestone-hs/reference-data';
import fetch, { RequestInfo } from 'node-fetch';

function partitionArray<T>(array: readonly T[], partitionSize: number): readonly T[][] {
Expand Down Expand Up @@ -60,3 +59,32 @@ export const getCardFromCardId = (cardId: number | string, cards: AllCardsServic
};

export { partitionArray, http, sleep };

export const normalizeHeroCardId = (heroCardId: string, allCards: AllCardsService = null): string => {
if (!heroCardId) {
return heroCardId;
}

if (allCards) {
const heroCard = allCards.getCard(heroCardId);
if (!!heroCard?.battlegroundsHeroParentDbfId) {
const parentCard = allCards.getCardFromDbfId(heroCard.battlegroundsHeroParentDbfId);
if (!!parentCard) {
return parentCard.id;
}
}
}
// Fallback to regex
const bgHeroSkinMatch = heroCardId.match(/(.*)_SKIN_.*/);
console.debug('normalizing', heroCardId, bgHeroSkinMatch);
if (bgHeroSkinMatch) {
return bgHeroSkinMatch[1];
}

switch (heroCardId) {
case 'TB_BaconShop_HERO_59t':
return 'TB_BaconShop_HERO_59';
default:
return heroCardId;
}
};

0 comments on commit d9e3bfb

Please sign in to comment.