diff --git a/interfaces/AbilityType.ts b/interfaces/AbilityType.ts index 393341cd8..1b150e589 100644 --- a/interfaces/AbilityType.ts +++ b/interfaces/AbilityType.ts @@ -5,7 +5,31 @@ enum AbilityType { ANCIENTTRAIT } +const en = [ + "Poké-Body", + "Poké-Power", + "Ability", + "Ancient Trait" +] + +const fr = [ + "Poké-Body", + "Poké-Power", + "Talent", + "Trait Ancien" +] + namespace AbilityType { + + export function toLang(a: AbilityType, lang: string): string { + switch (lang) { + case "en": + return en[a] + case "fr": + return fr[a] + } + throw new Error(`Error , abilityType not translated! (${lang})`) + } export function getFromText(txt: string): AbilityType { switch (txt) { case "Ability": diff --git a/interfaces/Card.ts b/interfaces/Card.ts index 574e6f3b2..4a0fc3586 100644 --- a/interfaces/Card.ts +++ b/interfaces/Card.ts @@ -2,16 +2,11 @@ import Type from "./Type"; import Category from "./Category"; import Tag from "./Tag"; import LangList from "./LangList"; -import AbilityType from "./AbilityType"; import Rarity from "./Rarity"; import Attack from "./Attack"; -import { Page } from "puppeteer-core"; import Illustrator from "./Illutrator"; -import { promises } from "fs"; import Ability from "./Ability"; -let abilities: Array = [] - interface Card { // global id made of setid and localId @@ -63,220 +58,4 @@ interface Card { } } -namespace Card { - export async function generate(tab: Page, existingCard: Card, lang: string, set: {name:string, code: string}): Promise { - const card: Card = Object.assign(n(), existingCard) - card.set = set - // LocalID - card.localId = await getId(tab) - card.id = `${card.set.code}-${card.localId}` - - // first line - card.name = LangList.insert(card.name, await getName(tab), lang) - try { - card.hp = await getHP(tab) - } catch {} - try { - card.type = await getType(tab) - } catch {} - - card.image.low = LangList.insert(card.image.low, await getImage(tab), lang) - - try { - const from = await getEvolveFrom(tab) - card.evolveFrom = LangList.insert(card.evolveFrom || {}, from, lang) - } catch {} - card.tags = await Tag.get(tab) - try { - card.illustrator = await Illustrator.getIllustrator(tab) - } catch {} - - try { - const ability = await getAbility(tab, lang) - if (!card.abilities) card.abilities = [] - if (!card.abilities[0]) card.abilities[0] = {id: -1, type: -1, name: {}, text: {}} - - if (ability.id) card.abilities[0].id = ability.id - try { - card.abilities[0].type = AbilityType.getFromText(ability.type) - } catch (e) { - console.log(`Error ${tab.url()}`) - console.log(e) - } - card.abilities[0].name = LangList.insert(card.abilities[0].name, ability.name, lang) - card.abilities[0].text = LangList.insert(card.abilities[0].text, ability.text, lang) - } catch (e) {} - - try { - card.attacks = await Attack.getAttacks(tab, lang, card.attacks) - } catch (e){ - console.log(e) - } - - try { - card.weaknesses = await getWeaknessOrResistance(tab, 1) - } catch {} - try { - card.resistances = await getWeaknessOrResistance(tab, 2) - } catch {} - - // Category - card.category = await Category.detect(tab) - - - try { - card.retreat = await getRetreat(tab) - } catch { - if (card.category === Category.POKEMON) { - card.retreat = 0 - } - } - - card.rarity = await Rarity.getRarity(tab) - - return card - } - - export function n() { - return { - name: {}, - evolveFrom: {}, - tags: [], - image: {} - } - } - - export async function fetchAbilities() { - abilities = JSON.parse((await promises.readFile("./generated/abilities.json")).toString()) - } - export function processAbility(str: string): number { - if (!abilities.includes(str)) { - abilities.push(str) - } - - return abilities.indexOf(str) - } - - export async function saveAbilities() { - await promises.writeFile("./generated/abilities.json", JSON.stringify(abilities)) - } - - function getEvolveFrom(tab: Page): Promise { - return tab.$eval(".card-type h4 a", (el: HTMLElement) => { - return el.innerText - }) - } - - - function getImage(tab: Page): Promise { - return tab.$eval(".card-image img", (el: HTMLImageElement) => { - return el.src - }) - } - - async function getType(tab: Page): Promise> { - try { - const types = await tab.$$eval(".card-basic-info .right a i", (els: Array) => { - const l = [] - for (const el of els) { - el.classList.remove("energy") - l.push( - el.classList.item(0) - ) - } - return l - }) - const res = [] - for (const t of types) { - res.push( - Type.getFromClass(t) - ) - } - return res - } catch { - return [] - } - } - - export function getId(tab: Page): Promise { - try { - return tab.$eval(".stats-footer span", (el: HTMLElement) => { - const g = /[\/ ]/g - const arr = el.innerText.split(g) - const r = /^[0-9]+$/ - return r.test(arr[0]) ? parseInt(arr[0]) : arr[0] - }) - } catch (e) { - console.log(tab.url()) - console.log(e) - process.exit(1) - } - } - - function getHP(tab: Page): Promise { - return tab.$eval(".card-basic-info .card-hp", (el: HTMLElement) => { - const r = /[^0-9]/g - return parseInt(el.innerText.replace(r, "")) - }) - } - - function getName(tab: Page): Promise { - return tab.$eval("h1", (el: HTMLElement) => { - return el.innerText - }) - } - - async function getAbility(tab: Page, lang: string): Promise<{id?: number, type: string, name: string, text: string}> { - const {type, name} = await tab.$eval(".pokemon-abilities h3", (el: HTMLElement) => { - return { - type: (el.querySelector("div[class]") as HTMLElement)?.innerText, - name: (el.querySelector("div:not([class])") as HTMLElement)?.innerText - } - }) - const text = await tab.$eval(".pokemon-abilities h3 ~ p", (el: HTMLElement) => { - return el.innerText - }) - - let id = undefined - - if (lang === "en") { - if (!abilities.includes(name)) { - abilities.push(name) - } - id = abilities.indexOf(name) - } - - return { - id, - type, - name, - text - } - } - - async function getWeaknessOrResistance(tab: Page, childNth: number): Promise> { - const t = await tab.$eval(`.pokemon-stats .stat:nth-child(${childNth})`, (el: HTMLElement) => { - if (el.childElementCount === 1) return - const energy = el.querySelector("ul li a i") - const txt = (el.querySelector("ul li") as HTMLElement).innerText - - energy.classList.remove("energy") - return { - energy: energy.classList.item(0), - txt - } - }) - return [{ - type: Type.getFromClass(t.energy), - value: t.txt - }] - } - - function getRetreat(tab: Page): Promise { - return tab.$eval(".stat.last .card-energies", (el: HTMLElement) => { - return el.childElementCount - }) - } -} - export default Card diff --git a/interfaces/Category.ts b/interfaces/Category.ts index 4e87adec5..55c7b8697 100644 --- a/interfaces/Category.ts +++ b/interfaces/Category.ts @@ -1,5 +1,3 @@ -import { Page } from "puppeteer-core" - enum Category { POKEMON, TRAINER, @@ -12,6 +10,12 @@ const en = [ "Energy" ] +const fr = [ + "Pokémon", + "Dresseur", + "Énergie" +] + namespace Category { export function fromEnglish(str: string) { let i = en.indexOf(str) @@ -20,38 +24,14 @@ namespace Category { return i } - export function toLang(i: Category, lang: string) { + export function toLang(i: Category, lang: string): string { switch (lang) { case "en": return en[i] - default: - break; + case "fr": + return fr[i] } - } - export async function detect(tab: Page): Promise { - const type = await tab.$eval(".card-basic-info .card-type h2", (el: HTMLElement) => { - return el.innerText - }) - if ( - type.startsWith("Dresseur") || - type.startsWith("Trainer") || - type.startsWith("Entrenador") - ) { - return Category.TRAINER - } - if ( - type.startsWith("Énergie") || - type.endsWith("Energy") || - type.startsWith("Energía") - ) { - return Category.ENERGY - } - try { - await tab.$(".pokemon-stats .stat:nth-child(3)") - return Category.POKEMON - } catch {} - console.log(tab.url()) - throw new Error("Pokemon Category not found !") + throw new Error(`Error, Language not implemented! (${lang})`) } } diff --git a/interfaces/Rarity.ts b/interfaces/Rarity.ts index 18c838f6d..f4d3ebc96 100644 --- a/interfaces/Rarity.ts +++ b/interfaces/Rarity.ts @@ -1,5 +1,3 @@ -import { Page } from "puppeteer-core" - enum Rarity { Common, Uncommon, @@ -26,15 +24,54 @@ enum Rarity { RareVMAX, } +const en = [ + "Common", + "unCommon", + "Rare", + + "Rare Holo", + "Rare Holo EX", + "Rare Holo GX", + "Rare Holo Lv.X", + + "Rare Ultra", + "Rare Prime", + "Rare ACE", + "Rare BREAK", + "Rainbow Rare", + + "LEGEND" +] + +const fr = [ + "Commun", + "Non Commun", + "Rare", + + "Rare Holo", + "Rare Holo EX", + "Rare Holo GX", + "Rare Holo Lv.X", + + "Rare Ultra", + "Rare Prime", + "Rare ACE", + "Rare BREAK", + "Rainbow Rare", + + "LEGEND" +] + namespace Rarity { - export async function getRarity(tab: Page): Promise { - const rarity = await tab.$eval(".stats-footer span", (el: HTMLElement) => { - const arr = el.innerText.split(" ") - arr.shift() - return arr.join(" ") - }) - return getLocalRarity(rarity) + export function toLang(r: Rarity, lang: string): string { + switch (lang) { + case "en": + return en[r] + case "fr": + return fr[r] + } + throw new Error(`Error, rarity lang not found! (${lang})`) } export function getLocalRarity(str?: string): Rarity { diff --git a/interfaces/Tag.ts b/interfaces/Tag.ts index 4442feae7..f1ce02b09 100644 --- a/interfaces/Tag.ts +++ b/interfaces/Tag.ts @@ -1,5 +1,3 @@ -import { Page } from "puppeteer-core"; - enum Tag { BASIC, BASICENERGY, @@ -84,20 +82,6 @@ namespace Tag { break; } } - export async function get(tab: Page): Promise> { - const h2 = await tab.$eval(".card-type h2", (el: HTMLElement) => { - return el.innerText - }) - const list: Array = lookup(h2) - - // SP - const isNull = await tab.$('h1 img[alt="[G]"]') - if (isNull !== null) { - list.push(Tag.SP) - } - - return list - } export function lookup(str: string): Array { const list: Array = []