Skip to content

Commit

Permalink
Add support for resetting failures and runtimes (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav authored Feb 4, 2024
1 parent 633d2e0 commit 21b6be4
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
10 changes: 9 additions & 1 deletion TestFlightAPI/TestFlightAPI/TestFlightAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,13 @@ string Configuration
/// </summary>
/// <returns>The current burn time for scope or 0 if this module does not track burn time.</returns>
float GetScopedRunTime(RatingScope ratingScope);


/// <summary>
/// Sets the current burn time for the given scope.
/// Does nothing if this module does not track burn time.
/// </summary>
void SetScopedRunTime(RatingScope ratingScope, float time);

/// <summary>
/// Should return a string if the module wants to report any information to the user in the TestFlight Editor window.
/// </summary>
Expand Down Expand Up @@ -959,6 +965,8 @@ bool DebugEnabled

float GetRunTime(RatingScope ratingScope);

void ResetRunTime();

/// <summary>
/// Called whenever an Interop value is added, changed, or removed to allow the modules on the part to update to the proper config
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions TestFlightAPI/TestFlightAPI/TestFlightReliability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public virtual float GetScopedRunTime(RatingScope ratingScope)
{
return 0f;
}

public virtual void SetScopedRunTime(RatingScope ratingScope, float time)
{
}
}
}

14 changes: 13 additions & 1 deletion TestFlightCore/TestFlightCore/TestFlightCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ public float GetRunTime(RatingScope ratingScope)
return burnTime;
}

public void ResetRunTime()
{
List<ITestFlightReliability> reliabilityModules = TestFlightUtil.GetReliabilityModules(this.part, Alias);

foreach (ITestFlightReliability rm in reliabilityModules)
{
rm.SetScopedRunTime(RatingScope.Continuous, 0);
rm.SetScopedRunTime(RatingScope.Cumulative, 0);
}
}

// Get the momentary (IE current dynamic) failure rates (Can vary per reliability/failure modules)
// These methods will let you get a list of all momentary rates or you can get the best (lowest chance of failure)/worst (highest chance of failure) rates
public MomentaryFailureRate GetWorstMomentaryFailureRate()
Expand Down Expand Up @@ -1020,6 +1031,7 @@ public float ForceRepair(ITestFlightFailure failure)
return 0;

failure.ForceRepair();
failures.Remove(failure);
// update our major failure flag in case this repair changes things
hasMajorFailure = HasMajorFailure();

Expand All @@ -1033,7 +1045,7 @@ private bool HasMajorFailure()
return true;
return false;
}

private void PollExistingFailuresOnStart()
{
List<ITestFlightFailure> m = TestFlightUtil.GetFailureModules(this.part, Alias);
Expand Down
57 changes: 55 additions & 2 deletions TestFlightCore/TestFlightCore/TestFlightInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,61 @@ public static bool TestFlightAvailable(Part part)
else
return true;
}

public static void ResetAllFailuresOnVessel(Vessel vessel)
{
foreach (Part part in vessel.parts)
{
ITestFlightCore core = GetCore(part);
if (core == null) continue;

List<ITestFlightFailure> failures = core.GetActiveFailures();
for (int i = failures.Count - 1; i >= 0; i--)
{
core.ForceRepair(failures[i]);
}
}
}

public static void ResetAllRunTimesOnVessel(Vessel vessel)
{
foreach (Part part in vessel.parts)
{
ITestFlightCore core = GetCore(part);
if (core == null) continue;

core.ResetRunTime();
}
}

/// <summary>
/// 0 = OK, 1 = Minor Failure, 2 = Failure, 3 = Major Failure, -1 = Could not find TestFlight Core on Part
/// 0 = OK, 1 = Has failure, -1 = Could not find TestFlight Core on Part
/// </summary>
/// <returns>The part status.</returns>
public static int GetVesselStatus(Vessel vessel)
{
int retVal = -1;
foreach (Part part in vessel.parts)
{
int statusForPart;
ITestFlightCore core = GetCore(part);
if (core == null)
statusForPart = - 1;
else
statusForPart = core.GetPartStatus();
retVal = Math.Max(retVal, statusForPart);
}

return retVal;
}

/// <summary>
/// 0 = OK, 1 = Has failure, -1 = Could not find TestFlight Core on Part
/// </summary>
/// <returns>The part status.</returns>
public static int GetPartStatus(Part part, string alias)
{
ITestFlightCore core = TestFlightInterface.GetCore(part, alias);
ITestFlightCore core = GetCore(part, alias);
if (core == null)
return -1;

Expand Down Expand Up @@ -346,6 +394,11 @@ public static bool IsPartOperating(Part part, string alias)
}
// Methods for accessing the TestFlight modules on a given part

// Get the active Core Module - can only ever be one.
public static ITestFlightCore GetCore(Part part)
{
return TestFlightUtil.GetCore(part);
}
// Get the active Core Modules that are bound to a given alias
public static ITestFlightCore GetCore(Part part, string alias)
{
Expand Down
15 changes: 15 additions & 0 deletions TestFlightReliability_EngineCycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,21 @@ public override float GetScopedRunTime(RatingScope ratingScope)
}
}

public override void SetScopedRunTime(RatingScope ratingScope, float time)
{
switch (ratingScope)
{
case RatingScope.Cumulative:
engineOperatingTime = time;
break;
case RatingScope.Continuous:
currentRunTime = time;
break;
default:
break;
}
}

public override void OnAwake()
{
base.OnAwake();
Expand Down

0 comments on commit 21b6be4

Please sign in to comment.