Refactor cape selection logic and remove trigger

Removed the 'unique_active_cape' database trigger and updated the showCape function to manually ensure only one cape is selected per player. This change centralizes the selection logic in application code for better maintainability and error handling.
This commit is contained in:
Gilles Lazures 2026-01-18 15:40:00 +01:00
parent b6b7cf7fe0
commit c6afafca2a
2 changed files with 12 additions and 22 deletions

View File

@ -258,20 +258,6 @@ async function setupDatabase() {
logger.log(`${"unique_active_skin".bold} trigger ready`, ["MariaDB", "yellow"]) logger.log(`${"unique_active_skin".bold} trigger ready`, ["MariaDB", "yellow"])
await conn.query(`DROP TRIGGER IF EXISTS unique_active_cape`) await conn.query(`DROP TRIGGER IF EXISTS unique_active_cape`)
await conn.query(`
CREATE TRIGGER unique_active_cape
AFTER UPDATE ON playersCapes
FOR EACH ROW
BEGIN
IF NEW.isSelected = 1 THEN
UPDATE playersCapes
SET isSelected = 0
WHERE playerUuid = NEW.playerUuid
AND id != NEW.id;
END IF;
END;
`)
logger.log(`${"unique_active_cape".bold} trigger ready`, ["MariaDB", "yellow"])
await conn.query(`DROP TRIGGER IF EXISTS auto_assign_random_default_skin`) await conn.query(`DROP TRIGGER IF EXISTS auto_assign_random_default_skin`)
await conn.query(` await conn.query(`

View File

@ -416,15 +416,19 @@ async function hideCape(uuid) {
async function showCape(uuid, hash) { async function showCape(uuid, hash) {
try { try {
const sql = ` await database.query(
UPDATE playersCapes "UPDATE playersCapes SET isSelected = 0 WHERE playerUuid = ?",
SET isSelected = (assetHash = ?) [uuid]
WHERE playerUuid = ? )
`
const result = await database.query(sql, [hash, uuid]) const [res] = await database.query(
return { code: 200, changed: result.affectedRows > 0 } "UPDATE playersCapes SET isSelected = 1 WHERE playerUuid = ? AND assetHash = ?",
[uuid, hash]
)
return { code: 200, changed: res.affectedRows > 0 }
} catch (error) { } catch (error) {
logger.log("Internal Server Error".bold + " : " + error.toString(), ["MariaDB", "yellow"]) logger.log("Database Error: " + error.toString(), ["MariaDB", "red"])
throw new DefaultError(500, "Internal Server Error", "Database Error") throw new DefaultError(500, "Internal Server Error", "Database Error")
} }
} }