199 lines
9.8 KiB
C#
199 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace KernelPanic.Meta
|
|
{
|
|
// Keep values stable: selections and claim bits are persisted in player saves.
|
|
public enum DailyQuestKind { Waves, Cards, Damage, BlockDamage, NativeCards, ShieldDamage, SurviveRounds, WaveFive }
|
|
|
|
[Serializable]
|
|
public sealed class DailyQuestState
|
|
{
|
|
public long refreshAtUtcTicks;
|
|
public int waves;
|
|
public int cards;
|
|
public int damage;
|
|
public int claimedMask;
|
|
public DailyQuestKind[] selectedQuests;
|
|
public int selectionSeed;
|
|
public int blockedDamage;
|
|
public int nativeCards;
|
|
public int shieldDamage;
|
|
public int survivedRounds;
|
|
public int waveFive;
|
|
}
|
|
|
|
/// <summary>One persistent, rolling 24-hour window. All time comes from the caller for deterministic tests.</summary>
|
|
public static class DailyQuests
|
|
{
|
|
public const int Count = 3;
|
|
public const long PeriodTicks = TimeSpan.TicksPerDay;
|
|
public static readonly IReadOnlyList<DailyQuestKind> Pool = Array.AsReadOnly((DailyQuestKind[])Enum.GetValues(typeof(DailyQuestKind)));
|
|
private static readonly DailyQuestKind[] LegacySelection = { DailyQuestKind.Waves, DailyQuestKind.Cards, DailyQuestKind.Damage };
|
|
|
|
public static IReadOnlyList<DailyQuestKind> Active(DailyQuestState state) =>
|
|
ValidSelection(state.selectedQuests) ? state.selectedQuests : LegacySelection;
|
|
|
|
public static bool IsActive(DailyQuestState state, DailyQuestKind kind)
|
|
{
|
|
foreach (var selected in Active(state)) if (selected == kind) return true;
|
|
return false;
|
|
}
|
|
|
|
private static bool ValidSelection(DailyQuestKind[] selection)
|
|
{
|
|
if (selection == null || selection.Length != Count) return false;
|
|
for (int i = 0; i < selection.Length; i++)
|
|
{
|
|
if (!Enum.IsDefined(typeof(DailyQuestKind), selection[i])) return false;
|
|
for (int j = 0; j < i; j++) if (selection[i] == selection[j]) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static void PickQuests(DailyQuestState state)
|
|
{
|
|
if (state.selectionSeed == 0)
|
|
{
|
|
state.selectionSeed = Guid.NewGuid().GetHashCode();
|
|
if (state.selectionSeed == 0) state.selectionSeed = 1;
|
|
}
|
|
// Local randomness never consumes the combat, shop, or Unity RNG streams.
|
|
var random = new Random(unchecked(state.selectionSeed ^ (int)state.refreshAtUtcTicks ^ (int)(state.refreshAtUtcTicks >> 32)));
|
|
var candidates = (DailyQuestKind[])Enum.GetValues(typeof(DailyQuestKind));
|
|
for (int i = candidates.Length - 1; i > 0; i--)
|
|
{
|
|
int swap = random.Next(i + 1);
|
|
(candidates[i], candidates[swap]) = (candidates[swap], candidates[i]);
|
|
}
|
|
bool sameSet = ValidSelection(state.selectedQuests);
|
|
for (int i = 0; i < Count; i++) sameSet &= IsActive(state, candidates[i]);
|
|
if (sameSet) (candidates[Count - 1], candidates[Count]) = (candidates[Count], candidates[Count - 1]);
|
|
state.selectedQuests = new DailyQuestKind[Count];
|
|
Array.Copy(candidates, state.selectedQuests, Count);
|
|
}
|
|
|
|
public static int Target(DailyQuestKind kind) => kind switch
|
|
{
|
|
DailyQuestKind.Waves => 3,
|
|
DailyQuestKind.Cards => 25,
|
|
DailyQuestKind.Damage => 200,
|
|
DailyQuestKind.BlockDamage => 40,
|
|
DailyQuestKind.NativeCards => 15,
|
|
DailyQuestKind.ShieldDamage => 60,
|
|
DailyQuestKind.SurviveRounds => 8,
|
|
DailyQuestKind.WaveFive => 1,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
|
};
|
|
|
|
// Routine actions < focused combat objectives < sustained/encounter-dependent play < wave-5 milestone.
|
|
// Display and settlement share this table; rewards are not persisted separately from claim flags.
|
|
public static (int Bandwidth, int Entropy) Reward(DailyQuestKind kind) => kind switch
|
|
{
|
|
DailyQuestKind.Cards => (75, 25),
|
|
DailyQuestKind.NativeCards => (75, 25),
|
|
DailyQuestKind.Damage => (100, 50),
|
|
DailyQuestKind.BlockDamage => (100, 50),
|
|
DailyQuestKind.Waves => (150, 75),
|
|
DailyQuestKind.ShieldDamage => (150, 75),
|
|
DailyQuestKind.SurviveRounds => (150, 75),
|
|
DailyQuestKind.WaveFive => (250, 125),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
|
};
|
|
|
|
public static string Description(DailyQuestKind kind) => kind switch
|
|
{
|
|
DailyQuestKind.Waves => "Clear 3 waves",
|
|
DailyQuestKind.Cards => "Play 25 cards",
|
|
DailyQuestKind.Damage => "Deal 200 Uptime damage",
|
|
DailyQuestKind.BlockDamage => "Block 40 damage with Shield",
|
|
DailyQuestKind.NativeCards => "Play 15 Native cards",
|
|
DailyQuestKind.ShieldDamage => "Deal 60 damage to enemy Shields",
|
|
DailyQuestKind.SurviveRounds => "Survive 8 enemy rounds",
|
|
DailyQuestKind.WaveFive => "Clear wave 5",
|
|
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
|
};
|
|
|
|
public static int Progress(DailyQuestState state, DailyQuestKind kind) => Math.Clamp(kind switch
|
|
{
|
|
DailyQuestKind.Waves => state.waves,
|
|
DailyQuestKind.Cards => state.cards,
|
|
DailyQuestKind.Damage => state.damage,
|
|
DailyQuestKind.BlockDamage => state.blockedDamage,
|
|
DailyQuestKind.NativeCards => state.nativeCards,
|
|
DailyQuestKind.ShieldDamage => state.shieldDamage,
|
|
DailyQuestKind.SurviveRounds => state.survivedRounds,
|
|
DailyQuestKind.WaveFive => state.waveFive,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(kind))
|
|
}, 0, Target(kind));
|
|
|
|
public static bool Claimed(DailyQuestState state, DailyQuestKind kind) =>
|
|
(state.claimedMask & (1 << (int)kind)) != 0;
|
|
|
|
public static bool Refresh(DailyQuestState state, DateTime utcNow)
|
|
{
|
|
long now = utcNow.ToUniversalTime().Ticks;
|
|
if (state.refreshAtUtcTicks > now && state.refreshAtUtcTicks <= DateTime.MaxValue.Ticks)
|
|
{
|
|
if (ValidSelection(state.selectedQuests)) return false;
|
|
// Schema 7 had three fixed quests. Preserve that day's progress and rewards.
|
|
state.selectedQuests = (DailyQuestKind[])LegacySelection.Clone();
|
|
return true;
|
|
}
|
|
// Advance by whole periods, retaining the original reset time even after days away.
|
|
if (state.refreshAtUtcTicks > 0 && !ValidSelection(state.selectedQuests))
|
|
state.selectedQuests = (DailyQuestKind[])LegacySelection.Clone();
|
|
state.refreshAtUtcTicks = state.refreshAtUtcTicks > 0 && state.refreshAtUtcTicks <= now
|
|
? state.refreshAtUtcTicks + ((now - state.refreshAtUtcTicks) / PeriodTicks + 1) * PeriodTicks
|
|
: now + PeriodTicks;
|
|
state.waves = state.cards = state.damage = state.claimedMask = 0;
|
|
state.blockedDamage = state.nativeCards = state.shieldDamage = state.survivedRounds = state.waveFive = 0;
|
|
PickQuests(state);
|
|
return true;
|
|
}
|
|
|
|
public static bool Record(DailyQuestState state, DailyQuestKind kind, int amount, DateTime utcNow)
|
|
{
|
|
if (amount <= 0 || !Enum.IsDefined(typeof(DailyQuestKind), kind)) return false;
|
|
bool changed = Refresh(state, utcNow);
|
|
// Delayed retries from an expired period must not count toward today's quests.
|
|
if (utcNow.ToUniversalTime().Ticks < state.refreshAtUtcTicks - PeriodTicks || !IsActive(state, kind)) return changed;
|
|
int previous = Progress(state, kind);
|
|
int next = (int)Math.Min(Target(kind), (long)previous + amount);
|
|
switch (kind)
|
|
{
|
|
case DailyQuestKind.Waves: state.waves = next; break;
|
|
case DailyQuestKind.Cards: state.cards = next; break;
|
|
case DailyQuestKind.Damage: state.damage = next; break;
|
|
case DailyQuestKind.BlockDamage: state.blockedDamage = next; break;
|
|
case DailyQuestKind.NativeCards: state.nativeCards = next; break;
|
|
case DailyQuestKind.ShieldDamage: state.shieldDamage = next; break;
|
|
case DailyQuestKind.SurviveRounds: state.survivedRounds = next; break;
|
|
case DailyQuestKind.WaveFive: state.waveFive = next; break;
|
|
}
|
|
return changed || next != previous;
|
|
}
|
|
|
|
public static bool Claim(SaveData data, DailyQuestKind kind, long expectedRefresh, DateTime utcNow)
|
|
{
|
|
data.dailyQuests ??= new DailyQuestState();
|
|
Refresh(data.dailyQuests, utcNow);
|
|
if (!Enum.IsDefined(typeof(DailyQuestKind), kind) || !IsActive(data.dailyQuests, kind) || data.dailyQuests.refreshAtUtcTicks != expectedRefresh
|
|
|| Claimed(data.dailyQuests, kind) || Progress(data.dailyQuests, kind) < Target(kind)) return false;
|
|
var reward = Reward(kind);
|
|
if (data.bandwidthBalance > int.MaxValue - reward.Bandwidth
|
|
|| data.entropyBalance > int.MaxValue - reward.Entropy) return false;
|
|
data.dailyQuests.claimedMask |= 1 << (int)kind;
|
|
data.bandwidthBalance += reward.Bandwidth;
|
|
data.entropyBalance += reward.Entropy;
|
|
return true;
|
|
}
|
|
|
|
public static string Countdown(DailyQuestState state, DateTime utcNow)
|
|
{
|
|
var remaining = TimeSpan.FromTicks(Math.Max(0, state.refreshAtUtcTicks - utcNow.ToUniversalTime().Ticks));
|
|
return $"refresh in {(int)remaining.TotalHours:00}:{remaining.Minutes:00}:{remaining.Seconds:00}";
|
|
}
|
|
}
|
|
}
|