263 lines
12 KiB
C#
263 lines
12 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 = 5;
|
|
public const int CompletionBonusBandwidth = 250;
|
|
public const int CompletionBonusEntropy = 125;
|
|
public const int CompletionBonusCommits = 2;
|
|
public const long PeriodTicks = TimeSpan.TicksPerDay;
|
|
public static readonly IReadOnlyList<DailyQuestKind> Pool = Array.AsReadOnly((DailyQuestKind[])Enum.GetValues(typeof(DailyQuestKind)));
|
|
private const int CompletionBonusBit = 1 << 8;
|
|
private static readonly DailyQuestKind[] FallbackSelection =
|
|
{
|
|
DailyQuestKind.Waves,
|
|
DailyQuestKind.Cards,
|
|
DailyQuestKind.Damage,
|
|
DailyQuestKind.BlockDamage,
|
|
DailyQuestKind.NativeCards
|
|
};
|
|
|
|
public static IReadOnlyList<DailyQuestKind> Active(DailyQuestState state) =>
|
|
ValidSelection(state.selectedQuests) ? state.selectedQuests : FallbackSelection;
|
|
|
|
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 bool EnsureFiveQuestSelection(DailyQuestState state)
|
|
{
|
|
if (ValidSelection(state.selectedQuests)) return false;
|
|
var expanded = new List<DailyQuestKind>(Count);
|
|
if (state.selectedQuests != null)
|
|
{
|
|
foreach (var kind in state.selectedQuests)
|
|
{
|
|
if (!Enum.IsDefined(typeof(DailyQuestKind), kind) || expanded.Contains(kind)) continue;
|
|
expanded.Add(kind);
|
|
if (expanded.Count == Count) break;
|
|
}
|
|
}
|
|
foreach (var kind in Pool)
|
|
{
|
|
if (!expanded.Contains(kind)) expanded.Add(kind);
|
|
if (expanded.Count == Count) break;
|
|
}
|
|
state.selectedQuests = expanded.ToArray();
|
|
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))
|
|
};
|
|
|
|
// Commits are earned by objectives that represent sustained play or a run milestone.
|
|
public static int CommitReward(DailyQuestKind kind) => kind switch
|
|
{
|
|
DailyQuestKind.Waves => 1,
|
|
DailyQuestKind.ShieldDamage => 1,
|
|
DailyQuestKind.SurviveRounds => 1,
|
|
DailyQuestKind.WaveFive => 2,
|
|
DailyQuestKind.Cards => 0,
|
|
DailyQuestKind.NativeCards => 0,
|
|
DailyQuestKind.Damage => 0,
|
|
DailyQuestKind.BlockDamage => 0,
|
|
_ => 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 CompletionBonusClaimed(DailyQuestState state) =>
|
|
(state.claimedMask & CompletionBonusBit) != 0;
|
|
|
|
public static bool AllClaimed(DailyQuestState state)
|
|
{
|
|
foreach (var kind in Active(state)) if (!Claimed(state, kind)) return false;
|
|
return true;
|
|
}
|
|
|
|
public static bool Refresh(DailyQuestState state, DateTime utcNow)
|
|
{
|
|
long now = utcNow.ToUniversalTime().Ticks;
|
|
if (state.refreshAtUtcTicks > now && state.refreshAtUtcTicks <= DateTime.MaxValue.Ticks)
|
|
{
|
|
// Expand older three-task periods in place so their progress and claims survive migration.
|
|
return EnsureFiveQuestSelection(state);
|
|
}
|
|
// Advance by whole periods, retaining the original reset time even after days away.
|
|
if (state.refreshAtUtcTicks > 0) EnsureFiveQuestSelection(state);
|
|
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);
|
|
int commits = CommitReward(kind);
|
|
int questBit = 1 << (int)kind;
|
|
int nextMask = data.dailyQuests.claimedMask | questBit;
|
|
bool awardsCompletionBonus = !CompletionBonusClaimed(data.dailyQuests);
|
|
foreach (var activeKind in Active(data.dailyQuests))
|
|
awardsCompletionBonus &= (nextMask & (1 << (int)activeKind)) != 0;
|
|
int bandwidth = reward.Bandwidth + (awardsCompletionBonus ? CompletionBonusBandwidth : 0);
|
|
int entropy = reward.Entropy + (awardsCompletionBonus ? CompletionBonusEntropy : 0);
|
|
int totalCommits = commits + (awardsCompletionBonus ? CompletionBonusCommits : 0);
|
|
if (data.bandwidthBalance > int.MaxValue - bandwidth
|
|
|| data.entropyBalance > int.MaxValue - entropy
|
|
|| data.commitsBalance > int.MaxValue - totalCommits) return false;
|
|
data.dailyQuests.claimedMask = nextMask | (awardsCompletionBonus ? CompletionBonusBit : 0);
|
|
data.bandwidthBalance += bandwidth;
|
|
data.entropyBalance += entropy;
|
|
data.commitsBalance += totalCommits;
|
|
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}";
|
|
}
|
|
}
|
|
}
|