moved rankHelpers to utils

This commit is contained in:
dan13ram
2021-05-01 10:09:40 +05:30
committed by Alec LaLonde
parent 69319a59de
commit 96f40d90a0
4 changed files with 13 additions and 19 deletions

View File

@@ -1,3 +1,5 @@
import { computeRank as computeRankUtil, computeRankCap } from '@metafam/utils';
import { PlayerRank_Enum } from './autogen/hasura-sdk';
export const RANKS = [
@@ -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;
}

View File

@@ -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';

View File

@@ -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>,

View File

@@ -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';
@@ -29,7 +30,6 @@ import {
getPlayerImage,
getPlayerName,
} from 'utils/playerHelpers';
import { computeRank } from 'utils/rankHelpers';
const PATRON_RANKS = [
PlayerRank_Enum.Diamond,