From 0e839992e6ee72bb850786ad047fbbeb2c5980bc Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 10 Sep 2023 13:01:04 +0100 Subject: [PATCH] Fix bug where the randomiser would sometimes pick a legendary that does not exist --- src/client/client.ts | 1 - src/helpers/CardDropHelper.ts | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index b517a28..66907bb 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -9,7 +9,6 @@ import { Events } from "./events"; import { Util } from "./util"; import CardSetupFunction from "../Functions/CardSetupFunction"; import CardDataSource from "../database/dataSources/cardDataSource"; -import CardDropHelper from "../helpers/CardDropHelper"; import IButtonEventItem from "../contracts/IButtonEventItem"; import { ButtonEvent } from "../type/buttonEvent"; import AppDataSource from "../database/dataSources/appDataSource"; diff --git a/src/helpers/CardDropHelper.ts b/src/helpers/CardDropHelper.ts index a7d0a02..d5d8743 100644 --- a/src/helpers/CardDropHelper.ts +++ b/src/helpers/CardDropHelper.ts @@ -4,13 +4,6 @@ import Series from "../database/entities/card/Series"; export default class CardDropHelper { public static async GetRandomCard(): Promise { - const allSeries = await Series.FetchAll(Series, [ "Cards", "Cards.Series" ]); - const allSeriesWithCards = allSeries.filter(x => x.Cards.length > 0); - - const randomSeriesIndex = Math.floor(Math.random() * allSeriesWithCards.length); - - const randomSeries = allSeriesWithCards[randomSeriesIndex]; - const randomRarity = Math.random() * 100; let cardRarity: CardRarity; @@ -24,6 +17,13 @@ export default class CardDropHelper { else if (randomRarity < goldChance) cardRarity = CardRarity.Gold; else cardRarity = CardRarity.Legendary; + const allSeries = await Series.FetchAll(Series, [ "Cards", "Cards.Series" ]); + const allSeriesWithCards = allSeries.filter(x => x.Cards.length > 0 && x.Cards.find(x => x.Rarity == cardRarity)); + + const randomSeriesIndex = Math.floor(Math.random() * allSeriesWithCards.length); + + const randomSeries = allSeriesWithCards[randomSeriesIndex]; + const allCards = randomSeries.Cards.filter(x => x.Rarity == cardRarity && x.Path && x.FileName); const randomCardIndex = Math.floor(Math.random() * allCards.length);