mirror of
https://github.com/tcgdex/compiler.git
synced 2025-04-22 10:42:09 +00:00
Added Tags endpoint and fixed rarity bug
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
b524f47a88
commit
ab5911fae9
@ -1,9 +1,8 @@
|
||||
import Rarity from "@tcgdex/sdk/interfaces/Rarity";
|
||||
import Rarity, { RaritySimple } from "@tcgdex/sdk/interfaces/Rarity";
|
||||
import { Langs } from "@tcgdex/sdk/interfaces/LangList";
|
||||
import { raritySimple } from "./rarities/rarity";
|
||||
import TranslationUtil from "@tcgdex/sdk/TranslationUtil";
|
||||
|
||||
export function rarityToRaritySimple(rarity: Rarity, lang: Langs): raritySimple {
|
||||
export function rarityToRaritySimple(rarity: Rarity, lang: Langs): RaritySimple {
|
||||
return {
|
||||
id: rarity,
|
||||
name: TranslationUtil.translate("rarity", rarity, lang)
|
||||
|
29
endpoints/tags/index.ts
Normal file
29
endpoints/tags/index.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { getBaseFolder } from "../util"
|
||||
import TranslationUtil from "@tcgdex/sdk/TranslationUtil"
|
||||
import { Langs } from "@tcgdex/sdk/interfaces/LangList"
|
||||
import { promises as fs } from 'fs'
|
||||
import Tag, { TagSimple, TagList } from "@tcgdex/sdk/interfaces/Tag"
|
||||
|
||||
const lang = process.env.CARDLANG as Langs || "en"
|
||||
const endpoint = getBaseFolder(lang, "tags")
|
||||
|
||||
const btsp = async () => {
|
||||
|
||||
const list: Array<TagSimple> = []
|
||||
for (const cat of Object.values(Tag)) {
|
||||
if (typeof cat !== "number") continue
|
||||
list.push({
|
||||
id: cat,
|
||||
name: TranslationUtil.translate("tag", cat, lang)
|
||||
})
|
||||
}
|
||||
|
||||
const res: TagList = {
|
||||
count: list.length,
|
||||
list: list
|
||||
}
|
||||
|
||||
await fs.mkdir(endpoint, {recursive: true})
|
||||
await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(res))
|
||||
}
|
||||
btsp()
|
54
endpoints/tags/item.ts
Normal file
54
endpoints/tags/item.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { getAllCards, getBaseFolder, urlize } from "../util"
|
||||
import { fetchCard, isCardAvailable, cardToCardSimple } from "../cardUtil"
|
||||
import Type, { TypeSingle } from "@tcgdex/sdk/interfaces/Type"
|
||||
import Card from "@tcgdex/sdk/interfaces/Card"
|
||||
import { Langs } from "@tcgdex/sdk/interfaces/LangList"
|
||||
import TranslationUtil from "@tcgdex/sdk/TranslationUtil"
|
||||
import { promises } from "fs"
|
||||
import Tag, { TagSingle } from "@tcgdex/sdk/interfaces/Tag"
|
||||
|
||||
type tagCards = {
|
||||
[key in Tag]?: Array<Card>
|
||||
}
|
||||
|
||||
const lang = process.env.CARDLANG as Langs || "en"
|
||||
const endpoint = getBaseFolder(lang, "tags")
|
||||
|
||||
|
||||
const btsp = async () => {
|
||||
const list = getAllCards()
|
||||
const arr: tagCards = {}
|
||||
for (const i of list) {
|
||||
const card = await fetchCard(i)
|
||||
|
||||
if (!isCardAvailable(card, lang)) continue
|
||||
|
||||
for (const tag of card.tags) {
|
||||
if (!(tag in arr)) arr[tag] = []
|
||||
arr[tag].push(card)
|
||||
}
|
||||
}
|
||||
|
||||
for (const type in arr) {
|
||||
if (arr.hasOwnProperty(type)) {
|
||||
const cards: Array<Card> = arr[type];
|
||||
const rTag: Tag = parseInt(type)
|
||||
const toSave: TagSingle = {
|
||||
id: rTag,
|
||||
name: TranslationUtil.translate("tag", rTag, lang),
|
||||
cards: cards.map(el => cardToCardSimple(el, lang))
|
||||
}
|
||||
|
||||
const index = `${endpoint}/${toSave.id}`
|
||||
const name = `${endpoint}/${urlize(toSave.name)}`
|
||||
|
||||
await promises.mkdir(index, {recursive: true})
|
||||
await promises.mkdir(name, {recursive: true})
|
||||
|
||||
await promises.writeFile(`${index}/index.json`, JSON.stringify(toSave))
|
||||
await promises.writeFile(`${name}/index.json`, JSON.stringify(toSave))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
btsp()
|
15
endpoints/tags/tag.ts
Normal file
15
endpoints/tags/tag.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { cardSimple } from "../cards/card";
|
||||
|
||||
export interface tagSimple {
|
||||
id: number
|
||||
name: string
|
||||
}
|
||||
|
||||
export interface tagSingle extends tagSimple {
|
||||
cards: Array<cardSimple>
|
||||
}
|
||||
|
||||
export interface tagList {
|
||||
count: number
|
||||
list: Array<tagSimple>
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user