fix: death:recovery-roll — add is_dead guard, try/catch, ownership check, subtype
This commit is contained in:
parent
43a5b85dcf
commit
f853fdbce3
1 changed files with 86 additions and 73 deletions
|
|
@ -158,6 +158,7 @@ export function setupSocket(io: Server) {
|
|||
campaignId: number;
|
||||
characterId: number;
|
||||
}) => {
|
||||
try {
|
||||
const userId = socket.data.user?.userId;
|
||||
|
||||
// Verify caller is DM
|
||||
|
|
@ -174,6 +175,13 @@ export function setupSocket(io: Server) {
|
|||
);
|
||||
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
|
||||
const [charRows] = await db.execute<RowDataPacket[]>(
|
||||
"SELECT name, color, campaign_id FROM characters WHERE id = ?",
|
||||
|
|
@ -182,6 +190,8 @@ export function setupSocket(io: Server) {
|
|||
if (charRows.length === 0) return;
|
||||
const char = charRows[0];
|
||||
|
||||
if (char.campaign_id !== data.campaignId) return;
|
||||
|
||||
// Roll d20 server-side
|
||||
const result = rollDice("1d20");
|
||||
const roll = result.total;
|
||||
|
|
@ -193,9 +203,9 @@ export function setupSocket(io: Server) {
|
|||
|
||||
const [insertResult] = await db.execute<import("mysql2").ResultSetHeader>(
|
||||
`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)
|
||||
VALUES (?, ?, ?, ?, 'custom', ?, '1d20', ?, 0, ?, 0, 0, ?)`,
|
||||
VALUES (?, ?, ?, ?, 'custom', 'death-save', ?, '1d20', ?, 0, ?, 0, 0, ?)`,
|
||||
[
|
||||
data.campaignId,
|
||||
data.characterId,
|
||||
|
|
@ -242,6 +252,9 @@ export function setupSocket(io: Server) {
|
|||
conditions: updatedConditions,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[death:recovery-roll]", err);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue