Skip to content

Commit

Permalink
more bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastientromp committed Jul 19, 2022
1 parent 7bb1253 commit f0cb59a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
8 changes: 2 additions & 6 deletions src/build-bgs-stats-slices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default async (event, context): Promise<any> => {

const allTribes: readonly Race[] = extractAllTribes(rows);
const allRankGroups: readonly RankGroup[] = buildAllRankGroups(existingSlices, rows);
logger.log('allRankGroups', allRankGroups);
const newSlice = buildNewSlice(validRows, allRankGroups, allTribes);
logger.log('newSlice', newSlice);
await saveSingleSlice(newSlice);
Expand Down Expand Up @@ -81,13 +82,8 @@ const buildFinalStats = async (allSlices: readonly Slice[], allTribes: readonly
tribes,
mmrPercentiles,
timePeriod,
allTribes,
);
// const mergedData: FinalBgsDataForTimePeriod = buildFinalStats(
// relevantSlices,
// mmrPercentiles,
// allTribes,
// timePeriod,
// );
await saveFinalFile(stats, tribes, timePeriod);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/internal-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface Slice {
readonly lastUpdateDate: Date;
readonly dataPoints: number;
readonly dataForTribes: readonly DataForTribes[];
readonly allMmr: readonly number[];
readonly highestMmr: number;
readonly mmrGroups: readonly RankGroup[];
}

export interface DataForTribes {
Expand Down Expand Up @@ -59,4 +60,5 @@ export interface InternalBgsGlobalStats extends BgsGlobalStats2 {
export interface RankGroup {
readonly mmrThreshold: number;
readonly mmrRangeUp: number;
readonly quantity?: number;
}
22 changes: 2 additions & 20 deletions src/slices/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,16 @@ import { BgsGlobalHeroStat2, MmrPercentile } from '../bgs-global-stats';
import { DataForTribes, HeroStat, InternalBgsGlobalStats, Slice } from '../internal-model';
import { formatDate } from '../utils/util-functions';

// export const buildFinalStats = (
// slices: readonly Slice[],
// mmrPercentiles: readonly MmrPercentile[],
// allTribes: readonly Race[],
// timePeriod: 'all-time' | 'past-three' | 'past-seven' | 'last-patch',
// ): FinalBgsDataForTimePeriod => {
// const tribePermutations = [null, ...combine(allTribes, 5)];
// const statsForTribes: readonly InternalBgsGlobalStats[] = tribePermutations
// .map(permutation => buildStatsForTribes(slices, permutation, mmrPercentiles, timePeriod))
// .map(stats => ({
// ...stats,
// allTribes: allTribes,
// }));
// return {
// statsForTribes: statsForTribes,
// };
// };

export const buildStatsForTribes = (
slices: readonly Slice[],
tribes: readonly Race[],
mmrPercentiles: readonly MmrPercentile[],
timePeriod: 'all-time' | 'past-three' | 'past-seven' | 'last-patch',
allTribes: readonly Race[],
): InternalBgsGlobalStats => {
const relevantDataForTribes = slices
.flatMap(slice => slice.dataForTribes)
.filter(dataForTribes => dataForTribes.tribes?.join('-') == tribes?.join('-'));

const heroStats: readonly BgsGlobalHeroStat2[] = mmrPercentiles
.flatMap(mmr => buildStatsForMmr(relevantDataForTribes, mmr))
.map(stat => ({
Expand All @@ -41,7 +23,7 @@ export const buildStatsForTribes = (
}));
return {
lastUpdateDate: formatDate(new Date()),
allTribes: null, // Populated outside of the loop
allTribes: allTribes,
tribes: tribes,
timePeriod: timePeriod,
mmrPercentiles: mmrPercentiles,
Expand Down
38 changes: 33 additions & 5 deletions src/slices/rank-groups.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { groupByFunction, logger } from '@firestone-hs/aws-lambda-utils';
import { MmrPercentile } from '../bgs-global-stats';
import { InternalBgsRow, RankGroup, Slice } from '../internal-model';
import { getMax } from '../utils/util-functions';

export const buildAllRankGroups = (slices: readonly Slice[], rows: readonly InternalBgsRow[]): readonly RankGroup[] => {
const highestRowMmr = getMax(rows.map(row => row.rating ?? 0));
const highestSlicesMmrs = slices.map(slice => getMax(slice.allMmr));
const highestSlicesMmrs = slices.map(slice => slice.highestMmr);
const highestMmr = getMax([highestRowMmr, ...highestSlicesMmrs]);
logger.log('highestMmr', highestMmr, highestSlicesMmrs, highestRowMmr);
return buildDefaultRankGroups(highestMmr);
};

Expand All @@ -25,10 +27,36 @@ const buildDefaultRankGroups = (highestMmr: number): readonly RankGroup[] => {
};

export const buildMmrPercentiles = (slices: readonly Slice[]): readonly MmrPercentile[] => {
const sortedMmrs: readonly number[] = slices.flatMap(slice => slice.allMmr).sort((a, b) => a - b);
const median = sortedMmrs[Math.floor(sortedMmrs.length / 2)];
const top25 = sortedMmrs[Math.floor((sortedMmrs.length / 4) * 3)];
const top10 = sortedMmrs[Math.floor((sortedMmrs.length / 10) * 9)];
const groupedGroups = groupByFunction((group: RankGroup) => group.mmrThreshold)(
slices.flatMap(slice => slice.mmrGroups),
);
const consolidatedGroups: readonly RankGroup[] = Object.keys(groupedGroups)
.sort((a, b) => +a - +b)
.map(mmrThreshold => {
logger.log('will map group', mmrThreshold, groupedGroups[mmrThreshold]);
return {
mmrThreshold: +mmrThreshold,
mmrRangeUp: groupedGroups[mmrThreshold][0].mmrRangeUp,
quantity: groupedGroups[mmrThreshold].map(g => g.quantity ?? 0).reduce((a, b) => a + b, 0),
};
});
const highestMmr = Math.max(...consolidatedGroups.map(g => g.mmrThreshold));
let median = 0;
let top25 = 0;
let top10 = 0;
for (const group of consolidatedGroups) {
logger.log('handling goup', group, highestMmr);
if (!median && group.mmrThreshold >= highestMmr / 2) {
median = group.mmrThreshold;
}
if (!top25 && group.mmrThreshold >= highestMmr * 0.75) {
top25 = group.mmrThreshold;
}
if (!top10 && group.mmrThreshold >= highestMmr * 0.9) {
top10 = group.mmrThreshold;
}
}
logger.log('mmrPercentile data', median, top25, top10);
return [
{
percentile: 100,
Expand Down
15 changes: 12 additions & 3 deletions src/slices/slice-builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { groupByFunction } from '@firestone-hs/aws-lambda-utils';
import { groupByFunction, logger } from '@firestone-hs/aws-lambda-utils';
import { Race } from '@firestone-hs/reference-data';
import { DataForMmr, DataForTribes, HeroStat, InternalBgsRow, RankGroup, Slice } from '../internal-model';
import { combine } from '../utils/util-functions';
import { combine, getMax } from '../utils/util-functions';

export const buildNewSlice = (
rows: readonly InternalBgsRow[],
Expand All @@ -12,11 +12,20 @@ export const buildNewSlice = (
const dataForTribes: readonly DataForTribes[] = tribePermutations.map(permutation =>
buildDataForTribes(rows, permutation, ranks),
);
const groupedMmr = groupByFunction((row: InternalBgsRow) => 100 * Math.round(row.rating / 100))(
rows.filter(row => row.rating != null),
);
logger.log('groupedMmr', Object.keys(groupedMmr), groupedMmr);
return {
lastUpdateDate: new Date(),
dataPoints: rows.length,
dataForTribes: dataForTribes,
allMmr: rows.map(row => row.rating).filter(rating => rating != null),
highestMmr: getMax(rows.map(row => row.rating)),
mmrGroups: Object.keys(groupedMmr).map(mmrThreshold => ({
mmrThreshold: +mmrThreshold,
mmrRangeUp: 100,
quantity: groupedMmr[mmrThreshold].length,
})),
};
};

Expand Down

0 comments on commit f0cb59a

Please sign in to comment.