Merge branch 'sky15-second-wave'

This commit is contained in:
Yanick Champoux 2023-01-14 15:00:51 -05:00
commit 6e2bb0ddab
3 changed files with 52 additions and 16 deletions

View File

@ -17,7 +17,7 @@
</header> </header>
<h6> <h6>
Chapter {chapter}, Chapter {chapter},
<span class="battleNbr">battle {chapterBattle}?</span> <span class="battleNbr">battle {chapterBattle}</span>
-- {status} -- {status}
</h6> </h6>
<dl> <dl>
@ -122,6 +122,8 @@
$: character = battle?.character; $: character = battle?.character;
$: city = battle?.city; $: city = battle?.city;
$: console.log(battle);
let chapter = 1 + parseInt(params.battleId / 2); let chapter = 1 + parseInt(params.battleId / 2);
let chapterBattle = let chapterBattle =
params.battleId >= 7 params.battleId >= 7

View File

@ -9,6 +9,27 @@ const seedCampaign = {
battles: [], battles: [],
}; };
export function updateBattle(campaign, battleId, status) {
const battle = campaign.battles[battleId - 1];
if (status === 'lost' && battle.wave == 1) {
return u.updateIn(campaign, `battles.${battleId - 1}`, {
wave: 2,
});
}
campaign = u.updateIn(campaign, `battles.${battleId - 1}`, {
status,
});
campaign = u(campaign, {
battles: (battles) => [
...battles,
u(genNextBattle(battles), {
id: battles.length + 1,
}),
],
});
return campaign;
}
export function genApi(options = {}) { export function genApi(options = {}) {
// if (options.local) MyPouch.plugin(pouchMem); // if (options.local) MyPouch.plugin(pouchMem);
if (options.pouch) options.pouch(PouchDB); if (options.pouch) options.pouch(PouchDB);
@ -69,21 +90,7 @@ export function genApi(options = {}) {
}; };
const setBattleVerdict = (battleId, status) => { const setBattleVerdict = (battleId, status) => {
let campaign = get(activeCampaign); const campaign = updateBattle(get(activeCampaign), battleId, status);
campaign = u.updateIn(campaign, `battles.${battleId - 1}`, {
status,
});
campaign = u(campaign, {
battles: (battles) => [
...battles,
u(genNextBattle(campaign.battles), {
id: battles.length + 1,
}),
],
});
pouchdb.put(campaign).catch((e) => console.error(e)); pouchdb.put(campaign).catch((e) => console.error(e));
}; };

View File

@ -0,0 +1,27 @@
import { test, expect } from 'vitest';
import { updateBattle } from './api.js';
test('lost, wave 1', () => {
const result = updateBattle(
{
battles: [{ id: 1, wave: 1, status: 'ongoing' }],
},
1,
'lost',
);
expect(result).toHaveProperty('battles.0.wave', 2);
expect(result).toHaveProperty('battles.0.status', 'ongoing');
});
test('lost, wave 2', () => {
const result = updateBattle(
{
battles: [{ id: 1, wave: 2, status: 'ongoing' }],
},
1,
'lost',
);
expect(result).toHaveProperty('battles.0.status', 'lost');
});