feature/5-drop-command #17

Merged
Vylpes merged 4 commits from feature/5-drop-command into develop 2023-09-03 20:27:31 +01:00
11 changed files with 125 additions and 27 deletions
Showing only changes of commit 1887470e2f - Show all commits

View file

@ -7,7 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.1.0 BOT_VER=0.1.0 DEV
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=682942374040961060 BOT_CLIENTID=682942374040961060

View file

@ -7,7 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.1.0 BOT_VER=0.1.0 BETA
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=1016767908740857949 BOT_CLIENTID=1016767908740857949

View file

@ -42,7 +42,7 @@ export default class CardSetupFunction {
} }
private async ReadCards() { private async ReadCards() {
const loadedSeries = await Series.FetchAll(Series); const loadedSeries = await Series.FetchAll(Series, [ "Cards", "Cards.Series" ]);
const cardRepository = CardDataSource.getRepository(Card); const cardRepository = CardDataSource.getRepository(Card);
@ -65,7 +65,7 @@ export default class CardSetupFunction {
const cardId = filePart[0]; const cardId = filePart[0];
const cardName = filePart[0]; const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Bronze); const card = new Card(cardId, cardName, CardRarity.Bronze, path.join(path.join(process.cwd(), 'cards', series.Path, 'BRONZE', file)), series);
cardsToSave.push(card); cardsToSave.push(card);
} }
@ -76,7 +76,7 @@ export default class CardSetupFunction {
const cardId = filePart[0]; const cardId = filePart[0];
const cardName = filePart[0]; const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Gold); const card = new Card(cardId, cardName, CardRarity.Gold, path.join(path.join(process.cwd(), 'cards', series.Path, 'GOLD', file)), series);
cardsToSave.push(card); cardsToSave.push(card);
} }
@ -87,7 +87,7 @@ export default class CardSetupFunction {
const cardId = filePart[0]; const cardId = filePart[0];
const cardName = filePart[0]; const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Legendary); const card = new Card(cardId, cardName, CardRarity.Legendary, path.join(path.join(process.cwd(), 'cards', series.Path, 'LEGENDARY', file)), series);
cardsToSave.push(card); cardsToSave.push(card);
} }
@ -98,7 +98,7 @@ export default class CardSetupFunction {
const cardId = filePart[0]; const cardId = filePart[0];
const cardName = filePart[0]; const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Silver); const card = new Card(cardId, cardName, CardRarity.Silver, path.join(path.join(process.cwd(), 'cards', series.Path, 'SILVER', file)), series);
cardsToSave.push(card); cardsToSave.push(card);
} }

View file

@ -9,6 +9,7 @@ import { Events } from "./events";
import { Util } from "./util"; import { Util } from "./util";
import CardSetupFunction from "../Functions/CardSetupFunction"; import CardSetupFunction from "../Functions/CardSetupFunction";
import CardDataSource from "../database/dataSources/cardDataSource"; import CardDataSource from "../database/dataSources/cardDataSource";
import CardDropHelper from "../helpers/CardDropHelper";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];

35
src/commands/drop.ts Normal file
View file

@ -0,0 +1,35 @@
import { AttachmentBuilder, CommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
import { Command } from "../type/command";
import CardDropHelper from "../helpers/CardDropHelper";
import { CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
import { readFileSync } from "fs";
export default class Drop extends Command {
constructor() {
super();
super.CommandBuilder = new SlashCommandBuilder()
.setName('drop')
.setDescription('Summon a new card drop');
}
public override async execute(interaction: CommandInteraction) {
const randomCard = await CardDropHelper.GetRandomCard();
const image = readFileSync(randomCard.Path);
const attachment = new AttachmentBuilder(image, { name: `${randomCard.Id}.png` });
const embed = new EmbedBuilder()
.setTitle(randomCard.Name)
.setDescription(randomCard.Series.Name)
.setFooter({ text: CardRarityToString(randomCard.Rarity) })
.setColor(CardRarityToColour(randomCard.Rarity))
.setImage(`attachment://${randomCard.Id}.png`);
await interaction.reply({
embeds: [ embed ],
files: [ attachment ],
});
}
}

View file

@ -1,6 +1,34 @@
import EmbedColours from "./EmbedColours";
export enum CardRarity { export enum CardRarity {
Bronze, Bronze,
Silver, Silver,
Gold, Gold,
Legendary, Legendary,
} }
export function CardRarityToString(rarity: CardRarity): string {
switch (rarity) {
case CardRarity.Bronze:
return "Bronze";
case CardRarity.Silver:
return "Silver";
case CardRarity.Gold:
return "Gold";
case CardRarity.Legendary:
return "Legendary";
}
}
export function CardRarityToColour(rarity: CardRarity): number {
switch (rarity) {
case CardRarity.Bronze:
return EmbedColours.BronzeCard;
case CardRarity.Silver:
return EmbedColours.SilverCard;
case CardRarity.Gold:
return EmbedColours.GoldCard;
case CardRarity.Legendary:
return EmbedColours.LegendaryCard;
}
}

View file

@ -1,3 +1,7 @@
export default class EmbedColours { export default class EmbedColours {
public static readonly Ok = 0x3050ba; public static readonly Ok = 0x3050ba;
public static readonly BronzeCard = 0xcd7f32;
public static readonly SilverCard = 0xc0c0c0;
public static readonly GoldCard = 0xffd700;
public static readonly LegendaryCard = 0x50c878;
} }

View file

@ -1,17 +1,18 @@
import { Column, Entity, OneToMany } from "typeorm"; import { Column, Entity, ManyToOne } from "typeorm";
import CardBaseEntity from "../../../contracts/CardBaseEntity"; import CardBaseEntity from "../../../contracts/CardBaseEntity";
import { CardRarity } from "../../../constants/CardRarity"; import { CardRarity } from "../../../constants/CardRarity";
import Series from "./Series"; import Series from "./Series";
import CardDataSource from "../../dataSources/cardDataSource";
@Entity() @Entity()
export default class Card extends CardBaseEntity { export default class Card extends CardBaseEntity {
constructor(cardNumber: string, name: string, rarity: CardRarity) { constructor(cardNumber: string, name: string, rarity: CardRarity, path: string, series: Series) {
super(); super();
this.CardNumber = cardNumber; this.CardNumber = cardNumber;
this.Name = name; this.Name = name;
this.Rarity = rarity; this.Rarity = rarity;
this.Path = path;
this.Series = series;
} }
@Column() @Column()
@ -23,14 +24,9 @@ export default class Card extends CardBaseEntity {
@Column() @Column()
Rarity: CardRarity; Rarity: CardRarity;
@OneToMany(() => Series, x => x.Cards) @Column()
Path: string
@ManyToOne(() => Series, x => x.Cards)
Series: Series; Series: Series;
public static async FetchAllByRarity(rarity: CardRarity): Promise<Card[]> {
const repository = CardDataSource.getRepository(Card);
const all = await repository.find({ where: { Rarity: rarity }});
return all;
}
} }

View file

@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne } from "typeorm"; import { Column, Entity, OneToMany } from "typeorm";
import CardBaseEntity from "../../../contracts/CardBaseEntity"; import CardBaseEntity from "../../../contracts/CardBaseEntity";
import Card from "./Card"; import Card from "./Card";
@ -18,12 +18,6 @@ export default class Series extends CardBaseEntity {
@Column() @Column()
Path: string; Path: string;
@ManyToOne(() => Card, x => x.Series) @OneToMany(() => Card, x => x.Series)
Cards: Card[]; Cards: Card[];
public async AddCard(card: Card) {
if (!this.Cards) return;
this.Cards.push(card);
}
} }

View file

@ -0,0 +1,38 @@
import { CardRarity } from "../constants/CardRarity";
import CardDataSource from "../database/dataSources/cardDataSource";
import Card from "../database/entities/card/Card";
import Series from "../database/entities/card/Series";
export default class CardDropHelper {
public static async GetRandomCard(): Promise<Card> {
const seriesRepository = CardDataSource.getRepository(Series);
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;
const bronzeChance = 62;
const silverChance = bronzeChance + 31;
const goldChance = silverChance + 6.4;
if (randomRarity < bronzeChance) cardRarity = CardRarity.Bronze;
else if (randomRarity < silverChance) cardRarity = CardRarity.Silver;
else if (randomRarity < goldChance) cardRarity = CardRarity.Gold;
else cardRarity = CardRarity.Legendary;
const allCards = randomSeries.Cards.filter(x => x.Rarity == cardRarity);
const randomCardIndex = Math.floor(Math.random() * allCards.length);
const randomCard = allCards[randomCardIndex];
return randomCard;
}
}

View file

@ -1,10 +1,12 @@
import { CoreClient } from "./client/client"; import { CoreClient } from "./client/client";
import About from "./commands/about"; import About from "./commands/about";
import Drop from "./commands/drop";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
CoreClient.RegisterCommand('about', new About()); CoreClient.RegisterCommand('about', new About());
CoreClient.RegisterCommand('drop', new Drop());
} }
public static RegisterEvents() { public static RegisterEvents() {