Merge branch 'sky19-chapter2-battles'

This commit is contained in:
Yanick Champoux 2023-01-14 16:05:06 -05:00
commit 3284e94ad4
3 changed files with 86 additions and 21 deletions

View File

@ -5,7 +5,7 @@ version: '3'
vars: vars:
GREETING: Hello, World! GREETING: Hello, World!
FILES: FILES:
sh: git diff-ls --diff-filter=d main sh: git diff-ls --diff-filter=d main | grep -v .eslintignore
tasks: tasks:
build: vite build build: vite build
@ -19,7 +19,7 @@ tasks:
lint:fix: lint:fix:
cmds: cmds:
- npx prettier --write {{.CLI_ARGS | default .FILES | catLines }} - npx prettier --write {{.CLI_ARGS | default .FILES | catLines }}
- npx eslint --fix --quiet {{.CLI_ARGS | default .FILES | without '.eslintignore' | catLines }} - npx eslint --fix --quiet {{.CLI_ARGS | default .FILES | catLines }}
integrate: integrate:
cmds: cmds:
- git is-clean - git is-clean

View File

@ -3,26 +3,67 @@ import chapters from './chapters.js';
const pickOne = (choices) => choices[parseInt(Math.random() * choices.length)]; 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).additionalCharacter[0].selection,
);
return {
scenario,
character,
city,
status: 'ongoing',
wave: 1,
additionalCharacters: [{ choices, selection: R.first(choices) }],
};
}
export function genNextBattle(battles = []) { export function genNextBattle(battles = []) {
let chapter = R.clamp(1 + parseInt(battles.length / 2), { min: 1, max: 4 }); let chapter = R.clamp(1 + parseInt(battles.length / 2), { min: 1, max: 4 });
if (chapter === 1) { if (chapter === 1) return genChapter1Battle(battles);
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 }; if (chapter === 2) return genChapter2Battle(battles);
}
} }

View File

@ -1,9 +1,33 @@
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
import { genNextBattle } from './genNextBattle.js'; import { genNextBattle, genChapter2Battle } from './genNextBattle.js';
test('generate for the first chapter', () => { test('generate for the first chapter', () => {
const next = genNextBattle(); const next = genNextBattle();
expect(next).toHaveProperty('city'); expect(next).toHaveProperty('city');
expect(next).toHaveProperty('status', 'ongoing'); expect(next).toHaveProperty('status', 'ongoing');
}); });
test('chapter 2, first battle', () => {
const result = genChapter2Battle([
{ character: 'one' },
{ character: 'two' },
]);
expect(result.additionalCharacters[0].selection).toEqual('one');
expect(result.additionalCharacters[0].choices).toEqual(['one', 'two']);
});
test('chapter 2, second battle', () => {
const result = genChapter2Battle([
{ character: 'one' },
{ character: 'two' },
{
character: 'three',
additionalCharacter: [{ selection: 'two' }],
},
]);
expect(result.additionalCharacters[0].selection).toEqual('one');
expect(result.additionalCharacters[0].choices).toEqual(['one']);
});