From 8bd3e372bf208a6828afc469379ee82a2e3d3b5c Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 12 Sep 2019 10:42:10 +0200 Subject: [PATCH] Refactor sorting helpers to mlpccg --- src/mlpccg/card.ts | 58 ++++++++++++++++++++++++++++++++++ src/views/DeckBuilder.vue | 66 +++++---------------------------------- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/mlpccg/card.ts b/src/mlpccg/card.ts index d700878..448f11d 100644 --- a/src/mlpccg/card.ts +++ b/src/mlpccg/card.ts @@ -11,3 +11,61 @@ export function createPonyheadURL(cards: Card[]): string { const cardlist = cards.map(c => `${c.ID}x1`); return "https://ponyhead.com/deckbuilder?v1code=" + cardlist.join("-"); } + +export const colorNames = [ + "Loyalty", + "Honesty", + "Laughter", + "Magic", + "Generosity", + "Kindness", + "None" +]; + +export const typeNames = [ + "Mane Character", + "Friend", + "Event", + "Resource", + "Troublemaker", + "Problem" +]; + +export const rarityNames = ["C", "U", "R", "SR", "UR", "RR", "F"]; + +// Trasform string from list to a number that can be used for comparison/sorting +function arrIndex(arr: string[]) { + return function(comp: string) { + const idx = arr.indexOf(comp); + if (idx < 0) { + return arr.length; + } + return idx; + }; +} + +export const elemIndex = arrIndex(colorNames); +export const typeIndex = arrIndex(typeNames); +export const rarityIndex = arrIndex(rarityNames); + +// Convert Element[] to number by scaling elements for fair comparisons +// Example: ["Loyalty", "Kindness"] -> [0, 5] -> [1, 6] -> 16 +export function multiElemStr(elems: string[]): number { + return elems + .map(elemIndex) + .reduce( + (acc, elem, idx, arr) => acc + (elem + 1) * 10 ** (arr.length - idx - 1), + 0 + ); +} + +export function cardLimit(type: string) { + switch (type) { + case "Mane Character": + return 1; + case "Problem": + return 2; + default: + return 3; + } +} diff --git a/src/views/DeckBuilder.vue b/src/views/DeckBuilder.vue index 6e9f38f..ef2b9d3 100644 --- a/src/views/DeckBuilder.vue +++ b/src/views/DeckBuilder.vue @@ -191,56 +191,15 @@ import { getCards, allSets, cardFullName, - createPonyheadURL + createPonyheadURL, + multiElemStr, + typeIndex, + rarityIndex, + colorNames, + typeNames, + cardLimit } from "@/mlpccg"; -const colorNames = [ - "Loyalty", - "Honesty", - "Laughter", - "Magic", - "Generosity", - "Kindness", - "None" -]; - -const typeNames = [ - "Mane Character", - "Friend", - "Event", - "Resource", - "Troublemaker", - "Problem" -]; - -const rarityNames = ["C", "U", "R", "SR", "UR", "RR", "F"]; - -// Trasform string from list to a number that can be used for comparison/sorting -function arrIndex(arr: string[]) { - return function(comp: string) { - const idx = arr.indexOf(comp); - if (idx < 0) { - return arr.length; - } - return idx; - }; -} - -const elemIndex = arrIndex(colorNames); -const typeIndex = arrIndex(typeNames); -const rarityIndex = arrIndex(rarityNames); - -// Convert Element[] to number by scaling elements for fair comparisons -// Example: ["Loyalty", "Kindness"] -> [0, 5] -> [1, 6] -> 16 -function multiElemStr(elems: string[]): number { - return elems - .map(elemIndex) - .reduce( - (acc, elem, idx, arr) => acc + (elem + 1) * 10 ** (arr.length - idx - 1), - 0 - ); -} - // Sort function for sorting cards function sortByColor(a: Card, b: Card) { const typeA = typeIndex(a.Type); @@ -314,17 +273,6 @@ function sortByColor(a: Card, b: Card) { return 0; } -function cardLimit(type: string) { - switch (type) { - case "Mane Character": - return 1; - case "Problem": - return 2; - default: - return 3; - } -} - @Component({ components: { DeckList,