Added rank helpers for computing rank, hooked up to account migrator, with bonus tests

This commit is contained in:
Alec LaLonde
2021-02-18 11:05:36 -07:00
parent ba4807f62c
commit 00339464c9
5 changed files with 67 additions and 13 deletions

View File

@@ -35,6 +35,9 @@
"import/order": "off",
"simple-import-sort/sort": "error",
// unary operators are ok
"no-plusplus": "off",
// Using a type system makes it safe enough to spread props
"react/jsx-props-no-spreading": "off",

View File

@@ -12,7 +12,8 @@
"typecheck": "yarn build",
"precommit": "yarn lint-staged",
"generate": "graphql-codegen --config=codegen.yml",
"lintfix": "eslint --fix"
"lintfix": "eslint --fix",
"test": "tsdx test --passWithNoTests"
},
"author": "",
"license": "ISC",

View File

@@ -10,9 +10,9 @@ import {
Player_Constraint,
Player_Insert_Input,
Player_Update_Column,
PlayerRank_Enum,
} from '../../../lib/autogen/hasura-sdk';
import { client } from '../../../lib/hasuraClient';
import { computeRank } from '../../../lib/rankHelpers';
import { AddressBookEntry, SCAccountsData, SCAlias } from './types';
const ACCOUNTS_FILE =
@@ -69,16 +69,6 @@ const parseAlias = (alias: SCAlias) => {
}
};
const RANKS = [
PlayerRank_Enum.Diamond,
PlayerRank_Enum.Platinum,
PlayerRank_Enum.Gold,
PlayerRank_Enum.Silver,
PlayerRank_Enum.Bronze,
];
const NUM_PLAYERS_PER_RANK = 10;
export const migrateSourceCredAccounts = async (
_: Request,
res: Response,
@@ -115,12 +105,14 @@ export const migrateSourceCredAccounts = async (
const addressEntry =
discordId && addressBook.find((adr) => adr.discordId === discordId);
const rank = computeRank(index);
return {
ethereum_address: addressEntry && addressEntry.address,
scIdentityId: a.account.identity.id,
username: a.account.identity.name.toLowerCase(),
totalXp: a.totalCred,
rank: RANKS[Math.floor(index / NUM_PLAYERS_PER_RANK)],
rank,
discordId,
mergedIdentityIds,
Accounts: {

View File

@@ -0,0 +1,32 @@
import { PlayerRank_Enum } from './autogen/hasura-sdk';
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);
// 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;
}

View File

@@ -0,0 +1,26 @@
import { PlayerRank_Enum } from '../../src/lib/autogen/hasura-sdk';
import { computeRank, RANKED_CAP } from '../../src/lib/rankHelpers';
describe('RANKED_CAP', () => {
it('should equal 56', () => {
expect(RANKED_CAP).toBe(56);
});
});
describe('computeRank', () => {
it('index 6 should be diamond', () => {
expect(computeRank(6)).toBe(PlayerRank_Enum.Diamond)
});
it('index 7 should be platinum', () => {
expect(computeRank(7)).toBe(PlayerRank_Enum.Platinum)
});
it('index 21 should be silver', () => {
expect(computeRank(21)).toBe(PlayerRank_Enum.Silver)
});
it('index 55 should be bronze', () => {
expect(computeRank(55)).toBe(PlayerRank_Enum.Bronze)
});
it('index 56 should not be ranked', () => {
expect(computeRank(56)).toBeNull();
});
})