Skip to content

Commit

Permalink
moved rankHelpers to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dan13ram authored and alalonde committed May 1, 2021
1 parent 69319a5 commit 96f40d9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
25 changes: 9 additions & 16 deletions packages/backend/src/lib/rankHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { computeRank as computeRankUtil, computeRankCap } from '@metafam/utils';

import { PlayerRank_Enum } from './autogen/hasura-sdk';

export const RANKS = [
Expand All @@ -6,27 +8,18 @@ export const RANKS = [
PlayerRank_Enum.Gold,
PlayerRank_Enum.Silver,
PlayerRank_Enum.Bronze,
]
];

export const PLAYERS_PER_RANK = [7, 7, 7, 14, 21];

// A summation of PLAYERS_PER_RANK.
// This is the first index for which players will NOT be ranked, e.g.
// the 56th player will be Bronze, and the 57th player will not be ranked.
export const RANKED_CAP: number = PLAYERS_PER_RANK.reduce((sum, rankCount) => {
return sum + rankCount;
}, 0);
export const RANKED_CAP = computeRankCap(PLAYERS_PER_RANK);

// Computes the rank for the given index. This would be the index corresponding
// to all players ordered by total_xp DESC.
export function computeRank(totalRankIndex: number): PlayerRank_Enum | null {
if (totalRankIndex >= RANKED_CAP) return null;
let indexSum = 0;
for (let i = 0; i < PLAYERS_PER_RANK.length; i++) {
indexSum += PLAYERS_PER_RANK[i];
if (totalRankIndex < indexSum) {
return RANKS[i];
}
}
return null;
return computeRankUtil(
totalRankIndex,
PLAYERS_PER_RANK,
RANKS,
) as PlayerRank_Enum | null;
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * as did from './did';
export * as DiscordUtil from './discordHelpers';
export * as numbers from './numbers';
export * from './promiseHelpers';
export * from './rankHelpers';
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// A summation of usersPerRank
// This is the first index for which users will NOT be ranked
const computeRankCap = (usersPerRank: Array<number>) =>
export const computeRankCap = (usersPerRank: Array<number>) =>
usersPerRank.reduce((sum, rankCount) => {
return sum + rankCount;
}, 0);

// Computes the rank for the given index. This would be the index corresponding
// to all players ordered by total_xp DESC.
// to all users ordered by total_xp DESC.
export function computeRank(
rankIndex: number,
usersPerRank: Array<number>,
Expand Down
2 changes: 1 addition & 1 deletion packages/web/components/Patron/PatronTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Wrap,
WrapItem,
} from '@metafam/ds';
import { computeRank } from '@metafam/utils';
import { MetaLink } from 'components/Link';
import { PlayerContacts } from 'components/Player/PlayerContacts';
import { PlayerTileMemberships } from 'components/Player/PlayerTileMemberships';
Expand All @@ -29,7 +30,6 @@ import {
getPlayerImage,
getPlayerName,
} from 'utils/playerHelpers';
import { computeRank } from 'utils/rankHelpers';

const PATRON_RANKS = [
PlayerRank_Enum.Diamond,
Expand Down

0 comments on commit 96f40d9

Please sign in to comment.