made computeRank more generic

This commit is contained in:
dan13ram
2021-05-01 09:52:25 +05:30
committed by Alec LaLonde
parent c3248aaad3
commit 69319a59de
2 changed files with 33 additions and 26 deletions

View File

@@ -17,7 +17,11 @@ import { PlayerContacts } from 'components/Player/PlayerContacts';
import { PlayerTileMemberships } from 'components/Player/PlayerTileMemberships';
import { SkillsTags } from 'components/Skills';
import { utils } from 'ethers';
import { PlayerFragmentFragment, Skill } from 'graphql/autogen/types';
import {
PlayerFragmentFragment,
PlayerRank_Enum,
Skill,
} from 'graphql/autogen/types';
import { Patron } from 'graphql/types';
import React from 'react';
import {
@@ -27,6 +31,16 @@ import {
} from 'utils/playerHelpers';
import { computeRank } from 'utils/rankHelpers';
const PATRON_RANKS = [
PlayerRank_Enum.Diamond,
PlayerRank_Enum.Platinum,
PlayerRank_Enum.Gold,
PlayerRank_Enum.Silver,
PlayerRank_Enum.Bronze,
];
const PATRONS_PER_RANK = [7, 7, 7, 14, 21];
type Props = {
patron: Patron;
index: number;
@@ -34,7 +48,7 @@ type Props = {
export const PatronTile: React.FC<Props> = ({ index, patron }) => {
const player = patron as PlayerFragmentFragment;
const patronRank = computeRank(index);
const patronRank = computeRank(index, PATRONS_PER_RANK, PATRON_RANKS);
return (
<MetaTile>
<Box

View File

@@ -1,31 +1,24 @@
import { PlayerRank_Enum } from 'graphql/autogen/types';
export const RANKS = [
PlayerRank_Enum.Diamond,
PlayerRank_Enum.Platinum,
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);
// A summation of usersPerRank
// This is the first index for which users will NOT be ranked
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.
export function computeRank(totalRankIndex: number): PlayerRank_Enum | null {
if (totalRankIndex >= RANKED_CAP) return null;
export function computeRank(
rankIndex: number,
usersPerRank: Array<number>,
ranks: Array<string>,
): string | null {
const rankCap = computeRankCap(usersPerRank);
if (rankIndex >= rankCap) 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];
for (let i = 0; i < usersPerRank.length; i++) {
indexSum += usersPerRank[i];
if (rankIndex < indexSum && i < ranks.length) {
return ranks[i];
}
}
return null;