66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
import { FIGHTER } from "../../constants.js";
|
|
import { getFighterType } from "./fighterStats.js";
|
|
|
|
export function pickUniqueFighters(fighters, count) {
|
|
if (count > fighters.length) {
|
|
throw new Error(
|
|
`Cannot pick ${count} fighters from ${fighters.length} entries.`,
|
|
);
|
|
}
|
|
|
|
return shuffleFighters(fighters).slice(0, count);
|
|
}
|
|
|
|
export function pickFighters(fighters, count) {
|
|
if (fighters.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const picks = [];
|
|
|
|
while (picks.length < count) {
|
|
picks.push(...shuffleFighters(fighters).slice(0, count - picks.length));
|
|
}
|
|
|
|
return picks;
|
|
}
|
|
|
|
export function pickFightersForSetups(fighters, fighterSetups) {
|
|
const eliteCount = fighterSetups.filter(
|
|
(fighterSetup) => fighterSetup.isElite,
|
|
).length;
|
|
const normalCount = fighterSetups.length - eliteCount;
|
|
|
|
const eligibleEliteFighters = fighters.filter((fighter) =>
|
|
FIGHTER.ELITE.TYPE.includes(getFighterType(fighter)),
|
|
);
|
|
|
|
if (eliteCount > 0 && eligibleEliteFighters.length === 0) {
|
|
throw new Error(
|
|
`Cannot create elite fighters without ${FIGHTER.ELITE.TYPE} fighter skins.`,
|
|
);
|
|
}
|
|
|
|
const elitePicks = pickFighters(eligibleEliteFighters, eliteCount);
|
|
const normalPicks = pickFighters(fighters, normalCount);
|
|
let eliteIndex = 0;
|
|
let normalIndex = 0;
|
|
|
|
return fighterSetups.map((fighterSetup) =>
|
|
fighterSetup.isElite
|
|
? elitePicks[eliteIndex++]
|
|
: normalPicks[normalIndex++],
|
|
);
|
|
}
|
|
|
|
function shuffleFighters(fighters) {
|
|
const pool = [...fighters];
|
|
|
|
for (let index = pool.length - 1; index > 0; index -= 1) {
|
|
const randomIndex = Math.floor(Math.random() * (index + 1));
|
|
[pool[index], pool[randomIndex]] = [pool[randomIndex], pool[index]];
|
|
}
|
|
|
|
return pool;
|
|
}
|