191 lines
9.8 KiB
C#
191 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using KernelPanic.Core;
|
|
using KernelPanic.Data;
|
|
using KernelPanic.Run;
|
|
|
|
namespace KernelPanic.Meta
|
|
{
|
|
[Serializable]
|
|
public sealed class RunHistoryEntry
|
|
{
|
|
public string runId;
|
|
public string endedUtc;
|
|
public string distroId;
|
|
public string distroName;
|
|
public int distroVersion;
|
|
public string languages;
|
|
public int seed;
|
|
public int waveReached;
|
|
public int wavesCleared;
|
|
public bool surrendered;
|
|
public bool victory;
|
|
public string cause;
|
|
public int finalRound;
|
|
public int damageDealt;
|
|
public int damageTaken;
|
|
public int shieldBlocked;
|
|
public List<string> finalHits = new();
|
|
public RunHistoryBuildSnapshot build;
|
|
public RunPerformanceSnapshot performance;
|
|
public List<CheckpointCard> finalDeck;
|
|
}
|
|
|
|
/// <summary>The original starting build, not the deck after repository upgrades.</summary>
|
|
[Serializable]
|
|
public sealed class RunHistoryBuildSnapshot
|
|
{
|
|
// Keep the default zero: JsonUtility may create an empty object for old saves.
|
|
public int schemaVersion;
|
|
public string distroId;
|
|
public int distroVersion;
|
|
public int seed;
|
|
public List<string> exclusiveCardIds = new();
|
|
public List<string> startingDeckCardIds = new();
|
|
public List<Language> languages = new();
|
|
public List<RunBuildPackageSaveEntry> packages = new();
|
|
}
|
|
|
|
public sealed class ResolvedHistoryBuild
|
|
{
|
|
public DistroDefinition Distro { get; internal set; }
|
|
public List<string> CardIds { get; } = new();
|
|
public List<Language> Languages { get; } = new();
|
|
public List<PackageLoadoutSlotSaveEntry> PackageSlots { get; } = new();
|
|
public int Seed { get; internal set; }
|
|
public string Notice { get; internal set; }
|
|
}
|
|
|
|
public static class RunHistory
|
|
{
|
|
public const int Capacity = 20;
|
|
|
|
public static bool HasBuildSnapshot(RunHistoryEntry entry) => entry?.build?.schemaVersion == 1;
|
|
|
|
public static RunHistoryBuildSnapshot CaptureBuild(RunConfig config)
|
|
{
|
|
if (config?.Distro == null) return null;
|
|
var snapshot = new RunHistoryBuildSnapshot
|
|
{
|
|
schemaVersion = 1, distroId = config.Distro.Id,
|
|
distroVersion = config.DistroVersion, seed = config.RunSeed,
|
|
languages = new List<Language> { config.PrimaryLanguage, config.SecondaryLanguage }
|
|
};
|
|
var exclusiveIds = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var card in config.Distro.ExclusiveCards)
|
|
if (card != null && !card.IsRunOnly && !card.IsToken) exclusiveIds.Add(card.Id);
|
|
foreach (var card in config.StartingDeck ?? Array.Empty<CardDefinition>())
|
|
{
|
|
if (card == null) continue;
|
|
snapshot.startingDeckCardIds.Add(card.Id);
|
|
if (exclusiveIds.Contains(card.Id)) snapshot.exclusiveCardIds.Add(card.Id);
|
|
}
|
|
foreach (var package in config.EquippedPackages)
|
|
if (package?.Definition != null)
|
|
snapshot.packages.Add(new RunBuildPackageSaveEntry
|
|
{
|
|
packageId = package.Definition.Id, slot = package.Definition.Slot,
|
|
upgradeLevel = package.UpgradeLevel
|
|
});
|
|
return snapshot;
|
|
}
|
|
|
|
/// <summary>Resolve without changing drafts. Setup deliberately uses current owned upgrades.</summary>
|
|
public static bool TryResolveBuild(RunHistoryEntry entry, PlayerCollection collection,
|
|
DistroDatabase distros, CardDatabase cards, PackageDatabase packages, LanguageDeckDatabase languageDecks,
|
|
out ResolvedHistoryBuild resolved, out string reason)
|
|
{
|
|
resolved = null;
|
|
reason = "Build snapshot unavailable for this older run.";
|
|
if (!HasBuildSnapshot(entry)) return false;
|
|
var build = entry.build;
|
|
reason = "This build snapshot is incomplete or uses an unsupported version.";
|
|
if (build.distroVersion < 1 || build.distroVersion > GachaTuning.MaxVersion
|
|
|| build.exclusiveCardIds?.Count != CardLoadout.MaxEquippedCards
|
|
|| build.languages?.Count != 2 || build.languages[0] == build.languages[1]
|
|
|| build.packages == null || build.startingDeckCardIds == null
|
|
|| build.startingDeckCardIds.Count < CardLoadout.MaxEquippedCards) return false;
|
|
var distro = distros == null ? null : distros.FindById(build.distroId);
|
|
reason = $"Distro '{build.distroId}' is missing or is not currently owned.";
|
|
if (distro == null || collection == null || !collection.IsOwned(distro.Id)) return false;
|
|
var candidate = new ResolvedHistoryBuild { Distro = distro, Seed = build.seed };
|
|
var changes = new List<string>();
|
|
int currentVersion = collection.GetVersion(distro.Id);
|
|
if (currentVersion != build.distroVersion)
|
|
changes.Add($"{distro.DisplayName}: archived V{build.distroVersion} -> owned V{currentVersion}");
|
|
var uniqueCards = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (string id in build.exclusiveCardIds)
|
|
{
|
|
CardDefinition card = null;
|
|
foreach (var exclusive in distro.ExclusiveCards)
|
|
if (exclusive != null && string.Equals(exclusive.Id, id, StringComparison.OrdinalIgnoreCase)) { card = exclusive; break; }
|
|
reason = $"Exclusive card '{id}' is missing or no longer available in this distro's setup.";
|
|
if (card == null || card.IsRunOnly || card.IsToken || !uniqueCards.Add(card.Id)) return false;
|
|
candidate.CardIds.Add(card.Id);
|
|
if (!build.startingDeckCardIds.Exists(savedId => string.Equals(savedId, card.Id, StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
reason = "This build snapshot is incomplete: a selected card is missing from its starting deck.";
|
|
return false;
|
|
}
|
|
}
|
|
foreach (var language in build.languages)
|
|
{
|
|
reason = $"Language '{language}' is missing or is not currently unlocked.";
|
|
if (!Enum.IsDefined(typeof(Language), language) || !LanguageUnlock.IsUnlocked(language, collection)
|
|
|| languageDecks == null || languageDecks.FindByLanguage(language) == null) return false;
|
|
candidate.Languages.Add(language);
|
|
}
|
|
foreach (string id in build.startingDeckCardIds)
|
|
{
|
|
reason = $"Starting-deck card '{id}' is missing from the current content.";
|
|
if (string.IsNullOrWhiteSpace(id) || (cards?.FindById(id) == null && !uniqueCards.Contains(id))) return false;
|
|
}
|
|
var slots = new HashSet<PackageSlot>();
|
|
foreach (var saved in build.packages)
|
|
{
|
|
var definition = saved == null || packages == null ? null : packages.FindById(saved.packageId);
|
|
var owned = definition == null ? null : collection.GetOwnedPackage(definition.Id);
|
|
reason = $"Package '{saved?.packageId ?? "unknown"}' is missing, not owned, or has an invalid saved slot.";
|
|
if (definition == null || owned == null || !Enum.IsDefined(typeof(PackageSlot), saved.slot)
|
|
|| definition.Slot != saved.slot || !slots.Add(saved.slot)
|
|
|| saved.upgradeLevel < 0 || saved.upgradeLevel > PackageTuning.MaxPackageLevel) return false;
|
|
candidate.PackageSlots.Add(new PackageLoadoutSlotSaveEntry { packageId = definition.Id, slot = definition.Slot });
|
|
if (owned.UpgradeLevel != saved.upgradeLevel)
|
|
changes.Add($"{definition.DisplayName}: archived +{saved.upgradeLevel} -> owned +{owned.UpgradeLevel}");
|
|
}
|
|
candidate.Notice = "Archive build loaded into setup with its original seed. Nothing has launched. Setup uses current owned distro and package upgrades."
|
|
+ (changes.Count == 0 ? "" : " Changed: " + string.Join("; ", changes) + ".")
|
|
+ " Card and language rules use the currently installed content.";
|
|
resolved = candidate;
|
|
reason = null;
|
|
return true;
|
|
}
|
|
|
|
public static void Record(List<RunHistoryEntry> entries, RunHistoryEntry entry)
|
|
{
|
|
if (entries == null || entry == null || string.IsNullOrWhiteSpace(entry.runId)) return;
|
|
entries.RemoveAll(old => old == null || string.Equals(old.runId, entry.runId, StringComparison.Ordinal));
|
|
entries.Insert(0, entry);
|
|
Normalize(entries);
|
|
}
|
|
|
|
public static void Normalize(List<RunHistoryEntry> entries)
|
|
{
|
|
if (entries == null) return;
|
|
var seen = new HashSet<string>(StringComparer.Ordinal);
|
|
entries.RemoveAll(entry => entry == null || string.IsNullOrWhiteSpace(entry.runId) || !seen.Add(entry.runId));
|
|
if (entries.Count > Capacity) entries.RemoveRange(Capacity, entries.Count - Capacity);
|
|
foreach (var entry in entries)
|
|
{
|
|
entry.waveReached = Math.Max(0, entry.waveReached);
|
|
entry.wavesCleared = Math.Max(0, entry.wavesCleared);
|
|
entry.damageDealt = Math.Max(0, entry.damageDealt);
|
|
entry.damageTaken = Math.Max(0, entry.damageTaken);
|
|
entry.shieldBlocked = Math.Max(0, entry.shieldBlocked);
|
|
entry.finalHits ??= new List<string>();
|
|
if (entry.finalHits.Count > 8) entry.finalHits.RemoveRange(0, entry.finalHits.Count - 8);
|
|
}
|
|
}
|
|
}
|
|
}
|