Forráskód Böngészése

coop: cont sell for each person

raylu 1 hete
szülő
commit
c0bb047b80
2 módosított fájl, 32 hozzáadás és 9 törlés
  1. 29 9
      ts/coop.ts
  2. 3 0
      www/coop.html

+ 29 - 9
ts/coop.ts

@@ -164,13 +164,32 @@ async function _render(apiKey: string, planet: string, usernames: string[], carg
 			${burnTable(combinedBurn, combinedStorage)}`;
 
 	const materials = new Map(rawMaterials.map((m) => [m.ticker, m]));
-	renderTarget.innerHTML += ship(materials, combinedBurn, combinedStorage, cargo);
+	const combinedShipping = ship(materials, combinedBurn, combinedStorage, cargo);
+	renderTarget.innerHTML += combinedShipping.html;
 
-	renderTarget.innerHTML += userBurns.map(ub => ub.html).join('');
+	for (const ub of userBurns) {
+		renderTarget.innerHTML += ub.html;
+
+		const items = allStorages.get(ub.username);
+		if (combinedShipping.supply !== null && items !== undefined) {
+			let pasteLines: string[] = [];
+			for (const mat of combinedShipping.supply.keys()) {
+				const burn = ub.burn.get(mat);
+				if (burn === undefined || burn.Net >= 0)
+					continue;
+				const need = -burn.Net * combinedShipping.days - (items.get(mat) ?? 0);
+				if (need > 0)
+					// unfortunately, rounding means if 4 people each need 0.25, we'll buy 1 and contract each person 0
+					pasteLines.push(`${mat}\t${Math.round(need)}\t0.01`);
+			}
+			if (pasteLines.length > 0)
+				renderTarget.innerHTML += `<textarea readonly>${pasteLines.join('\n')}</textarea>`;
+		}
+	}
 }
 
-async function userBurn(apiKey: string, planet: string, username: string, items?: Map<string, number>,
-		lastUpdated?: number, wfNeeds?: Map<string, number>): Promise<{burn: Map<string, Burn>, html: string}> {
+async function userBurn(apiKey: string, planet: string, username: string, items?: Map<string, number>, lastUpdated?: number,
+		wfNeeds?: Map<string, number>): Promise<{username: string, burn: Map<string, Burn>, html: string}> {
 	const burns: Record<string, Burn[]> = await cachedFetchJSON(
 			`https://api.punoted.net/v1/production/user/burn?location=${planet}&username=${username}`,
 			{headers: {'X-Data-Token': apiKey}});
@@ -195,7 +214,7 @@ async function userBurn(apiKey: string, planet: string, username: string, items?
 	const html = `<h2>${username}</h2>
 			last updated: ${lastUpdated ? new Date(lastUpdated).toLocaleString() : ''}
 			${burnTable(burn, items)}`;
-	return {burn, html};
+	return {username, burn, html};
 }
 
 function burnTable(burn: Map<string, Burn>, items?: Map<string, number>): string {
@@ -223,7 +242,7 @@ function burnTable(burn: Map<string, Burn>, items?: Map<string, number>): string
 }
 
 function ship(materials: Map<string, Material>, burn: Map<string, Burn>, items: Map<string, number>,
-		cargo: {weight: number, volume: number}): string {
+		cargo: {weight: number, volume: number}): {supply: Map<string, number> | null, days: number, html: string} {
 	let targetDays = Infinity;
 	for (const b of burn.values()) {
 		if (b.Net >= 0) continue;
@@ -253,7 +272,7 @@ function ship(materials: Map<string, Material>, burn: Map<string, Burn>, items:
 	}
 
 	if (optimal === null)
-		return 'nothing to supply';
+		return {supply: optimal, days: NaN, html: 'nothing to supply'};
 	const xitAct = {
 		'actions': [
 			{'name': 'BuyItems', 'type': 'CX Buy', 'group': 'A1', 'exchange': 'IC1',
@@ -266,8 +285,9 @@ function ship(materials: Map<string, Material>, burn: Map<string, Burn>, items:
 			'type': 'Manual', 'name': 'A1', 'materials': Object.fromEntries(optimal.entries())
 		}],
 	};
-	return `<h2>supply for ${format(optimalDays)} days (${format(totalWeightUsed)}t, ${format(totalVolumeUsed)}m³)</h2>
-		<textarea>${JSON.stringify(xitAct)}</textarea>`;
+	const html = `<h2>supply for ${format(optimalDays)} days (${format(totalWeightUsed)}t, ${format(totalVolumeUsed)}m³)</h2>
+		<textarea readonly>${JSON.stringify(xitAct)}</textarea>`;
+	return {supply: optimal, days: optimalDays, html};
 }
 
 function supplyForDays(burn: Map<string, Burn>, items: Map<string, number>, targetDays: number): Map<string, number> {

+ 3 - 0
www/coop.html

@@ -24,6 +24,9 @@
 		</form>
 		<section id="loader"></section>
 		<div id="coop"></div>
+
+		<h2>contract XIT ACT</h2>
+		<textarea readonly>{"global":{"name":"sell cont"}, "groups":[{"name":"m", "type":"Paste"}], "actions":[{"name":"c", "type":"CONT Trade", "group":"m", "contTradeType":"SELLING", "contLocation":"Configure on Execution", "currency":"ICA", "daysToFulfill":3}]}</textarea>
 	</main>
 	<div id="popover" popover="hint"></div>
 	<script src="coop.js"></script>