Add battle timeline markers and incremental web map updates (#153)
build / mod (push) Successful in 30m14s
build / mod (push) Successful in 30m14s
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// Markers are grouped into a small number of time buckets so every control
|
||||
// remains large enough to tap, including on narrow screens.
|
||||
const names = { impact: "Попадание", refusal: "Отказ", miss: "Промах" };
|
||||
const symbols = {impact: "◆", miss: "×", refusal: "!"};
|
||||
const refusalLabels = new Set([
|
||||
"Огонь запрещён", "Комплекс на марше", "Перезарядка", "Ожидает пополнения",
|
||||
"Ожидает данных о рельефе", "Траектория перекрыта",
|
||||
"Орудие недоступно", "Пусковая недоступна", "Нет свежего захвата", "Вне дальности орудия",
|
||||
]);
|
||||
|
||||
function markerType(event) {
|
||||
if (Object.hasOwn(names, event.marker)) return event.marker;
|
||||
const label = event.label || "";
|
||||
if (label.startsWith("Попадание") || label === "Комплекс уничтожен") return "impact";
|
||||
if (label.startsWith("Промах")) return "miss";
|
||||
if (event.kind === "decisions" && refusalLabels.has(label)) return "refusal";
|
||||
return null;
|
||||
}
|
||||
|
||||
export function createTimeline(container, onOpen, clock) {
|
||||
let expanded = null;
|
||||
let raidKey = null;
|
||||
let signature = "";
|
||||
return {
|
||||
render(raid, events, selectedId) {
|
||||
const key = raid ? `${raid.session}:${raid.raid}` : null;
|
||||
if (key !== raidKey) { expanded = null; raidKey = key; }
|
||||
const buckets = Array.from({ length: 5 }, () => []);
|
||||
for (const event of events) {
|
||||
const type = markerType(event);
|
||||
const at = event.data?.at;
|
||||
if (!raid || !type || !Number.isFinite(at) || at < 0) continue;
|
||||
// A terminal event can be a tick beyond the sampled replay length.
|
||||
const slot = Math.min(4, Math.floor(at / Math.max(1, raid.length) * 5));
|
||||
buckets[slot].push({ event, type, at });
|
||||
}
|
||||
const next = JSON.stringify([key, expanded, selectedId, buckets.map(bucket => bucket.map(item => item.event.id))]);
|
||||
if (next === signature) return;
|
||||
signature = next;
|
||||
const focused = container.contains(document.activeElement) ? document.activeElement.dataset.markerFocus : null;
|
||||
const oldScroll = container.querySelector(".timeline-marker-details")?.scrollTop ?? 0;
|
||||
container.replaceChildren();
|
||||
if (buckets.every(bucket => !bucket.length)) return;
|
||||
const rail = document.createElement("div");
|
||||
rail.className = "timeline-marker-rail";
|
||||
rail.setAttribute("role", "group");
|
||||
rail.setAttribute("aria-label", "Маркеры событий повтора");
|
||||
const details = document.createElement("div");
|
||||
details.className = "timeline-marker-details";
|
||||
for (const [index, bucket] of buckets.entries()) {
|
||||
const slot = document.createElement("div");
|
||||
slot.className = "timeline-marker-slot";
|
||||
rail.append(slot);
|
||||
if (!bucket.length) continue;
|
||||
bucket.sort((a, b) => a.at - b.at || a.event.id - b.event.id);
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "timeline-marker";
|
||||
button.dataset.markerFocus = `group-${index}`;
|
||||
const types = [...new Set(bucket.map(item => item.type))];
|
||||
button.dataset.type = types.length === 1 ? types[0] : "mixed";
|
||||
const description = types.map(type => `${names[type]} × ${bucket.filter(item => item.type === type).length}`).join(", ");
|
||||
const span = bucket.length === 1 ? clock(bucket[0].at) : `${clock(bucket[0].at)}–${clock(bucket.at(-1).at)}`;
|
||||
button.setAttribute("aria-label", `${span} · ${description} · ${bucket.length === 1 ? bucket[0].event.label : `${bucket.length} событий`}`);
|
||||
if (bucket.some(item => String(item.event.id) === String(selectedId))) button.classList.add("selected");
|
||||
const primary = Object.keys(symbols).find(type => types.includes(type));
|
||||
button.textContent = bucket.length === 1 ? symbols[primary] : `${symbols[primary]} ${bucket.length}`;
|
||||
if (bucket.length > 1) button.setAttribute("aria-expanded", String(expanded === index));
|
||||
button.addEventListener("click", () => {
|
||||
if (bucket.length === 1) { onOpen(bucket[0].event); return; }
|
||||
expanded = expanded === index ? null : index;
|
||||
this.render(raid, events, selectedId);
|
||||
});
|
||||
slot.append(button);
|
||||
if (expanded === index && bucket.length > 1) {
|
||||
const heading = document.createElement("p");
|
||||
heading.className = "muted timeline-marker-heading";
|
||||
heading.textContent = `Событий в группе ${bucket.length}`;
|
||||
details.append(heading);
|
||||
for (const { event, at } of bucket) {
|
||||
const choice = document.createElement("button");
|
||||
choice.type = "button";
|
||||
choice.className = "timeline-marker-choice";
|
||||
choice.dataset.markerFocus = `event-${event.id}`;
|
||||
choice.textContent = `${clock(at)} · ${event.label}`;
|
||||
if (String(event.id) === String(selectedId)) choice.setAttribute("aria-current", "true");
|
||||
choice.addEventListener("click", () => onOpen(event));
|
||||
details.append(choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
container.append(rail, details);
|
||||
details.scrollTop = oldScroll;
|
||||
if (focused) {
|
||||
[...container.querySelectorAll("button")].find(node => node.dataset.markerFocus === focused)?.focus({preventScroll: true});
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user