chapter 3

This commit is contained in:
Yanick Champoux 2023-01-15 13:54:16 -05:00
parent 8d1ec92cfd
commit 85ad1bcaad
3 changed files with 133 additions and 1 deletions

View File

@ -0,0 +1,53 @@
<Beer />
<Hst.Story>
<Battle {status} params={{ battleId: 5 }} {api} />
<svelte:fragment slot="controls">
<Hst.Select
bind:value={status}
title="status"
options={Object.fromEntries(statuses.map((s) => [s, s]))}
/>
<Hst.Number bind:value={wave} title="wave" />
</svelte:fragment>
</Hst.Story>
<script>
/** @type any */
export let Hst;
import { logEvent } from 'histoire/client';
import { readable } from 'svelte/store';
import Beer from '../Beer.svelte';
import Battle from '../Battle.svelte';
let status = 'upcoming';
let wave = '1';
const api = {
event: {
setCharacter: (...e) => logEvent('setCharacter', e),
setActiveCampaign: () => {},
},
activeCampaign: readable({
battles: [
null,
null,
null,
null,
{
wave,
status: 'ongoing',
scenario: 's',
city: 'c',
character: 'bob',
additionalCharacters: [
{ choices: ['one', 'two'], selection: 'one' },
{ choices: ['three', 'four'], selection: 'three' },
],
},
],
}),
};
let statuses = ['upcoming', 'prep', 'ongoing', 'won', 'lost'];
</script>

View File

@ -59,6 +59,48 @@ export function genChapter2Battle(battles) {
additionalCharacters: [{ choices, selection: R.first(choices) }], 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 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 });
@ -66,4 +108,6 @@ export function genNextBattle(battles = []) {
if (chapter === 1) return genChapter1Battle(battles); if (chapter === 1) return genChapter1Battle(battles);
if (chapter === 2) return genChapter2Battle(battles); if (chapter === 2) return genChapter2Battle(battles);
if (chapter === 3) return genChapter3Battle(battles);
} }

View File

@ -1,6 +1,10 @@
import { test, expect } from 'vitest'; import { test, expect } from 'vitest';
import { genNextBattle, genChapter2Battle } from './genNextBattle.js'; import {
genNextBattle,
genChapter2Battle,
genChapter3Battle,
} from './genNextBattle.js';
test('generate for the first chapter', () => { test('generate for the first chapter', () => {
const next = genNextBattle(); const next = genNextBattle();
@ -31,3 +35,34 @@ test('chapter 2, second battle', () => {
expect(result.additionalCharacters[0].selection).toEqual('one'); expect(result.additionalCharacters[0].selection).toEqual('one');
expect(result.additionalCharacters[0].choices).toEqual(['one']); expect(result.additionalCharacters[0].choices).toEqual(['one']);
}); });
test('chapter 3, first battle', () => {
const result = genChapter3Battle([
{ character: 'one' },
{ character: 'two' },
{ character: 'three' },
{ character: 'four' },
]);
expect(result.additionalCharacters[0].selection).toEqual('one');
expect(result.additionalCharacters[1].selection).toEqual('three');
});
test('chapter 3, second battle', () => {
const result = genChapter3Battle([
{ character: 'one' },
{ character: 'two' },
{ character: 'three' },
{ character: 'four' },
{
character: 'five',
additionalCharacters: [
{ selection: 'one' },
{ selection: 'three' },
],
},
]);
expect(result.additionalCharacters[0].selection).toEqual('two');
expect(result.additionalCharacters[1].selection).toEqual('four');
});