Updated Utils

Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
Florian Bouillon 2020-03-10 14:39:38 +01:00
parent 55c33bb2a3
commit cee4181627
No known key found for this signature in database
GPG Key ID: B143FF27EF555D16
7 changed files with 62 additions and 12 deletions

View File

@ -1,15 +1,15 @@
import Ability from "../db/interfaces/Ability";
import { Langs } from "../db/interfaces/LangList";
import { abilitySimple, abilitySingle } from "./abilities/ability";
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 {
name: ability.name[lang]
}
}
export function abilityToAbilitySingle(ability: Ability, lang: Langs): abilitySingle {
export function abilityToAbilitySingle(ability: Ability, lang: Langs): AbilitySingle {
return {
name: ability.name[lang],
text: ability.text[lang],

View File

@ -1,9 +1,9 @@
import Attack from "../db/interfaces/Attack";
import { Langs } from "../db/interfaces/LangList";
import { attackSingle } from "./attacks/attack";
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 {
name: attack.name[lang],
cost: attack.cost && attack.cost.map(el => Type.toLang(el, lang)),

View File

@ -10,6 +10,7 @@ import { abilityToAbilitySingle } from "./abilityUtil";
import { getExpansion } from "./expansionUtil";
import { getSet } from "./setUtil";
import Expansion from "../db/interfaces/Expansion";
import { fetchIllustrators, fetchIllustratorsSync } from "./illustratorUtil";
export function cardToCardSimple(card: Card, lang: Langs): CardSimple {
return {
@ -47,9 +48,9 @@ export function cardToCardSingle(card: Card, lang: Langs): CardSingle {
evolveFrom: card.evolveFrom && card.evolveFrom[lang],
evolveTo: card.evolveTo && card.evolveTo.map((el) => el[lang]),
tags: card.tags.map((el) => tagToTagSimple(el, lang)),
illustrator: {
id: 0,
name: typeof card.illustrator === "object" ? card.illustrator.name : card.illustrator,
illustrator: card.illustrator && {
id: fetchIllustratorsSync().indexOf(card.illustrator),
name: card.illustrator,
},
abilities: card.abilities && card.abilities.map((el) => abilityToAbilitySingle(el, lang)),

View File

@ -1,7 +1,20 @@
import Expansion from "../db/interfaces/Expansion"
import Set from "../db/interfaces/Set"
import * as glob from 'glob'
import { Langs } from "../db/interfaces/LangList"
export function getExpansion(set: Set): Expansion {
if ("expansion" in set) return set.expansion
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]
}
}

View 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
}
}

View File

@ -28,6 +28,10 @@ export function getSet(card: Card): Set {
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) {
if (!set.availability || !(lang in set.availability)) return true
return set.availability

View File

@ -1,16 +1,19 @@
import { promises as fs, promises } from 'fs'
import * as glob from 'glob'
export function getAllCards() {
return listFolder("./assets")
export function getAllCards(set = "**", expansion = "**") {
return glob.sync(`./db/cards/${expansion}/${set}/*.js`).map(el => {
return el.substr(11, el.length-10-1-3)
})
}
export function getAllCards2(set = "**", expansion = "**") {
return glob.sync(`./db/cards/${expansion}/${set}/*.js`)
}
export function getAllSets() {
return glob.sync('./db/sets/**/*.js')
export function getAllSets(expansion = "**", nameOnly = false) {
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() {