'use strict';
function shuffle(a) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
export const initHeroesFromJson = function(allHeroesJson) {
let abilitiesMap = new Map();
allHeroesJson.abilities.forEach(ability => {
abilitiesMap.set(ability.abilityName, {
name: ability.abilityName,
hook: ability.abilityHook,
isOptionnal: ability.optionnal,
desc: ability['abilityDesc-FR']
});
});
let heroes = [];
let heroUniqueId = 0;
allHeroesJson.heroes.forEach(hero => {
let i = 0;
while (i < hero.nbInDeck) {
heroes.push({
id: heroUniqueId,
name: hero.name,
cost: hero.cost,
power: hero.power,
faction: hero.faction,
ability: abilitiesMap.get(hero.ability),
isDraftable: hero.draftMode,
popularity: hero.popularity
});
heroUniqueId++;
i++;
}
});
return heroes;
};
/**
* Get 12 heroes id by faction in random order.
*
* @param {import("type/game").HeroCard[]} allHeroes - Array of all heroes cards
* @param {import("type/game").Faction} faction - Faction to filter on
* @param {boolean} popularityRule - Are we playing with popularity rule
* @returns {Array<number>} Ids of heroes
*/
export const getHeroesIdsByFaction = function(
allHeroes,
faction,
popularityRule
) {
/** @type {import("type/game").Popularity} */
let popularity = 'ds';
if (popularityRule === true) {
popularity = 'with';
}
let heroIds = allHeroes
.filter(hero => {
return (
hero.faction === faction &&
(hero.popularity === popularity || hero.popularity === 'any')
);
})
.map(hero => hero.id);
return shuffle(heroIds);
};
/**
* Get draft sets to play draft mode
*
* @param {import("type/game").HeroCard[]} allHeroes - Array of all heroes cards
* @param {import("type/game").Popularity} popularityRule - Are we playing with popularity rule
* @returns {Array<Array<number>>} Ids of heroes (4 arrays of 6 cards)
*/
export const getDraftSets = function(allHeroes, popularityRule) {
let popularity = 'without';
if (popularityRule === true) {
popularity = 'with';
}
let heroesDraftable = allHeroes
.filter(hero => {
return (
hero.isDraftable === true &&
(hero.popularity === popularity || hero.popularity === 'any')
);
})
.map(hero => hero.id);
let shuffledHeroes = shuffle(heroesDraftable);
// Return 4 sets of 6 heroes for draft mode
let draftSets = [
[shuffledHeroes.slice(0, 6), shuffledHeroes.slice(6, 12)],
[shuffledHeroes.slice(12, 18), shuffledHeroes.slice(18, 24)]
];
return draftSets;
};
/**
* Shuffle Heroes
*
* @param {import("type/game").HeroCard[]|Array} heroes - Array of heroes
* @returns {Array} Same Array in random order
*/
export const shuffleHeroes = function(heroes) {
return shuffle(heroes);
};