Initial commit
This commit is contained in:
@@ -0,0 +1,236 @@
|
||||
import { useState } from 'react';
|
||||
import { Link, useParams } from 'react-router-dom';
|
||||
import {
|
||||
useEncounter,
|
||||
useCampaign,
|
||||
useCampaignCharacters,
|
||||
useCampaignMonsters,
|
||||
useAddParticipant,
|
||||
usePatchParticipant,
|
||||
useRemoveParticipant,
|
||||
useAdvanceRound,
|
||||
usePatchEncounter,
|
||||
} from '../api/queries';
|
||||
import { useCampaignSocket } from '../hooks/useCampaignSocket';
|
||||
import { Spinner, ErrorBanner } from '../components/Spinner';
|
||||
import { extractErrorMessage } from '../lib/format';
|
||||
import type { EncounterParticipant } from '../api/types';
|
||||
|
||||
function ParticipantRow({
|
||||
participant,
|
||||
isDM,
|
||||
onPatch,
|
||||
onRemove,
|
||||
}: {
|
||||
participant: EncounterParticipant;
|
||||
isDM: boolean;
|
||||
onPatch: (data: Partial<EncounterParticipant>) => void;
|
||||
onRemove: () => void;
|
||||
}) {
|
||||
const [hpDelta, setHpDelta] = useState('');
|
||||
const pct = Math.max(0, Math.min(100, (participant.currentHp / participant.maxHp) * 100));
|
||||
|
||||
function applyDelta(sign: 1 | -1) {
|
||||
const amount = Number(hpDelta);
|
||||
if (!amount) return;
|
||||
onPatch({ currentHp: participant.currentHp + sign * amount });
|
||||
setHpDelta('');
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="card">
|
||||
<div className="flex-between flex-wrap">
|
||||
<div className="flex-row">
|
||||
<span className={`badge ${participant.isMonster ? 'badge-dm' : 'badge-player'}`}>
|
||||
{participant.isMonster ? 'Monster' : 'PC'}
|
||||
</span>
|
||||
<strong>{participant.displayName}</strong>
|
||||
</div>
|
||||
{isDM && (
|
||||
<label className="flex-row gap-sm">
|
||||
Init
|
||||
<input
|
||||
type="number"
|
||||
defaultValue={participant.initiative}
|
||||
style={{ width: 60 }}
|
||||
onBlur={(e) => onPatch({ initiative: Number(e.target.value) })}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
<div className="hp-bar-track mt-sm">
|
||||
<div className="hp-bar-fill" style={{ width: `${pct}%` }} />
|
||||
</div>
|
||||
<div className="flex-between mt-sm flex-wrap">
|
||||
<span>
|
||||
HP {participant.currentHp}/{participant.maxHp}
|
||||
</span>
|
||||
{isDM && (
|
||||
<div className="flex-row gap-sm">
|
||||
<input type="number" placeholder="Amt" value={hpDelta} onChange={(e) => setHpDelta(e.target.value)} style={{ width: 70 }} />
|
||||
<button className="btn btn-danger" onClick={() => applyDelta(-1)}>
|
||||
−
|
||||
</button>
|
||||
<button className="btn" onClick={() => applyDelta(1)}>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{isDM && (
|
||||
<div className="flex-row mt-sm flex-wrap">
|
||||
<input
|
||||
placeholder="Conditions, comma separated"
|
||||
defaultValue={participant.conditions.join(', ')}
|
||||
style={{ flex: 1, minWidth: 160 }}
|
||||
onBlur={(e) =>
|
||||
onPatch({
|
||||
conditions: e.target.value
|
||||
.split(',')
|
||||
.map((c) => c.trim())
|
||||
.filter(Boolean),
|
||||
})
|
||||
}
|
||||
/>
|
||||
<button className="btn btn-ghost" onClick={onRemove}>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
{!isDM && participant.conditions.length > 0 && (
|
||||
<div className="flex-row flex-wrap mt-sm">
|
||||
{participant.conditions.map((c) => (
|
||||
<span key={c} className="badge">
|
||||
{c}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function EncounterPage() {
|
||||
const { encounterId = '' } = useParams();
|
||||
const { data: encounter, isLoading, error } = useEncounter(encounterId);
|
||||
useCampaignSocket(encounter?.campaignId);
|
||||
|
||||
const { data: campaignData } = useCampaign(encounter?.campaignId);
|
||||
const { data: characters } = useCampaignCharacters(encounter?.campaignId);
|
||||
const { data: monsters } = useCampaignMonsters(encounter?.campaignId);
|
||||
|
||||
const addParticipant = useAddParticipant(encounterId);
|
||||
const patchParticipant = usePatchParticipant(encounterId);
|
||||
const removeParticipant = useRemoveParticipant(encounterId);
|
||||
const advanceRound = useAdvanceRound(encounterId);
|
||||
const patchEncounter = usePatchEncounter(encounterId);
|
||||
|
||||
const [pickType, setPickType] = useState<'character' | 'monster'>('character');
|
||||
const [pickId, setPickId] = useState('');
|
||||
const [initiative, setInitiative] = useState('0');
|
||||
const [formError, setFormError] = useState<string | null>(null);
|
||||
|
||||
if (isLoading) return <Spinner label="Setting the battlefield…" />;
|
||||
if (error || !encounter) return <ErrorBanner message="Could not load this encounter." />;
|
||||
|
||||
const isDM = campaignData?.campaign.role === 'DM';
|
||||
const sorted = [...(encounter.participants || [])].sort((a, b) => b.initiative - a.initiative);
|
||||
const availableCharacters = characters?.filter((c) => !encounter.participants?.some((p) => p.characterId === c.id) && c.buildStage === 'COMPLETE');
|
||||
const availableMonsters = monsters;
|
||||
|
||||
async function handleAdd() {
|
||||
if (!pickId) return;
|
||||
setFormError(null);
|
||||
try {
|
||||
await addParticipant.mutateAsync({
|
||||
[pickType === 'character' ? 'characterId' : 'monsterId']: pickId,
|
||||
initiative: Number(initiative) || 0,
|
||||
});
|
||||
setPickId('');
|
||||
} catch (err) {
|
||||
setFormError(extractErrorMessage(err));
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="page-header">
|
||||
<div>
|
||||
<h1>⚔ {encounter.name}</h1>
|
||||
<p className="subtitle">
|
||||
{encounter.status} · Round {encounter.currentRound}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex-row">
|
||||
<Link to={`/campaigns/${encounter.campaignId}`} className="btn btn-ghost">
|
||||
← Campaign
|
||||
</Link>
|
||||
{isDM && (
|
||||
<>
|
||||
{encounter.status === 'PLANNING' && (
|
||||
<button className="btn btn-primary" onClick={() => patchEncounter.mutate({ status: 'ACTIVE' })}>
|
||||
Begin Combat
|
||||
</button>
|
||||
)}
|
||||
{encounter.status === 'ACTIVE' && (
|
||||
<>
|
||||
<button className="btn btn-primary" onClick={() => advanceRound.mutate()}>
|
||||
Advance Round
|
||||
</button>
|
||||
<button className="btn btn-danger" onClick={() => patchEncounter.mutate({ status: 'COMPLETE' })}>
|
||||
End Encounter
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{formError && <ErrorBanner message={formError} />}
|
||||
|
||||
{isDM && (
|
||||
<div className="card mt-md">
|
||||
<div className="card-title">Add Combatant</div>
|
||||
<div className="flex-row flex-wrap">
|
||||
<select value={pickType} onChange={(e) => setPickType(e.target.value as 'character' | 'monster')}>
|
||||
<option value="character">Character</option>
|
||||
<option value="monster">Monster</option>
|
||||
</select>
|
||||
<select value={pickId} onChange={(e) => setPickId(e.target.value)} style={{ flex: 1, minWidth: 160 }}>
|
||||
<option value="">Select…</option>
|
||||
{(pickType === 'character' ? availableCharacters : availableMonsters)?.map((entity) => (
|
||||
<option key={entity.id} value={entity.id}>
|
||||
{entity.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<input
|
||||
type="number"
|
||||
placeholder="Initiative"
|
||||
value={initiative}
|
||||
onChange={(e) => setInitiative(e.target.value)}
|
||||
style={{ width: 100 }}
|
||||
/>
|
||||
<button className="btn btn-primary" onClick={handleAdd} disabled={!pickId}>
|
||||
Add
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="mt-lg">
|
||||
{sorted.length === 0 && <div className="empty-state">No combatants yet.</div>}
|
||||
{sorted.map((p) => (
|
||||
<ParticipantRow
|
||||
key={p.id}
|
||||
participant={p}
|
||||
isDM={Boolean(isDM)}
|
||||
onPatch={(data) => patchParticipant.mutate({ participantId: p.id, ...data })}
|
||||
onRemove={() => removeParticipant.mutate(p.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user