Make force rarity configurable
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-09-05 19:34:25 +01:00
parent 1a4d4b4e23
commit ada907e2a4
7 changed files with 31 additions and 2 deletions

View file

@ -15,6 +15,8 @@ BOT_CLIENTID=682942374040961060
ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DB_HOST=127.0.0.1
DB_PORT=3301
DB_NAME=carddrop

View file

@ -15,6 +15,8 @@ BOT_CLIENTID=1093810443589529631
ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DB_HOST=127.0.0.1
DB_PORT=3321
DB_NAME=carddrop

View file

@ -15,6 +15,8 @@ BOT_CLIENTID=1147976642942214235
ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DB_HOST=127.0.0.1
DB_PORT=3311
DB_NAME=carddrop

BIN
cards.db Normal file

Binary file not shown.

View file

@ -16,7 +16,11 @@ export default class Drop extends Command {
}
public override async execute(interaction: CommandInteraction) {
const randomCard = await CardDropHelper.GetRandomCard();
let randomCard = await CardDropHelper.GetRandomCard();
if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {
randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY));
}
const image = readFileSync(randomCard.Path);

View file

@ -2,6 +2,7 @@ import { Column, Entity, ManyToOne } from "typeorm";
import CardBaseEntity from "../../../contracts/CardBaseEntity";
import { CardRarity } from "../../../constants/CardRarity";
import Series from "./Series";
import CardDataSource from "../../dataSources/cardDataSource";
@Entity()
export default class Card extends CardBaseEntity {
@ -33,4 +34,12 @@ export default class Card extends CardBaseEntity {
@ManyToOne(() => Series, x => x.Cards)
Series: Series;
public static async FetchAllByRarity(rarity: CardRarity, relations?: string[]): Promise<Card[]> {
const repository = CardDataSource.getRepository(Card);
const all = await repository.find({ where: { Rarity: rarity }, relations: relations || [] });
return all;
}
}

View file

@ -24,7 +24,7 @@ export default class CardDropHelper {
else if (randomRarity < goldChance) cardRarity = CardRarity.Gold;
else cardRarity = CardRarity.Legendary;
const allCards = randomSeries.Cards.filter(x => x.Rarity == cardRarity && x.Path && x.FileName && x.Rarity == CardRarity.Legendary);
const allCards = randomSeries.Cards.filter(x => x.Rarity == cardRarity && x.Path && x.FileName);
const randomCardIndex = Math.floor(Math.random() * allCards.length);
@ -32,4 +32,14 @@ export default class CardDropHelper {
return randomCard;
}
public static async GetRandomCardByRarity(rarity: CardRarity): Promise<Card> {
const allCards = await Card.FetchAllByRarity(rarity, [ "Series" ]);
const randomCardIndex = Math.floor(Math.random() * allCards.length);
const card = allCards[randomCardIndex];
return card;
}
}