Skip to content

Commit

Permalink
Reorder matches in rounds based on number
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Mar 23, 2023
1 parent 09c5040 commit 20ae0b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/brackets-viewer.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/brackets-viewer.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stage-form-creator.min.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ export function splitBy<
return Object.values(map);
}

/**
* Sorts the objects in the given array by a given key.
*
* @param array The array to sort.
* @param key The key of T.
*/
export function sortBy<
T extends Record<string, unknown>,
K extends keyof T,
U extends Record<K, number>
>(array: U[], key: K): U[] {
return [...array].sort((a, b) => a[key] - b[key]);
}

/**
* Finds the root element
*
Expand Down
29 changes: 19 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './style.scss';
import { Participant, Match, ParticipantResult, Stage, Status } from 'brackets-model';
import { splitBy, getRanking, getOriginAbbreviation, findRoot, completeWithBlankMatches } from './helpers';
import { splitBy, getRanking, getOriginAbbreviation, findRoot, completeWithBlankMatches, sortBy } from './helpers';
import * as dom from './dom';
import * as lang from './lang';
import { Locale } from './lang';
Expand Down Expand Up @@ -160,7 +160,7 @@ export class BracketsViewer {
for (const groupMatches of matchesByGroup) {
const groupId = groupMatches[0].group_id;
const groupContainer = dom.createGroupContainer(groupId, lang.getGroupName(groupNumber++));
const matchesByRound = splitBy(groupMatches, 'round_id');
const matchesByRound = splitBy(groupMatches, 'round_id').map(matches => sortBy(matches, 'number'));

let roundNumber = 1;

Expand Down Expand Up @@ -210,10 +210,14 @@ export class BracketsViewer {
*/
private renderSingleElimination(container: HTMLElement, matchesByGroup: Match[][]): void {
const hasFinal = matchesByGroup[1] !== undefined;
this.renderBracket(container, splitBy(matchesByGroup[0], 'round_id'), lang.getRoundName, 'single-bracket');
const bracketMatches = splitBy(matchesByGroup[0], 'round_id').map(matches => sortBy(matches, 'number'));

if (hasFinal)
this.renderFinal(container, 'consolation_final', matchesByGroup[1]);
this.renderBracket(container, bracketMatches, lang.getRoundName, 'single-bracket');

if (hasFinal) {
const finalMatches = sortBy(matchesByGroup[1], 'number');
this.renderFinal(container, 'consolation_final', finalMatches);
}
}

/**
Expand All @@ -225,14 +229,19 @@ export class BracketsViewer {
private renderDoubleElimination(container: HTMLElement, matchesByGroup: Match[][]): void {
const hasLoserBracket = matchesByGroup[1] !== undefined;
const hasFinal = matchesByGroup[2] !== undefined;
const winnerBracketMatches = splitBy(matchesByGroup[0], 'round_id').map(matches => sortBy(matches, 'number'));

this.renderBracket(container, splitBy(matchesByGroup[0], 'round_id'), lang.getWinnerBracketRoundName, 'winner-bracket', hasFinal);
this.renderBracket(container, winnerBracketMatches, lang.getWinnerBracketRoundName, 'winner-bracket', hasFinal);

if (hasLoserBracket)
this.renderBracket(container, splitBy(matchesByGroup[1], 'round_id'), lang.getLoserBracketRoundName, 'loser-bracket');
if (hasLoserBracket) {
const loserBracketMatches = splitBy(matchesByGroup[1], 'round_id').map(matches => sortBy(matches, 'number'));
this.renderBracket(container, loserBracketMatches, lang.getLoserBracketRoundName, 'loser-bracket');
}

if (hasFinal)
this.renderFinal(container, 'grand_final', matchesByGroup[2]);
if (hasFinal) {
const finalMatches = sortBy(matchesByGroup[2], 'number');
this.renderFinal(container, 'grand_final', finalMatches);
}
}

/**
Expand Down

0 comments on commit 20ae0b1

Please sign in to comment.