V1.4.0: silver and copper, with automatic change
- A character's purse holds gold, silver and copper as separate coins: 1 gold = 10 silver = 100 copper. The money log records each entry's coin. - Spending and giving are paid from the whole purse: coins of the kind named go first, then the nearest bigger coin is broken (change comes back in as few coins as possible), then smaller coins are counted out. A payment is refused only when the whole purse isn't worth enough. - Exchange converts one coin into another at the fixed worth. - Encounter rewards and gifts between characters can be in any coin; the party overview shows the whole purse. - All packages at version 1.4.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1972,3 +1972,146 @@ describe('temporary hit points', () => {
|
||||
expect([now.currentHp, now.tempHp]).toEqual([0, 0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('silver and copper', () => {
|
||||
itIfDb('are kept as separate coins, exchanged at fixed worth, given and rewarded', async () => {
|
||||
const { dmAuth, playerAuth, campaign } = await setupCampaign('coins');
|
||||
const c = `/campaigns/${campaign.id}`;
|
||||
await request(app).patch(c).set(dmAuth).send({ startingGold: 5 });
|
||||
const heroId = await buildCharacter(campaign.id, playerAuth, 'Purse');
|
||||
const friendId = await buildCharacter(campaign.id, dmAuth, 'Friend');
|
||||
const ch = `/characters/${heroId}`;
|
||||
const purse = async (id = heroId, auth = playerAuth) => {
|
||||
const { gold, silver, copper } = (await request(app).get(`/characters/${id}`).set(auth)).body.character;
|
||||
return [gold, silver, copper];
|
||||
};
|
||||
const money = (body) => request(app).post(`${ch}/gold`).set(playerAuth).send(body);
|
||||
const exchange = (body) => request(app).post(`${ch}/gold/exchange`).set(playerAuth).send(body);
|
||||
|
||||
expect(await purse()).toEqual([5, 0, 0]);
|
||||
|
||||
// Each coin has its own balance; an amount with no coin named is gold.
|
||||
expect((await money({ amount: 7, currency: 'SILVER', reason: 'Sold a pelt' })).status).toBe(201);
|
||||
expect((await money({ amount: 3, currency: 'COPPER' })).body.copper).toBe(3);
|
||||
expect((await money({ amount: 1 })).body.gold).toBe(6);
|
||||
expect(await purse()).toEqual([6, 7, 3]);
|
||||
expect((await money({ amount: 1, currency: 'PLATINUM' })).status).toBe(400);
|
||||
|
||||
// A spend the whole purse can't cover is refused and moves nothing.
|
||||
const short = await money({ amount: -1000, currency: 'SILVER' });
|
||||
expect(short.status).toBe(400);
|
||||
expect(short.body.error).toMatch(/Not enough money/);
|
||||
expect(await purse()).toEqual([6, 7, 3]);
|
||||
expect((await request(app).patch(ch).set(dmAuth).send({ silver: 99 })).status).toBe(400);
|
||||
|
||||
// 1 gold = 10 silver = 100 copper.
|
||||
expect((await exchange({ from: 'GOLD', to: 'SILVER', amount: 1 })).body.received).toBe(10);
|
||||
expect(await purse()).toEqual([5, 17, 3]);
|
||||
expect((await exchange({ from: 'GOLD', to: 'COPPER', amount: 1 })).body.received).toBe(100);
|
||||
expect((await exchange({ from: 'SILVER', to: 'COPPER', amount: 1 })).body.received).toBe(10);
|
||||
expect(await purse()).toEqual([4, 16, 113]);
|
||||
expect((await exchange({ from: 'COPPER', to: 'SILVER', amount: 20 })).body.received).toBe(2);
|
||||
expect((await exchange({ from: 'SILVER', to: 'GOLD', amount: 10 })).body.received).toBe(1);
|
||||
expect((await exchange({ from: 'COPPER', to: 'SILVER', amount: 90 })).body.received).toBe(9);
|
||||
expect(await purse()).toEqual([5, 17, 3]);
|
||||
|
||||
// It has to come to whole coins, be affordable, and be between two different coins.
|
||||
const uneven = await exchange({ from: 'SILVER', to: 'GOLD', amount: 5 });
|
||||
expect(uneven.status).toBe(400);
|
||||
expect(uneven.body.error).toMatch(/multiples of 10/);
|
||||
expect((await exchange({ from: 'COPPER', to: 'SILVER', amount: 1 })).status).toBe(400);
|
||||
expect((await exchange({ from: 'COPPER', to: 'GOLD', amount: 50 })).body.error).toMatch(/multiples of 100/);
|
||||
expect((await exchange({ from: 'SILVER', to: 'COPPER', amount: 18 })).status).toBe(400);
|
||||
expect((await exchange({ from: 'GOLD', to: 'GOLD', amount: 1 })).status).toBe(400);
|
||||
expect((await request(app).post(`/characters/${friendId}/gold/exchange`).set(playerAuth).send({ from: 'GOLD', to: 'SILVER', amount: 1 })).status).toBe(403);
|
||||
expect(await purse()).toEqual([5, 17, 3]);
|
||||
|
||||
// The ledger says which coin every entry is in.
|
||||
const ledger = (await request(app).get(`${ch}/gold`).set(playerAuth)).body;
|
||||
expect([ledger.gold, ledger.silver, ledger.copper]).toEqual([5, 17, 3]);
|
||||
expect(ledger.transactions[0]).toMatchObject({ currency: 'SILVER', amount: 9, balanceAfter: 17, reason: 'Exchanged from 90 copper' });
|
||||
expect(ledger.transactions.find((t) => t.reason === 'Sold a pelt')).toMatchObject({ currency: 'SILVER', amount: 7, balanceAfter: 7 });
|
||||
|
||||
// Giving and rewards work per coin.
|
||||
expect((await request(app).post(`${ch}/give`).set(playerAuth).send({ kind: 'gold', currency: 'SILVER', amount: 3, toCharacterId: friendId })).status).toBe(200);
|
||||
expect((await request(app).post(`${ch}/give`).set(playerAuth).send({ kind: 'gold', currency: 'COPPER', amount: 5000, toCharacterId: friendId })).status).toBe(400);
|
||||
expect(await purse()).toEqual([5, 14, 3]);
|
||||
expect(await purse(friendId, dmAuth)).toEqual([5, 3, 0]);
|
||||
|
||||
const e = `/encounters/${(await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Loose change' })).body.encounter.id}`;
|
||||
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId });
|
||||
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: friendId });
|
||||
const reward = (await request(app).post(`${e}/rewards`).set(dmAuth).send({ type: 'GOLD', gold: 9, currency: 'COPPER' })).body.reward;
|
||||
expect(reward).toMatchObject({ gold: 9, currency: 'COPPER' });
|
||||
await request(app).post(`${e}/rewards/${reward.id}/award`).set(dmAuth).send({ split: true });
|
||||
const split = [(await purse())[2] - 3, (await purse(friendId, dmAuth))[2]].sort();
|
||||
expect(split).toEqual([4, 5]); // 9 copper between two
|
||||
|
||||
// Another player still sees none of it.
|
||||
const outsiderView = (await request(app).get(`/characters/${friendId}`).set(playerAuth)).body.character;
|
||||
expect([outsiderView.gold, outsiderView.silver, outsiderView.copper]).toEqual([null, null, null]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('paying with change', () => {
|
||||
itIfDb('a spend or a gift is paid from the whole purse: bigger coins are broken, smaller ones counted out', async () => {
|
||||
const { dmAuth, playerAuth, campaign } = await setupCampaign('change');
|
||||
await request(app).patch(`/campaigns/${campaign.id}`).set(dmAuth).send({ startingGold: 5 });
|
||||
const heroId = await buildCharacter(campaign.id, playerAuth, 'Spender');
|
||||
const friendId = await buildCharacter(campaign.id, dmAuth, 'Friend');
|
||||
const ch = `/characters/${heroId}`;
|
||||
const purse = async (id = heroId, auth = playerAuth) => {
|
||||
const { gold, silver, copper } = (await request(app).get(`/characters/${id}`).set(auth)).body.character;
|
||||
return [gold, silver, copper];
|
||||
};
|
||||
const money = (body) => request(app).post(`${ch}/gold`).set(playerAuth).send(body);
|
||||
const newest = async (count) =>
|
||||
(await request(app).get(`${ch}/gold`).set(playerAuth)).body.transactions.slice(0, count).map((t) => [t.currency, t.amount, t.balanceAfter, t.reason]);
|
||||
|
||||
// 3 silver from a purse of 5 gold: one gold is broken, 7 silver comes back.
|
||||
const boots = await money({ amount: -3, currency: 'SILVER', reason: 'New boots' });
|
||||
expect(boots.status).toBe(201);
|
||||
expect([boots.body.gold, boots.body.silver, boots.body.copper]).toEqual([4, 7, 0]);
|
||||
expect(await purse()).toEqual([4, 7, 0]);
|
||||
expect((await newest(2)).sort()).toEqual([
|
||||
['GOLD', -1, 4, 'New boots (paid 3 silver, with change)'],
|
||||
['SILVER', 7, 7, 'New boots (paid 3 silver, with change)'],
|
||||
]);
|
||||
|
||||
// 5 copper: the nearest bigger coin is a silver, so that is what breaks.
|
||||
expect((await money({ amount: -5, currency: 'COPPER' })).status).toBe(201);
|
||||
expect(await purse()).toEqual([4, 6, 5]);
|
||||
|
||||
// Enough of the coin named: nothing else moves, and the entry reads as usual.
|
||||
await money({ amount: -2, reason: 'Horse feed' });
|
||||
expect(await purse()).toEqual([2, 6, 5]);
|
||||
expect((await newest(1))[0]).toEqual(['GOLD', -2, 2, 'Horse feed']);
|
||||
|
||||
// More than the whole purse is worth (2.65 gold): refused, nothing moves.
|
||||
const tooMuch = await money({ amount: -3 });
|
||||
expect(tooMuch.status).toBe(400);
|
||||
expect(tooMuch.body.error).toMatch(/Not enough money/);
|
||||
expect(tooMuch.body.error).toMatch(/2\.65 gold/);
|
||||
expect(await purse()).toEqual([2, 6, 5]);
|
||||
|
||||
// No bigger coin to break: smaller ones are counted out, biggest first.
|
||||
await money({ amount: -2 });
|
||||
await money({ amount: 40, currency: 'COPPER' });
|
||||
expect(await purse()).toEqual([0, 6, 45]);
|
||||
expect((await money({ amount: -1, reason: 'Toll' })).status).toBe(201);
|
||||
expect(await purse()).toEqual([0, 0, 5]);
|
||||
|
||||
// Giving works the same way for the giver; the recipient gets the coins named.
|
||||
await money({ amount: 1 });
|
||||
expect((await request(app).post(`${ch}/give`).set(playerAuth).send({ kind: 'gold', currency: 'SILVER', amount: 4, toCharacterId: friendId })).status).toBe(200);
|
||||
expect(await purse()).toEqual([0, 6, 5]);
|
||||
expect(await purse(friendId, dmAuth)).toEqual([5, 4, 0]);
|
||||
const beyond = await request(app).post(`${ch}/give`).set(playerAuth).send({ kind: 'gold', amount: 1, toCharacterId: friendId });
|
||||
expect(beyond.status).toBe(400);
|
||||
expect(beyond.body.error).toMatch(/Not enough money/);
|
||||
|
||||
// Exchanging is the exception: it moves exactly the coins named.
|
||||
expect((await request(app).post(`${ch}/gold/exchange`).set(playerAuth).send({ from: 'SILVER', to: 'COPPER', amount: 7 })).status).toBe(400);
|
||||
expect(await purse()).toEqual([0, 6, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user