Refactor alot of stuff
This commit is contained in:
@@ -51,10 +51,10 @@ namespace KernelPanic.Meta
|
||||
},
|
||||
"ubuntu" => safeVersion switch
|
||||
{
|
||||
1 => "start of turn: apt update chooses the cheaper of top 2 draw cards",
|
||||
2 => "apt update looks at top 3 instead of top 2",
|
||||
1 => "turn draw: choose one of the top 2 cards with apt update",
|
||||
2 => "apt update lets you choose from the top 3 cards",
|
||||
3 => "ask ubuntu inspects 5 cards instead of 3; take one",
|
||||
4 => "apt update card costs 1 less this turn",
|
||||
4 => "apt update lets you choose from the top 4 cards; chosen card costs 1 less this turn",
|
||||
5 => "once per combat, empty hand refills up to RAM",
|
||||
_ => "--"
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using KernelPanic.Data;
|
||||
|
||||
namespace KernelPanic.Meta
|
||||
@@ -22,6 +23,7 @@ namespace KernelPanic.Meta
|
||||
private readonly PlayerCollection _collection;
|
||||
private readonly PackageDatabase _database;
|
||||
private readonly Dictionary<string, Dictionary<PackageSlot, string>> _equippedByDistro = new(StringComparer.OrdinalIgnoreCase);
|
||||
public List<string> MigrationNotices { get; } = new();
|
||||
|
||||
public PackageLoadout(PlayerCollection collection, PackageDatabase database)
|
||||
{
|
||||
@@ -137,21 +139,35 @@ namespace KernelPanic.Meta
|
||||
|
||||
Dictionary<PackageSlot, string> equipped = EnsureDistro(distroId);
|
||||
equipped.Clear();
|
||||
foreach (PackageLoadoutSlotSaveEntry slotEntry in slots ?? Array.Empty<PackageLoadoutSlotSaveEntry>())
|
||||
// Keep items already in their category first, then relocate old equipment.
|
||||
// A collision never changes ownership or upgrade levels.
|
||||
foreach (PackageLoadoutSlotSaveEntry slotEntry in (slots ?? Array.Empty<PackageLoadoutSlotSaveEntry>())
|
||||
.Where(s => s != null)
|
||||
.OrderBy(s => _database?.FindById(s.packageId)?.Slot == s.slot ? 0 : 1)
|
||||
.ThenBy(s => s.slot).ThenBy(s => s.packageId, StringComparer.Ordinal))
|
||||
{
|
||||
PackageDefinition package = _database == null ? null : _database.FindById(slotEntry?.packageId);
|
||||
if (package == null || !_collection.IsPackageOwned(package.Id) || package.Slot != slotEntry.slot)
|
||||
if (package == null || !_collection.IsPackageOwned(package.Id) || !package.AcceptsSavedSlot(slotEntry.slot))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (equipped.TryGetValue(package.Slot, out string existing))
|
||||
{
|
||||
if (existing != package.Id)
|
||||
MigrationNotices.Add($"{distroId}: {package.DisplayName} is now in {package.Slot.DisplayName()}. Unequipped because that slot is occupied; ownership and upgrades are retained.");
|
||||
continue;
|
||||
}
|
||||
equipped[package.Slot] = package.Id;
|
||||
if (package.Slot != slotEntry.slot)
|
||||
MigrationNotices.Add($"{distroId}: {package.DisplayName} moved to {package.Slot.DisplayName()}.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_equippedByDistro.Clear();
|
||||
MigrationNotices.Clear();
|
||||
}
|
||||
|
||||
private void AddIfEquipped(string distroId, PackageSlot slot, List<PackageDefinition> packages)
|
||||
|
||||
@@ -1,17 +1,36 @@
|
||||
using System;
|
||||
using KernelPanic.Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace KernelPanic.Meta
|
||||
{
|
||||
public static class PackageTuning
|
||||
{
|
||||
public const int MaxPackageLevel = 5;
|
||||
// Serialization ceiling; each definition owns its actual upgrade cap.
|
||||
public const int MaxPackageLevel = 8;
|
||||
public const float RefundFraction = 1f;
|
||||
public const float RarityCostMultiplier = 1f;
|
||||
|
||||
private static readonly int[] CachePerRarityValues = { 5, 12, 30, 80, 200 };
|
||||
private static readonly int[] UpgradeCacheCostValues = { 10, 25, 50, 90, 150 };
|
||||
private static readonly int[] UpgradeBandwidthCostValues = { 20, 50, 100, 180, 300 };
|
||||
private static readonly int[] UpgradeCacheCostValues = { 10, 25, 50, 90, 150, 230, 330, 450 };
|
||||
private static readonly int[] UpgradeBandwidthCostValues = { 20, 50, 100, 180, 300, 460, 660, 900 };
|
||||
|
||||
public static void ReconcileUpgradeCaps(SaveData save, PackageDatabase database)
|
||||
{
|
||||
if (save?.ownedPackages == null || database == null) return;
|
||||
foreach (var entry in save.ownedPackages)
|
||||
{
|
||||
var definition = database.FindById(entry?.id);
|
||||
if (definition == null || entry.upgradeLevel <= definition.MaxUpgradeLevel) continue;
|
||||
int previous = entry.upgradeLevel;
|
||||
int current = definition.MaxUpgradeLevel;
|
||||
int cache = GetInvestedCache(previous, definition.Rarity) - GetInvestedCache(current, definition.Rarity);
|
||||
int bandwidth = GetInvestedBandwidth(previous, definition.Rarity) - GetInvestedBandwidth(current, definition.Rarity);
|
||||
save.cacheBalance = (int)Math.Min(int.MaxValue, (long)save.cacheBalance + cache);
|
||||
save.bandwidthBalance = (int)Math.Min(int.MaxValue, (long)save.bandwidthBalance + bandwidth);
|
||||
entry.upgradeLevel = current; // Same save transaction as the refund; safe to retry loading.
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetCacheForRarity(int rarity)
|
||||
{
|
||||
|
||||
@@ -36,13 +36,13 @@ namespace KernelPanic.Meta
|
||||
|
||||
public static PackageUpgradeResult TryUpgrade(OwnedPackageInstance packageInstance, SaveData wallet)
|
||||
{
|
||||
if (packageInstance == null || wallet == null)
|
||||
if (packageInstance?.Definition == null || wallet == null)
|
||||
{
|
||||
return new PackageUpgradeResult(false, PackageUpgradeFailureReason.NotOwned, 0, 0, 0);
|
||||
}
|
||||
|
||||
int currentLevel = Math.Max(0, packageInstance.UpgradeLevel);
|
||||
if (currentLevel >= PackageTuning.MaxPackageLevel)
|
||||
if (currentLevel >= packageInstance.MaxUpgradeLevel)
|
||||
{
|
||||
return new PackageUpgradeResult(false, PackageUpgradeFailureReason.MaxLevel, 0, 0, currentLevel);
|
||||
}
|
||||
|
||||
@@ -246,12 +246,13 @@ namespace KernelPanic.Meta
|
||||
public OwnedPackageInstance(PackageDefinition definition, int upgradeLevel)
|
||||
{
|
||||
Definition = definition;
|
||||
UpgradeLevel = Math.Max(0, Math.Min(PackageTuning.MaxPackageLevel, upgradeLevel));
|
||||
UpgradeLevel = Math.Max(0, Math.Min(MaxUpgradeLevel, upgradeLevel));
|
||||
}
|
||||
|
||||
public PackageDefinition Definition { get; }
|
||||
public string PackageId => Definition == null ? string.Empty : Definition.Id;
|
||||
public int UpgradeLevel { get; private set; }
|
||||
public int MaxUpgradeLevel => Definition == null ? 0 : Definition.MaxUpgradeLevel;
|
||||
|
||||
public PackageInstance ToRuntimeInstance()
|
||||
{
|
||||
@@ -260,7 +261,7 @@ namespace KernelPanic.Meta
|
||||
|
||||
public void SetUpgradeLevel(int level)
|
||||
{
|
||||
UpgradeLevel = Math.Max(0, Math.Min(PackageTuning.MaxPackageLevel, level));
|
||||
UpgradeLevel = Math.Max(0, Math.Min(MaxUpgradeLevel, level));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace KernelPanic.Meta
|
||||
if (package?.Definition != null)
|
||||
snapshot.packages.Add(new RunBuildPackageSaveEntry
|
||||
{
|
||||
packageId = package.Definition.Id, slot = package.Definition.Slot,
|
||||
packageId = package.Definition.Id, slot = package.SavedSlot,
|
||||
upgradeLevel = package.UpgradeLevel
|
||||
});
|
||||
return snapshot;
|
||||
@@ -141,15 +141,19 @@ namespace KernelPanic.Meta
|
||||
if (string.IsNullOrWhiteSpace(id) || (cards?.FindById(id) == null && !uniqueCards.Contains(id))) return false;
|
||||
}
|
||||
var slots = new HashSet<PackageSlot>();
|
||||
var currentSlots = 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)
|
||||
|| !definition.AcceptsSavedSlot(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 (currentSlots.Add(definition.Slot))
|
||||
candidate.PackageSlots.Add(new PackageLoadoutSlotSaveEntry { packageId = definition.Id, slot = definition.Slot });
|
||||
else
|
||||
changes.Add($"{definition.DisplayName} left unequipped: {definition.Slot.DisplayName()} is occupied; ownership and upgrades retained");
|
||||
if (owned.UpgradeLevel != saved.upgradeLevel)
|
||||
changes.Add($"{definition.DisplayName}: archived +{saved.upgradeLevel} -> owned +{owned.UpgradeLevel}");
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace KernelPanic.Meta
|
||||
public sealed class SaveService
|
||||
{
|
||||
private const string SaveFileName = "kernel-panic-save.json";
|
||||
public const int CurrentSchemaVersion = 9;
|
||||
public const int CurrentSchemaVersion = 10;
|
||||
private static readonly object FileLock = new();
|
||||
|
||||
public string SavePath { get; }
|
||||
|
||||
Reference in New Issue
Block a user