141 lines
3.3 KiB
JavaScript
141 lines
3.3 KiB
JavaScript
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(
|
|
(n) => n !== R.last(battles).additionalCharacters[0].selection,
|
|
);
|
|
|
|
return {
|
|
scenario,
|
|
character,
|
|
city,
|
|
status: 'ongoing',
|
|
wave: 1,
|
|
additionalCharacters: [{ choices, selection: R.first(choices) }],
|
|
};
|
|
}
|
|
export function genChapter3Battle(battles) {
|
|
const chapter = 3;
|
|
|
|
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 choices1 = battles.slice(0, 2).map(R.prop('character'));
|
|
let choices2 = battles.slice(2, 4).map(R.prop('character'));
|
|
if (battles.length == 5) {
|
|
choices1 = choices1.filter(
|
|
(n) => n !== R.last(battles).additionalCharacters[0].selection,
|
|
);
|
|
choices2 = choices2.filter(
|
|
(n) => n !== R.last(battles).additionalCharacters[1].selection,
|
|
);
|
|
}
|
|
|
|
return {
|
|
scenario,
|
|
character,
|
|
city,
|
|
status: 'ongoing',
|
|
wave: 1,
|
|
additionalCharacters: [choices1, choices2].map((choices) => ({
|
|
choices,
|
|
selection: R.first(choices),
|
|
})),
|
|
};
|
|
}
|
|
|
|
export function genChapter4Battle(battles) {
|
|
const scenario = 'The Last Battle';
|
|
|
|
const cities = R.difference(
|
|
battles
|
|
.slice(0, 6)
|
|
.filter(({ status }) => status !== 'lost')
|
|
.map(R.prop('city')),
|
|
battles.slice(6).map((b) => b.city?.selection),
|
|
);
|
|
|
|
const characters = battles.slice(0, 6).map(R.prop('character'));
|
|
|
|
return {
|
|
wave: 1,
|
|
scenario,
|
|
city: { choices: cities, selection: R.first(cities) },
|
|
status: 'ongoing',
|
|
additionalCharacters: R.range(0, 3).map((i) => ({
|
|
choices: characters,
|
|
selection: characters[i],
|
|
})),
|
|
};
|
|
}
|
|
|
|
export function genNextBattle(battles = []) {
|
|
let chapter = R.clamp(1 + parseInt(battles.length / 2), { min: 1, max: 4 });
|
|
|
|
if (chapter === 1) return genChapter1Battle(battles);
|
|
|
|
if (chapter === 2) return genChapter2Battle(battles);
|
|
|
|
if (chapter === 3) return genChapter3Battle(battles);
|
|
|
|
return genChapter4Battle(battles);
|
|
}
|