mirror of
https://github.com/tcgdex/compiler.git
synced 2025-04-22 10:42:09 +00:00
Updated Utils
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
55c33bb2a3
commit
cee4181627
@ -1,15 +1,15 @@
|
|||||||
import Ability from "../db/interfaces/Ability";
|
import Ability from "../db/interfaces/Ability";
|
||||||
import { Langs } from "../db/interfaces/LangList";
|
import { Langs } from "../db/interfaces/LangList";
|
||||||
import { abilitySimple, abilitySingle } from "./abilities/ability";
|
|
||||||
import AbilityType from "../db/interfaces/AbilityType";
|
import AbilityType from "../db/interfaces/AbilityType";
|
||||||
|
import { AbilitySimple, AbilitySingle } from "../sdk/dist/types/interfaces/Ability";
|
||||||
|
|
||||||
export function abilityToAbilitySimple(ability: Ability, lang: Langs): abilitySimple {
|
export function abilityToAbilitySimple(ability: Ability, lang: Langs): AbilitySimple {
|
||||||
return {
|
return {
|
||||||
name: ability.name[lang]
|
name: ability.name[lang]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function abilityToAbilitySingle(ability: Ability, lang: Langs): abilitySingle {
|
export function abilityToAbilitySingle(ability: Ability, lang: Langs): AbilitySingle {
|
||||||
return {
|
return {
|
||||||
name: ability.name[lang],
|
name: ability.name[lang],
|
||||||
text: ability.text[lang],
|
text: ability.text[lang],
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import Attack from "../db/interfaces/Attack";
|
import Attack from "../db/interfaces/Attack";
|
||||||
import { Langs } from "../db/interfaces/LangList";
|
import { Langs } from "../db/interfaces/LangList";
|
||||||
import { attackSingle } from "./attacks/attack";
|
|
||||||
import Type from "../db/interfaces/Type";
|
import Type from "../db/interfaces/Type";
|
||||||
|
import { AttackSingle } from "../sdk/dist/types/interfaces/Attack";
|
||||||
|
|
||||||
export function attackToAttackSingle(attack: Attack, lang: Langs): attackSingle {
|
export function attackToAttackSingle(attack: Attack, lang: Langs): AttackSingle {
|
||||||
return {
|
return {
|
||||||
name: attack.name[lang],
|
name: attack.name[lang],
|
||||||
cost: attack.cost && attack.cost.map(el => Type.toLang(el, lang)),
|
cost: attack.cost && attack.cost.map(el => Type.toLang(el, lang)),
|
||||||
|
@ -10,6 +10,7 @@ import { abilityToAbilitySingle } from "./abilityUtil";
|
|||||||
import { getExpansion } from "./expansionUtil";
|
import { getExpansion } from "./expansionUtil";
|
||||||
import { getSet } from "./setUtil";
|
import { getSet } from "./setUtil";
|
||||||
import Expansion from "../db/interfaces/Expansion";
|
import Expansion from "../db/interfaces/Expansion";
|
||||||
|
import { fetchIllustrators, fetchIllustratorsSync } from "./illustratorUtil";
|
||||||
|
|
||||||
export function cardToCardSimple(card: Card, lang: Langs): CardSimple {
|
export function cardToCardSimple(card: Card, lang: Langs): CardSimple {
|
||||||
return {
|
return {
|
||||||
@ -47,9 +48,9 @@ export function cardToCardSingle(card: Card, lang: Langs): CardSingle {
|
|||||||
evolveFrom: card.evolveFrom && card.evolveFrom[lang],
|
evolveFrom: card.evolveFrom && card.evolveFrom[lang],
|
||||||
evolveTo: card.evolveTo && card.evolveTo.map((el) => el[lang]),
|
evolveTo: card.evolveTo && card.evolveTo.map((el) => el[lang]),
|
||||||
tags: card.tags.map((el) => tagToTagSimple(el, lang)),
|
tags: card.tags.map((el) => tagToTagSimple(el, lang)),
|
||||||
illustrator: {
|
illustrator: card.illustrator && {
|
||||||
id: 0,
|
id: fetchIllustratorsSync().indexOf(card.illustrator),
|
||||||
name: typeof card.illustrator === "object" ? card.illustrator.name : card.illustrator,
|
name: card.illustrator,
|
||||||
},
|
},
|
||||||
|
|
||||||
abilities: card.abilities && card.abilities.map((el) => abilityToAbilitySingle(el, lang)),
|
abilities: card.abilities && card.abilities.map((el) => abilityToAbilitySingle(el, lang)),
|
||||||
|
@ -1,7 +1,20 @@
|
|||||||
import Expansion from "../db/interfaces/Expansion"
|
import Expansion from "../db/interfaces/Expansion"
|
||||||
import Set from "../db/interfaces/Set"
|
import Set from "../db/interfaces/Set"
|
||||||
|
import * as glob from 'glob'
|
||||||
|
import { Langs } from "../db/interfaces/LangList"
|
||||||
|
|
||||||
export function getExpansion(set: Set): Expansion {
|
export function getExpansion(set: Set): Expansion {
|
||||||
if ("expansion" in set) return set.expansion
|
if ("expansion" in set) return set.expansion
|
||||||
return require(`../../db/expansions/${set.expansionCode}`)
|
return require(`../../db/expansions/${set.expansionCode}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAllExpansions(): Array<string> {
|
||||||
|
return glob.sync("./db/expansions/*.ts").map(el => el.substr(16, el.length-15-1-3)) // -15 = start -1 = 0 index -3 = .ts
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expansionToExpansionSimple(expansion: Expansion, lang: Langs) {
|
||||||
|
return {
|
||||||
|
code: expansion.code,
|
||||||
|
name: typeof expansion.name === "string" ? expansion.name : expansion.name[lang]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
29
endpoints/illustratorUtil.ts
Normal file
29
endpoints/illustratorUtil.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { promises as fs} from "fs"
|
||||||
|
import * as fsSync from 'fs'
|
||||||
|
import { IllustratorSimple } from "../sdk/dist/types/interfaces/Illustrator"
|
||||||
|
|
||||||
|
export const illustratorsFile = "./generated/illustrators.json"
|
||||||
|
|
||||||
|
let illustratorsCache: Array<string> = []
|
||||||
|
|
||||||
|
|
||||||
|
export async function fetchIllustrators(): Promise<Array<string>> {
|
||||||
|
if (illustratorsCache.length === 0) {
|
||||||
|
illustratorsCache = JSON.parse(await (await fs.readFile(illustratorsFile)).toString())
|
||||||
|
}
|
||||||
|
return illustratorsCache
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fetchIllustratorsSync(): Array<string> {
|
||||||
|
if (illustratorsCache.length === 0) {
|
||||||
|
illustratorsCache = JSON.parse(fsSync.readFileSync(illustratorsFile).toString())
|
||||||
|
}
|
||||||
|
return illustratorsCache
|
||||||
|
}
|
||||||
|
|
||||||
|
export function illustratorToIllustratorSimple(illustrator: string, index: number): IllustratorSimple {
|
||||||
|
return {
|
||||||
|
id: index,
|
||||||
|
name: illustrator
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,10 @@ export function getSet(card: Card): Set {
|
|||||||
return setCache[card.set.code]
|
return setCache[card.set.code]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fetchSet(expansion: string, set: string): Set {
|
||||||
|
return require(`../db/sets/${expansion}/${set}.js`).default
|
||||||
|
}
|
||||||
|
|
||||||
export function isSetAvailable(set: Set, lang: Langs) {
|
export function isSetAvailable(set: Set, lang: Langs) {
|
||||||
if (!set.availability || !(lang in set.availability)) return true
|
if (!set.availability || !(lang in set.availability)) return true
|
||||||
return set.availability
|
return set.availability
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
import { promises as fs, promises } from 'fs'
|
import { promises as fs, promises } from 'fs'
|
||||||
import * as glob from 'glob'
|
import * as glob from 'glob'
|
||||||
|
|
||||||
export function getAllCards() {
|
export function getAllCards(set = "**", expansion = "**") {
|
||||||
return listFolder("./assets")
|
return glob.sync(`./db/cards/${expansion}/${set}/*.js`).map(el => {
|
||||||
|
return el.substr(11, el.length-10-1-3)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllCards2(set = "**", expansion = "**") {
|
export function getAllCards2(set = "**", expansion = "**") {
|
||||||
return glob.sync(`./db/cards/${expansion}/${set}/*.js`)
|
return glob.sync(`./db/cards/${expansion}/${set}/*.js`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllSets() {
|
export function getAllSets(expansion = "**", nameOnly = false) {
|
||||||
return glob.sync('./db/sets/**/*.js')
|
if (nameOnly) return glob.sync(`./db/sets/${expansion}/*.js`).map(el => el.substr(11+expansion.length, el.length-(10+expansion.length)-1-3))
|
||||||
|
return glob.sync(`./db/sets/${expansion}/*.js`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllCardsJSON() {
|
export function getAllCardsJSON() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user