using XFEEDToolBox.Core.Data;
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Session;
public enum ShipProtectionReason { None,ShieldsLost,HullBelowHalf,Interdicted }
public sealed record ShipProtectionTrigger(ShipProtectionReason Reason,double? HullHealth,DateTimeOffset At)
{
public string Message=>Reason switch{ShipProtectionReason.Interdicted=>"检测到正在被拦截",ShipProtectionReason.HullBelowHalf=>$"受损后船体完整度低于 50%({HullHealth:P1})",_=>"受损后护盾失效"};
}
public sealed record ShipHealthSample(long Sequence,DateTimeOffset At,int ProcessId,long ProcessStartedUtcTicks,ulong EntityId,long ShipId,
double HullHealth,double? ShieldHealth,bool DamageObserved);
/// <summary>Damage-edge protection. Initial/replayed low readings are baselines, never emergency triggers.</summary>
public sealed class ShieldRestartGuard
{
private string identity="";
private bool armed;
private DateTimeOffset enabledAt,lastEvent;
private long sequence;
private long nativeSequence;
private ulong entity;
private double? hull,shield;
private string healthIdentity="";
private DateTimeOffset healthAt;
private bool consumed;
private bool damageEmergency;
private ulong stableSince,lastHealthMilliseconds;
public bool Enabled{get;private set;}
public bool Armed=>Enabled&&armed;
public bool TestingBlocked{get;private set;}
public ShipProtectionTrigger? LastTrigger{get;private set;}
public void SetEnabled(bool value,DateTimeOffset now){Enabled=value;enabledAt=now;identity="";armed=false;sequence=nativeSequence=0;lastEvent=default;entity=0;shield=null;consumed=false;LastTrigger=null;ResetDamage();}
private void ResetDamage(){damageEmergency=false;stableSince=lastHealthMilliseconds=0;TestingBlocked=false;}
private void ObserveDamage(bool danger,DateTimeOffset at)
{
var now=checked((ulong)at.ToUnixTimeMilliseconds());var previous=damageEmergency;
var recovery=Navigation.ShipDamageRecoveryPolicy.Observe(damageEmergency,danger,stableSince,lastHealthMilliseconds,now);
damageEmergency=recovery.Emergency;stableSince=recovery.StableSince;lastHealthMilliseconds=now;
TestingBlocked=damageEmergency||hull is <=0;
if(previous&&!damageEmergency)consumed=false;
}
private static string Identity(PlayerState p,GameSessionTarget target)=>$"{target.ProcessId}:{target.StartedUtcTicks}:{p.CommanderId}:{p.CurrentShipId}:{p.LoadGameAt:O}";
private static bool Eligible(PlayerState p,GameSessionTarget target,bool allowDocked=false)
{
if(!p.InGame||p.Docked&&!allowDocked||p.CommanderId.Length==0||p.CurrentShipId is null||p.Status is not {} status||p.LoadGameAt is not {} load||p.StatusAt is not {} statusAt||statusAt<load)return false;
if(load.UtcTicks<target.StartedUtcTicks-TimeSpan.FromSeconds(5).Ticks)return false;
if((status.Flags&(1L<<24))==0||(status.Flags&((allowDocked?0:1L<<0)|(1L<<1)|(1L<<25)|(1L<<26)))!=0||(status.Flags2&7)!=0)return false;
return p.Ships.Any(s=>s.ShipId==p.CurrentShipId&&!s.ConfigurationChanged&&s.ConfigurationAt is not null);
}
private static bool ShieldFitted(PlayerState p)=>p.Ships.FirstOrDefault(s=>s.ShipId==p.CurrentShipId)?.Modules.Any(m=>m.Enabled&&m.Item.Contains("shieldgenerator",StringComparison.OrdinalIgnoreCase))==true;
private void HullBaseline(PlayerState player)
{
var key=$"{player.CommanderId}:{player.CurrentShipId}";
if(key!=healthIdentity){healthIdentity=key;hull=null;healthAt=default;ResetDamage();}
var ship=player.Ships.FirstOrDefault(s=>s.ShipId==player.CurrentShipId);
if(ship?.HullHealth is {} value&&double.IsFinite(value)&&value is >=0 and <=1&&(ship.HullHealthAt??ship.ConfigurationAt) is {} at&&at>=healthAt)
{hull=value;healthAt=at;TestingBlocked=damageEmergency||value<=0;}
}
public void Prime(PlayerState player,GameSessionTarget target)
{
var nextIdentity=Identity(player,target);if(identity!=nextIdentity)ResetDamage();identity=nextIdentity;
HullBaseline(player);
armed=Enabled&&Eligible(player,target)&&ShieldFitted(player)&&(player.Status!.Flags&8)!=0;
}
public bool Observe(ObservedJournalEvent observed,PlayerState player,GameSessionTarget target)
{
if(!Enabled)return false;
var data=observed.Data;var name=data.Text("event");
if(name is "Shutdown" or "Died" or "LoadGame" or "Docked" or "ShipyardSwap" or "Disembark" or "LaunchFighter" or "LaunchSRV"){armed=false;identity="";shield=null;entity=0;ResetDamage();return false;}
if(name is not("ShieldState" or "Status" or "HullDamage" or "Loadout"))return false;
var timestamp=data.Time();
if(observed.Sequence<=sequence||timestamp is null||timestamp<enabledAt.AddSeconds(-1)||timestamp<lastEvent||observed.ObservedAt-timestamp>TimeSpan.FromSeconds(5)||timestamp-observed.ObservedAt>TimeSpan.FromSeconds(2))return false;
sequence=observed.Sequence;lastEvent=timestamp.Value;
var current=Identity(player,target);
if(identity!=current){identity=current;armed=false;shield=null;entity=0;consumed=false;ResetDamage();HullBaseline(player);return false;}
if(!Eligible(player,target)){armed=false;return false;}
if(name=="Loadout"){HullBaseline(player);return false;}
if(name=="HullDamage")
{
if(!data.Flag("PlayerPilot")||data.Flag("Fighter")||data.Number("Health") is not {} value||!double.IsFinite(value)||value is <0 or >1)return false;
if(timestamp<healthAt)return false;
var decreased=hull is null||value<hull-.000001;
hull=value;healthAt=timestamp.Value;ObserveDamage(decreased,timestamp.Value);
return decreased&&value<.5&&Trigger(ShipProtectionReason.HullBelowHalf,timestamp.Value);
}
if(!ShieldFitted(player)){armed=false;return false;}
bool up;
if(name=="ShieldState")
{
if(data.Get("ShieldsUp").ValueKind is not(System.Text.Json.JsonValueKind.True or System.Text.Json.JsonValueKind.False))return false;
up=data.Flag("ShieldsUp");
}
else {if(data.Get("Flags").ValueKind!=System.Text.Json.JsonValueKind.Number)return false;up=(data.Long("Flags")&8)!=0;}
if(up){armed=true;consumed=false;return false;}
if(!armed)return false;
armed=false;
return Trigger(TestingBlocked?ShipProtectionReason.HullBelowHalf:ShipProtectionReason.ShieldsLost,timestamp.Value);
}
private bool Trigger(ShipProtectionReason reason,DateTimeOffset at)
{
ObserveDamage(true,at);
if(consumed)return false;
consumed=true;armed=false;LastTrigger=new(reason,hull,at);return true;
}
public bool ObserveHealth(ShipHealthSample sample,PlayerState player,GameSessionTarget target,DateTimeOffset now)
{
if(!Enabled||!Eligible(player,target,allowDocked:true)||sample.ProcessId!=target.ProcessId||sample.ProcessStartedUtcTicks!=target.StartedUtcTicks||sample.ShipId!=player.CurrentShipId
||sample.EntityId==0||sample.Sequence<=nativeSequence||sample.At<enabledAt||sample.At>now||now-sample.At>TimeSpan.FromMilliseconds(500)
||!double.IsFinite(sample.HullHealth)||sample.HullHealth is <0 or >1||sample.ShieldHealth is {} bad&&(!double.IsFinite(bad)||bad is <0 or >1))return false;
var current=Identity(player,target);
if(identity!=current||entity!=sample.EntityId)
{identity=current;entity=sample.EntityId;nativeSequence=sample.Sequence;hull=sample.HullHealth;shield=sample.ShieldHealth;healthAt=sample.At;ResetDamage();ObserveDamage(sample.DamageObserved,sample.At);consumed=false;return false;}
nativeSequence=sample.Sequence;
var hullLost=hull is {} oldHull&&sample.HullHealth<oldHull-.000001;
var shieldLost=shield is {} oldShield&&sample.ShieldHealth is {} newShield&&newShield<oldShield-.000001;
hull=sample.HullHealth;shield=sample.ShieldHealth;healthAt=sample.At;
ObserveDamage(hullLost||sample.DamageObserved||shieldLost&&shield is <=0,sample.At);
if(!hullLost&&!shieldLost&&!sample.DamageObserved)return false;
if(hull<.5)return Trigger(ShipProtectionReason.HullBelowHalf,sample.At);
if(shield is <=0&&ShieldFitted(player))return Trigger(ShipProtectionReason.ShieldsLost,sample.At);
return false;
}
}
using XFEEDToolBox.Core.Data;
using XFEEDToolBox.Core.Models;
namespace XFEEDToolBox.Core.Session;
public enum ShipProtectionReason { None,ShieldsLost,HullBelowHalf,Interdicted }
public sealed record ShipProtectionTrigger(ShipProtectionReason Reason,double? HullHealth,DateTimeOffset At)
{
public string Message=>Reason switch{ShipProtectionReason.Interdicted=>"检测到正在被拦截",ShipProtectionReason.HullBelowHalf=>$"受损后船体完整度低于 50%({HullHealth:P1})",_=>"受损后护盾失效"};
}
public sealed record ShipHealthSample(long Sequence,DateTimeOffset At,int ProcessId,long ProcessStartedUtcTicks,ulong EntityId,long ShipId,
double HullHealth,double? ShieldHealth,bool DamageObserved);
/// <summary>Damage-edge protection. Initial/replayed low readings are baselines, never emergency triggers.</summary>
public sealed class ShieldRestartGuard
{
private string identity="";
private bool armed;
private DateTimeOffset enabledAt,lastEvent;
private long sequence;
private long nativeSequence;
private ulong entity;
private double? hull,shield;
private string healthIdentity="";
private DateTimeOffset healthAt;
private bool consumed;
private bool damageEmergency;
private ulong stableSince,lastHealthMilliseconds;
public bool Enabled{get;private set;}
public bool Armed=>Enabled&&armed;
public bool TestingBlocked{get;private set;}
public ShipProtectionTrigger? LastTrigger{get;private set;}
public void SetEnabled(bool value,DateTimeOffset now){Enabled=value;enabledAt=now;identity="";armed=false;sequence=nativeSequence=0;lastEvent=default;entity=0;shield=null;consumed=false;LastTrigger=null;ResetDamage();}
private void ResetDamage(){damageEmergency=false;stableSince=lastHealthMilliseconds=0;TestingBlocked=false;}
private void ObserveDamage(bool danger,DateTimeOffset at)
{
var now=checked((ulong)at.ToUnixTimeMilliseconds());var previous=damageEmergency;
var recovery=Navigation.ShipDamageRecoveryPolicy.Observe(damageEmergency,danger,stableSince,lastHealthMilliseconds,now);
damageEmergency=recovery.Emergency;stableSince=recovery.StableSince;lastHealthMilliseconds=now;
TestingBlocked=damageEmergency||hull is <=0;
if(previous&&!damageEmergency)consumed=false;
}
private static string Identity(PlayerState p,GameSessionTarget target)=>$"{target.ProcessId}:{target.StartedUtcTicks}:{p.CommanderId}:{p.CurrentShipId}:{p.LoadGameAt:O}";
private static bool Eligible(PlayerState p,GameSessionTarget target,bool allowDocked=false)
{
if(!p.InGame||p.Docked&&!allowDocked||p.CommanderId.Length==0||p.CurrentShipId is null||p.Status is not {} status||p.LoadGameAt is not {} load||p.StatusAt is not {} statusAt||statusAt<load)return false;
if(load.UtcTicks<target.StartedUtcTicks-TimeSpan.FromSeconds(5).Ticks)return false;
if((status.Flags&(1L<<24))==0||(status.Flags&((allowDocked?0:1L<<0)|(1L<<1)|(1L<<25)|(1L<<26)))!=0||(status.Flags2&7)!=0)return false;
return p.Ships.Any(s=>s.ShipId==p.CurrentShipId&&!s.ConfigurationChanged&&s.ConfigurationAt is not null);
}
private static bool ShieldFitted(PlayerState p)=>p.Ships.FirstOrDefault(s=>s.ShipId==p.CurrentShipId)?.Modules.Any(m=>m.Enabled&&m.Item.Contains("shieldgenerator",StringComparison.OrdinalIgnoreCase))==true;
private void HullBaseline(PlayerState player)
{
var key=$"{player.CommanderId}:{player.CurrentShipId}";
if(key!=healthIdentity){healthIdentity=key;hull=null;healthAt=default;ResetDamage();}
var ship=player.Ships.FirstOrDefault(s=>s.ShipId==player.CurrentShipId);
if(ship?.HullHealth is {} value&&double.IsFinite(value)&&value is >=0 and <=1&&(ship.HullHealthAt??ship.ConfigurationAt) is {} at&&at>=healthAt)
{hull=value;healthAt=at;TestingBlocked=damageEmergency||value<=0;}
}
public void Prime(PlayerState player,GameSessionTarget target)
{
var nextIdentity=Identity(player,target);if(identity!=nextIdentity)ResetDamage();identity=nextIdentity;
HullBaseline(player);
armed=Enabled&&Eligible(player,target)&&ShieldFitted(player)&&(player.Status!.Flags&8)!=0;
}
public bool Observe(ObservedJournalEvent observed,PlayerState player,GameSessionTarget target)
{
if(!Enabled)return false;
var data=observed.Data;var name=data.Text("event");
if(name is "Shutdown" or "Died" or "LoadGame" or "Docked" or "ShipyardSwap" or "Disembark" or "LaunchFighter" or "LaunchSRV"){armed=false;identity="";shield=null;entity=0;ResetDamage();return false;}
if(name is not("ShieldState" or "Status" or "HullDamage" or "Loadout"))return false;
var timestamp=data.Time();
if(observed.Sequence<=sequence||timestamp is null||timestamp<enabledAt.AddSeconds(-1)||timestamp<lastEvent||observed.ObservedAt-timestamp>TimeSpan.FromSeconds(5)||timestamp-observed.ObservedAt>TimeSpan.FromSeconds(2))return false;
sequence=observed.Sequence;lastEvent=timestamp.Value;
var current=Identity(player,target);
if(identity!=current){identity=current;armed=false;shield=null;entity=0;consumed=false;ResetDamage();HullBaseline(player);return false;}
if(!Eligible(player,target)){armed=false;return false;}
if(name=="Loadout"){HullBaseline(player);return false;}
if(name=="HullDamage")
{
if(!data.Flag("PlayerPilot")||data.Flag("Fighter")||data.Number("Health") is not {} value||!double.IsFinite(value)||value is <0 or >1)return false;
if(timestamp<healthAt)return false;
var decreased=hull is null||value<hull-.000001;
hull=value;healthAt=timestamp.Value;ObserveDamage(decreased,timestamp.Value);
return decreased&&value<.5&&Trigger(ShipProtectionReason.HullBelowHalf,timestamp.Value);
}
if(!ShieldFitted(player)){armed=false;return false;}
bool up;
if(name=="ShieldState")
{
if(data.Get("ShieldsUp").ValueKind is not(System.Text.Json.JsonValueKind.True or System.Text.Json.JsonValueKind.False))return false;
up=data.Flag("ShieldsUp");
}
else {if(data.Get("Flags").ValueKind!=System.Text.Json.JsonValueKind.Number)return false;up=(data.Long("Flags")&8)!=0;}
if(up){armed=true;consumed=false;return false;}
if(!armed)return false;
armed=false;
return Trigger(TestingBlocked?ShipProtectionReason.HullBelowHalf:ShipProtectionReason.ShieldsLost,timestamp.Value);
}
private bool Trigger(ShipProtectionReason reason,DateTimeOffset at)
{
ObserveDamage(true,at);
if(consumed)return false;
consumed=true;armed=false;LastTrigger=new(reason,hull,at);return true;
}
public bool ObserveHealth(ShipHealthSample sample,PlayerState player,GameSessionTarget target,DateTimeOffset now)
{
if(!Enabled||!Eligible(player,target,allowDocked:true)||sample.ProcessId!=target.ProcessId||sample.ProcessStartedUtcTicks!=target.StartedUtcTicks||sample.ShipId!=player.CurrentShipId
||sample.EntityId==0||sample.Sequence<=nativeSequence||sample.At<enabledAt||sample.At>now||now-sample.At>TimeSpan.FromMilliseconds(500)
||!double.IsFinite(sample.HullHealth)||sample.HullHealth is <0 or >1||sample.ShieldHealth is {} bad&&(!double.IsFinite(bad)||bad is <0 or >1))return false;
var current=Identity(player,target);
if(identity!=current||entity!=sample.EntityId)
{identity=current;entity=sample.EntityId;nativeSequence=sample.Sequence;hull=sample.HullHealth;shield=sample.ShieldHealth;healthAt=sample.At;ResetDamage();ObserveDamage(sample.DamageObserved,sample.At);consumed=false;return false;}
nativeSequence=sample.Sequence;
var hullLost=hull is {} oldHull&&sample.HullHealth<oldHull-.000001;
var shieldLost=shield is {} oldShield&&sample.ShieldHealth is {} newShield&&newShield<oldShield-.000001;
hull=sample.HullHealth;shield=sample.ShieldHealth;healthAt=sample.At;
ObserveDamage(hullLost||sample.DamageObserved||shieldLost&&shield is <=0,sample.At);
if(!hullLost&&!shieldLost&&!sample.DamageObserved)return false;
if(hull<.5)return Trigger(ShipProtectionReason.HullBelowHalf,sample.At);
if(shield is <=0&&ShieldFitted(player))return Trigger(ShipProtectionReason.ShieldsLost,sample.At);
return false;
}
}