From 47f4ee646419839e5fc61500cc95b6776c7222ee Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Sat, 19 Oct 2024 23:49:27 +0200 Subject: [PATCH] added no dupe for lootbox --- backend/controller/userController.ts | 11 ++++++++--- shared/lootboxes.ts | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/controller/userController.ts b/backend/controller/userController.ts index edbc884..5f24a84 100644 --- a/backend/controller/userController.ts +++ b/backend/controller/userController.ts @@ -147,10 +147,15 @@ export const userController = createController({ if (!lootbox) { throw new Error("Lootbox not found"); } + const itemsCopy = [...lootbox.items]; + if (lootbox.noDuplicates) { + itemsCopy.filter((i) => !collection.entries.some((e) => e.id === i.id)); + } + if (itemsCopy.length === 0) { + throw new Error("No items left"); + } await removeGems(db, user, lootbox.price); - const result = weightedPickRandom(lootbox.items, (i) => - getWeight(i.rarity), - ); + const result = weightedPickRandom(itemsCopy, (i) => getWeight(i.rarity)); collection.entries.push({ id: result.id, aquired: Date.now(), diff --git a/shared/lootboxes.ts b/shared/lootboxes.ts index 402154a..27f0279 100644 --- a/shared/lootboxes.ts +++ b/shared/lootboxes.ts @@ -37,6 +37,7 @@ interface Lootbox { price: number; priceText: string; image: string; + noDuplicates: boolean; items: { id: ThemeId; rarity: Rarity; @@ -49,6 +50,7 @@ export const series1: Lootbox = { price: 5000, priceText: "5.000", image: lootbox1, + noDuplicates: false, items: [ { id: "basic", @@ -218,6 +220,7 @@ export const halloween: Lootbox = { id: "halloween", price: 6000, priceText: "6.000", + noDuplicates: true, image: lootboxEvent1, items: [ {