summaryrefslogtreecommitdiff
path: root/server/frob/manamarket.ts
blob: 1ea3918b7d8e78d8cb9adc0a55fae3cbd97d5f69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DOMParser, Element } from "https://deno.land/x/deno_dom@v0.1.2-alpha3/deno-dom-wasm.ts";
import { itemsXML } from "./itemsXML.ts";

class ManaMarketHandler {
    private stats_html: string = "";
    items: Map<number, any> = new Map();
    private itemsXML: itemsXML;

    constructor (server: string = "https://server.themanaworld.org") {
        this.stats_html = `${server}/manamarket_stats.html`;
        this.itemsXML = new itemsXML();
    }

    async init () {
        console.log("Fetching ManaMarket stats...");

        const res = await fetch(this.stats_html);
        const html = await res.text();

        await this.itemsXML.init();

        this.parseHTML(html);
    }

    private parseHTML (html: string) {
        console.log("\r                                                                                                  \rParsing ManaMarket stats...                ");

        const domparser = new DOMParser();
        const root = domparser.parseFromString(html, "text/html")!;
        const rows = root.querySelectorAll("tr:nth-of-type(n+3)");

        for (const row of rows) {
            const el = row as Element;
            const name = el.children[0].innerHTML as string;
            const xml = this.itemsXML.getItem(name);

            if (!xml) {
                console.warn(`Cannot find item \`${name}\` in the item xml files`);
                continue;
            }

            const item = {
                name: name,
                id: xml.id,
                totalSold: +el.children[1].innerHTML.split(",").join(""),
                minValue: +el.children[2].innerHTML.split(",").join(""),
                maxValue: +el.children[3].innerHTML.split(",").join(""),
                averageValue: {
                    week: +el.children[4].innerHTML.split(",").join(""),
                    month: +el.children[5].innerHTML.split(",").join(""),
                    overall: +el.children[6].innerHTML.split(",").join(""),
                },
                lastSold: new Date(el.children[7].innerHTML),
            };

            this.items.set(item.id, item);
        }
    }
}

export {
    ManaMarketHandler,
}