Refactor sorting helpers to mlpccg
This commit is contained in:
parent
e415da87c3
commit
8bd3e372bf
2 changed files with 65 additions and 59 deletions
|
@ -11,3 +11,61 @@ export function createPonyheadURL(cards: Card[]): string {
|
||||||
const cardlist = cards.map(c => `${c.ID}x1`);
|
const cardlist = cards.map(c => `${c.ID}x1`);
|
||||||
return "https://ponyhead.com/deckbuilder?v1code=" + cardlist.join("-");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -191,56 +191,15 @@ import {
|
||||||
getCards,
|
getCards,
|
||||||
allSets,
|
allSets,
|
||||||
cardFullName,
|
cardFullName,
|
||||||
createPonyheadURL
|
createPonyheadURL,
|
||||||
|
multiElemStr,
|
||||||
|
typeIndex,
|
||||||
|
rarityIndex,
|
||||||
|
colorNames,
|
||||||
|
typeNames,
|
||||||
|
cardLimit
|
||||||
} from "@/mlpccg";
|
} 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
|
// Sort function for sorting cards
|
||||||
function sortByColor(a: Card, b: Card) {
|
function sortByColor(a: Card, b: Card) {
|
||||||
const typeA = typeIndex(a.Type);
|
const typeA = typeIndex(a.Type);
|
||||||
|
@ -314,17 +273,6 @@ function sortByColor(a: Card, b: Card) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cardLimit(type: string) {
|
|
||||||
switch (type) {
|
|
||||||
case "Mane Character":
|
|
||||||
return 1;
|
|
||||||
case "Problem":
|
|
||||||
return 2;
|
|
||||||
default:
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
components: {
|
components: {
|
||||||
DeckList,
|
DeckList,
|
||||||
|
|
Loading…
Reference in a new issue