Update card database to use JSON files #107

Merged
Vylpes merged 6 commits from feature/27-json-setup into develop 2023-12-09 16:48:20 +00:00
8 changed files with 38 additions and 19 deletions
Showing only changes of commit 9bf145c4e3 - Show all commits

View file

@ -6,14 +6,16 @@ import SeriesMetadata from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client";
export default class CardMetadataFunction {
public static async Execute(): Promise<boolean> {
if (await Config.GetValue('safemode') == "true") return false;
public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
try {
CoreClient.Cards = await this.FindMetadataJSONs();
console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`);
} catch {
} catch (e) {
console.error(e);
await Config.SetValue('safemode', 'true');
return false;
}
@ -27,6 +29,7 @@ export default class CardMetadataFunction {
const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json'));
for (let jsonPath of seriesJSONs) {
console.log(`Reading file ${jsonPath}`);
const jsonFile = readFileSync(jsonPath);
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString());

View file

@ -7,8 +7,8 @@ import { CardRarity, CardRarityToString } from "../constants/CardRarity";
import Config from "../database/entities/app/Config";
export default class CardSetupFunction {
public static async Execute(): Promise<boolean> {
if (await Config.GetValue('safemode') == "true") return false;
public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
try {
await this.ClearDatabase();

View file

@ -33,16 +33,24 @@ export default class Reroll extends ButtonEvent {
return;
}
const image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
let image: Buffer;
const imageFileName = randomCard.card.path.split("/").pop()!;
try {
image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
} catch {
await interaction.reply(`Unable to fetch image for card ${randomCard.card.id}`);
return;
}
await interaction.deferReply();
const attachment = new AttachmentBuilder(image, { name: randomCard.card.id });
const attachment = new AttachmentBuilder(image, { name: imageFileName });
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed);
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4();

View file

@ -79,9 +79,9 @@ export class CoreClient extends Client {
super.on("ready", this._events.onReady);
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
await CardMetadataFunction.Execute();
await CardMetadataFunction.Execute(true);
} else {
await CardSetupFunction.Execute();
await CardSetupFunction.Execute(true);
}
this._util.loadEvents(this, CoreClient._eventItems);

View file

@ -41,16 +41,24 @@ export default class Drop extends Command {
return;
}
const image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
let image: Buffer;
const imageFileName = randomCard.card.path.split("/").pop()!;
try {
image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
} catch {
await interaction.reply(`Unable to fetch image for card ${randomCard.card.id}`);
return;
}
await interaction.deferReply();
const attachment = new AttachmentBuilder(image, { name: randomCard.card.id });
const attachment = new AttachmentBuilder(image, { name: imageFileName });
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed);
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4();

View file

@ -35,7 +35,7 @@ export default class Gdrivesync extends Command {
await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`);
await Config.SetValue('safemode', 'true');
} else {
if (!(process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true')) {
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
await CardMetadataFunction.Execute();
} else {
await CardSetupFunction.Execute();

View file

@ -26,10 +26,10 @@ export default class Resync extends Command {
let result: boolean;
if (!(process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true')) {
result = await CardMetadataFunction.Execute();
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
result = await CardMetadataFunction.Execute(true);
} else {
result = await CardSetupFunction.Execute();
result = await CardSetupFunction.Execute(true);
}
if (result) {

View file

@ -48,7 +48,7 @@ export default class CardDropHelperMetadata {
};
}
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: Number): EmbedBuilder {
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: Number, imageFileName: string): EmbedBuilder {
let description = "";
description += `Series: ${drop.series.name}\n`;
description += `Claimed: ${quantityClaimed}\n`;
@ -58,7 +58,7 @@ export default class CardDropHelperMetadata {
.setDescription(description)
.setFooter({ text: CardRarityToString(drop.card.type) })
.setColor(CardRarityToColour(drop.card.type))
.setImage(`attachment://${drop.card.id}`);
.setImage(`attachment://${imageFileName}`);
}
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder<ButtonBuilder> {