From f5c94268beef8f214b3db600a30999d7baf5756e Mon Sep 17 00:00:00 2001 From: Hamcha Date: Thu, 21 Nov 2019 17:59:49 +0100 Subject: [PATCH] Start work on cube page --- .gitignore | 3 +- cmd/cube-dev.ts | 114 +++++++++++++++++++++++++++++++++++++++++++++ cmd/cube.ts | 101 +++++++++++++++++++++++++++++++++++++++ cmd/find.ts | 16 ++++--- lib/types.ts | 2 + lib/utils.ts | 6 ++- package.json | 6 ++- templates/cube.ejs | 92 ++++++++++++++++++++++++++++++++++++ yarn.lock | 10 ++++ 9 files changed, 340 insertions(+), 10 deletions(-) create mode 100644 cmd/cube-dev.ts create mode 100644 templates/cube.ejs diff --git a/.gitignore b/.gitignore index 7da4385..b74f3c2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ env.ps1 node_modules AllPrintings.json mcmCards.json -*-cards.json \ No newline at end of file +*-cards.json +*-cube.html \ No newline at end of file diff --git a/cmd/cube-dev.ts b/cmd/cube-dev.ts new file mode 100644 index 0000000..911bade --- /dev/null +++ b/cmd/cube-dev.ts @@ -0,0 +1,114 @@ +import * as ejs from "ejs"; +import { existsSync } from "fs"; +import { createServer } from "http"; + +import { Article, asyncLoadJSON, CardItem, leanArticle, leanCard, MCMDB, onlyUnique } from "../lib"; + +const colorNames = { + CL: "Colorless", + MC: "Multicolor", + W: "White", + U: "Blue", + B: "Black", + R: "Red", + G: "Green", + WU: "Azorius", + UB: "Dimir", + BR: "Rakdos", + RG: "Gruul", + WG: "Selesnya", + WB: "Orzhov", + UR: "Izzet", + BG: "Golgari", + WR: "Boros", + UG: "Simic", + WUG: "Bant", + WUB: "Esper", + UBR: "Grixis", + BRG: "Jund", + WRG: "Naya", + WBG: "Abzan", + WUR: "Jeskai", + UBG: "Sultai", + WBR: "Mardu", + URG: "Temur" +}; + +const columns: Record boolean> = { + W: c => colorid(c.colorIdentity) == "W", + U: c => colorid(c.colorIdentity) == "U", + B: c => colorid(c.colorIdentity) == "B", + R: c => colorid(c.colorIdentity) == "R", + G: c => colorid(c.colorIdentity) == "G", + MC: c => c.colorIdentity.length > 0, + CL: c => colorid(c.colorIdentity) == "CL" +}; + +function wubrg(a: string, b: string) { + const order = ["W", "U", "B", "R", "G"]; + const indexA = order.indexOf(a); + const indexB = order.indexOf(b); + return indexA - indexB; +} + +function colorid(colors: string[]): string { + if (colors.length < 1) { + return "CL"; + } + return colors.sort(wubrg).join(""); +} + +async function run() { + if (process.argv.length < 3) { + console.error("Usage: yarn fetch "); + process.exit(1); + return; + } + const uid = process.argv[2]; + const uidCards = `${uid}-cards.json`; + if (!existsSync("mcmCards.json")) { + console.error("Card db is missing! Run 'yarn convert-db' first."); + process.exit(1); + } + if (!existsSync(uidCards)) { + console.error(`Could not find ${uidCards}! Run 'yarn fetch ${uid}' first.`); + process.exit(1); + } + let db = await asyncLoadJSON("mcmCards.json"); + let articles = await asyncLoadJSON(`${uid}-cards.json`); + + let cards: CardItem[] = articles + .filter(art => art.idProduct in db) + .map(art => { + const card = db[art.idProduct]; + return { + ...leanArticle(art), + ...leanCard(card) + }; + }); + + let valid = cards + .filter( + c => + (c.language == "Italian" || c.language == "English") && + c.price <= 0.1 && + (c.rarity == "rare" || c.rarity == "mythic") + ) + .filter(onlyUnique); + + const server = createServer(async (req, res) => { + const template = await ejs.renderFile("templates/cube.ejs", { + user: uid, + cards: valid, + columns, + utils: { wubrg, colorNames, colorid } + }); + res.end(template); + }); + server.on("clientError", (err, socket) => { + socket.end("HTTP/1.1 400 Bad Request\r\n\r\n"); + }); + server.listen(8000); +} + +run(); diff --git a/cmd/cube.ts b/cmd/cube.ts index e69de29..068c108 100644 --- a/cmd/cube.ts +++ b/cmd/cube.ts @@ -0,0 +1,101 @@ +import * as ejs from "ejs"; +import { existsSync, writeFile } from "fs"; + +import { Article, asyncLoadJSON, CardItem, leanArticle, leanCard, MCMDB, onlyUnique } from "../lib"; + +const colorNames = { + CL: "Colorless", + W: "White", + U: "Blue", + B: "Black", + R: "Red", + G: "Green", + WU: "Azorius", + UB: "Dimir", + BR: "Rakdos", + RG: "Gruul", + WG: "Selesnya", + WB: "Orzhov", + UR: "Izzet", + BG: "Golgari", + WR: "Boros", + UG: "Simic", + WUG: "Bant", + WUB: "Esper", + UBR: "Grixis", + BRG: "Jund", + WRG: "Naya", + WBG: "Abzan", + WUR: "Jeskai", + UBG: "Sultai", + WBR: "Mardu", + URG: "Temur" +}; + +function wubrg(a: string, b: string) { + const order = ["W", "U", "B", "R", "G"]; + const indexA = order.indexOf(a); + const indexB = order.indexOf(b); + return indexA - indexB; +} + +function colorid(colors: string[]): string { + if (colors.length < 1) { + return "CL"; + } + return colors.sort(wubrg).join(""); +} + +async function run() { + if (process.argv.length < 3) { + console.error("Usage: yarn fetch "); + process.exit(1); + return; + } + const uid = process.argv[2]; + const uidCards = `${uid}-cards.json`; + if (!existsSync("mcmCards.json")) { + console.error("Card db is missing! Run 'yarn convert-db' first."); + process.exit(1); + } + if (!existsSync(uidCards)) { + console.error(`Could not find ${uidCards}! Run 'yarn fetch ${uid}' first.`); + process.exit(1); + } + let db = await asyncLoadJSON("mcmCards.json"); + let articles = await asyncLoadJSON(`${uid}-cards.json`); + + let cards: CardItem[] = articles + .filter(art => art.idProduct in db) + .map(art => { + const card = db[art.idProduct]; + return { + ...leanArticle(art), + ...leanCard(card) + }; + }); + + let valid = cards + .filter( + c => + (c.language == "Italian" || c.language == "English") && + c.price <= 0.1 && + (c.rarity == "rare" || c.rarity == "mythic") + ) + .filter(onlyUnique); + + const template = await ejs.renderFile("templates/cube.ejs", { + user: uid, + cards: valid, + utils: { wubrg, colorNames, colorid } + }); + let cubeFile = `${uid}-cube.html`; + writeFile(cubeFile, template, {}, err => { + if (err) { + throw err; + } + console.log(`Wrote cube data to ${cubeFile}`); + }); +} + +run(); diff --git a/cmd/find.ts b/cmd/find.ts index ab60920..5b755d0 100644 --- a/cmd/find.ts +++ b/cmd/find.ts @@ -1,6 +1,6 @@ import { existsSync } from "fs"; -import { Article, asyncLoadJSON, asyncSaveJSON, leanArticle, leanCard, MCMDB } from "../lib"; +import { Article, asyncLoadJSON, asyncSaveJSON, leanArticle, leanCard, MCMDB, onlyUnique } from "../lib"; async function run() { if (process.argv.length < 3) { @@ -31,12 +31,14 @@ async function run() { }; }); - let valid = cards.filter( - c => - (c.language == "Italian" || c.language == "English") && - c.price <= 0.1 && - (c.rarity == "rare" || c.rarity == "mythic") - ); + let valid = cards + .filter( + c => + (c.language == "Italian" || c.language == "English") && + c.price <= 0.1 && + (c.rarity == "rare" || c.rarity == "mythic") + ) + .filter(onlyUnique); let netprice = valid.reduce((a, c) => (a += c.price), 0); console.log( `Found ${ diff --git a/lib/types.ts b/lib/types.ts index a9457bf..9b96faa 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -131,3 +131,5 @@ export interface LeanArticle { count: number; condition: string; } + +export type CardItem = LeanMCMCard & LeanArticle; diff --git a/lib/utils.ts b/lib/utils.ts index 9a28c64..c6d30b8 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,6 +1,6 @@ import { readFile, writeFile } from "fs"; -import { Article, LeanArticle, LeanMCMCard, MCMCard } from "./types"; +import { Article, CardItem, LeanArticle, LeanMCMCard, MCMCard } from "./types"; export async function asyncLoadJSON(filename: string): Promise { return new Promise((resolve, reject) => { @@ -57,3 +57,7 @@ export function leanArticle(article: Article): LeanArticle { condition: article.condition }; } + +export function onlyUnique(value: CardItem, index: number, self: CardItem[]) { + return self.findIndex(c => c.name == value.name) == index; +} diff --git a/package.json b/package.json index 9bb9842..588d2d8 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,15 @@ "scripts": { "fetch": "ts-node cmd/fetch.ts", "init-db": "ts-node cmd/convert.ts", - "find": "ts-node cmd/find.ts" + "find": "ts-node cmd/find.ts", + "cube": "ts-node cmd/cube.ts", + "cube-watch": "ts-node cmd/cube-dev.ts" }, "dependencies": { + "@types/ejs": "^2.6.3", "@types/node": "^12.12.11", "csv-parser": "^2.3.2", + "ejs": "^2.7.4", "request": "^2.88.0", "ts-node": "^8.5.2", "typescript": "^3.7.2" diff --git a/templates/cube.ejs b/templates/cube.ejs new file mode 100644 index 0000000..972306b --- /dev/null +++ b/templates/cube.ejs @@ -0,0 +1,92 @@ + + + + + Junk rare cube + + + + +
+
+

Junk rare cube

+

Using cards from <%= user %> +

+
+
+ <% for (const column in columns) { %> +
+
+

<%=utils.colorNames[column]%>

+
+
+ <% } %> +
+
+ + + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 8fd2861..3e4fbe2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -40,6 +40,11 @@ dependencies: "@hapi/hoek" "^8.3.0" +"@types/ejs@^2.6.3": + version "2.6.3" + resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-2.6.3.tgz#b6509e9925d7eb5e95c8c73b6492e5baae7c1e6a" + integrity sha512-/F+qQ0Fr0Dr1YvHjX+FCvbba4sQ27RdCPDqmP/si0e1v1GOkbQ3VRBvZPSQM7NoQ3iz3SyiJVscCP2f0vKuIhQ== + "@types/node@^12.12.11": version "12.12.11" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.11.tgz#bec2961975888d964196bf0016a2f984d793d3ce" @@ -171,6 +176,11 @@ ecc-jsbn@~0.1.1: jsbn "~0.1.0" safer-buffer "^2.1.0" +ejs@^2.7.4: + version "2.7.4" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-2.7.4.tgz#48661287573dcc53e366c7a1ae52c3a120eec9ba" + integrity sha512-7vmuyh5+kuUyJKePhQfRQBhXV5Ce+RnaeeQArKu1EAMpL3WbgMt5WG6uQZpEVvYSSsxMXRKOewtDk9RaTKXRlA== + extend@~3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"