Skip to content

Commit

Permalink
refactor: ♻️ refactor for cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
CourtHive committed Dec 10, 2024
1 parent 1046cb0 commit 2d86d4e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { definedAttributes } from '@Tools/definedAttributes';
import { replaceQualifier } from './replaceQualifier';
import { placeQualifier } from './placeQualifier';

// Constants
import { POLICY_TYPE_PROGRESSION } from '@Constants/policyConstants';
import { SUCCESS } from '@Constants/resultConstants';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getAffectedTargetStructureIds } from './getAffectedTargetStructureIds';
import { isCompletedStructure } from '@Query/drawDefinition/structureActions';

// Constants
import { WIN_RATIO } from '@Constants/drawDefinitionConstants';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,19 @@ import { isOdd } from '@Tools/math';
[[[5, 13], [21, 29]], [[37, 45], [53, 61]]], [[[68, 76], [84, 92]], [[100, 108], [116, 124]]],
*/

interface SeedBlock {
drawPositions: number[];
seedNumbers: number[];
}

export function getBlockSortedRandomDrawPositions({
orderedSortedFirstRoundSeededDrawPositions: strictOrder,
validSeedBlocks,
byesToPlace,
}) {
const drawPositions: number[] = [];

validSeedBlocks.forEach((seedBlock) => {
const leftToPlace = byesToPlace - drawPositions.length;
if (leftToPlace > seedBlock.drawPositions.length) {
drawPositions.push(...seedBlock.drawPositions);
} else {
const nestedDrawPositions = nestArray(chunkArray(seedBlock.drawPositions, 2));

let drawPosition;
let desiredPosition = strictOrder[drawPositions.length];
while ((drawPosition = popFromLargerSide(nestedDrawPositions, desiredPosition))) {
drawPositions.push(drawPosition);
desiredPosition = strictOrder[drawPositions.length];
}
}
});

validSeedBlocks.forEach((seedBlock) => processSeedBlock(seedBlock, byesToPlace, drawPositions, strictOrder));
const blockSortedRandom = drawPositions.map((p) => (Array.isArray(p) ? shuffleArray(p) : p)).flat(Infinity);

if (isOdd(byesToPlace)) {
Expand All @@ -75,15 +65,43 @@ export function getBlockSortedRandomDrawPositions({
return blockSortedRandom;
}

function processSeedBlock(seedBlock: SeedBlock, byesToPlace: number, drawPositions: number[], strictOrder: number[]) {
const leftToPlace = byesToPlace - drawPositions.length;
if (leftToPlace > seedBlock.drawPositions.length) {
drawPositions.push(...seedBlock.drawPositions);
} else {
const nestedDrawPositions = nestArray(chunkArray(seedBlock.drawPositions, 2));
placeDrawPositions(nestedDrawPositions, drawPositions, strictOrder);
}
}

function placeDrawPositions(nestedDrawPositions: any[], drawPositions: number[], strictOrder: number[]) {
let drawPosition: number | undefined;
let desiredPosition = strictOrder[drawPositions.length];
while ((drawPosition = popFromLargerSide(nestedDrawPositions, desiredPosition))) {
drawPositions.push(drawPosition);
desiredPosition = strictOrder[drawPositions.length];
}
}

// desiredPosition is provided by strict seed order bye placement
// when the sides are balanced, side selection is driven by desiredPosition
/* eslint-disable sonarjs/pseudo-random */
function popFromLargerSide(arr, desiredPosition) {
if (Array.isArray(arr) && arr.length !== 2) return arr.pop();
if (!Array.isArray(arr[0])) {
if (arr.includes(desiredPosition)) return arr.indexOf(desiredPosition) ? arr.pop() : arr.shift();
return Math.round(Math.random()) ? arr.pop() : arr.shift();
return handleNonNestedArray(arr, desiredPosition);
}

return handleNestedArray(arr, desiredPosition);
}

function handleNonNestedArray(arr, desiredPosition) {
if (arr.includes(desiredPosition)) return arr.indexOf(desiredPosition) ? arr.pop() : arr.shift();
return Math.round(Math.random()) ? arr.pop() : arr.shift();
}

function handleNestedArray(arr, desiredPosition) {
const side1 = arr[0].flat(Infinity).length;
const side2 = arr[1].flat(Infinity).length;
if (side1 === side2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,8 @@ it('generates expected finishingPositions for qualifying structures', () => {

// now auto place qualifier in MAIN draw
result = tournamentEngine.setMatchUpStatus({
policyDefinitions: { [POLICY_TYPE_PROGRESSION]: { autoPlaceQualifiers: true } },
matchUpId: qualifyingMatchUp.matchUpId,
policyDefinitions: {
[POLICY_TYPE_PROGRESSION]: { autoPlaceQualifiers: true },
},
outcome,
drawId,
});
Expand Down

0 comments on commit 2d86d4e

Please sign in to comment.