From ab5911fae9c3ad37936b5f1b15b9ccfd999331d5 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 11 Mar 2020 11:41:15 +0100 Subject: [PATCH] Added Tags endpoint and fixed rarity bug Signed-off-by: Avior --- endpoints/RarityUtil.ts | 5 ++-- endpoints/tags/index.ts | 29 ++++++++++++++++++++++ endpoints/tags/item.ts | 54 +++++++++++++++++++++++++++++++++++++++++ endpoints/tags/tag.ts | 15 ++++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 endpoints/tags/index.ts create mode 100644 endpoints/tags/item.ts create mode 100644 endpoints/tags/tag.ts diff --git a/endpoints/RarityUtil.ts b/endpoints/RarityUtil.ts index de98e59..6e4fd04 100644 --- a/endpoints/RarityUtil.ts +++ b/endpoints/RarityUtil.ts @@ -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) diff --git a/endpoints/tags/index.ts b/endpoints/tags/index.ts new file mode 100644 index 0000000..2c1c785 --- /dev/null +++ b/endpoints/tags/index.ts @@ -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 = [] + 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() diff --git a/endpoints/tags/item.ts b/endpoints/tags/item.ts new file mode 100644 index 0000000..372e19a --- /dev/null +++ b/endpoints/tags/item.ts @@ -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 +} + +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 = 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() diff --git a/endpoints/tags/tag.ts b/endpoints/tags/tag.ts new file mode 100644 index 0000000..58e5a43 --- /dev/null +++ b/endpoints/tags/tag.ts @@ -0,0 +1,15 @@ +import { cardSimple } from "../cards/card"; + +export interface tagSimple { + id: number + name: string +} + +export interface tagSingle extends tagSimple { + cards: Array +} + +export interface tagList { + count: number + list: Array +}