diff --git a/server/src/routes/characters.ts b/server/src/routes/characters.ts index 6d38281..974c256 100644 --- a/server/src/routes/characters.ts +++ b/server/src/routes/characters.ts @@ -94,7 +94,18 @@ router.get("/", async (req, res) => { router.post("/", requireAuth, async (req, res) => { try { const { campaignId } = req.params; - const { name, class: charClass, ancestry, hp_max } = req.body; + const { + name, + class: charClass, + ancestry, + hp_max, + alignment, + background, + deity, + title, + gp, + stats, + } = req.body; if (!name?.trim()) { res.status(400).json({ error: "Character name is required" }); @@ -105,26 +116,36 @@ router.post("/", requireAuth, async (req, res) => { const [result] = await db.execute( `INSERT INTO characters - (campaign_id, user_id, name, class, ancestry, hp_current, hp_max, color) - VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, + (campaign_id, user_id, name, class, ancestry, hp_current, hp_max, + alignment, background, deity, title, gp, color) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [ campaignId, userId, name.trim(), charClass ?? "Fighter", ancestry ?? "Human", - hp_max ?? 0, - hp_max ?? 0, + hp_max ?? 1, + hp_max ?? 1, + alignment ?? "Neutral", + background ?? "", + deity ?? "", + title ?? "", + gp ?? 0, generateCharacterColor(), ] ); const characterId = result.insertId; + const statNames = ["STR", "DEX", "CON", "INT", "WIS", "CHA"]; + const providedStats: Record = + stats && typeof stats === "object" ? (stats as Record) : {}; + await Promise.all( - DEFAULT_STATS.map((stat) => + statNames.map((stat) => db.execute( - "INSERT INTO character_stats (character_id, stat_name, value) VALUES (?, ?, 10)", - [characterId, stat] + "INSERT INTO character_stats (character_id, stat_name, value) VALUES (?, ?, ?)", + [characterId, stat, providedStats[stat] ?? 10] ) ) ); @@ -136,7 +157,7 @@ router.post("/", requireAuth, async (req, res) => { const enriched = { ...charRows[0], overrides: {}, - stats: DEFAULT_STATS.map((s) => ({ stat_name: s, value: 10 })), + stats: statNames.map((s) => ({ stat_name: s, value: providedStats[s] ?? 10 })), gear: [], talents: [], };