fix: death:recovery-roll — add is_dead guard, try/catch, ownership check, subtype

This commit is contained in:
Aaron Wood 2026-04-12 01:32:24 -04:00
parent 43a5b85dcf
commit f853fdbce3

View file

@ -158,6 +158,7 @@ export function setupSocket(io: Server) {
campaignId: number; campaignId: number;
characterId: number; characterId: number;
}) => { }) => {
try {
const userId = socket.data.user?.userId; const userId = socket.data.user?.userId;
// Verify caller is DM // Verify caller is DM
@ -174,6 +175,13 @@ export function setupSocket(io: Server) {
); );
if (dyingRows.length === 0) return; if (dyingRows.length === 0) return;
// Verify character is not permanently dead
const [deadCheck] = await db.execute<RowDataPacket[]>(
"SELECT is_dead FROM characters WHERE id = ?",
[data.characterId]
);
if (deadCheck.length === 0 || deadCheck[0].is_dead) return;
// Get character info for roll log // Get character info for roll log
const [charRows] = await db.execute<RowDataPacket[]>( const [charRows] = await db.execute<RowDataPacket[]>(
"SELECT name, color, campaign_id FROM characters WHERE id = ?", "SELECT name, color, campaign_id FROM characters WHERE id = ?",
@ -182,6 +190,8 @@ export function setupSocket(io: Server) {
if (charRows.length === 0) return; if (charRows.length === 0) return;
const char = charRows[0]; const char = charRows[0];
if (char.campaign_id !== data.campaignId) return;
// Roll d20 server-side // Roll d20 server-side
const result = rollDice("1d20"); const result = rollDice("1d20");
const roll = result.total; const roll = result.total;
@ -193,9 +203,9 @@ export function setupSocket(io: Server) {
const [insertResult] = await db.execute<import("mysql2").ResultSetHeader>( const [insertResult] = await db.execute<import("mysql2").ResultSetHeader>(
`INSERT INTO roll_log `INSERT INTO roll_log
(campaign_id, character_id, character_name, character_color, type, label, (campaign_id, character_id, character_name, character_color, type, subtype, label,
dice_expression, rolls, modifier, total, advantage, disadvantage, nat20) dice_expression, rolls, modifier, total, advantage, disadvantage, nat20)
VALUES (?, ?, ?, ?, 'custom', ?, '1d20', ?, 0, ?, 0, 0, ?)`, VALUES (?, ?, ?, ?, 'custom', 'death-save', ?, '1d20', ?, 0, ?, 0, 0, ?)`,
[ [
data.campaignId, data.campaignId,
data.characterId, data.characterId,
@ -242,6 +252,9 @@ export function setupSocket(io: Server) {
conditions: updatedConditions, conditions: updatedConditions,
}); });
} }
} catch (err) {
console.error("[death:recovery-roll]", err);
}
}); });
socket.on("disconnect", () => { socket.on("disconnect", () => {