under-falling-skies/src/lib/store/genNextBattle.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-01-12 23:02:29 +00:00
import * as R from 'remeda';
import chapters from './chapters.js';
const pickOne = (choices) => choices[parseInt(Math.random() * choices.length)];
function genChapter1Battle(battles) {
const chapter = 1;
const scenario = pickOne(
chapters[chapter - 1].scenarios.filter(
(s) => !battles.map(R.prop('scenario')).includes(s),
),
);
const character = pickOne(
chapters[chapter - 1].characters.filter(
(s) => !battles.map(R.prop('character')).includes(s),
),
);
const city = pickOne(
chapters[chapter - 1].cities.filter(
(s) => !battles.map(R.prop('city')).includes(s),
),
);
return { scenario, character, city, status: 'ongoing', wave: 1 };
}
export function genChapter2Battle(battles) {
const chapter = 2;
const scenario = pickOne(
chapters[chapter - 1].scenarios.filter(
(s) => !battles.map(R.prop('scenario')).includes(s),
),
);
const character = pickOne(
chapters[chapter - 1].characters.filter(
(s) => !battles.map(R.prop('character')).includes(s),
),
);
const city = pickOne(
chapters[chapter - 1].cities.filter(
(s) => !battles.map(R.prop('city')).includes(s),
),
);
let choices = battles.slice(0, 2).map(R.prop('character'));
if (battles.length == 3)
choices = choices.filter(
2023-01-14 22:11:54 +00:00
(n) => n !== R.last(battles).additionalCharacters[0].selection,
);
return {
scenario,
character,
city,
status: 'ongoing',
wave: 1,
additionalCharacters: [{ choices, selection: R.first(choices) }],
};
}
2023-01-12 23:02:29 +00:00
export function genNextBattle(battles = []) {
2023-01-14 18:25:46 +00:00
let chapter = R.clamp(1 + parseInt(battles.length / 2), { min: 1, max: 4 });
2023-01-12 23:02:29 +00:00
if (chapter === 1) return genChapter1Battle(battles);
2023-01-12 23:02:29 +00:00
if (chapter === 2) return genChapter2Battle(battles);
2023-01-12 23:02:29 +00:00
}