dok/routes/booster.route.js
2026-04-11 13:22:26 +01:00

249 lines
7.4 KiB
JavaScript

const express = require("express");
const router = express.Router();
const db = require("../database/database");
/* ================================
Gewichtete Zufallsauswahl
================================ */
function weightedRandom(weights) {
const total = weights.reduce((s, w) => s + w.weight, 0);
let r = Math.random() * total;
for (const entry of weights) {
r -= entry.weight;
if (r <= 0) return entry.rarity;
}
return weights[weights.length - 1].rarity;
}
/* ================================
Gewichte je Spielerlevel
================================ */
function getWeights(playerLevel) {
if (playerLevel < 10)
return [
{ rarity: 1, weight: 95 }, // 95%
{ rarity: 2, weight: 5 }, // 5%
];
if (playerLevel < 20)
return [
{ rarity: 1, weight: 87 }, // 87%
{ rarity: 2, weight: 11 }, // 11%
{ rarity: 3, weight: 2 }, // 2%
];
if (playerLevel < 30)
return [
{ rarity: 1, weight: 80 }, // 80%
{ rarity: 2, weight: 14 }, // 14%
{ rarity: 3, weight: 4 }, // 4%
{ rarity: 4, weight: 2 }, // 2%
];
if (playerLevel < 40)
return [
{ rarity: 1, weight: 75 }, // 75%
{ rarity: 2, weight: 16 }, // 16%
{ rarity: 3, weight: 5 }, // 5%
{ rarity: 4, weight: 3 }, // 3%
{ rarity: 5, weight: 1 }, // 1%
];
return [
{ rarity: 1, weight: 60 }, // 60.0%
{ rarity: 2, weight: 25 }, // 25.0%
{ rarity: 3, weight: 9 }, // 9.0%
{ rarity: 4, weight: 4 }, // 4.0%
{ rarity: 5, weight: 1.5 }, // 1.5%
{ rarity: 6, weight: 0.5 }, // 0.5%
];
}
/* ================================
GET /api/booster/cards
Alle Karten inkl. Stats für Slot-Animation
================================ */
router.get("/booster/cards", async (req, res) => {
if (!req.session?.user)
return res.status(401).json({ error: "Nicht eingeloggt" });
try {
const [cards] = await db.query(
"SELECT id, name, image, icon, max_level, rarity, attack, defends, cooldown, `range`, `race` FROM cards ORDER BY id",
);
res.json(cards);
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
}
});
/* ================================
POST /api/booster/open
5 gewichtete Zufallskarten inkl. Stats
================================ */
router.post("/booster/open", async (req, res) => {
if (!req.session?.user)
return res.status(401).json({ error: "Nicht eingeloggt" });
const userId = req.session.user.id;
try {
const [[account]] = await db.query(
"SELECT level FROM accounts WHERE id = ?",
[userId],
);
const playerLevel = account?.level ?? 1;
const weights = getWeights(playerLevel);
const maxAllowed = Math.max(...weights.map((w) => w.rarity));
const [allCards] = await db.query(
"SELECT id, name, image, icon, max_level, rarity, attack, defends, cooldown, `range`, `race` FROM cards WHERE rarity <= ?",
[maxAllowed],
);
if (!allCards.length)
return res.status(400).json({ error: "Keine Karten verfügbar" });
const result = [];
for (let i = 0; i < 5; i++) {
const targetRarity = weightedRandom(weights);
let pool = allCards.filter((c) => parseInt(c.rarity) === targetRarity);
if (!pool.length) {
for (let fb = targetRarity - 1; fb >= 1; fb--) {
pool = allCards.filter((c) => parseInt(c.rarity) === fb);
if (pool.length) break;
}
}
if (!pool.length) pool = allCards;
result.push(pool[Math.floor(Math.random() * pool.length)]);
}
res.json({ cards: result, playerLevel });
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
}
});
/* ================================
POST /api/booster/save
Gezogene Karten in user_cards speichern
Body: { cardIds: [1, 2, 3, 4, 5] }
================================ */
router.post("/booster/save", async (req, res) => {
if (!req.session?.user)
return res.status(401).json({ error: "Nicht eingeloggt" });
const userId = req.session.user.id;
const cardIds = req.body?.cardIds;
if (!Array.isArray(cardIds) || cardIds.length === 0) {
return res.status(400).json({ error: "Keine Karten übergeben" });
}
try {
for (const cardId of cardIds) {
await db.query(
`INSERT INTO user_cards (user_id, card_id, amount)
VALUES (?, ?, 1)
ON DUPLICATE KEY UPDATE amount = amount + 1`,
[userId, cardId],
);
}
res.json({ success: true });
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler beim Speichern" });
}
});
/* ================================
POST /api/booster/wood-donate
100 Holz bezahlen → 1 zufällige Rarity-3 Karte
================================ */
router.post("/booster/wood-donate", async (req, res) => {
if (!req.session?.user)
return res.status(401).json({ error: "Nicht eingeloggt" });
const userId = req.session.user.id;
try {
// Holz prüfen
const [[currency]] = await db.query(
"SELECT wood FROM account_currency WHERE account_id = ?",
[userId],
);
if (!currency || currency.wood < 100) {
return res.status(400).json({ error: "Nicht genug Holz (100 benötigt)" });
}
// Rarity-3 Karten laden
const [pool] = await db.query(
"SELECT id, name, image, icon, max_level, rarity, attack, defends, cooldown, `range`, `race` FROM cards WHERE rarity = 3",
);
if (!pool.length)
return res.status(400).json({ error: "Keine Rarity-3 Karten verfügbar" });
// 100 Holz abziehen
await db.query(
"UPDATE account_currency SET wood = wood - 100 WHERE account_id = ?",
[userId],
);
// 1 zufällige Rarity-3 Karte ziehen
const card = pool[Math.floor(Math.random() * pool.length)];
// Direkt in user_cards speichern
await db.query(
`INSERT INTO user_cards (user_id, card_id, amount) VALUES (?, ?, 1)
ON DUPLICATE KEY UPDATE amount = amount + 1`,
[userId, card.id],
);
res.json({ card });
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
}
});
/* ================================
POST /api/booster/gold-donate
100 Gold bezahlen → 1 zufällige Rarity-4 Karte
================================ */
router.post("/booster/gold-donate", async (req, res) => {
if (!req.session?.user)
return res.status(401).json({ error: "Nicht eingeloggt" });
const userId = req.session.user.id;
try {
const [[currency]] = await db.query(
"SELECT gold FROM account_currency WHERE account_id = ?",
[userId],
);
if (!currency || currency.gold < 100) {
return res.status(400).json({ error: "Nicht genug Gold (100 benötigt)" });
}
const [pool] = await db.query(
"SELECT id, name, image, icon, max_level, rarity, attack, defends, cooldown, `range`, `race` FROM cards WHERE rarity = 4",
);
if (!pool.length)
return res.status(400).json({ error: "Keine Rarity-4 Karten verfügbar" });
await db.query(
"UPDATE account_currency SET gold = gold - 100 WHERE account_id = ?",
[userId],
);
const card = pool[Math.floor(Math.random() * pool.length)];
await db.query(
`INSERT INTO user_cards (user_id, card_id, amount) VALUES (?, ?, 1)
ON DUPLICATE KEY UPDATE amount = amount + 1`,
[userId, card.id],
);
res.json({ card });
} catch (err) {
console.error(err);
res.status(500).json({ error: "DB Fehler" });
}
});
module.exports = router;