V1.3.0: Hollowcrown seed, player visibility rules, initiative rolls, number limits, temp HP

- Hollowcrown: The Rift Between Worlds as its own seed (npm run
  seed:hollowcrown), built from the campaign book: pools, 58 monsters,
  57 NPCs, 28 locations, 72 encounters with rewards, and DM notes. Shared
  seeding logic moved to prisma/seedCampaign.js.
- What players can see: only encounters their own character is in; only
  the pool entries their character holds (plus starting options while
  building); only locations their character has been to; no invite code;
  no gold, inventory or money log of other players' characters. Socket
  events carry ids only and campaign rooms are members-only.
- Encounters have a location; while one is in progress its characters are
  there, and visits are remembered.
- Campaign setting to show players a health bar only for monsters and NPCs.
- Initiative is entered as the d20 roll (1-20) and the bonus is added;
  players can enter their own character's roll.
- Number fields enforce their limits when typing, and the API enforces
  the same limits.
- Temp HP shows on HP bars and absorbs damage before real HP.
- All packages at version 1.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 10:53:48 +02:00
co-authored by Claude Opus 5.5
parent 2637a510df
commit 932e755895
38 changed files with 5841 additions and 409 deletions
+334 -22
View File
@@ -439,8 +439,8 @@ describe('who can change what on a character', () => {
expect(itemRes.status).toBe(201);
const itemId = itemRes.body.item.id;
// Players can see the pool but not add to it or help themselves from it.
expect((await request(app).get(`/campaigns/${campaign.id}/items`).set(playerAuth)).body.items).toHaveLength(1);
// Players can't see what is in the pool, add to it, or help themselves from it.
expect((await request(app).get(`/campaigns/${campaign.id}/items`).set(playerAuth)).body.items).toHaveLength(0);
expect((await request(app).post(`/campaigns/${campaign.id}/items`).set(playerAuth).send({ name: 'Crown' })).status).toBe(403);
expect((await request(app).post(`/characters/${characterId}/weapons`).set(playerAuth).send({ weaponId })).status).toBe(403);
expect((await request(app).post(`/characters/${characterId}/inventory`).set(playerAuth).send({ itemId })).status).toBe(403);
@@ -598,7 +598,7 @@ describe('hit points', () => {
const maxHp = (await request(app).get(`/characters/${characterId}`).set(playerAuth)).body.character.sheet.hp.max;
const encounterId = (await request(app).post(`/campaigns/${campaign.id}/encounters`).set(dmAuth).send({ name: 'Brawl' })).body.encounter.id;
const added = await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId, initiative: 12 });
const added = await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId, initiativeRoll: 12 });
const participantId = added.body.participant.id;
const combatant = async () => (await request(app).get(`/encounters/${encounterId}`).set(playerAuth)).body.encounter.participants[0];
const sheetHp = async () => (await request(app).get(`/characters/${characterId}`).set(playerAuth)).body.character.currentHp;
@@ -629,7 +629,7 @@ describe('hit points', () => {
// A combatant can only be edited through its own encounter.
const otherEncounterId = (await request(app).post(`/campaigns/${campaign.id}/encounters`).set(dmAuth).send({ name: 'Elsewhere' })).body.encounter.id;
const wrong = await request(app).patch(`/encounters/${otherEncounterId}/participants/${participantId}`).set(dmAuth).send({ initiative: 1 });
const wrong = await request(app).patch(`/encounters/${otherEncounterId}/participants/${participantId}`).set(dmAuth).send({ initiativeRoll: 1 });
expect(wrong.status).toBe(404);
});
});
@@ -654,7 +654,7 @@ describe('money tracker', () => {
expect((await gold(playerAuth, { amount: 0 })).status).toBe(400);
expect((await gold(playerAuth, { amount: 1.5 })).status).toBe(400);
// Another player can read the ledger but not write to it.
// Another player can neither write to the ledger nor read it.
const stranger = await request(app)
.post('/auth/register')
.send({ email: uniqueEmail('gold-other'), password: 'password123', displayName: 'Other' });
@@ -663,7 +663,9 @@ describe('money tracker', () => {
await request(app).post(`/campaigns/join/${campaign.inviteCode}`).set(strangerAuth);
expect((await gold(strangerAuth, { amount: 5 })).status).toBe(403);
const ledger = await request(app).get(`/characters/${characterId}/gold`).set(strangerAuth);
expect((await request(app).get(`/characters/${characterId}/gold`).set(strangerAuth)).status).toBe(403);
const ledger = await request(app).get(`/characters/${characterId}/gold`).set(playerAuth);
expect(ledger.status).toBe(200);
expect(ledger.body.gold).toBe(45);
expect(ledger.body.transactions.map((t) => [t.amount, t.balanceAfter, t.reason, t.createdBy.displayName])).toEqual([
@@ -890,13 +892,17 @@ describe('encounters', () => {
}
itIfDb('have a mode, a public description and DM-only notes', async () => {
const { dmAuth, playerAuth, c } = await scene('encmode');
const { dmAuth, playerAuth, c, heroId } = await scene('encmode');
const quest = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'The Missing Miller', mode: 'QUEST', description: 'Find the miller.' })).body.encounter;
expect(quest.mode).toBe('QUEST');
const battle = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Ambush' })).body.encounter;
expect(battle.mode).toBe('BATTLE');
const e = `/encounters/${quest.id}`;
// A player whose character isn't in a scene can't see it exists.
expect((await request(app).get(e).set(playerAuth)).status).toBe(404);
expect((await request(app).get(`${c}/encounters`).set(playerAuth)).body.encounters).toEqual([]);
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId });
expect((await request(app).patch(e).set(playerAuth).send({ mode: 'BATTLE' })).status).toBe(403);
const patched = await request(app).patch(e).set(dmAuth).send({ dmNotes: 'The miller is the werewolf.' });
expect(patched.body.encounter.dmNotes).toBe('The miller is the werewolf.');
@@ -905,14 +911,20 @@ describe('encounters', () => {
expect(playerView.description).toBe('Find the miller.');
expect(playerView.dmNotes).toBeUndefined();
const playerList = (await request(app).get(`${c}/encounters`).set(playerAuth)).body.encounters;
expect(playerList.map((x) => [x.name, x.mode]).sort()).toEqual([['Ambush', 'BATTLE'], ['The Missing Miller', 'QUEST']]);
expect(playerList.map((x) => [x.name, x.mode])).toEqual([['The Missing Miller', 'QUEST']]);
expect((await request(app).get(`/encounters/${battle.id}`).set(playerAuth)).status).toBe(404);
expect(playerList.every((x) => x.dmNotes === undefined)).toBe(true);
const dmList = (await request(app).get(`${c}/encounters`).set(dmAuth)).body.encounters;
expect(dmList.map((x) => x.name).sort()).toEqual(['Ambush', 'The Missing Miller']);
expect(dmList.find((x) => x.id === quest.id).dmNotes).toBe('The miller is the werewolf.');
// Taken out of the scene, the player loses sight of it again.
const heroRow = (await request(app).get(e).set(dmAuth)).body.encounter.participants.find((p) => p.characterId === heroId);
await request(app).delete(`${e}/participants/${heroRow.id}`).set(dmAuth);
expect((await request(app).get(e).set(playerAuth)).status).toBe(404);
});
itIfDb('NPCs are DM-managed and join encounters without leaking the DM\'s notes or stats', async () => {
const { dmAuth, playerAuth, c } = await scene('npcs');
const { dmAuth, playerAuth, c, heroId } = await scene('npcs');
const other = await setupCampaign('npcs-other');
expect((await request(app).post(`${c}/npcs`).set(playerAuth).send({ name: 'Nope' })).status).toBe(403);
@@ -942,12 +954,13 @@ describe('encounters', () => {
expect(dmView['Mayor Orla'].npc).toMatchObject({ role: 'Mayor', disposition: 'HOSTILE', dmNotes: 'Secretly bankrupt.', ac: 11 });
expect(dmView['Old Hermit']).toMatchObject({ kind: 'NPC', maxHp: 0 }); // no combat stats
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId });
const playerView = byName((await request(app).get(e).set(playerAuth)).body.encounter);
expect(playerView['Mayor Orla'].npc).toEqual({ id: mayor.id, name: 'Mayor Orla', role: 'Mayor', disposition: 'HOSTILE', description: 'Tired but kind.' });
// Deleting an NPC takes it out of the scenes it was in.
expect((await request(app).delete(`${c}/npcs/${hermit.id}`).set(dmAuth)).status).toBe(204);
expect(Object.keys(byName((await request(app).get(e).set(dmAuth)).body.encounter))).toEqual(['Mayor Orla']);
expect(Object.keys(byName((await request(app).get(e).set(dmAuth)).body.encounter)).sort()).toEqual(['Aria', 'Mayor Orla']);
});
itIfDb('show the DM full monster stat blocks and everyone a character\'s headline numbers', async () => {
@@ -955,8 +968,8 @@ describe('encounters', () => {
const goblin = (await request(app).post(`${c}/monsters/from-library`).set(dmAuth).send({ key: 'goblin' })).body.monster;
const encounterId = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Cave' })).body.encounter.id;
const e = `/encounters/${encounterId}`;
await request(app).post(`${e}/participants`).set(dmAuth).send({ monsterId: goblin.id, initiative: 5 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId, initiative: 15 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ monsterId: goblin.id, initiativeRoll: 5 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId, initiativeRoll: 20 });
const [hero, monster] = (await request(app).get(e).set(dmAuth)).body.encounter.participants;
expect(hero.kind).toBe('CHARACTER');
@@ -980,9 +993,9 @@ describe('encounters', () => {
expect((await request(app).post(`${e}/next-turn`).set(dmAuth)).status).toBe(400); // nobody to take a turn
const add = async (body) => (await request(app).post(`${e}/participants`).set(dmAuth).send(body)).body.participant.id;
const slow = await add({ monsterId: goblin.id, initiative: 3 });
const fast = await add({ characterId: heroId, initiative: 18 });
const mid = await add({ characterId: allyId, initiative: 10 });
const slow = await add({ monsterId: goblin.id, initiativeRoll: 1 });
const fast = await add({ characterId: heroId, initiativeRoll: 20 });
const mid = await add({ characterId: allyId, initiativeRoll: 10 });
const state = (res) => [res.body.encounter.activeParticipantId, res.body.encounter.currentRound, res.body.encounter.status];
expect((await request(app).post(`${e}/next-turn`).set(playerAuth)).status).toBe(403);
@@ -1009,8 +1022,8 @@ describe('encounters', () => {
const encounterId = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Dragon Hoard' })).body.encounter.id;
const e = `/encounters/${encounterId}`;
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId, initiative: 12 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: allyId, initiative: 8 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId, initiativeRoll: 20 });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: allyId, initiativeRoll: 1 });
const add = (body, auth = dmAuth) => request(app).post(`${e}/rewards`).set(auth).send(body);
expect((await add({ type: 'GOLD', gold: 5 }, playerAuth)).status).toBe(403);
@@ -1350,7 +1363,7 @@ describe('locations', () => {
const listRes = await request(app).get(`/campaigns/${campaign.id}/locations`).set(playerAuth);
expect(listRes.status).toBe(200);
expect(listRes.body.locations.map((l) => l.id)).toContain(locationId);
expect(listRes.body.locations).toEqual([]); // nowhere this player's character has been
const charRes = await request(app)
.post(`/campaigns/${campaign.id}/characters`)
@@ -1375,7 +1388,8 @@ describe('default content library', () => {
itIfDb('lists library entries and copies one into the campaign', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('library');
const listRes = await request(app).get(`/campaigns/${campaign.id}/library`).set(playerAuth);
expect((await request(app).get(`/campaigns/${campaign.id}/library`).set(playerAuth)).status).toBe(403);
const listRes = await request(app).get(`/campaigns/${campaign.id}/library`).set(dmAuth);
expect(listRes.status).toBe(200);
expect(listRes.body.weapons.length).toBeGreaterThan(20);
expect(listRes.body.spells.length).toBeGreaterThan(50);
@@ -1468,7 +1482,7 @@ describe('DM-only campaign notes', () => {
});
describe('encounter combat info', () => {
itIfDb('shows a character combatant\'s weapons to every campaign member, not just the DM', async () => {
itIfDb('shows a character combatant\'s weapons to the other players in the encounter, not just the DM', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('encountercombat');
const weaponRes = await request(app)
@@ -1511,7 +1525,9 @@ describe('encounter combat info', () => {
const encounterRes = await request(app).post(`/campaigns/${campaign.id}/encounters`).set(dmAuth).send({ name: 'Ambush' });
const encounterId = encounterRes.body.encounter.id;
await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId, initiative: 10 });
await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId, initiativeRoll: 10 });
const watcherId = await buildCharacter(campaign.id, playerAuth, 'Watcher');
await request(app).post(`/encounters/${encounterId}/participants`).set(dmAuth).send({ characterId: watcherId });
const playerViewRes = await request(app).get(`/encounters/${encounterId}`).set(playerAuth);
expect(playerViewRes.status).toBe(200);
@@ -1611,6 +1627,7 @@ describe('ordering, archiving and deleting encounters', () => {
const ids = {};
for (const name of ['One', 'Two', 'Three']) {
ids[name] = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name })).body.encounter.id;
await request(app).post(`/encounters/${ids[name]}/participants`).set(dmAuth).send({ characterId: heroId });
}
const names = async (auth) => (await request(app).get(`${c}/encounters`).set(auth)).body.encounters.map((e) => e.name);
@@ -1637,7 +1654,6 @@ describe('ordering, archiving and deleting encounters', () => {
expect(await names(playerAuth)).toEqual(['One', 'Two', 'Three']);
// Deleting takes the scene but leaves what it already gave out.
await request(app).post(`/encounters/${ids.One}/participants`).set(dmAuth).send({ characterId: heroId });
const rewardRes = await request(app).post(`/encounters/${ids.One}/rewards`).set(dmAuth).send({ type: 'GOLD', gold: 12 });
await request(app).post(`/encounters/${ids.One}/rewards/${rewardRes.body.reward.id}/award`).set(dmAuth).send({ characterId: heroId });
const goldBefore = (await request(app).get(`/characters/${heroId}`).set(playerAuth)).body.character.gold;
@@ -1651,3 +1667,299 @@ describe('ordering, archiving and deleting encounters', () => {
expect(goldBefore).toBeGreaterThanOrEqual(12);
});
});
describe('what a player may see', () => {
itIfDb('the pools: starting options while building, then only what their character holds', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('poolview');
const c = `/campaigns/${campaign.id}`;
const weapon = (name, isStartingOption) =>
request(app).post(`${c}/weapons`).set(dmAuth).send({ name, damageDice: '1d6', damageType: 'slashing', attackAbility: 'STR', isStartingOption });
await weapon('Starter Sword', true);
const secret = (await weapon('Secret Blade', false)).body.weapon;
await request(app).post(`${c}/items`).set(dmAuth).send({ name: 'Rope', isStartingOption: true });
await request(app).post(`${c}/items`).set(dmAuth).send({ name: 'Crown Shard' });
await request(app).post(`${c}/spells`).set(dmAuth).send({ name: 'Spark', level: 0, isStartingOption: true });
await request(app).post(`${c}/spells`).set(dmAuth).send({ name: 'Doom', level: 5 });
const names = async (pool, auth) => (await request(app).get(`${c}/${pool}`).set(auth)).body[pool].map((x) => x.name);
expect(await names('weapons', dmAuth)).toEqual(['Secret Blade', 'Starter Sword']);
// No character yet: nothing to choose from, nothing held.
expect(await names('weapons', playerAuth)).toEqual([]);
expect(await names('items', playerAuth)).toEqual([]);
expect(await names('spells', playerAuth)).toEqual([]);
// Building: the starting options, and only those.
const hero = (await request(app).post(`${c}/characters`).set(playerAuth).send({ name: 'Newcomer' })).body.character;
expect(await names('weapons', playerAuth)).toEqual(['Starter Sword']);
expect(await names('items', playerAuth)).toEqual(['Rope']);
expect(await names('spells', playerAuth)).toEqual(['Spark']);
await request(app).delete(`/characters/${hero.id}`).set(dmAuth);
// Built: only what the character holds.
const heroId = await buildCharacter(campaign.id, playerAuth, 'Veteran');
expect(await names('weapons', playerAuth)).toEqual([]);
await request(app).post(`/characters/${heroId}/weapons`).set(dmAuth).send({ weaponId: secret.id });
expect(await names('weapons', playerAuth)).toEqual(['Secret Blade']);
expect(await names('items', playerAuth)).toEqual([]);
// The built-in library is the DM's tool.
expect((await request(app).get(`${c}/library`).set(playerAuth)).status).toBe(403);
expect((await request(app).get(`${c}/library`).set(dmAuth)).status).toBe(200);
});
itIfDb('the invite code is the DM’s alone', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('invitehide');
const c = `/campaigns/${campaign.id}`;
expect((await request(app).get(c).set(dmAuth)).body.campaign.inviteCode).toBe(campaign.inviteCode);
expect((await request(app).get(c).set(playerAuth)).body.campaign.inviteCode).toBeUndefined();
const listed = (await request(app).get('/campaigns').set(playerAuth)).body.campaigns.find((x) => x.id === campaign.id);
expect(listed.inviteCode).toBeUndefined();
});
itIfDb('another player’s character: HP and AC, but not their gold, pack or money log', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('sheetview');
const c = `/campaigns/${campaign.id}`;
await request(app).patch(c).set(dmAuth).send({ startingGold: 20 });
const mineId = await buildCharacter(campaign.id, playerAuth, 'Mine');
const theirsId = await buildCharacter(campaign.id, dmAuth, 'Theirs');
const item = (await request(app).post(`${c}/items`).set(dmAuth).send({ name: 'Love Letter' })).body.item;
await request(app).post(`/characters/${theirsId}/inventory`).set(dmAuth).send({ itemId: item.id });
const theirs = (await request(app).get(`/characters/${theirsId}`).set(playerAuth)).body.character;
expect(theirs.restricted).toBe(true);
expect(theirs.gold).toBeNull();
expect(theirs.inventoryItems).toEqual([]);
expect(theirs.sheet.armorClass).toEqual(expect.any(Number));
expect(theirs.currentHp).toEqual(expect.any(Number));
expect((await request(app).get(`/characters/${theirsId}/gold`).set(playerAuth)).status).toBe(403);
const mine = (await request(app).get(`/characters/${mineId}`).set(playerAuth)).body.character;
expect(mine.restricted).toBe(false);
expect(mine.gold).toBe(20);
expect((await request(app).get(`/characters/${mineId}/gold`).set(playerAuth)).status).toBe(200);
const dmView = (await request(app).get(`/characters/${theirsId}`).set(dmAuth)).body.character;
expect(dmView.gold).toBe(20);
expect(dmView.inventoryItems.map((i) => i.item.name)).toEqual(['Love Letter']);
const list = (await request(app).get(`${c}/characters`).set(playerAuth)).body.characters;
expect(list.find((x) => x.id === mineId).gold).toBe(20);
expect(list.find((x) => x.id === theirsId)).toMatchObject({ gold: null, armorClass: expect.any(Number), currentHp: expect.any(Number) });
const dmList = (await request(app).get(`${c}/characters`).set(dmAuth)).body.characters;
expect(dmList.find((x) => x.id === theirsId).gold).toBe(20);
});
itIfDb('locations: only where their character has been, and an encounter in progress takes them there', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('locview');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Wanderer');
const strangerId = await buildCharacter(campaign.id, dmAuth, 'Stranger');
const place = async (name) => (await request(app).post(`${c}/locations`).set(dmAuth).send({ name, description: `About ${name}.` })).body.location;
const cave = await place('Cave');
const keep = await place('Keep');
const vault = await place('Vault');
const seen = async () => (await request(app).get(`${c}/locations`).set(playerAuth)).body.locations.map((l) => l.name);
expect((await request(app).get(`${c}/locations`).set(dmAuth)).body.locations).toHaveLength(3);
expect(await seen()).toEqual([]);
// A planned encounter with a location moves nobody and reveals nothing.
const other = await setupCampaign('locview-other');
const foreign = (await request(app).post(`/campaigns/${other.campaign.id}/locations`).set(other.dmAuth).send({ name: 'Elsewhere' })).body.location;
expect((await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Bad', locationId: foreign.id })).status).toBe(400);
const enc = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Into the Cave', locationId: cave.id })).body.encounter;
const e = `/encounters/${enc.id}`;
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId });
expect(await seen()).toEqual([]);
expect((await request(app).get(e).set(playerAuth)).body.encounter.location).toBeNull();
expect((await request(app).get(e).set(dmAuth)).body.encounter.location.name).toBe('Cave');
// Beginning it puts the characters in it there.
await request(app).patch(e).set(dmAuth).send({ status: 'ACTIVE' });
expect((await request(app).get(`/characters/${heroId}`).set(playerAuth)).body.character.currentLocation.name).toBe('Cave');
expect(await seen()).toEqual(['Cave']);
expect((await request(app).get(e).set(playerAuth)).body.encounter.location).toMatchObject({ name: 'Cave', description: 'About Cave.' });
// Moving a running encounter, or joining one, does the same.
expect((await request(app).patch(e).set(dmAuth).send({ locationId: foreign.id })).status).toBe(400);
await request(app).patch(e).set(dmAuth).send({ locationId: keep.id });
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: strangerId });
expect((await request(app).get(`/characters/${strangerId}`).set(dmAuth)).body.character.currentLocation.name).toBe('Keep');
expect(await seen()).toEqual(['Cave', 'Keep']);
// Places stay known after moving on, and after the encounter is archived or deleted.
await request(app).patch(e).set(dmAuth).send({ status: 'COMPLETE', archived: true });
expect(await seen()).toEqual(['Cave', 'Keep']);
await request(app).delete(e).set(dmAuth);
expect(await seen()).toEqual(['Cave', 'Keep']);
// The DM placing a character by hand counts as a visit too.
await request(app).patch(`/characters/${heroId}`).set(dmAuth).send({ currentLocationId: vault.id });
expect(await seen()).toEqual(['Cave', 'Keep', 'Vault']);
// Where the party is: named only if this player has been there themselves.
await request(app).patch(`/characters/${strangerId}`).set(dmAuth).send({ currentLocationId: cave.id });
const list = async () => (await request(app).get(`${c}/characters`).set(playerAuth)).body.characters.find((x) => x.id === strangerId);
expect((await list()).currentLocationName).toBe('Cave');
const hidden = await place('Hidden Grove');
await request(app).patch(`/characters/${strangerId}`).set(dmAuth).send({ currentLocationId: hidden.id });
expect((await list()).currentLocationName).toBeNull();
expect((await request(app).get(`/characters/${strangerId}`).set(playerAuth)).body.character.currentLocation).toBeNull();
});
itIfDb('monster hit points: numbers, or only a bar when the campaign says so', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('hpview');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Watcher');
const monster = (await request(app).post(`${c}/monsters`).set(dmAuth).send({ name: 'Ogre', ac: 11, maxHp: 59 })).body.monster;
const npc = (await request(app).post(`${c}/npcs`).set(dmAuth).send({ name: 'Captain', maxHp: 40 })).body.npc;
const enc = (await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Bridge' })).body.encounter;
const e = `/encounters/${enc.id}`;
await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId });
const ogreRow = (await request(app).post(`${e}/participants`).set(dmAuth).send({ monsterId: monster.id })).body.participant;
await request(app).post(`${e}/participants`).set(dmAuth).send({ npcId: npc.id });
const view = async (auth) => Object.fromEntries((await request(app).get(e).set(auth)).body.encounter.participants.map((p) => [p.displayName, p]));
expect(campaign.showMonsterHp).toBe(true);
expect((await view(playerAuth)).Ogre).toMatchObject({ currentHp: 59, maxHp: 59 });
expect((await request(app).patch(c).set(playerAuth).send({ showMonsterHp: false })).status).toBe(403);
expect((await request(app).patch(c).set(dmAuth).send({ showMonsterHp: false })).body.campaign.showMonsterHp).toBe(false);
await request(app).patch(`${e}/participants/${ogreRow.id}`).set(dmAuth).send({ currentHp: 1 });
const player = await view(playerAuth);
expect(player.Ogre).toMatchObject({ currentHp: null, maxHp: null, hpPercent: 2 }); // 1 of 59, rounded up
expect(player.Captain).toMatchObject({ currentHp: null, maxHp: null, hpPercent: 100 });
expect(player.Watcher.currentHp).toEqual(expect.any(Number)); // characters keep their numbers
expect(player.Watcher.hpPercent).toBeUndefined();
const dm = await view(dmAuth);
expect(dm.Ogre).toMatchObject({ currentHp: 1, maxHp: 59 });
expect(dm.Ogre.hpPercent).toBeUndefined();
await request(app).patch(`${e}/participants/${ogreRow.id}`).set(dmAuth).send({ currentHp: 0 });
expect((await view(playerAuth)).Ogre.hpPercent).toBe(0);
});
});
describe('initiative', () => {
itIfDb('is the d20 roll plus the combatant\'s bonus, and a player enters only their own', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('initroll');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Quick');
const allyId = await buildCharacter(campaign.id, dmAuth, 'Steady');
const heroBonus = (await request(app).get(`/characters/${heroId}`).set(playerAuth)).body.character.sheet.initiative;
const goblin = (await request(app).post(`${c}/monsters/from-library`).set(dmAuth).send({ key: 'goblin' })).body.monster; // DEX 14: +2
const npc = (await request(app).post(`${c}/npcs`).set(dmAuth).send({ name: 'Mayor', maxHp: 9 })).body.npc;
const e = `/encounters/${(await request(app).post(`${c}/encounters`).set(dmAuth).send({ name: 'Roll for it' })).body.encounter.id}`;
const add = (body) => request(app).post(`${e}/participants`).set(dmAuth).send(body);
// A roll is a d20: nothing outside 1 to 20, and no fractions.
expect((await add({ monsterId: goblin.id, initiativeRoll: 21 })).status).toBe(400);
expect((await add({ monsterId: goblin.id, initiativeRoll: 0 })).status).toBe(400);
expect((await add({ monsterId: goblin.id, initiativeRoll: 7.5 })).status).toBe(400);
const goblinRow = (await add({ monsterId: goblin.id, initiativeRoll: 10 })).body.participant;
expect(goblinRow).toMatchObject({ initiativeRoll: 10, initiative: 12 });
const npcRow = (await add({ npcId: npc.id, initiativeRoll: 10 })).body.participant;
expect(npcRow).toMatchObject({ initiativeRoll: 10, initiative: 10 }); // an NPC has no bonus
const heroRow = (await add({ characterId: heroId })).body.participant;
expect(heroRow).toMatchObject({ initiativeRoll: null, initiative: 0 });
const allyRow = (await add({ characterId: allyId })).body.participant;
const patch = (row, auth, body) => request(app).patch(`${e}/participants/${row.id}`).set(auth).send(body);
const order = async (auth) => (await request(app).get(e).set(auth)).body.encounter.participants;
// Not rolled yet: at the bottom of the order.
expect((await order(dmAuth)).slice(0, 2).map((p) => p.displayName)).toEqual(['Goblin', 'Mayor']);
// The player types what their die showed; the bonus is added for them.
expect((await patch(heroRow, playerAuth, { initiativeRoll: 25 })).status).toBe(400);
const rolled = await patch(heroRow, playerAuth, { initiativeRoll: 20 });
expect(rolled.status).toBe(200);
expect(rolled.body.participant).toMatchObject({ initiativeRoll: 20, initiative: 20 + heroBonus });
expect((await order(playerAuth))[0]).toMatchObject({ displayName: 'Quick', initiativeRoll: 20, initiativeBonus: heroBonus });
// ...and nothing else: not someone else's roll, not a monster's, not their own HP.
expect((await patch(allyRow, playerAuth, { initiativeRoll: 15 })).status).toBe(403);
expect((await patch(goblinRow, playerAuth, { initiativeRoll: 1 })).status).toBe(403);
expect((await patch(heroRow, playerAuth, { initiativeRoll: 20, currentHp: 999 })).status).toBe(403);
expect((await patch(heroRow, playerAuth, { conditions: ['hasted'] })).status).toBe(403);
// The DM sees every bonus; a player isn't handed a monster's.
const dmView = Object.fromEntries((await order(dmAuth)).map((p) => [p.displayName, p]));
expect(dmView.Goblin.initiativeBonus).toBe(2);
expect(dmView.Mayor.initiativeBonus).toBe(0);
const playerView = Object.fromEntries((await order(playerAuth)).map((p) => [p.displayName, p]));
expect(playerView.Goblin.initiativeBonus).toBeUndefined();
expect(playerView.Goblin.initiative).toBe(12);
expect(playerView.Quick.character.userId).toBeTruthy();
// The DM can re-roll anyone, or clear a roll.
expect((await patch(goblinRow, dmAuth, { initiativeRoll: 1 })).body.participant.initiative).toBe(3);
expect((await patch(heroRow, dmAuth, { initiativeRoll: null })).body.participant).toMatchObject({ initiativeRoll: null, initiative: 0 });
expect((await order(dmAuth)).map((p) => p.displayName).slice(0, 2)).toEqual(['Mayor', 'Goblin']);
});
});
describe('number limits', () => {
itIfDb('are enforced by the server, whatever a form sends', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('limits');
const c = `/campaigns/${campaign.id}`;
const heroId = await buildCharacter(campaign.id, playerAuth, 'Bounded');
const sheet = (body) => request(app).patch(`/characters/${heroId}`).set(dmAuth).send(body);
expect((await sheet({ level: 21 })).status).toBe(400);
expect((await sheet({ level: 0 })).status).toBe(400);
expect((await sheet({ level: 20 })).status).toBe(200);
expect((await sheet({ armorBonus: 31 })).status).toBe(400);
expect((await sheet({ tempHp: -1 })).status).toBe(400);
expect((await sheet({ tempHp: 10000 })).status).toBe(400);
expect((await request(app).patch(c).set(dmAuth).send({ startingGold: -5 })).status).toBe(400);
expect((await request(app).patch(c).set(dmAuth).send({ startingItemCount: 51 })).status).toBe(400);
expect((await request(app).post(`${c}/monsters`).set(dmAuth).send({ name: 'Wall', ac: 51, maxHp: 10 })).status).toBe(400);
expect((await request(app).post(`${c}/monsters`).set(dmAuth).send({ name: 'Ghost', ac: 10, maxHp: 0 })).status).toBe(400);
expect((await request(app).post(`${c}/npcs`).set(dmAuth).send({ name: 'Titan', attackBonus: 51 })).status).toBe(400);
});
});
describe('temporary hit points', () => {
itIfDb('soak damage before real HP, on the sheet and in an encounter; healing leaves them alone', async () => {
const { dmAuth, playerAuth, campaign } = await setupCampaign('temphp');
const heroId = await buildCharacter(campaign.id, playerAuth, 'Shielded');
const ch = `/characters/${heroId}`;
const sheet = async () => (await request(app).get(ch).set(playerAuth)).body.character;
const max = (await sheet()).sheet.hp.max;
expect((await sheet()).currentHp).toBe(max);
// On the sheet: 5 temp, 3 damage — all of it soaked.
await request(app).patch(ch).set(playerAuth).send({ tempHp: 5 });
let now = (await request(app).patch(ch).set(playerAuth).send({ currentHp: max - 3 })).body.character;
expect([now.currentHp, now.tempHp]).toEqual([max, 2]);
// More damage than is left: the rest reaches real HP.
now = (await request(app).patch(ch).set(playerAuth).send({ currentHp: max - 3 })).body.character;
expect([now.currentHp, now.tempHp]).toEqual([max - 1, 0]);
// Healing doesn't touch temp HP, and setting temp HP doesn't touch HP.
await request(app).patch(ch).set(playerAuth).send({ tempHp: 4 });
now = (await request(app).patch(ch).set(playerAuth).send({ currentHp: max })).body.character;
expect([now.currentHp, now.tempHp]).toEqual([max, 4]);
// In an encounter the damage lands on the same sheet, the same way.
const e = `/encounters/${(await request(app).post(`/campaigns/${campaign.id}/encounters`).set(dmAuth).send({ name: 'Arrows' })).body.encounter.id}`;
const row = (await request(app).post(`${e}/participants`).set(dmAuth).send({ characterId: heroId })).body.participant;
const hit = await request(app).patch(`${e}/participants/${row.id}`).set(dmAuth).send({ currentHp: max - 6 });
expect(hit.body.participant.currentHp).toBe(max - 2);
now = await sheet();
expect([now.currentHp, now.tempHp]).toEqual([max - 2, 0]);
// A blow far past zero still only takes what there is.
await request(app).patch(ch).set(playerAuth).send({ tempHp: 2 });
await request(app).patch(`${e}/participants/${row.id}`).set(dmAuth).send({ currentHp: -500 });
now = await sheet();
expect([now.currentHp, now.tempHp]).toEqual([0, 0]);
});
});