V1.1.0: gear levels, encounter overhaul, guided builder, trading, docs
Characters - Builder: pick-then-Next on every step with live preview, Back button, plain-language help throughout, and "Suggest for me" for ability scores - Motivation is picked from a list; new steps for special abilities, background and appearance, all composed from fixed lists and DM-only to edit afterwards - Copy a character to another campaign - Characters can give gold, weapons, items and spells to each other - Money tracker: gold only changes through a logged ledger - Fixes: level/XP/location are DM-only, HP stays within 0..max, physical dice ability rolls, every die is rolled once Gear - DM-managed item pool; weapons, items and spells are handed out by the DM - Starting-option flag per pool entry - Gear levels 1-9 per held copy with a damage ladder, growth rate and starting level; spells gated by spell level with a fixed damage value - Spell library carries default damage; damage is picked from a list Encounters - Battle and NPC quest modes, turn tracking, scene description, DM notes - NPCs, richer combatant cards, conditions from a list - Rewards (gold, weapons, items) lined up and awarded, with gold splitting - Character HP is shared between the sheet and the encounter - Monster stat blocks and NPC secrets are never sent to players Other - Dice roller on every page - Docs for characters, weapons, spells, items, encounters, monsters and NPCs, with full lists generated from the data (npm run docs:lists) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+245
-17
@@ -1,6 +1,7 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { apiClient } from './client';
|
||||
import type {
|
||||
BuildOptions,
|
||||
Campaign,
|
||||
CampaignLibrary,
|
||||
CampaignMemberSummary,
|
||||
@@ -8,11 +9,14 @@ import type {
|
||||
CharacterSummary,
|
||||
DiceMode,
|
||||
Encounter,
|
||||
EncounterMode,
|
||||
EncounterParticipant,
|
||||
InventoryItem,
|
||||
GoldTransaction,
|
||||
Item,
|
||||
Location,
|
||||
Monster,
|
||||
Note,
|
||||
Npc,
|
||||
QuickStats,
|
||||
Spell,
|
||||
Weapon,
|
||||
@@ -120,10 +124,11 @@ export function useCharacter(characterId: string | undefined) {
|
||||
});
|
||||
}
|
||||
|
||||
// `gold` is deliberately not patchable — it only moves through useAdjustGold.
|
||||
export function usePatchCharacter(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: Partial<CharacterDetail>) =>
|
||||
mutationFn: async (data: Partial<Omit<CharacterDetail, 'gold'>>) =>
|
||||
(await apiClient.patch<{ character: CharacterDetail }>(`/characters/${characterId}`, data)).data.character,
|
||||
onSuccess: (character) => {
|
||||
qc.setQueryData(['character', characterId], character);
|
||||
@@ -140,6 +145,38 @@ export function useDeleteCharacter(campaignId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
// What one character can hand to another. Whatever is given leaves the giver.
|
||||
export type Gift =
|
||||
| { kind: 'gold'; amount: number }
|
||||
| { kind: 'weapon' | 'item'; id: string; quantity: number }
|
||||
| { kind: 'spell'; id: string };
|
||||
|
||||
export function useGive(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (gift: Gift & { toCharacterId: string }) =>
|
||||
(await apiClient.post<{ character: CharacterDetail }>(`/characters/${characterId}/give`, gift)).data.character,
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['character'] }); // both sheets, and both money logs
|
||||
qc.invalidateQueries({ queryKey: ['campaign'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useCopyCharacter(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (targetCampaignId: string) =>
|
||||
(await apiClient.post<{ character: CharacterDetail }>(`/characters/${characterId}/copy`, { targetCampaignId })).data
|
||||
.character,
|
||||
onSuccess: (character) => {
|
||||
qc.setQueryData(['character', character.id], character);
|
||||
// The copy can add missing weapons/spells to the target campaign's pools.
|
||||
qc.invalidateQueries({ queryKey: ['campaign', character.campaignId] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useQuickStats(characterId: string | undefined, enabled: boolean) {
|
||||
return useQuery({
|
||||
queryKey: ['character', characterId, 'quick-stats'],
|
||||
@@ -153,6 +190,14 @@ export function useQuickStats(characterId: string | undefined, enabled: boolean)
|
||||
// Character build flow
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
export function useBuildOptions() {
|
||||
return useQuery({
|
||||
queryKey: ['build-options'],
|
||||
staleTime: Infinity, // fixed lists — they never change while the app is open
|
||||
queryFn: async () => (await apiClient.get<BuildOptions>('/characters/build/options')).data,
|
||||
});
|
||||
}
|
||||
|
||||
function invalidateCharacter(qc: ReturnType<typeof useQueryClient>, characterId: string) {
|
||||
qc.invalidateQueries({ queryKey: ['character', characterId] });
|
||||
}
|
||||
@@ -191,6 +236,16 @@ export function useBuildChoose(characterId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
// Steps back one step so its choice can be changed. Dice are never re-rolled.
|
||||
export function useBuildBack(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async () =>
|
||||
(await apiClient.post<{ buildStage: string; buildState: Record<string, unknown> }>(`/characters/${characterId}/build/back`)).data,
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
|
||||
export function useBuildFinalize(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
@@ -208,6 +263,8 @@ export function useBuildFinalize(characterId: string) {
|
||||
// Weapons / inventory / spells on a character
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
// Handing out, removing and re-counting gear is DM-only on the server; the
|
||||
// owning player can only toggle equipped / prepared.
|
||||
export function useAssignWeapon(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
@@ -217,20 +274,29 @@ export function useAssignWeapon(characterId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function useRemoveCharacterWeapon(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (weaponId: string) => apiClient.delete(`/characters/${characterId}/weapons/${weaponId}`),
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
|
||||
export function usePatchCharacterWeapon(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({ weaponId, ...data }: { weaponId: string; equipped?: boolean; quantity?: number }) =>
|
||||
mutationFn: async ({ weaponId, ...data }: { weaponId: string; equipped?: boolean; quantity?: number; level?: number }) =>
|
||||
(await apiClient.patch(`/characters/${characterId}/weapons/${weaponId}`, data)).data,
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAddInventoryItem(characterId: string) {
|
||||
// itemId here is the campaign pool item's id, as with weapons and spells.
|
||||
export function useAssignItem(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: { name: string; description?: string; quantity?: number }) =>
|
||||
(await apiClient.post<{ item: InventoryItem }>(`/characters/${characterId}/inventory`, data)).data.item,
|
||||
mutationFn: async (data: { itemId: string; quantity?: number }) =>
|
||||
(await apiClient.post(`/characters/${characterId}/inventory`, data)).data,
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
@@ -238,13 +304,13 @@ export function useAddInventoryItem(characterId: string) {
|
||||
export function usePatchInventoryItem(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({ itemId, ...data }: { itemId: string; quantity?: number; equipped?: boolean }) =>
|
||||
mutationFn: async ({ itemId, ...data }: { itemId: string; quantity?: number; equipped?: boolean; level?: number }) =>
|
||||
(await apiClient.patch(`/characters/${characterId}/inventory/${itemId}`, data)).data,
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteInventoryItem(characterId: string) {
|
||||
export function useRemoveInventoryItem(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (itemId: string) => apiClient.delete(`/characters/${characterId}/inventory/${itemId}`),
|
||||
@@ -270,6 +336,38 @@ export function usePatchCharacterSpell(characterId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function useRemoveCharacterSpell(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (spellId: string) => apiClient.delete(`/characters/${characterId}/spells/${spellId}`),
|
||||
onSuccess: () => invalidateCharacter(qc, characterId),
|
||||
});
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Money tracker
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
export function useGoldLedger(characterId: string | undefined) {
|
||||
return useQuery({
|
||||
queryKey: ['character', characterId, 'gold'],
|
||||
enabled: Boolean(characterId),
|
||||
queryFn: async () =>
|
||||
(await apiClient.get<{ gold: number; transactions: GoldTransaction[] }>(`/characters/${characterId}/gold`)).data
|
||||
.transactions,
|
||||
});
|
||||
}
|
||||
|
||||
// amount is signed: positive = gained, negative = spent.
|
||||
export function useAdjustGold(characterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: { amount: number; reason?: string }) =>
|
||||
(await apiClient.post<{ gold: number; transaction: GoldTransaction }>(`/characters/${characterId}/gold`, data)).data,
|
||||
onSuccess: () => invalidateCharacter(qc, characterId), // prefix match also refreshes the ledger
|
||||
});
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Campaign weapons / spells / monsters pools
|
||||
// --------------------------------------------------------------------------
|
||||
@@ -291,6 +389,58 @@ export function useCreateWeapon(campaignId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
// Edits (or, with useDeletePoolEntry, removes) one entry of a campaign pool.
|
||||
// Used for the "starting option" toggle on weapons, spells and items.
|
||||
type Pool = 'weapons' | 'spells' | 'items' | 'npcs';
|
||||
|
||||
export function usePatchPoolEntry(campaignId: string, pool: Pool) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
id,
|
||||
...data
|
||||
}: {
|
||||
id: string;
|
||||
isStartingOption?: boolean;
|
||||
damageDice?: string | null;
|
||||
damageScaling?: number;
|
||||
minLevel?: number;
|
||||
name?: string;
|
||||
description?: string | null;
|
||||
}) =>
|
||||
(await apiClient.patch(`/campaigns/${campaignId}/${pool}/${id}`, data)).data,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['campaign', campaignId, pool] }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeletePoolEntry(campaignId: string, pool: Pool) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => apiClient.delete(`/campaigns/${campaignId}/${pool}/${id}`),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['campaign', campaignId, pool] });
|
||||
qc.invalidateQueries({ queryKey: ['character'] }); // characters holding it just lost it
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useCampaignItems(campaignId: string | undefined) {
|
||||
return useQuery({
|
||||
queryKey: ['campaign', campaignId, 'items'],
|
||||
enabled: Boolean(campaignId),
|
||||
queryFn: async () => (await apiClient.get<{ items: Item[] }>(`/campaigns/${campaignId}/items`)).data.items,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateItem(campaignId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: Omit<Item, 'id' | 'campaignId'>) =>
|
||||
(await apiClient.post<{ item: Item }>(`/campaigns/${campaignId}/items`, data)).data.item,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'items'] }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCampaignSpells(campaignId: string | undefined) {
|
||||
return useQuery({
|
||||
queryKey: ['campaign', campaignId, 'spells'],
|
||||
@@ -308,10 +458,11 @@ export function useCreateSpell(campaignId: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export function useCampaignMonsters(campaignId: string | undefined) {
|
||||
// Monster stat blocks are DM-only on the server, so only the DM asks for them.
|
||||
export function useCampaignMonsters(campaignId: string | undefined, isDM: boolean) {
|
||||
return useQuery({
|
||||
queryKey: ['campaign', campaignId, 'monsters'],
|
||||
enabled: Boolean(campaignId),
|
||||
enabled: Boolean(campaignId) && isDM,
|
||||
queryFn: async () =>
|
||||
(await apiClient.get<{ monsters: Monster[] }>(`/campaigns/${campaignId}/monsters`)).data.monsters,
|
||||
});
|
||||
@@ -422,9 +573,8 @@ export function useCampaignEncounters(campaignId: string | undefined) {
|
||||
export function useCreateEncounter(campaignId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (name: string) =>
|
||||
(await apiClient.post<{ encounter: Encounter }>(`/campaigns/${campaignId}/encounters`, { name })).data
|
||||
.encounter,
|
||||
mutationFn: async (data: { name: string; mode?: EncounterMode }) =>
|
||||
(await apiClient.post<{ encounter: Encounter }>(`/campaigns/${campaignId}/encounters`, data)).data.encounter,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'encounters'] }),
|
||||
});
|
||||
}
|
||||
@@ -440,7 +590,7 @@ export function useEncounter(encounterId: string | undefined) {
|
||||
export function useAddParticipant(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: { characterId?: string; monsterId?: string; displayName?: string; initiative?: number }) =>
|
||||
mutationFn: async (data: { characterId?: string; monsterId?: string; npcId?: string; displayName?: string; initiative?: number }) =>
|
||||
(await apiClient.post(`/encounters/${encounterId}/participants`, data)).data,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
|
||||
});
|
||||
@@ -475,13 +625,91 @@ export function useAdvanceRound(encounterId: string) {
|
||||
export function usePatchEncounter(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: { status?: EncounterStatusInput; name?: string }) =>
|
||||
(await apiClient.patch(`/encounters/${encounterId}`, data)).data,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
|
||||
mutationFn: async (data: {
|
||||
status?: EncounterStatusInput;
|
||||
mode?: EncounterMode;
|
||||
name?: string;
|
||||
description?: string | null;
|
||||
dmNotes?: string | null;
|
||||
}) => (await apiClient.patch(`/encounters/${encounterId}`, data)).data,
|
||||
onSuccess: (_data, variables) => {
|
||||
qc.invalidateQueries({ queryKey: ['encounter', encounterId] });
|
||||
// The campaign page lists encounters by name, mode and status.
|
||||
if (variables.status || variables.mode || variables.name) qc.invalidateQueries({ queryKey: ['campaign'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
type EncounterStatusInput = 'PLANNING' | 'ACTIVE' | 'COMPLETE';
|
||||
|
||||
export function useNextTurn(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async () => (await apiClient.post(`/encounters/${encounterId}/next-turn`)).data,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
|
||||
});
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Encounter rewards — DM lines loot up, then awards it to a character
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
export type NewReward =
|
||||
| { type: 'GOLD'; gold: number; note?: string }
|
||||
| { type: 'WEAPON'; weaponId: string; quantity?: number; level?: number; note?: string }
|
||||
| { type: 'ITEM'; itemId: string; quantity?: number; level?: number; note?: string };
|
||||
|
||||
export function useAddReward(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: NewReward) => (await apiClient.post(`/encounters/${encounterId}/rewards`, data)).data,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteReward(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (rewardId: string) => apiClient.delete(`/encounters/${encounterId}/rewards/${rewardId}`),
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['encounter', encounterId] }),
|
||||
});
|
||||
}
|
||||
|
||||
// Awarding is what actually changes the character: a gold ledger entry, or
|
||||
// the weapon/item added to what they hold.
|
||||
export function useAwardReward(encounterId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async ({ rewardId, ...target }: { rewardId: string } & ({ characterId: string } | { split: true })) =>
|
||||
(await apiClient.post(`/encounters/${encounterId}/rewards/${rewardId}/award`, target)).data,
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ['encounter', encounterId] });
|
||||
qc.invalidateQueries({ queryKey: ['character'] });
|
||||
qc.invalidateQueries({ queryKey: ['campaign'] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// NPCs — a DM-only pool; players meet them through encounters
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
export function useCampaignNpcs(campaignId: string | undefined, isDM: boolean) {
|
||||
return useQuery({
|
||||
queryKey: ['campaign', campaignId, 'npcs'],
|
||||
enabled: Boolean(campaignId) && isDM,
|
||||
queryFn: async () => (await apiClient.get<{ npcs: Npc[] }>(`/campaigns/${campaignId}/npcs`)).data.npcs,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateNpc(campaignId: string) {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: async (data: Partial<Omit<Npc, 'id'>> & { name: string }) =>
|
||||
(await apiClient.post<{ npc: Npc }>(`/campaigns/${campaignId}/npcs`, data)).data.npc,
|
||||
onSuccess: () => qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'npcs'] }),
|
||||
});
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Notes
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user