2023-01-13 22:27:08 +00:00
|
|
|
import PouchDB from 'pouchdb';
|
2023-01-12 23:02:29 +00:00
|
|
|
import * as R from 'remeda';
|
2023-01-12 19:20:54 +00:00
|
|
|
import Dexie, { liveQuery } from 'dexie';
|
2023-01-13 17:51:02 +00:00
|
|
|
import { writable, derived, get } from 'svelte/store';
|
2023-01-12 23:02:29 +00:00
|
|
|
import { genNextBattle } from './genNextBattle.js';
|
2023-01-13 17:51:02 +00:00
|
|
|
import u from '@yanick/updeep-remeda';
|
2023-01-12 19:20:54 +00:00
|
|
|
|
|
|
|
const seedCampaign = {
|
2023-01-13 17:51:02 +00:00
|
|
|
battles: [],
|
2023-01-12 19:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function genApi(options = {}) {
|
2023-01-14 18:25:46 +00:00
|
|
|
// if (options.local) MyPouch.plugin(pouchMem);
|
2023-01-14 18:42:43 +00:00
|
|
|
if (options.pouch) options.pouch(PouchDB);
|
2023-01-13 22:27:08 +00:00
|
|
|
|
2023-01-13 17:51:02 +00:00
|
|
|
const pouchdb = new PouchDB(
|
|
|
|
'Campaigns',
|
|
|
|
options.local
|
|
|
|
? {
|
|
|
|
adapter: 'memory',
|
|
|
|
}
|
|
|
|
: {},
|
|
|
|
);
|
|
|
|
|
|
|
|
const campaignsCore = writable([]);
|
|
|
|
|
|
|
|
pouchdb
|
|
|
|
.allDocs({ include_docs: true })
|
|
|
|
.then((r) => r.rows.map(({ id, doc }) => [id, doc]))
|
|
|
|
.then(Object.fromEntries)
|
|
|
|
.then(campaignsCore.set);
|
|
|
|
|
|
|
|
pouchdb
|
|
|
|
.changes({ since: 'now', live: true, include_docs: true })
|
|
|
|
.on('change', (change) => {
|
|
|
|
if (change.deleted) {
|
2023-01-13 22:27:08 +00:00
|
|
|
campaignsCore.update(u.updateIn(change.id, u.skip));
|
2023-01-13 17:51:02 +00:00
|
|
|
} else {
|
|
|
|
campaignsCore.update(u.updateIn(change.id, change.doc));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const campaigns = derived(campaignsCore, Object.values);
|
|
|
|
|
|
|
|
const addCampaign = (name) =>
|
|
|
|
pouchdb.post({
|
|
|
|
name,
|
|
|
|
...seedCampaign,
|
|
|
|
battles: [{ id: 1, ...genNextBattle() }],
|
|
|
|
});
|
|
|
|
|
|
|
|
const activeCampaignId = writable(0);
|
|
|
|
|
|
|
|
const setActiveCampaign = activeCampaignId.set;
|
|
|
|
|
|
|
|
const activeCampaign = derived(
|
|
|
|
[campaignsCore, activeCampaignId],
|
|
|
|
([$c, $id]) => $c[$id],
|
|
|
|
);
|
|
|
|
|
|
|
|
const setBattleDifficulty = (battleId, difficulty) => {
|
|
|
|
pouchdb
|
|
|
|
.put(
|
|
|
|
u.updateIn(get(activeCampaign), `battles.${battleId - 1}`, {
|
|
|
|
difficulty,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
};
|
|
|
|
|
|
|
|
const setBattleVerdict = (battleId, status) => {
|
|
|
|
let campaign = get(activeCampaign);
|
|
|
|
|
|
|
|
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));
|
|
|
|
};
|
|
|
|
|
2023-01-13 22:27:08 +00:00
|
|
|
const deleteCampaign = (_id) =>
|
|
|
|
pouchdb.remove(get(campaigns).find(u.matches({ _id })));
|
|
|
|
|
2023-01-13 17:51:02 +00:00
|
|
|
return {
|
|
|
|
campaigns,
|
|
|
|
activeCampaign,
|
|
|
|
event: {
|
2023-01-13 22:27:08 +00:00
|
|
|
deleteCampaign,
|
2023-01-13 17:51:02 +00:00
|
|
|
addCampaign,
|
|
|
|
setActiveCampaign,
|
|
|
|
setBattleVerdict,
|
|
|
|
setBattleDifficulty,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
const activeCampaign = writeable();
|
|
|
|
|
|
|
|
addCampaign: (name) =>
|
|
|
|
storage.campaigns.add({
|
|
|
|
name,
|
|
|
|
...seedCampaign,
|
|
|
|
battles: [{ id: 1, ...genNextBattle() }],
|
|
|
|
}),
|
|
|
|
|
|
|
|
return {
|
|
|
|
campaigns,
|
|
|
|
event: {
|
|
|
|
,
|
|
|
|
addCampaign: (name) =>
|
|
|
|
storage.campaigns.add({
|
|
|
|
name,
|
|
|
|
...seedCampaign,
|
|
|
|
battles: [{ id: 1, ...genNextBattle() }],
|
|
|
|
}),
|
|
|
|
deleteCampaign: (id) => storage.campaigns.delete(id),
|
2023-01-12 21:42:34 +00:00
|
|
|
},
|
2023-01-13 17:51:02 +00:00
|
|
|
};
|
|
|
|
*/
|
2023-01-12 19:20:54 +00:00
|
|
|
}
|