primordial-soup/viewer/scoring-legend.js

55 lines
2.7 KiB
JavaScript

import { populationPoints, genePoints } from "../engine/catalog.js";
import { amoeba, colors, colonySymbol, icon } from "./art.js";
import { evolutionPoints } from "./rich-text.js";
const escape = (text) =>
String(text).replace(
/[&<>"']/g,
(c) =>
({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
})[c],
);
export function scoringLegend(
population,
genes,
color,
expandable = true,
players = 4,
colonies = [],
) {
const counts = players >= 5 ? [1, 2, 3, 4, 5, 6] : [2, 3, 4, 5, 6, 7];
const bucket = (n) => Math.max(counts[0], Math.min(counts.at(-1), n));
const populationScoreFor = (n) => populationPoints(n, players);
const markers = (n, field, points) =>
`<span class="scoring-markers">${colonies
.filter((player) => bucket(player[field]) === n)
.map((player) => {
const count = player[field];
const label = `${player.name}: ${count} ${field === "population" ? "living amoebas" : "genes for scoring"} → ${points(count)} evolution points`;
return `<span class="scoring-marker" role="img" tabindex="0" title="${escape(label)}" aria-label="${escape(label)}" style="--marker-color:${colors[player.color].body};--marker-border:${colors[player.color].stroke}">${colonySymbol(player.color)}</span>`;
})
.join("")}</span>`;
const row = (label, symbol, current, points, field) =>
`<tr data-scoring="${field}"><th scope="row" title="${label}" aria-label="${label}">${symbol}</th>${counts
.map(
(n) =>
`<td data-count="${n}"${bucket(current) === n ? ' class="current"' : ""}><span class="scoring-points">${points(n)}</span>${colonies.length ? markers(n, field, points) : ""}</td>`,
)
.join("")}</tr>`;
const populationScore = populationScoreFor(population);
const geneScore = genePoints(genes);
return `<table class="scoring-table" style="--scoring-current:${colors[color].light}"><caption>${expandable ? `<button class="scoring-help" data-ui="scoring" aria-label="Show scoring reference">${icon("spark")} Each round</button>` : `${icon("spark")} Each round`}</caption><thead><tr><th scope="col" aria-label="Number owned">#</th>${counts
.map(
(n) =>
`<th scope="col">${n === counts[0] ? `≤${n}` : n === counts.at(-1) ? `${n}+` : n}</th>`,
)
.join(
"",
)}</tr></thead><tbody>${row("Living amoebas", amoeba(color), population, populationScoreFor, "population")}${row("Genes used for scoring", icon("gene"), genes, genePoints, "genes")}</tbody></table><div class="scoring-total" title="Population points + gene points = total for this colony">${populationScore} + ${geneScore} = ${evolutionPoints(populationScore + geneScore)}</div>`;
}