Files
linuxdistro-roguelite/Assets/Scripts/UI/RunHistoryView.cs
T
2026-10-29 22:54:29 +01:00

139 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using KernelPanic.Data;
using KernelPanic.Meta;
using UnityEngine;
using UnityEngine.UIElements;
namespace KernelPanic.UI
{
public static class RunHistoryView
{
public static VisualElement Build(IReadOnlyList<RunHistoryEntry> entries, Action close)
=> Build(entries, close, null, null, null, null);
public static VisualElement Build(IReadOnlyList<RunHistoryEntry> entries, Action close,
CardDatabase cards, PackageDatabase packages, Action<RunHistoryEntry> loadBuild,
Func<RunHistoryEntry, string> unavailableReason)
{
var overlay = new VisualElement { name = "RunHistoryOverlay" };
overlay.AddToClassList("modal-overlay");
overlay.AddToClassList("modal-open");
var dialog = new VisualElement();
dialog.AddToClassList("root-credit-dialog");
dialog.AddToClassList("run-history-dialog");
dialog.style.width = Length.Percent(94);
dialog.style.maxWidth = 760;
dialog.style.maxHeight = Length.Percent(92);
overlay.Add(dialog);
dialog.Add(SoftwareCardView.Text($"RECENT RUNS / last {RunHistory.Capacity}", "section-title"));
dialog.Add(new Label { name = "RunHistoryNotice", enableRichText = false });
dialog.Q<Label>("RunHistoryNotice").style.whiteSpace = WhiteSpace.Normal;
var scroll = new ScrollView(ScrollViewMode.Vertical);
scroll.style.flexShrink = 1;
scroll.style.flexGrow = 1;
scroll.style.minHeight = 0;
scroll.horizontalScrollerVisibility = ScrollerVisibility.Hidden;
dialog.Add(scroll);
if (entries == null || entries.Count == 0)
scroll.Add(Text("No completed runs yet. Your next run report will appear here."));
else for (int index = 0; index < entries.Count; index++)
{
var entry = entries[index];
if (entry == null) continue;
var item = new VisualElement();
item.style.marginBottom = 16;
item.Add(Text($"{entry.distroName ?? entry.distroId} V{entry.distroVersion} / {entry.languages}"));
item.Add(Text($"Wave {entry.waveReached} reached / {entry.wavesCleared} cleared / seed {entry.seed}"));
string when = DateTimeOffset.TryParse(entry.endedUtc, out var time) ? time.ToLocalTime().ToString("g") : entry.endedUtc;
item.Add(Text($"{when} / {(entry.victory ? "Victory" : entry.surrendered ? "Shutdown" : "Defeat")}: {entry.cause}"));
item.Add(Text(entry.victory && entry.finalRound == 0 ? "Combat recap unavailable after resuming the victory checkpoint."
: $"Final round {entry.finalRound}: {entry.damageDealt} uptime dealt / {entry.damageTaken} lost / {entry.shieldBlocked} blocked by shield"));
if (entry.finalHits?.Count > 0)
{
var details = new Foldout { text = "Last incoming hits", value = false };
foreach (var hit in entry.finalHits) details.Add(Text(hit));
item.Add(details);
}
var buildDetails = new Foldout { text = "Inspect original build", name = $"ArchivedBuildDetails_{index}", value = false };
if (!RunHistory.HasBuildSnapshot(entry))
buildDetails.Add(Text("Build snapshot unavailable for this older run. Its deck, packages and languages cannot be reconstructed."));
else
{
var snapshot = entry.build;
buildDetails.Add(Text($"Original distro: {entry.distroName ?? snapshot.distroId} V{snapshot.distroVersion}"));
buildDetails.Add(Text("Languages: " + (snapshot.languages == null ? "unavailable" : string.Join(" + ", snapshot.languages))));
buildDetails.Add(Text($"Original seed: {snapshot.seed.ToString(CultureInfo.InvariantCulture)}"));
buildDetails.Add(Text("Selected exclusive cards:"));
AddCards(buildDetails, snapshot.exclusiveCardIds, cards);
var deck = new Foldout { text = $"Full starting deck ({snapshot.startingDeckCardIds?.Count ?? 0} cards)", value = false };
AddCards(deck, snapshot.startingDeckCardIds, cards);
buildDetails.Add(deck);
buildDetails.Add(Text("Equipped packages (original levels):"));
if (snapshot.packages == null || snapshot.packages.Count == 0) buildDetails.Add(Text("None equipped."));
else foreach (var package in snapshot.packages)
{
if (package == null) { buildDetails.Add(Text("Unavailable package entry.")); continue; }
var definition = packages == null ? null : packages.FindById(package.packageId);
buildDetails.Add(Text($"{(definition?.Slot ?? package.slot).DisplayName()}: {definition?.DisplayName ?? package.packageId} +{package.upgradeLevel}"
+ (definition == null ? " (content unavailable)" : "")));
}
buildDetails.Add(Text("This is the original build, before repository changes. Loading copies its selections and seed into setup using current owned versions and package levels; it does not start a run. Card rules use current content."));
}
string reason = unavailableReason?.Invoke(entry);
if (!RunHistory.HasBuildSnapshot(entry)) reason = "Build snapshot unavailable for this older run.";
if (!string.IsNullOrWhiteSpace(reason)) buildDetails.Add(Text(reason));
var load = new Button(() => loadBuild?.Invoke(entry)) { text = "load build into setup", name = $"LoadArchivedBuild_{index}", tooltip = reason };
load.SetEnabled(RunHistory.HasBuildSnapshot(entry) && loadBuild != null && string.IsNullOrWhiteSpace(reason));
load.AddToClassList("terminal-button");
buildDetails.Add(load);
item.Add(buildDetails);
item.Add(RunPerformanceView.Build(entry, cards));
var copySeed = new Button { text = "copy seed", name = $"CopyRunSeed_{index}" };
copySeed.AddToClassList("terminal-button");
copySeed.clicked += () =>
{
GUIUtility.systemCopyBuffer = (RunHistory.HasBuildSnapshot(entry) ? entry.build.seed : entry.seed).ToString(CultureInfo.InvariantCulture);
copySeed.text = "seed copied";
};
item.Add(copySeed);
scroll.Add(item);
}
var dismiss = new Button(close) { text = "Close [Esc]", name = "CloseRunHistory" };
dismiss.AddToClassList("terminal-button");
dismiss.style.flexShrink = 0;
dialog.Add(dismiss);
TerminalTheme.ConstrainModalFocus(overlay);
return overlay;
}
private static void AddCards(VisualElement parent, IReadOnlyList<string> ids, CardDatabase cards)
{
if (ids == null || ids.Count == 0) { parent.Add(Text("Card list unavailable.")); return; }
var counts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
var order = new List<string>();
foreach (string id in ids)
{
string key = string.IsNullOrWhiteSpace(id) ? "unknown card" : id;
if (!counts.ContainsKey(key)) { counts[key] = 0; order.Add(key); }
counts[key]++;
}
foreach (string id in order)
{
var card = cards == null ? null : cards.FindById(id);
parent.Add(Text($"{card?.DisplayName ?? id} x{counts[id]}"
+ (card == null ? " (content unavailable)" : $" / {card.Language} / {card.CycleCost} base Cycles")));
}
}
private static Label Text(string value)
{
var label = new Label(value) { enableRichText = false };
label.style.whiteSpace = WhiteSpace.Normal;
label.style.flexShrink = 0;
return label;
}
}
}