- 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>
67 lines
2.7 KiB
TypeScript
67 lines
2.7 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
import { io, type Socket } from 'socket.io-client';
|
|
import { API_URL } from '../api/client';
|
|
import { useAuth } from '../auth/AuthContext';
|
|
|
|
let sharedSocket: Socket | null = null;
|
|
|
|
function getSocket(token: string): Socket {
|
|
if (!sharedSocket) {
|
|
sharedSocket = io(API_URL, { auth: { token }, transports: ['websocket', 'polling'] });
|
|
}
|
|
return sharedSocket;
|
|
}
|
|
|
|
// Joins the Socket.io room for a campaign and keeps React Query's cache in
|
|
// sync with server-pushed events (HP/condition changes, encounter rounds,
|
|
// new characters) so every open tab in the campaign updates live.
|
|
export function useCampaignSocket(campaignId: string | undefined) {
|
|
const { token } = useAuth();
|
|
const qc = useQueryClient();
|
|
|
|
useEffect(() => {
|
|
if (!campaignId || !token) return;
|
|
const socket = getSocket(token);
|
|
socket.emit('campaign:join', campaignId);
|
|
|
|
const onCharacterUpdated = () => {
|
|
qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'characters'] });
|
|
qc.invalidateQueries({ queryKey: ['character'] });
|
|
qc.invalidateQueries({ queryKey: ['encounter'] }); // a character combatant shows the sheet HP
|
|
// A character arriving somewhere new adds it to the places their player can see.
|
|
qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'locations'] });
|
|
};
|
|
const onCharacterCreated = () => {
|
|
qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'characters'] });
|
|
};
|
|
const onParticipantUpdated = () => {
|
|
qc.invalidateQueries({ queryKey: ['encounter'] });
|
|
// A player's list only holds the encounters their characters are in.
|
|
qc.invalidateQueries({ queryKey: ['campaign', campaignId, 'encounters'] });
|
|
};
|
|
const onRoundAdvanced = () => {
|
|
qc.invalidateQueries({ queryKey: ['encounter'] });
|
|
};
|
|
const onCampaignDeleted = () => {
|
|
qc.invalidateQueries({ queryKey: ['campaigns'] });
|
|
qc.invalidateQueries({ queryKey: ['campaign', campaignId] });
|
|
};
|
|
|
|
socket.on('character:updated', onCharacterUpdated);
|
|
socket.on('character:created', onCharacterCreated);
|
|
socket.on('encounter:participant-updated', onParticipantUpdated);
|
|
socket.on('encounter:round-advanced', onRoundAdvanced);
|
|
socket.on('campaign:deleted', onCampaignDeleted);
|
|
|
|
return () => {
|
|
socket.emit('campaign:leave', campaignId);
|
|
socket.off('character:updated', onCharacterUpdated);
|
|
socket.off('character:created', onCharacterCreated);
|
|
socket.off('encounter:participant-updated', onParticipantUpdated);
|
|
socket.off('encounter:round-advanced', onRoundAdvanced);
|
|
socket.off('campaign:deleted', onCampaignDeleted);
|
|
};
|
|
}, [campaignId, token, qc]);
|
|
}
|