116 lines
3.6 KiB
JavaScript
116 lines
3.6 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: 70 }, // 70.0%
|
|
{ rarity: 2, weight: 18 }, // 18.0%
|
|
{ rarity: 3, weight: 7 }, // 7.0%
|
|
{ rarity: 4, weight: 3 }, // 3.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 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 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" });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|