From 972653f159c95be046781977ec2bce522e3e73b6 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Tue, 11 Mar 2025 11:00:07 +0100 Subject: [PATCH] refactor --- jrpg.ts | 112 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 50 deletions(-) diff --git a/jrpg.ts b/jrpg.ts index 9dd88c0..5514254 100644 --- a/jrpg.ts +++ b/jrpg.ts @@ -3,68 +3,70 @@ import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' function getdompareser(html) { - const parser = new DOMParser(); - return parser.parseFromString(html, "text/html"); + const parser = new DOMParser() + return parser.parseFromString(html, "text/html") } function getcategory(doc, category, notes) { - const result = []; - const items = [...doc.getElementById(category).nextSibling.nextSibling.nextSibling.firstChild.children[0].children]; - const header = [...items.shift().children].map(h => h.textContent); + const result = [] + const items = [...doc.getElementById(category).nextSibling.nextSibling.nextSibling.firstChild.children[0].children] + const header = [...items.shift().children].map(h => h.textContent) items.forEach(elt => { - const item = {}; - const columns = [...elt.children]; + const item = {} + const columns = [...elt.children] header.forEach( (h,i) => { - item[h] = columns[i].textContent; - }); + item[h] = columns[i].textContent + }) item.Notes = "" if (item.Name) { - const sup = parseInt(item.Name.split(" ").pop()); + const sup = parseInt(item.Name.split(" ").pop()) if (!isNaN(sup)) { - item.Notes = notes[sup - 1]; + item.Notes = notes[sup - 1] item.Name = item.Name.substr(0, item.Name.length - 2) } } - result.push(item); - }); - return result; + result.push(item) + }) + return result } function getnotes(doc) { return [] - const lastsup = [...doc.getElementsByTagName("sup")].pop(); + const lastsup = [...doc.getElementsByTagName("sup")].pop() return lastsup.parentElement.textContent.split("\n") .filter(i => i.length) - .map(i => i.substr(3)); + .map(i => i.substr(3)) } async function getpage(url, tables) { const result = {} - const response = await fetch(root + url) + const response = await fetch(url) const html = await response.text() - const doc = getdompareser(html); - const notes = getnotes(doc); + const doc = getdompareser(html) + const notes = getnotes(doc) tables.forEach(category => { - result[category] = getcategory(doc, category, notes); - }); + result[category] = getcategory(doc, category, notes) + }) return result } -function output(equipment) +function format(equipment) { const result = [] - const exclude = ["Found", "Sold", "Available", "Ft","Mr","Go"] // add unused classes/jobs + const exclude = ["Found", "Sold", "Available", "Ft","Mr","Go"] + // todo: put excludes in config // todo: fix footer notes + // todo: guess tables list // todo: invert available X mark // todo: remove a column if always empty @@ -110,48 +112,58 @@ function output(equipment) result.push(" ") }) - console.log(result.join("\n")) + return result.join("\n") } -const root = "https://mikesrpgcenter.com" -let equipment = {}; - -const dq3pages = [ +async function getdata(configname) +{ + const pages = configs[configname] + let result = {} + const root = "https://mikesrpgcenter.com" + for (const index in pages) { - url: "/dw3/armor.html", - tables: ["armor", "shields", "helmets"] - }, - { - url: "/dw3/weapons.html", - tables: ["weapons"] + const page = pages[index] + const pagedata = await getpage([root, configname, page.url].join("/"), page.tables) + result = {...result, ...pagedata} } -] + return result +} -const ff1pages = [ +function output(string) +{ + console.log(string) +} + +const configs = { + ffantasy: [ { - url: "/ffantasy/weapons.html", + url: "weapons.html", tables: ["swords", "axes", "daggers", "staffs", "hammers", "nunchucks", "miscellaneous"] }, { - url: "/ffantasy/armor.html", + url: "armor.html", tables: ["armor", "braceletes", "shields", "helmets", "gauntlets"] }, { - url: "/ffantasy/blackmagic.html", + url: "blackmagic.html", tables: ["black_magic"] }, { - url: "/ffantasy/whitemagic.html", + url: "whitemagic.html", tables: ["white_magic"] - } -] - -const pages = ff1pages -for (const index in pages) -{ - const page = pages[index] - equipment = {...equipment, ...(await getpage(page.url, page.tables))} - + }], + dw3: [ + { + url: "armor.html", + tables: ["armor", "shields", "helmets"] + }, + { + url: "weapons.html", + tables: ["weapons"] + }] } -output(equipment) \ No newline at end of file +getdata("ffantasy") +.then(format) +.then(output) +