예제 #1
0
 /// <summary>
 /// Adds exhaustion of the given type.
 /// </summary>
 /// <param name="type">The type of exhaustion to add.</param>
 /// <param name="fromTime">The reference time from which to add.</param>
 /// <param name="timeSpan">The amount of time to add exhaustion for.</param>
 public void AddExhaustion(ExhaustionType type, DateTimeOffset fromTime, TimeSpan timeSpan)
 {
     lock (this.exhaustionLock)
     {
         this.ExhaustionInformation[type] = fromTime + timeSpan;
     }
 }
예제 #2
0
        /// <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, ExhaustionType 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));
        }
예제 #3
0
        /// <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(ExhaustionType exhaustionType, DateTimeOffset endTime)
            : base(ConditionType.Exhausted)
        {
            this.ExhaustionTimesPerType = new Dictionary <ExhaustionType, DateTimeOffset>()
            {
                { exhaustionType, endTime },
            };

            this.ExcludeFromTelemetry = true;
        }
예제 #4
0
        /// <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, ExhaustionType 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);
        }
예제 #5
0
        /// <summary>
        /// Calculates the remaining <see cref="TimeSpan"/> until the entity's exhaustion is recovered from.
        /// </summary>
        /// <param name="type">The type of exhaustion.</param>
        /// <param name="currentTime">The current time to calculate from.</param>
        /// <returns>The <see cref="TimeSpan"/> result.</returns>
        public TimeSpan CalculateRemainingCooldownTime(ExhaustionType type, DateTimeOffset currentTime)
        {
            lock (this.exhaustionLock)
            {
                if (!this.ExhaustionInformation.TryGetValue(type, out DateTimeOffset readyAtTime))
                {
                    return(TimeSpan.Zero);
                }

                var timeLeft = readyAtTime - currentTime;

                if (timeLeft < TimeSpan.Zero)
                {
                    this.ExhaustionInformation.Remove(type);
                    return(TimeSpan.Zero);
                }

                return(timeLeft);
            }
        }
예제 #6
0
 /// <summary>
 /// Adds exhaustion of the given type.
 /// </summary>
 /// <param name="type">The type of exhaustion to add.</param>
 /// <param name="fromTime">The reference time from which to add.</param>
 /// <param name="milliseconds">The amount of time in milliseconds to add exhaustion for.</param>
 public void AddExhaustion(ExhaustionType type, DateTimeOffset fromTime, uint milliseconds)
 {
     this.AddExhaustion(type, fromTime, TimeSpan.FromMilliseconds(milliseconds));
 }