mirror of
https://github.com/tcgdex/compiler.git
synced 2025-07-28 23:19:50 +00:00
Nearly finished 😄
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
146
utils/cardUtil.ts
Normal file
146
utils/cardUtil.ts
Normal file
@ -0,0 +1,146 @@
|
||||
import { getSet, setToSetSimple } from "./setUtil"
|
||||
import { fetchRemoteFile, smartGlob } from "./util"
|
||||
import { Set, SupportedLanguages, Card } from 'db/interfaces'
|
||||
import fetch from 'node-fetch'
|
||||
import { Card as CardSingle, CardResume } from '@tcgdex/sdk/interfaces'
|
||||
|
||||
interface ObjectList<T = any> {
|
||||
[key: string]: T
|
||||
}
|
||||
|
||||
type RemoteData = ObjectList<ObjectList<ObjectList<boolean>>>
|
||||
|
||||
export async function cardToCardSimple(id: string, card: Card, lang: SupportedLanguages): Promise<CardResume> {
|
||||
const cardName = card.name[lang]
|
||||
if (!cardName) {
|
||||
throw new Error(`Card (${card.set.id}-${id}) has no name in (${lang})`)
|
||||
}
|
||||
const img = await getCardPictures(id, card, lang)
|
||||
return {
|
||||
id: `${card.set.id}-${id}`,
|
||||
localId: id,
|
||||
name: cardName,
|
||||
image: img
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCardPictures(cardId: string, card: Card, lang: SupportedLanguages): Promise<string | undefined> {
|
||||
try {
|
||||
const file = await fetchRemoteFile(`https://assets.tcgdex.net/data-${lang}.json`)
|
||||
if (file[card.set.serie.id][card.set.id][cardId]) {
|
||||
return `https://assets.tcgdex.net/${lang}/${card.set.serie.id}/${card.set.id}/${cardId}`
|
||||
}
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export async function cardToCardSingle(localId: string, card: Card, lang: SupportedLanguages): Promise<CardSingle> {
|
||||
|
||||
const image = await getCardPictures(localId, card, lang)
|
||||
|
||||
if (!card.name[lang]) {
|
||||
throw new Error(`Card (${localId}) dont exist in (${lang})`)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return {
|
||||
id: `${card.set.id}-${localId}`,
|
||||
localId: localId,
|
||||
name: card.name[lang] as string,
|
||||
image: image,
|
||||
|
||||
illustrator: card.illustrator,
|
||||
rarity: card.rarity, // translate
|
||||
category: card.category, // translate
|
||||
variants: {
|
||||
normal: typeof card.variants?.normal === 'boolean' ? card.variants.normal : typeof card.set.variants?.normal === 'boolean' ? card.set.variants.normal : true,
|
||||
reverse: typeof card.variants?.reverse === 'boolean' ? card.variants.reverse : typeof card.set.variants?.reverse === 'boolean' ? card.set.variants.reverse : true,
|
||||
holo: typeof card.variants?.holo === 'boolean' ? card.variants.holo : typeof card.set.variants?.holo === 'boolean' ? card.set.variants.holo : true,
|
||||
firstEdition: typeof card.variants?.firstEdition === 'boolean' ? card.variants.firstEdition : typeof card.set.variants?.firstEdition === 'boolean' ? card.set.variants.firstEdition : false,
|
||||
},
|
||||
|
||||
set: await setToSetSimple(card.set, lang),
|
||||
|
||||
dexId: card.dexId,
|
||||
hp: card.hp,
|
||||
types: card.types, // translate
|
||||
evolveFrom: card.evolveFrom && card.evolveFrom[lang],
|
||||
weight: card.weight,
|
||||
description: card.description ? card.description[lang] as string : undefined,
|
||||
level: card.level,
|
||||
stage: card.stage, // translate
|
||||
suffix: card.suffix, // translate
|
||||
item: card.item ? {
|
||||
name: card.item.name[lang] as string,
|
||||
effect: card.item.effect[lang] as string
|
||||
} : undefined,
|
||||
|
||||
abilities: card.abilities?.map((el) => ({
|
||||
type: el.type, // translate
|
||||
name: el.name[lang] as string,
|
||||
effect: el.effect[lang] as string
|
||||
})),
|
||||
|
||||
attacks: card.attacks?.map((el) => ({
|
||||
cost: el.cost,
|
||||
name: el.name[lang] as string,
|
||||
effect: el.effect ? el.effect[lang] : undefined,
|
||||
damage: el.damage
|
||||
})),
|
||||
|
||||
weaknesses: card.weaknesses?.map((el) => ({
|
||||
type: el.type, // translate
|
||||
value: el.value
|
||||
})),
|
||||
|
||||
resistances: card.resistances?.map((el) => ({
|
||||
type: el.type, // translate
|
||||
value: el.value
|
||||
})),
|
||||
|
||||
retreat: card.retreat,
|
||||
|
||||
effect: card.effect ? card.effect[lang] : undefined,
|
||||
|
||||
trainerType: card.trainerType, // translate
|
||||
energyType: card.energyType // translate
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param setName the setname of the card
|
||||
* @param id the local id of the card
|
||||
* @returns [the local id, the Card object]
|
||||
*/
|
||||
export async function getCard(serie: string, setName: string, id: string): Promise<Card> {
|
||||
return (await import(`../db/data/${serie}/${setName}/${id}.js`)).default
|
||||
}
|
||||
|
||||
export async function getCards(set?: Set): Promise<Array<[string, Card]>> {
|
||||
const cards = (await smartGlob(`./db/data/${(set && set.serie.name.en) ?? '*'}/${(set && set.name.en) ?? '*'}/*.js`))
|
||||
const list: Array<[string, Card]> = []
|
||||
for (const path of cards) {
|
||||
const id = path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf('.'))
|
||||
const setName = (set && set.name.en) ?? (() => {
|
||||
const part1 = path.substr(0, path.lastIndexOf(id) - 1)
|
||||
return part1.substr(part1.lastIndexOf('/') + 1)
|
||||
})()
|
||||
const serieName = (set && set.serie.name.en) ?? (() => {
|
||||
const part1 = path.substr(0, path.lastIndexOf(setName) - 1)
|
||||
return part1.substr(part1.lastIndexOf('/') + 1)
|
||||
})()
|
||||
console.log(path, id, setName)
|
||||
const c = await getCard(serieName, setName, id)
|
||||
if (!c.name.en) {
|
||||
continue
|
||||
}
|
||||
list.push([id, c])
|
||||
}
|
||||
// console.log(list[0], list[0][0])
|
||||
// process.exit(0)
|
||||
return list
|
||||
}
|
38
utils/serieUtil.ts
Normal file
38
utils/serieUtil.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { smartGlob } from "./util"
|
||||
import { setToSetSimple, getSets } from "./setUtil"
|
||||
import Logger from "@dzeio/logger"
|
||||
import { Serie, SupportedLanguages } from 'db/interfaces'
|
||||
import { Serie as SerieSingle, SerieResume } from '@tcgdex/sdk/interfaces'
|
||||
|
||||
const logger = new Logger('ExpansionUtils')
|
||||
|
||||
export async function getSeries(): Promise<Array<Serie>> {
|
||||
return Promise.all((await smartGlob('./db/data/*.js'))
|
||||
.map((it) => it.substring(it.lastIndexOf('/') + 1, it.length - 3))
|
||||
.map((it) => getSerie(it)))
|
||||
}
|
||||
|
||||
export async function getSerie(name: string): Promise<Serie> {
|
||||
return (await import(`../db/data/${name}.js`)).default
|
||||
}
|
||||
|
||||
export async function serieToSerieSimple(serie: Serie, lang: SupportedLanguages): Promise<SerieResume> {
|
||||
return {
|
||||
id: serie.id,
|
||||
name: serie.name[lang] as string
|
||||
}
|
||||
}
|
||||
|
||||
export async function serieToSerieSingle(serie: Serie, lang: SupportedLanguages): Promise<SerieSingle> {
|
||||
const setsTmp = await getSets(serie.name.en)
|
||||
const sets = await Promise.all(setsTmp
|
||||
.sort((a, b) => {
|
||||
return a.releaseDate > b.releaseDate ? 1 : -1
|
||||
})
|
||||
.map(el => setToSetSimple(el, lang)))
|
||||
return {
|
||||
id: serie.id,
|
||||
name: serie.name[lang] as string,
|
||||
sets
|
||||
}
|
||||
}
|
81
utils/setUtil.ts
Normal file
81
utils/setUtil.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import { Set, SupportedLanguages } from "db/interfaces"
|
||||
import { smartGlob } from "./util"
|
||||
import { cardToCardSimple, getCards } from './cardUtil'
|
||||
import { SetResume, Set as SetSingle } from '@tcgdex/sdk/interfaces'
|
||||
|
||||
interface t {
|
||||
[key: string]: Set
|
||||
}
|
||||
|
||||
const setCache: t = {}
|
||||
|
||||
// Dont use cache as it wont necessary have them all
|
||||
export async function getSets(serie = '*'): Promise<Array<Set>> {
|
||||
const sets = (await smartGlob(`./db/data/${serie}/*.js`)).map((set) => set.substring(set.lastIndexOf('/')+1, set.lastIndexOf('.')))
|
||||
return Promise.all(sets.map((set) => getSet(set)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the set
|
||||
* @param name the name of the set (don't include.js/.ts)
|
||||
*/
|
||||
export async function getSet(name: string): Promise<Set> {
|
||||
if (!setCache[name]) {
|
||||
try {
|
||||
const [path] = await smartGlob(`./db/data/*/${name}.js`)
|
||||
setCache[name] = (await import(path.replace('./', '../'))).default
|
||||
} catch (e) {
|
||||
const set = (await getSets()).find((s) => s.id === name)
|
||||
if (set) {
|
||||
return set
|
||||
}
|
||||
console.error(e)
|
||||
console.error(`Error trying to import importing (${`db/data/*/${name}.js`})`)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
return setCache[name]
|
||||
}
|
||||
|
||||
export function isSetAvailable(set: Set, lang: SupportedLanguages) {
|
||||
return lang in set.name
|
||||
}
|
||||
|
||||
export async function setToSetSimple(set: Set, lang: SupportedLanguages): Promise<SetResume> {
|
||||
return {
|
||||
id: set.id,
|
||||
// logo: set.images && set.images.logo,
|
||||
// symbol: set.images && set.images.symbol,
|
||||
name: set.name[lang] as string,
|
||||
cardCount: {
|
||||
total: set.cardCount.total,
|
||||
official: set.cardCount.official
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function setToSetSingle(set: Set, lang: SupportedLanguages): Promise<SetSingle> {
|
||||
return {
|
||||
name: set.name[lang] as string,
|
||||
id: set.id,
|
||||
serie: {
|
||||
id: set.serie.id,
|
||||
name: set.serie.name[lang] as string
|
||||
},
|
||||
tcgOnline: set.tcgOnline,
|
||||
cardCount: {
|
||||
total: set.cardCount.total,
|
||||
official: set.cardCount.official
|
||||
},
|
||||
releaseDate: set.releaseDate,
|
||||
legal: set.legal && {
|
||||
standard: set.legal.standard,
|
||||
expanded: set.legal.expanded
|
||||
},
|
||||
// images: set.images && {
|
||||
// symbol: set.images.symbol,
|
||||
// logo: set.images.logo
|
||||
// },
|
||||
cards: await Promise.all((await getCards(set)).map(([id, card]) => cardToCardSimple(id, card, lang)))
|
||||
}
|
||||
}
|
31
utils/util.ts
Normal file
31
utils/util.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import glob from 'glob'
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
export function urlize(str: string): string {
|
||||
return str.replace('?', '%3F').normalize('NFD').replace(/["'\u0300-\u036f]/g, "")
|
||||
}
|
||||
|
||||
interface fileCacheInterface {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
const fileCache: fileCacheInterface = {}
|
||||
|
||||
export async function fetchRemoteFile<T = any>(url: string): Promise<T> {
|
||||
if (!fileCache[url]) {
|
||||
const resp = await fetch(url, {
|
||||
timeout: 60 * 1000
|
||||
})
|
||||
fileCache[url] = resp.json()
|
||||
}
|
||||
return fileCache[url]
|
||||
}
|
||||
|
||||
const globCache: Record<string, Array<string>> = {}
|
||||
|
||||
export async function smartGlob(query: string) {
|
||||
if (!globCache[query]) {
|
||||
globCache[query] = await new Promise((res) => glob(query, (err, matches) => res(matches)))
|
||||
}
|
||||
return globCache[query]
|
||||
}
|
Reference in New Issue
Block a user