diff --git a/Taskfile.yaml b/Taskfile.yaml index 61dcdc8..27293c5 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -5,7 +5,7 @@ version: '3' vars: GREETING: Hello, World! FILES: - sh: git diff-ls --diff-filter=d main + sh: git diff-ls --diff-filter=d main | grep -v .eslintignore tasks: build: vite build @@ -19,7 +19,7 @@ tasks: lint:fix: cmds: - 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: cmds: - git is-clean diff --git a/src/lib/store/genNextBattle.js b/src/lib/store/genNextBattle.js index 0f93f28..462add0 100644 --- a/src/lib/store/genNextBattle.js +++ b/src/lib/store/genNextBattle.js @@ -3,26 +3,67 @@ 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).additionalCharacter[0].selection, + ); + + return { + scenario, + character, + city, + status: 'ongoing', + wave: 1, + additionalCharacters: [{ choices, selection: R.first(choices) }], + }; +} + export function genNextBattle(battles = []) { let chapter = R.clamp(1 + parseInt(battles.length / 2), { min: 1, max: 4 }); - if (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), - ), - ); + if (chapter === 1) return genChapter1Battle(battles); - return { scenario, character, city, status: 'ongoing', wave: 1 }; - } + if (chapter === 2) return genChapter2Battle(battles); } diff --git a/src/lib/store/genNextBattle.test.js b/src/lib/store/genNextBattle.test.js index d2416cb..f42ad8d 100644 --- a/src/lib/store/genNextBattle.test.js +++ b/src/lib/store/genNextBattle.test.js @@ -1,9 +1,33 @@ import { test, expect } from 'vitest'; -import { genNextBattle } from './genNextBattle.js'; +import { genNextBattle, genChapter2Battle } from './genNextBattle.js'; test('generate for the first chapter', () => { const next = genNextBattle(); expect(next).toHaveProperty('city'); 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']); +});