raylu il y a 2 jours
Parent
commit
e68a561bb2
4 fichiers modifiés avec 114 ajouts et 0 suppressions
  1. 1 0
      .gitignore
  2. 73 0
      ts/rat.ts
  3. 19 0
      www/rat.html
  4. 21 0
      www/style.css

+ 1 - 0
.gitignore

@@ -13,5 +13,6 @@ www/ledger.js*
 www/mat.js*
 www/plan.js*
 www/production.js*
+www/rat.js*
 www/roi.js*
 www/roi_*.json

+ 73 - 0
ts/rat.ts

@@ -0,0 +1,73 @@
+import {cachedFetchJSON} from './cache';
+
+render();
+
+async function render(): Promise<void> {
+	const loader = document.querySelector('#loader') as HTMLElement;
+	loader.style.display = 'block';
+	try {
+		await _render();
+	} catch (e) {
+		console.error(e);
+	}
+	loader.style.display = 'none';
+}
+
+async function _render(): Promise<void> {
+	const allRecipes: readonly RecipeFull[] = await cachedFetchJSON('https://api.prunplanner.org/data/recipes/');
+	const cols = ratRecipes(allRecipes);
+	document.querySelector('#cols')!.innerHTML = cols.map((col) => 
+		`<div class="col">${Array.from(col).map((input) => `<div class="mat">${input}</div>`).join('')}</div>`
+	).join('');
+}
+
+function ratRecipes(allRecipes: readonly RecipeFull[]): Set<string>[] {
+	const ratRecipes = allRecipes.filter((recipe) =>
+			recipe.building_ticker === 'FP' && recipe.outputs.length === 1 && recipe.outputs[0].material_ticker === 'RAT');
+	const recipes: Recipe[] = ratRecipes.map((recipe) => {
+		return {
+			inputs: recipe.inputs.map((input) => input.material_ticker),
+			catalogued: false,
+		};
+	});
+
+	const cols: Set<string>[] = [new Set(), new Set(), new Set()];
+	const seenInputs = new Set<string>();
+	recipes[0].inputs.forEach((input, index) => {
+		cols[index].add(input);
+		seenInputs.add(input);
+	});
+	recipes[0].catalogued = true;
+	let allCatalogued;
+	do {
+		allCatalogued = true;
+		for (const recipe of recipes) {
+			if (recipe.catalogued) continue;
+			const inputs = new Set(recipe.inputs);
+			const newInputs = inputs.difference(seenInputs);
+			if (newInputs.size === 1) {
+				const newInput = newInputs.keys().next().value!;
+				const seenCols = Array.from(inputs.difference(newInputs)).map((oldInput) => 
+					cols.findIndex((col) => col.has(oldInput)));
+				const newCol = new Set([0, 1, 2]).difference(new Set(seenCols)).keys().next().value!;
+				cols[newCol].add(newInput);
+				seenInputs.add(newInput);
+				recipe.catalogued = true;
+			} else if (newInputs.size === 0)
+				recipe.catalogued = true;
+			else
+				allCatalogued = false;
+		}
+	} while (!allCatalogued);
+	return cols;
+}
+
+interface RecipeFull {
+	building_ticker: string;
+	inputs: Array<{material_ticker: string}>
+	outputs: Array<{material_ticker: string}>
+}
+interface Recipe {
+	readonly inputs: Array<string>
+	catalogued: boolean;
+}

+ 19 - 0
www/rat.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="UTF-8">
+	<title>PrUn RAT recipes</title>
+	<link rel="stylesheet" type="text/css" href="style.css">
+	<link rel="icon" href="https://www.raylu.net/hammer-man.svg" />
+	<meta name="viewport" content="width=device-width, initial-scale=1">
+	<meta name="theme-color" content="#222">
+</head>
+<body>
+	<a href="/">← back</a>
+	<main class="rat">
+		<section id="loader"></section>
+		<div id="cols"></div>
+	</main>
+	<script src="rat.js"></script>
+</body>
+</html>

+ 21 - 0
www/style.css

@@ -267,6 +267,27 @@ main.production {
 	}
 }
 
+main.rat {
+	width: auto;
+	max-width: 500px;
+
+	#cols {
+		display: flex;
+		justify-content: space-evenly;
+		.col {
+			.mat {
+				width: 60px;
+				height: 60px;
+				display: flex;
+				justify-content: space-evenly;
+				align-items: center;
+				margin-bottom: 1em;
+				background-color: #242;
+			}
+		}
+	}
+}
+
 #loader {
 	display: none;
 	width: 48px;