Compare commits

..

2 Commits

Author SHA1 Message Date
quenousimporte 7a4d9072df refactor 2025-03-19 15:49:19 +01:00
quenousimporte 98b602cdcc remove comments 2025-03-12 18:12:27 +01:00
1 changed files with 147 additions and 150 deletions

297
jrpg.ts
View File

@ -1,150 +1,147 @@
// deno // deno
import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts' import { DOMParser } from 'https://deno.land/x/deno_dom/deno-dom-wasm.ts'
function getdompareser(html) function getdompareser(html)
{ {
const parser = new DOMParser() const parser = new DOMParser()
return parser.parseFromString(html, "text/html") return parser.parseFromString(html, "text/html")
} }
function gettables(doc) function gettables(doc)
{ {
return [...doc.getElementsByTagName("h2")].map(h2 => h2.id) return [...doc.getElementsByTagName("h2")].map(h2 => h2.id)
} }
function gettable(doc, table, notes) function gettable(doc, table, notes)
{ {
const result = [] const result = []
const items = [...doc.getElementById(table).nextSibling.nextSibling.nextSibling.firstChild.children[0].children] const items = [...doc.getElementById(table).nextSibling.nextSibling.nextSibling.firstChild.children[0].children]
const header = [...items.shift().children].map(h => h.textContent) const header = [...items.shift().children].map(h => h.textContent)
items.forEach(elt => { items.forEach(elt => {
const item = {} const item = {}
const columns = [...elt.children] const columns = [...elt.children]
header.forEach( (h,i) => header.forEach( (h,i) =>
{ {
item[h] = columns[i].textContent item[h] = columns[i].textContent
}) })
if (item.Name) if (item.Name)
{ {
const sup = parseInt(item.Name.split(" ").pop()) const sup = parseInt(item.Name.split(" ").pop())
if (!isNaN(sup)) if (!isNaN(sup))
{ {
item.Notes = notes[sup - 1] item.Notes = notes[sup - 1]
item.Name = item.Name.substr(0, item.Name.length - 2) item.Name = item.Name.substr(0, item.Name.length - 2)
} }
} }
result.push(item) result.push(item)
}) })
return result return result
} }
function getnotes(doc) function getnotes(doc)
{ {
const lastsup = [...doc.getElementsByTagName("sup")].pop() const lastsup = [...doc.getElementsByTagName("sup")].pop()
if (!lastsup) if (!lastsup)
{ {
return [] return []
} }
return lastsup.parentElement.textContent.split("\n") return lastsup.parentElement.textContent.split("\n")
.filter(i => i.length) .filter(i => i.length)
.map(i => i.substr(3).trim()) .map(i => i.substr(3).trim())
} }
async function getpage(url) async function getpage(url)
{ {
const result = {} const result = {}
const response = await fetch(url) const response = await fetch(url)
const html = await response.text() const html = await response.text()
const doc = getdompareser(html) const doc = getdompareser(html)
const notes = getnotes(doc) const notes = getnotes(doc)
const tables = gettables(doc) const tables = gettables(doc)
tables.forEach(table => tables.forEach(table =>
{ {
result[table] = gettable(doc, table, notes) result[table] = gettable(doc, table, notes)
}) })
return result return result
} }
function format(equipment, exclude) function format(equipment, exclude)
{ {
const result = [] const result = []
// todo: notes as footers instead of column
// todo: invert available X mark Object.keys(equipment).forEach(table => {
// todo: remove a column if always empty const columnsizes = {}
result.push(table.toUpperCase())
Object.keys(equipment).forEach(table => { const items = equipment[table]
const columnsizes = {}
result.push(table.toUpperCase()) // exclude
const items = equipment[table] items.forEach(item => {
exclude.forEach(name => {
// exclude delete item[name]
items.forEach(item => { })
exclude.forEach(name => { })
delete item[name]
}) // init sizes
}) items.forEach(item =>
{
// init sizes Object.keys(item).forEach(columnname => {
items.forEach(item => columnsizes[columnname] = columnsizes[columnname] || columnname.length
{ columnsizes[columnname] = Math.max(columnsizes[columnname], (item[columnname] || "").length)
Object.keys(item).forEach(columnname => { })
columnsizes[columnname] = columnsizes[columnname] || columnname.length })
columnsizes[columnname] = Math.max(columnsizes[columnname], (item[columnname] || "").length)
}) // write header
}) let line = ""
Object.keys(columnsizes).forEach(columnname => {
// write header const size = columnsizes[columnname]
let line = "" line += columnname + " ".repeat((Math.max(0, size - columnname.length))) + "|"
Object.keys(columnsizes).forEach(columnname => { })
const size = columnsizes[columnname] result.push(line)
line += columnname + " ".repeat((Math.max(0, size - columnname.length))) + "|"
}) // write lines
result.push(line) items.forEach(item => {
let line = ""
// write lines Object.keys(columnsizes).forEach(columnname => {
items.forEach(item => { const size = columnsizes[columnname]
let line = "" const value = (item[columnname] || "").replace("\n", " ")
Object.keys(columnsizes).forEach(columnname => { line += value + " ".repeat(Math.max(0, size - value.length)) + "|"
const size = columnsizes[columnname] })
const value = (item[columnname] || "").replace("\n", " ") result.push(line)
line += value + " ".repeat(Math.max(0, size - value.length)) + "|" })
}) result.push(" ")
result.push(line) })
})
result.push(" ") return result.join("\n")
}) }
return result.join("\n") async function getdata(configname)
} {
const pages = configs[configname].pages
async function getdata(configname) let result = {}
{ const root = "https://mikesrpgcenter.com"
const pages = configs[configname].pages for (const index in pages)
let result = {} {
const root = "https://mikesrpgcenter.com" const page = pages[index]
for (const index in pages) const pagedata = await getpage([root, configname, page].join("/") + ".html")
{ result = {...result, ...pagedata}
const page = pages[index] }
const pagedata = await getpage([root, configname, page].join("/") + ".html") return result
result = {...result, ...pagedata} }
}
return result function output(stringresult, configname)
} {
//console.log(stringresult)
function output(stringresult, configname) Deno.writeTextFile(configname + ".txt", stringresult)
{ }
//console.log(stringresult)
Deno.writeTextFile(configname + ".txt", stringresult) const configs = {
} ffantasy: { pages: ["weapons", "armor", "blackmagic", "whitemagic"], exclude: ["Found", "Sold", "Available", "Notes", "Cost", "Ni", "WM", "WW", "BM", "BW", "Th", "BB", "Ma"] },
dw3: { pages: ["armor", "weapons"], exclude: [] },
const configs = { }
ffantasy: { pages: ["weapons", "armor", "blackmagic", "whitemagic"], exclude: ["Found", "Sold", "Available", "Notes", "Cost", "Ni", "WM", "WW", "BM", "BW", "Th", "BB", "Ma"] },
dw3: { pages: ["armor", "weapons"], exclude: [] }, for (const configname in configs) {
} const data = await getdata(configname)
const stringresult = format(data, configs[configname].exclude)
for (const configname in configs) { output(stringresult, configname)
const data = await getdata(configname) }
const stringresult = format(data, configs[configname].exclude)
output(stringresult, configname)
}