Add manga type and general refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-10-28 18:57:50 +01:00
parent a8062f20ae
commit 2899b876b4
8 changed files with 65 additions and 59 deletions

View file

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

View file

@ -7,7 +7,7 @@
# any secret values.
BOT_TOKEN=
BOT_VER=0.1.4
BOT_VER=0.1.7
BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816
BOT_CLIENTID=1093810443589529631

View file

@ -7,7 +7,7 @@
# any secret values.
BOT_TOKEN=
BOT_VER=0.1.4 BETA
BOT_VER=0.1.7 BETA
BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816
BOT_CLIENTID=1147976642942214235

View file

@ -3,7 +3,7 @@ import CardDataSource from "../database/dataSources/cardDataSource";
import Card from "../database/entities/card/Card";
import Series from "../database/entities/card/Series";
import path from "path";
import { CardRarity } from "../constants/CardRarity";
import { CardRarity, CardRarityToString } from "../constants/CardRarity";
export default class CardSetupFunction {
public async Execute() {
@ -49,63 +49,47 @@ export default class CardSetupFunction {
const cardsToSave: Card[] = [];
for (let series of loadedSeries) {
const bronzeExists = existsSync(path.join(process.cwd(), 'cards', series.Path, 'BRONZE'));
const goldExists = existsSync(path.join(process.cwd(), 'cards', series.Path, 'GOLD'));
const legendaryExists = existsSync(path.join(process.cwd(), 'cards', series.Path, 'LEGENDARY'));
const silverExists = existsSync(path.join(process.cwd(), 'cards', series.Path, 'SILVER'));
const cardDirBronze = this.GetCardFiles(CardRarity.Bronze, series);
const cardDirGold = this.GetCardFiles(CardRarity.Gold, series);
const cardDirLegendary = this.GetCardFiles(CardRarity.Legendary, series);
const cardDirSilver = this.GetCardFiles(CardRarity.Silver, series);
const cardDirManga = this.GetCardFiles(CardRarity.Manga, series);
const cardDirBronze = bronzeExists ? readdirSync(path.join(process.cwd(), 'cards', series.Path, 'BRONZE')) : [];
const cardDirGold = goldExists ? readdirSync(path.join(process.cwd(), 'cards', series.Path, 'GOLD')) : [];
const cardDirLegendary = legendaryExists ? readdirSync(path.join(process.cwd(), 'cards', series.Path, 'LEGENDARY')) : [];
const cardDirSilver = silverExists ? readdirSync(path.join(process.cwd(), 'cards', series.Path, 'SILVER')) : [];
for (let file of cardDirBronze.filter(x => !x.startsWith('.') && (x.endsWith('.png') || x.endsWith('.jpg') || x.endsWith('.gif')))) {
const filePart = file.split('.');
const cardId = filePart[0];
const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Bronze, path.join(path.join(process.cwd(), 'cards', series.Path, 'BRONZE', file)), file, series);
cardsToSave.push(card);
}
for (let file of cardDirGold.filter(x => !x.startsWith('.') && (x.endsWith('.png') || x.endsWith('.jpg') || x.endsWith('.gif')))) {
const filePart = file.split('.');
const cardId = filePart[0];
const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Gold, path.join(path.join(process.cwd(), 'cards', series.Path, 'GOLD', file)), file, series);
cardsToSave.push(card);
}
for (let file of cardDirLegendary.filter(x => !x.startsWith('.') && (x.endsWith('.png') || x.endsWith('.jpg') || x.endsWith('.gif')))) {
const filePart = file.split('.');
const cardId = filePart[0];
const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Legendary, path.join(path.join(process.cwd(), 'cards', series.Path, 'LEGENDARY', file)), file, series);
cardsToSave.push(card);
}
for (let file of cardDirSilver.filter(x => !x.startsWith('.') && (x.endsWith('.png') || x.endsWith('.jpg') || x.endsWith('.gif')))) {
const filePart = file.split('.');
const cardId = filePart[0];
const cardName = filePart[0];
const card = new Card(cardId, cardName, CardRarity.Silver, path.join(path.join(process.cwd(), 'cards', series.Path, 'SILVER', file)), file, series);
cardsToSave.push(card);
}
cardsToSave.push(
...this.GenerateCardData(cardDirBronze, CardRarity.Bronze, series),
...this.GenerateCardData(cardDirGold, CardRarity.Gold, series),
...this.GenerateCardData(cardDirLegendary, CardRarity.Legendary, series),
...this.GenerateCardData(cardDirSilver, CardRarity.Silver, series),
...this.GenerateCardData(cardDirManga, CardRarity.Manga, series)
);
}
await cardRepository.save(cardsToSave);
console.log(`Loaded ${cardsToSave.length} cards to database`);
}
private GenerateCardData(files: string[], rarity: CardRarity, series: Series): Card[] {
const result: Card[] = [];
for (let file of files.filter(x => !x.startsWith('.') && (x.endsWith('.png') || x.endsWith('.jpg') || x.endsWith('.gif')))) {
const filePart = file.split('.');
const cardId = filePart[0];
const cardName = filePart[0];
const card = new Card(cardId, cardName, rarity, path.join(process.cwd(), 'cards', series.Path, CardRarityToString(rarity).toUpperCase(), file), file, series);
result.push(card);
}
return result;
}
private GetCardFiles(rarity: CardRarity, series: Series): string[] {
const folder = path.join(process.cwd(), 'cards', series.Path, CardRarityToString(rarity).toUpperCase());
const folderExists = existsSync(folder);
return folderExists ? readdirSync(folder) : [];
}
}

View file

@ -1,14 +1,18 @@
import EmbedColours from "./EmbedColours";
export enum CardRarity {
Unknown,
Bronze,
Silver,
Gold,
Manga,
Legendary,
}
export function CardRarityToString(rarity: CardRarity): string {
switch (rarity) {
case CardRarity.Unknown:
return "Unknown";
case CardRarity.Bronze:
return "Bronze";
case CardRarity.Silver:
@ -17,11 +21,15 @@ export function CardRarityToString(rarity: CardRarity): string {
return "Gold";
case CardRarity.Legendary:
return "Legendary";
case CardRarity.Manga:
return "Manga";
}
}
export function CardRarityToColour(rarity: CardRarity): number {
switch (rarity) {
case CardRarity.Unknown:
return EmbedColours.Grey;
case CardRarity.Bronze:
return EmbedColours.BronzeCard;
case CardRarity.Silver:
@ -30,5 +38,7 @@ export function CardRarityToColour(rarity: CardRarity): number {
return EmbedColours.GoldCard;
case CardRarity.Legendary:
return EmbedColours.LegendaryCard;
case CardRarity.Manga:
return EmbedColours.MangaCard;
}
}

View file

@ -0,0 +1,7 @@
export default class CardRarityChances {
public static readonly Bronze = 62;
public static readonly Silver = 31;
public static readonly Gold = 4.4;
public static readonly Manga = 2;
// Legendary therefore = 0.6;
}

View file

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

View file

@ -1,4 +1,5 @@
import { CardRarity } from "../constants/CardRarity";
import CardRarityChances from "../constants/CardRarityChances";
import Card from "../database/entities/card/Card";
import Series from "../database/entities/card/Series";
@ -8,13 +9,15 @@ export default class CardDropHelper {
let cardRarity: CardRarity;
const bronzeChance = 62;
const silverChance = bronzeChance + 31;
const goldChance = silverChance + 6.4;
const bronzeChance = CardRarityChances.Bronze;
const silverChance = bronzeChance + CardRarityChances.Silver;
const goldChance = silverChance + CardRarityChances.Gold;
const mangaChance = goldChance + CardRarityChances.Manga;
if (randomRarity < bronzeChance) cardRarity = CardRarity.Bronze;
else if (randomRarity < silverChance) cardRarity = CardRarity.Silver;
else if (randomRarity < goldChance) cardRarity = CardRarity.Gold;
else if (randomRarity < mangaChance) cardRarity = CardRarity.Manga;
else cardRarity = CardRarity.Legendary;
const allSeries = await Series.FetchAll(Series, [ "Cards", "Cards.Series" ]);