0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 05:30:43 -05:00

Improve Robustness of RaidChangesUtil and RaidTimeUtil (!45)

While continuing mod development with SPT 3.7.4 (namely for Questing Bots and SAIN), I found that the API I added to SPT 3.7.4 is unintuitive and clunky to use after the raid is done loading but before the countdown timer expires. With the current SPT code, calling `GetRemainingRaidSeconds()` or `GetRaidTimeRemainingFraction()` will result in an exception during this time unless you check `HasRaidStarted()` first.

These changes will prevent those methods from throwing an exception unless you try calling them before a raid has finished loading (which should rarely happen). This allows mods to more easily do things using `GetRemainingRaidSeconds()` or `GetRaidTimeRemainingFraction()` before the countdown timer expires (i.e. cache bots).

These changes only improve the robustness of the existing code and will not break any mods that use the existing API.

Co-authored-by: dwesterwick <dwesterwick@yahoo.com>
Co-authored-by: chomp <chomp@noreply.dev.sp-tarkov.com>
Reviewed-on: SPT-AKI/Modules#45
Co-authored-by: DanW <danw@noreply.dev.sp-tarkov.com>
Co-committed-by: DanW <danw@noreply.dev.sp-tarkov.com>
This commit is contained in:
DanW 2023-12-21 22:31:33 +00:00 committed by Dev
parent 612e7f0b1b
commit bf07137cf7
2 changed files with 35 additions and 10 deletions

View File

@ -59,10 +59,25 @@ namespace Aki.SinglePlayer.Utils.InRaid
/// </summary> /// </summary>
public static int RaidTimeReductionSeconds => RaidTimeReductionMinutes * 60; public static int RaidTimeReductionSeconds => RaidTimeReductionMinutes * 60;
/// <summary>
/// The fraction of raid time that will be remaining when you spawn into the map
/// </summary>
public static float RaidTimeRemainingFraction => (float)NewEscapeTimeMinutes / OriginalEscapeTimeMinutes;
/// <summary>
/// The original minimum time (in seconds) you must stay in the raid to get a "Survived" status (unless your XP is high enough) for the current (or most recent) raid
/// </summary>
public static int OriginalSurvivalTimeSeconds { get; private set; } = int.MaxValue;
/// <summary>
/// The updated minimum time (in seconds) you must stay in the raid to get a "Survived" status (unless your XP is high enough) for the current (or most recent) raid
/// </summary>
public static int NewSurvivalTimeSeconds { get; private set; } = int.MaxValue;
/// <summary> /// <summary>
/// The reduction in the minimum time you must stay in the raid to get a "Survived" status (unless your XP is high enough) for the current (or most recent) raid /// The reduction in the minimum time you must stay in the raid to get a "Survived" status (unless your XP is high enough) for the current (or most recent) raid
/// </summary> /// </summary>
public static int SurvivalTimeReductionSeconds { get; private set; } = 0; public static int SurvivalTimeReductionSeconds => OriginalSurvivalTimeSeconds - NewSurvivalTimeSeconds;
/// <summary> /// <summary>
/// Update the changes that will be made for the raid. This should be called just before applying changes. /// Update the changes that will be made for the raid. This should be called just before applying changes.
@ -81,11 +96,8 @@ namespace Aki.SinglePlayer.Utils.InRaid
OriginalEscapeTimeMinutes = raidSettings.SelectedLocation.EscapeTimeLimit; OriginalEscapeTimeMinutes = raidSettings.SelectedLocation.EscapeTimeLimit;
NewEscapeTimeMinutes = raidChanges.RaidTimeMinutes; NewEscapeTimeMinutes = raidChanges.RaidTimeMinutes;
SurvivalTimeReductionSeconds = 0; OriginalSurvivalTimeSeconds = raidChanges.OriginalSurvivalTimeSeconds;
if (raidChanges.NewSurviveTimeSeconds.HasValue) NewSurvivalTimeSeconds = raidChanges.NewSurviveTimeSeconds ?? OriginalSurvivalTimeSeconds;
{
SurvivalTimeReductionSeconds += raidChanges.OriginalSurvivalTimeSeconds - raidChanges.NewSurviveTimeSeconds.Value;
}
} }
/// <summary> /// <summary>

View File

@ -9,6 +9,14 @@ namespace Aki.SinglePlayer.Utils.InRaid
/// </summary> /// </summary>
public static class RaidTimeUtil public static class RaidTimeUtil
{ {
/// <summary>
/// Determines if a raid has loaded by checking if a GameTimer instance has been created for one
/// </summary>
public static bool HasRaidLoaded()
{
return Singleton<AbstractGame>.Instance?.GameTimer != null;
}
/// <summary> /// <summary>
/// Determines if a raid is in-progress by checking if the GameTimer has started /// Determines if a raid is in-progress by checking if the GameTimer has started
/// </summary> /// </summary>
@ -19,17 +27,22 @@ namespace Aki.SinglePlayer.Utils.InRaid
/// <summary> /// <summary>
/// <para>Calculates the seconds remaining in the current raid.</para> /// <para>Calculates the seconds remaining in the current raid.</para>
/// <para>Please ensure <see cref="HasRaidStarted"/> is <c>true</c>, or this will throw an exception.</para> /// <para>Please ensure <see cref="HasRaidLoaded"/> is <c>true</c>, or this will throw an exception.</para>
/// </summary> /// </summary>
/// <returns>Seconds remaining in the raid</returns> /// <returns>Seconds remaining in the raid</returns>
/// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception> /// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception>
public static float GetRemainingRaidSeconds() public static float GetRemainingRaidSeconds()
{ {
if (!HasRaidStarted()) if (!HasRaidLoaded())
{ {
throw new InvalidOperationException("The raid-time remaining can only be calculated when a raid is in-progress"); throw new InvalidOperationException("The raid-time remaining can only be calculated when a raid is in-progress");
} }
if (!HasRaidStarted())
{
return RaidChangesUtil.NewEscapeTimeSeconds;
}
float remainingTimeSeconds = Singleton<AbstractGame>.Instance.GameTimer.EscapeTimeSeconds(); float remainingTimeSeconds = Singleton<AbstractGame>.Instance.GameTimer.EscapeTimeSeconds();
// Until the raid starts, remainingTimeSeconds is TimeSpan.MaxValue, so it needs to be reduced to the actual starting raid time // Until the raid starts, remainingTimeSeconds is TimeSpan.MaxValue, so it needs to be reduced to the actual starting raid time
@ -39,7 +52,7 @@ namespace Aki.SinglePlayer.Utils.InRaid
/// <summary> /// <summary>
/// <para>Calculates the fraction of raid-time remaining relative to the original escape time for the map. /// <para>Calculates the fraction of raid-time remaining relative to the original escape time for the map.
/// 1.0 = the raid just started, and 0.0 = the raid is over (and you're MIA).</para> /// 1.0 = the raid just started, and 0.0 = the raid is over (and you're MIA).</para>
/// <para>Please ensure <see cref="HasRaidStarted"/> is <c>true</c>, or this will throw an exception.</para> /// <para>Please ensure <see cref="HasRaidLoaded"/> is <c>true</c>, or this will throw an exception.</para>
/// </summary> /// </summary>
/// <returns>The fraction of raid-time remaining (0.0 - 1.0) relative to the original escape time for the map</returns> /// <returns>The fraction of raid-time remaining (0.0 - 1.0) relative to the original escape time for the map</returns>
/// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception> /// <exception cref="InvalidOperationException">Thrown if there is no raid in progress</exception>