This commit is contained in:
cay 2026-04-06 13:55:20 +01:00
parent cd927f67ba
commit 5d10ae0f35

View File

@ -10,9 +10,9 @@ function weightedRandom(weights) {
let r = Math.random() * total;
for (const entry of weights) {
r -= entry.weight;
if (r <= 0) return entry.maxLevel;
if (r <= 0) return entry.rarity;
}
return weights[weights.length - 1].maxLevel;
return weights[weights.length - 1].rarity;
}
/* ================================
@ -20,34 +20,34 @@ function weightedRandom(weights) {
================================ */
function getWeights(playerLevel) {
if (playerLevel < 10) return [
{ maxLevel: 1, weight: 85 },
{ maxLevel: 2, weight: 15 },
{ rarity: 1, weight: 85 },
{ rarity: 2, weight: 15 },
];
if (playerLevel < 20) return [
{ maxLevel: 1, weight: 65 },
{ maxLevel: 2, weight: 27 },
{ maxLevel: 3, weight: 8 },
{ rarity: 1, weight: 65 },
{ rarity: 2, weight: 27 },
{ rarity: 3, weight: 8 },
];
if (playerLevel < 30) return [
{ maxLevel: 1, weight: 55 },
{ maxLevel: 2, weight: 26 },
{ maxLevel: 3, weight: 13 },
{ maxLevel: 4, weight: 6 },
{ rarity: 1, weight: 55 },
{ rarity: 2, weight: 26 },
{ rarity: 3, weight: 13 },
{ rarity: 4, weight: 6 },
];
if (playerLevel < 40) return [
{ maxLevel: 1, weight: 50 },
{ maxLevel: 2, weight: 25 },
{ maxLevel: 3, weight: 14 },
{ maxLevel: 4, weight: 7 },
{ maxLevel: 5, weight: 4 },
{ rarity: 1, weight: 50 },
{ rarity: 2, weight: 25 },
{ rarity: 3, weight: 14 },
{ rarity: 4, weight: 7 },
{ rarity: 5, weight: 4 },
];
return [
{ maxLevel: 1, weight: 47 },
{ maxLevel: 2, weight: 25 },
{ maxLevel: 3, weight: 15 },
{ maxLevel: 4, weight: 8 },
{ maxLevel: 5, weight: 4.5 },
{ maxLevel: 6, weight: 0.5 },
{ rarity: 1, weight: 47 },
{ rarity: 2, weight: 25 },
{ rarity: 3, weight: 15 },
{ rarity: 4, weight: 8 },
{ rarity: 5, weight: 4.5 },
{ rarity: 6, weight: 0.5 },
];
}
@ -82,10 +82,10 @@ router.post("/booster/open", async (req, res) => {
);
const playerLevel = account?.level ?? 1;
const weights = getWeights(playerLevel);
const maxAllowed = Math.max(...weights.map(w => w.maxLevel));
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 max_level <= ?",
"SELECT id, name, image, icon, max_level, rarity, attack, defends, cooldown FROM cards WHERE rarity <= ?",
[maxAllowed]
);
@ -93,11 +93,11 @@ router.post("/booster/open", async (req, res) => {
const result = [];
for (let i = 0; i < 5; i++) {
const targetLevel = weightedRandom(weights);
let pool = allCards.filter(c => c.max_level === targetLevel);
const targetRarity = weightedRandom(weights);
let pool = allCards.filter(c => parseInt(c.rarity) === targetRarity);
if (!pool.length) {
for (let fb = targetLevel - 1; fb >= 1; fb--) {
pool = allCards.filter(c => c.max_level === fb);
for (let fb = targetRarity - 1; fb >= 1; fb--) {
pool = allCards.filter(c => parseInt(c.rarity) === fb);
if (pool.length) break;
}
}