Skip to content

Commit

Permalink
fix: canonical sideboard sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Apr 2, 2024
1 parent ef80c2a commit 6abd1ce
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ function isPositiveNaturalNumber(n: unknown): boolean {
return n > 0;
}

function sort_cards<T extends BaseCard>(cards: T[]): T[] {
return cards.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
function sort_cards<T extends BaseCard>(
cards: T[],
sideboard: boolean = false
): T[] {
if (sideboard) {
return cards.sort((a, b) => a[2] - b[2] || a[0] - b[0]) || 0;
}
return cards.sort((a, b) => a[0] - b[0] || 0);
}

function trisort_cards<T extends BaseCard>(cards: T[]): [T[], T[], T[]] {
Expand Down Expand Up @@ -69,7 +75,7 @@ export function encode(deck: DeckDefinition): string {
const format = deck.format;
const heroes = deck.heroes.slice().sort();
const cards = sort_cards(deck.cards.slice());
const sideboard = sort_cards((deck.sideboardCards || []).slice());
const sideboard = sort_cards((deck.sideboardCards || []).slice(), true);

writer.null();
writer.varint(DECKSTRING_VERSION);
Expand Down Expand Up @@ -165,7 +171,7 @@ export function decode(deckstring: string): DeckDefinition {
]);
}
}
sort_cards(sideboardCards);
sort_cards(sideboardCards, true);
}

return {
Expand Down

0 comments on commit 6abd1ce

Please sign in to comment.