/// <summary> /// Checks if the thing is exhausted. /// </summary> /// <param name="thing">The thing to check the exhaustion condition on.</param> /// <param name="type">The type of exhaustion to check for.</param> /// <returns>True if the thing has such condition, false otherwise.</returns> public static bool IsExhausted(this IThing thing, ExhaustionFlag type) { thing.ThrowIfNull(nameof(thing)); type.ThrowIfNull(nameof(type)); return(thing.TrackedEvents.TryGetValue(ConditionType.Exhausted.ToString(), out IEvent conditionEvent) && conditionEvent is IExhaustionCondition exhaustionCondition && exhaustionCondition.ExhaustionTimesPerType.ContainsKey(type)); }
/// <summary> /// Initializes a new instance of the <see cref="ExhaustionCondition"/> class. /// </summary> /// <param name="exhaustionType">The type of exhaustion.</param> /// <param name="endTime">The date and time at which the condition is set to end.</param> public ExhaustionCondition(ExhaustionFlag exhaustionType, DateTimeOffset endTime) : base(ConditionType.Exhausted) { this.ExhaustionTimesPerType = new Dictionary <ExhaustionFlag, DateTimeOffset>() { { exhaustionType, endTime }, }; this.ExcludeFromTelemetry = true; }
/// <summary> /// Calculates the remaining <see cref="TimeSpan"/> until the thing's exhaustion is over. /// </summary> /// <param name="thing">The thing to check the conditions on.</param> /// <param name="exhaustionType">The type of condition.</param> /// <param name="currentTime">The current time to calculate from.</param> /// <returns>The <see cref="TimeSpan"/> result.</returns> public static TimeSpan RemainingExhaustionTime(this IThing thing, ExhaustionFlag exhaustionType, DateTimeOffset currentTime) { thing.ThrowIfNull(nameof(thing)); if (!thing.IsExhausted(exhaustionType)) { return(TimeSpan.Zero); } var exhaustionCondition = thing.TrackedEvents[ConditionType.Exhausted.ToString()] as IExhaustionCondition; if (!exhaustionCondition.ExhaustionTimesPerType.TryGetValue(exhaustionType, out DateTimeOffset exhaustionEndTime)) { return(TimeSpan.Zero); } var timeLeft = exhaustionEndTime - currentTime; return(timeLeft < TimeSpan.Zero ? TimeSpan.Zero : timeLeft); }