winmr-api/Utils/Delay.cs
2026-01-08 21:35:06 +03:00

172 lines
4.5 KiB
C#

using System.Collections;
using WebmrAPI.Utils.Sequences;
namespace WebmrAPI.Utils
{
public class Delay : BaseRandom, IComparable<Delay>
{
private int _min = 0;
private int _max = 0;
private bool _mutable = true;
public readonly static Delay None = new Delay { _mutable = false };
public readonly static Delay Random = new Delay { _mutable = false, Min = 50, Max = 250 };
public int Value
{
set => _max = _min = value;
get => (_max > _min) ? Generate(_min, _max) : _min;
}
public int Min
{
get => _min > _max ? _max : _min;
set
{
if (!_mutable)
{
throw new InvalidOperationException("Object is not mutable");
}
_min = value;
}
}
public int Max
{
get => _max < _min ? _min : _max;
set
{
if (!_mutable)
{
throw new InvalidOperationException("Object is not mutable");
}
_max = value;
}
}
async public Task WaitAsync()
{
await Task.Delay(Value);
}
public void Wait()
{
Thread.Sleep(Value);
}
public static bool operator ==(Delay? left, Delay? right)
{
if (left == null && right == null) return true;
if (left == null || right == null) return false;
return left._min == right._min && left._max == right._max;
}
public static bool operator !=(Delay? left, Delay? right)
{
return !(left == right);
}
public override bool Equals(object? obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
Delay other = (Delay)obj;
return this == other;
}
public override int GetHashCode()
{
return HashCode.Combine(_min, _max);
}
public static bool operator <(Delay left, Delay right)
{
if (left._min < right._min)
{
return true;
}
if (left._min == right._min && left._max < right._max)
{
return true;
}
return false;
}
public static bool operator >(Delay left, Delay right)
{
if (left._min > right._min)
{
return true;
}
if (left._min == right._min && left._max > right._max)
{
return true;
}
return false;
}
public static bool operator <=(Delay left, Delay right)
{
return left < right || left == right;
}
public static bool operator >=(Delay left, Delay right)
{
return left > right || left == right;
}
public int CompareTo(Delay? other)
{
if (other == null)
{
return 1;
}
int result = _min.CompareTo(other._min);
if (result != 0)
{
return result;
}
return _max.CompareTo(other._max);
}
}
public class DelayGenerator : ISequenceGenerator<Delay>
{
private ISequenceGenerator<int> _generator;
public Delay Min { get => new Delay { Value = _generator.Min }; }
public Delay Max { get => new Delay { Value = _generator.Max }; }
public double Progress { get => _generator.Progress; }
public double Step { get => _generator.Step; }
public bool HasValues { get => _generator.HasValues; }
public Delay Next()
{
if (!HasValues)
{
Reset();
}
return new Delay { Value = _generator.Next() };
}
public ISequenceGenerator<Delay> Reset()
{
_generator.Reset();
return this;
}
public DelayGenerator(ISequenceGenerator<int> generator)
{
_generator = generator;
}
public IEnumerator<Delay> GetEnumerator()
{
while (HasValues)
{
yield return Next();
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}