namespace XFEEDToolBox.Core.Navigation; public sealed record ScoThermalSourceRate(ulong SourceId,string Label,bool VerifiedSco,double RequestedEnergyPerSecond,double UpperEnergyPerSecond); public sealed record ScoThermalSourceSnapshot(bool Ready,ulong ObservedAtUnixMilliseconds,double WindowSimulationSeconds,double TotalRequestedEnergyPerSecond, double ScoInputUpper,double OtherInputUpper,IReadOnlyList Sources,string? Issue); /// Accounts requested AddHeat energy by its original source label. The caller serializes access and validates the receiver/ship. public sealed class ScoThermalSourceLedger { private sealed class Source(ulong id,string label,bool sco) { internal readonly ulong Id=id; internal readonly string Label=label; internal readonly bool Sco=sco; internal double Pending,Rate; internal readonly Queue<(double At,double Rate)> History=new(); } private readonly Dictionary<(ulong Id,bool Sco),Source> sources=new(); private bool synchronized; private ulong lastAt; private double bucketSeconds,simulationSeconds,windowSeconds; private string? issue; public void Reset() {sources.Clear();synchronized=false;lastAt=0;bucketSeconds=simulationSeconds=windowSeconds=0;issue=null;} public bool ObserveInput(ulong sourceId,string label,double requestedEnergy,bool verifiedSco) { if(!double.IsFinite(requestedEnergy)){issue="non-finite-input";return false;} var key=(sourceId,verifiedSco); if(!sources.TryGetValue(key,out var source)) { if(sources.Count>=128){issue="source-capacity";return false;} source=new(sourceId,string.IsNullOrWhiteSpace(label)?"Unidentified source":label[..Math.Min(label.Length,128)],verifiedSco);sources.Add(key,source); } source.Pending+=requestedEnergy; if(!double.IsFinite(source.Pending)){issue="input-overflow";return false;} return true; } public bool ObserveTick(double simulationDt,ulong observedAtUnixMilliseconds) { if(!double.IsFinite(simulationDt)||simulationDt<=0||simulationDt>.25||observedAtUnixMilliseconds==0) {Reset();issue="invalid-simulation-step";return false;} if(!synchronized||observedAtUnixMilliseconds1000) { // The first partial interval has no known start boundary. Never divide it by a later callback's shorter dt. foreach(var source in sources.Values){source.Pending=source.Rate=0;source.History.Clear();} synchronized=true;lastAt=observedAtUnixMilliseconds;bucketSeconds=simulationSeconds=windowSeconds=0;issue=null;return false; } lastAt=observedAtUnixMilliseconds;bucketSeconds+=simulationDt;simulationSeconds+=simulationDt; if(bucketSeconds+1e-12<.1)return false; windowSeconds=bucketSeconds; foreach(var source in sources.Values) { source.Rate=source.Pending/bucketSeconds;source.Pending=0; if(!double.IsFinite(source.Rate)){issue="rate-overflow";return false;} source.History.Enqueue((simulationSeconds,Math.Max(0,source.Rate))); while(source.History.Count>0&&simulationSeconds-source.History.Peek().At>.2)source.History.Dequeue(); } bucketSeconds=0;return issue is null; } public ScoThermalSourceSnapshot Read(ulong now) { var rows=sources.Values.Select(source=> { var current=bucketSeconds>0?source.Pending/bucketSeconds:source.Rate; var upper=Math.Max(0,current); foreach(var point in source.History)if(simulationSeconds-point.At<=.2+1e-12)upper=Math.Max(upper,point.Rate); return new ScoThermalSourceRate(source.Id,source.Label,source.Sco,current,upper); }).OrderBy(source=>source.SourceId).ThenBy(source=>source.VerifiedSco).ToArray(); var ready=issue is null&&windowSeconds>=.1-1e-12&&lastAt!=0&&lastAt<=now&&now-lastAt<=200; return new(ready,lastAt,windowSeconds,rows.Sum(source=>source.RequestedEnergyPerSecond),rows.Where(source=>source.VerifiedSco).Sum(source=>source.UpperEnergyPerSecond), rows.Where(source=>!source.VerifiedSco).Sum(source=>source.UpperEnergyPerSecond),rows,issue??(ready?null:windowSeconds==0?"awaiting-complete-source-window":"stale-source-window")); } }