Updated HP

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2020-03-10 15:19:08 +01:00
parent a30d338103
commit b36dd9046f
No known key found for this signature in database
GPG Key ID: B143FF27EF555D16
3 changed files with 80 additions and 0 deletions

View File

@ -89,3 +89,7 @@ export function isCardAvailable(card: Card, lang: Langs): boolean {
}
return true
}
export function fetchCard(card: string, set?: string, expansion?: string): Card {
return require(`../db/cards/${expansion && (expansion + "/") || ""}${set && (set + "/") || ""}${card}`).default
}

34
endpoints/hp/index.ts Normal file
View File

@ -0,0 +1,34 @@
import { getAllCards, getBaseFolder } from "../util"
import { fetchCard, isCardAvailable } from "../cardUtil"
import { Langs } from "../../db/interfaces/LangList"
import { HpList } from "../../sdk/dist/types/interfaces/Hp"
import { promises as fs } from 'fs'
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "hp")
const btsp = async () => {
const cards = getAllCards()
const hps: Array<number> = []
for (const i of cards) {
const card = fetchCard(i)
if (!isCardAvailable(card, lang) || !card.hp) continue
if (hps.includes(card.hp)) continue
hps.push(card.hp)
}
const hpList: HpList = {
count: hps.length,
list: hps.sort((a, b) => a > b ? 1 : -1)
}
await fs.mkdir(endpoint, {recursive: true})
await fs.writeFile(`${endpoint}/index.json`, JSON.stringify(hpList))
}
btsp()

42
endpoints/hp/item.ts Normal file
View File

@ -0,0 +1,42 @@
import { getAllCards, getBaseFolder } from "../util"
import { fetchCard, isCardAvailable, cardToCardSimple } from "../cardUtil"
import Card from "../../db/interfaces/Card"
import { Langs } from "../../db/interfaces/LangList"
import { HpSingle } from "../../sdk/dist/types/interfaces/Hp"
import { promises as fs } from 'fs'
interface t {
[key: number]: Array<Card>
}
const lang = process.env.CARDLANG as Langs || "en"
const endpoint = getBaseFolder(lang, "hp")
const btsp = async () => {
const files = getAllCards()
const pools: t = {}
for (const file of files) {
const card = fetchCard(file)
if (!isCardAvailable(card, lang) || !card.hp) continue
if (!(card.hp in pools)) pools[card.hp] = []
pools[card.hp].push(card)
}
for (const hp in pools) {
if (pools.hasOwnProperty(hp)) {
const cards = pools[hp];
const toSave: HpSingle = {
hp: hp as unknown as number,
cards: cards.map(el => cardToCardSimple(el, lang))
}
await fs.mkdir(`${endpoint}/${toSave.hp}/`, {recursive: true})
await fs.writeFile(`${endpoint}/${toSave.hp}/index.json`, JSON.stringify(toSave))
}
}
}
btsp()