Update card database to use JSON files #107
8 changed files with 38 additions and 19 deletions
|
@ -6,14 +6,16 @@ import SeriesMetadata from "../contracts/SeriesMetadata";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
|
|
||||||
export default class CardMetadataFunction {
|
export default class CardMetadataFunction {
|
||||||
public static async Execute(): Promise<boolean> {
|
public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
|
||||||
if (await Config.GetValue('safemode') == "true") return false;
|
if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CoreClient.Cards = await this.FindMetadataJSONs();
|
CoreClient.Cards = await this.FindMetadataJSONs();
|
||||||
|
|
||||||
console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`);
|
console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`);
|
||||||
} catch {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
|
||||||
await Config.SetValue('safemode', 'true');
|
await Config.SetValue('safemode', 'true');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -27,6 +29,7 @@ export default class CardMetadataFunction {
|
||||||
const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json'));
|
const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json'));
|
||||||
|
|
||||||
for (let jsonPath of seriesJSONs) {
|
for (let jsonPath of seriesJSONs) {
|
||||||
|
console.log(`Reading file ${jsonPath}`);
|
||||||
const jsonFile = readFileSync(jsonPath);
|
const jsonFile = readFileSync(jsonPath);
|
||||||
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString());
|
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString());
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ import { CardRarity, CardRarityToString } from "../constants/CardRarity";
|
||||||
import Config from "../database/entities/app/Config";
|
import Config from "../database/entities/app/Config";
|
||||||
|
|
||||||
export default class CardSetupFunction {
|
export default class CardSetupFunction {
|
||||||
public static async Execute(): Promise<boolean> {
|
public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
|
||||||
if (await Config.GetValue('safemode') == "true") return false;
|
if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.ClearDatabase();
|
await this.ClearDatabase();
|
||||||
|
|
|
@ -33,16 +33,24 @@ export default class Reroll extends ButtonEvent {
|
||||||
return;
|
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();
|
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 inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
|
||||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||||
|
|
||||||
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed);
|
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
|
||||||
|
|
||||||
const claimId = v4();
|
const claimId = v4();
|
||||||
|
|
||||||
|
|
|
@ -79,9 +79,9 @@ export class CoreClient extends Client {
|
||||||
super.on("ready", this._events.onReady);
|
super.on("ready", this._events.onReady);
|
||||||
|
|
||||||
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
|
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
|
||||||
await CardMetadataFunction.Execute();
|
await CardMetadataFunction.Execute(true);
|
||||||
} else {
|
} else {
|
||||||
await CardSetupFunction.Execute();
|
await CardSetupFunction.Execute(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._util.loadEvents(this, CoreClient._eventItems);
|
this._util.loadEvents(this, CoreClient._eventItems);
|
||||||
|
|
|
@ -41,16 +41,24 @@ export default class Drop extends Command {
|
||||||
return;
|
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();
|
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 inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
|
||||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||||
|
|
||||||
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed);
|
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
|
||||||
|
|
||||||
const claimId = v4();
|
const claimId = v4();
|
||||||
|
|
||||||
|
|
|
@ -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 interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`);
|
||||||
await Config.SetValue('safemode', 'true');
|
await Config.SetValue('safemode', 'true');
|
||||||
} else {
|
} else {
|
||||||
if (!(process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true')) {
|
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
|
||||||
await CardMetadataFunction.Execute();
|
await CardMetadataFunction.Execute();
|
||||||
} else {
|
} else {
|
||||||
await CardSetupFunction.Execute();
|
await CardSetupFunction.Execute();
|
||||||
|
|
|
@ -26,10 +26,10 @@ export default class Resync extends Command {
|
||||||
|
|
||||||
let result: boolean;
|
let result: boolean;
|
||||||
|
|
||||||
if (!(process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true')) {
|
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
|
||||||
result = await CardMetadataFunction.Execute();
|
result = await CardMetadataFunction.Execute(true);
|
||||||
} else {
|
} else {
|
||||||
result = await CardSetupFunction.Execute();
|
result = await CardSetupFunction.Execute(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
|
|
|
@ -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 = "";
|
let description = "";
|
||||||
description += `Series: ${drop.series.name}\n`;
|
description += `Series: ${drop.series.name}\n`;
|
||||||
description += `Claimed: ${quantityClaimed}\n`;
|
description += `Claimed: ${quantityClaimed}\n`;
|
||||||
|
@ -58,7 +58,7 @@ export default class CardDropHelperMetadata {
|
||||||
.setDescription(description)
|
.setDescription(description)
|
||||||
.setFooter({ text: CardRarityToString(drop.card.type) })
|
.setFooter({ text: CardRarityToString(drop.card.type) })
|
||||||
.setColor(CardRarityToColour(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> {
|
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder<ButtonBuilder> {
|
||||||
|
|
Loading…
Reference in a new issue