Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function recurse(nodes) {
- return (reqs, r) => {
- nodes[r]?.forEach(sr => {
- if (!reqs.includes(sr)) {
- reqs = [...reqs, sr];
- }
- });
- return reqs;
- };
- }
- function setVizNode(graph) {
- window.Viz.instance().then(viz => {
- let spoiler = document.querySelector('.if-spoiled.expanded');
- if (spoiler) {
- let node = spoiler.querySelector('div#graph');
- if (node && spoiler.contains(node)) {
- spoiler.removeChild(node);
- }
- node = document.createElement('div');
- node.id = 'graph';
- spoiler.appendChild(node);
- let svg = viz.renderSVGElement(graph);
- let xml = new XMLSerializer().serializeToString(svg);
- let data = btoa(xml);
- let img = new Image();
- img.width = 640;
- img.src = 'data:image/svg+xml;name=spoiler_graph.svg;base64,' + data;
- node.appendChild(img);
- }
- });
- }
- function depthSort(depth) {
- return (a, b) => {
- if (Array.isArray(a)) {
- a = a[0];
- b = b[0];
- }
- return (depth[b] ?? 0) - (depth[a] ?? 0);
- };
- }
- if (!window.buttonsListened) {
- document.querySelector('a#generate').addEventListener('click', () => {
- document.querySelector('div#graph')?.remove();
- });
- document.querySelector('a#spoiler').addEventListener('click', () => {
- setTimeout(parseTree, 1000);
- });
- window.buttonsListened = true;
- }
- function parseTree() {
- let viz = document.querySelector('script[src="https://unpkg.com/@viz-js/[email protected]/lib/viz-standalone.js"');
- if (viz == null) {
- viz = document.createElement('script');
- viz.integrity = 'sha256-gXCCx2fSZS8HWKcJspr35g7dpDiFy2nJX8Fse2OCeXo=';
- viz.crossOrigin = 'anonymous';
- document.querySelector('head').appendChild(viz);
- }
- let nodes = {};
- let depth = {};
- let ul = document.querySelector('ul#spoiler-route');
- ul.querySelectorAll('li').forEach(li => {
- let [all, nameOrItem, item, reqs] = /([^()]+)(?: \(([^)]+)\))?: \[(.*)\]/.exec(li.textContent);
- reqs = reqs.split(', ').map(r => r.trim()).filter(r => !!r);
- if (nameOrItem === 'Draygon 2') {
- ['Kelbesque 1', 'Sabera 1', 'Mado 1', 'Kelbesque 2', 'Sabera 2', 'Mado 2', 'Karmine', 'Sword of Wind', 'Sword of Fire', 'Sword of Water', 'Sword of Thunder'].forEach(r => {
- if (!reqs.includes(r)) {
- reqs.push(r);
- }
- });
- }
- nodes[nameOrItem] = reqs;
- depth[nameOrItem] = Math.max(...[0, ...reqs.map(r => depth[r] || 0)]) + 1;
- if (item) {
- nodes[item] = [nameOrItem, ...reqs];
- depth[item] = Math.max(...[0, depth[nameOrItem], ...reqs.map(r => depth[r] || 0)]) + 1;
- }
- if (nameOrItem.startsWith('Karmine ') && nodes['Karmine'] == null) {
- nodes['Karmine'] = reqs.includes('Shooting Statue') ? reqs : [...reqs, 'Shooting Statue'];
- depth['Karmine'] = Math.max(...[0, ...nodes['Karmine'].map(r => depth[r] || 0)]) + 1;
- }
- });
- Object.entries(nodes).sort(depthSort(depth)).reverse().forEach(([req, reqs], n, entries) => {
- nodes[req] = reqs.reduce(recurse(nodes), reqs).sort(depthSort(depth));
- });
- Object.values(nodes).forEach(reqs => {
- reqs.sort(depthSort(depth));
- });
- let graph = 'digraph G {\n';
- graph +=
- ` subgraph cluster_0 {
- node [style=filled, color=lightgreen];
- "Crystalis";
- `;
- Object.entries(nodes).forEach(([req, reqs]) => {
- if (nodes['Crystalis'].includes(req)) {
- let implied = [];
- reqs.forEach(v => {
- if (!implied.includes(v)) {
- implied = [...implied, ...(nodes[v] || [])];
- graph += ` "${v}" -> "${req}";\n`;
- console.log(`adding "${v}" -> "${req}"`);
- } else {
- console.log(`skipping "${v}" -> "${req}"`);
- }
- });
- }
- });
- graph += ` }\n`;
- Object.entries(nodes).forEach(([req, reqs]) => {
- if (!nodes['Crystalis'].includes(req)) {
- if (reqs.length === 0) {
- graph += ` "${req}";\n`;
- } else {
- let implied = [];
- reqs.forEach(v => {
- if (!implied.includes(v)) {
- implied = [...implied, ...(nodes[v] || [])];
- graph += ` "${v}" -> "${req}";\n`;
- console.log(`adding "${v}" -> "${req}"`);
- } else {
- console.log(`skipping "${v}" -> "${req}"`);
- }
- });
- }
- }
- });
- graph += '}';
- if (!window.Viz) {
- viz.addEventListener('load', () => {
- setVizNode(graph);
- });
- } else {
- setVizNode(graph);
- }
- return [nodes, depth, graph];
- }
Add Comment
Please, Sign In to add comment