EventMap: Switch internal timer over to TimeSpan type

Port From (https://github.com/TrinityCore/TrinityCore/commit/7847589d9ca35de80dce68ac5cf5cf42c77b39a4)
This commit is contained in:
hondacrx
2022-06-18 17:48:30 -04:00
parent 1bb3a79b54
commit 82c2d2fb2a
+19 -10
View File
@@ -29,7 +29,7 @@ namespace Framework.Dynamic
public void Reset() public void Reset()
{ {
_eventMap.Clear(); _eventMap.Clear();
_time = 0; _time = TimeSpan.Zero;
_phase = 0; _phase = 0;
} }
@@ -38,6 +38,15 @@ namespace Framework.Dynamic
/// </summary> /// </summary>
/// <param name="time">Value in ms to be added to time.</param> /// <param name="time">Value in ms to be added to time.</param>
public void Update(uint time) public void Update(uint time)
{
Update(TimeSpan.FromMilliseconds(time));
}
/// <summary>
/// Updates the timer of the event map.
/// </summary>
/// <param name="time">Value in ms to be added to time.</param>
void Update(TimeSpan time)
{ {
_time += time; _time += time;
} }
@@ -107,7 +116,7 @@ namespace Framework.Dynamic
if (phase != 0 && phase <= 8) if (phase != 0 && phase <= 8)
eventId |= (uint)(1 << (phase + 23)); eventId |= (uint)(1 << (phase + 23));
_eventMap.Add(_time + (uint)time.TotalMilliseconds, eventId); _eventMap.Add(_time + time, eventId);
} }
/// <summary> /// <summary>
@@ -155,7 +164,7 @@ namespace Framework.Dynamic
/// <param name="time">Time until the event occurs as TimeSpan.</param> /// <param name="time">Time until the event occurs as TimeSpan.</param>
public void Repeat(TimeSpan time) public void Repeat(TimeSpan time)
{ {
_eventMap.Add(_time + (uint)time.TotalMilliseconds, _lastEvent); _eventMap.Add(_time + time, _lastEvent);
} }
/// <summary> /// <summary>
@@ -211,11 +220,11 @@ namespace Framework.Dynamic
if (Empty()) if (Empty())
return; return;
MultiMap<uint, uint> delayed = new(); MultiMap<TimeSpan, uint> delayed = new();
foreach (var pair in _eventMap.KeyValueList) foreach (var pair in _eventMap.KeyValueList)
{ {
delayed.Add(pair.Key + (uint)delay.TotalMilliseconds, pair.Value); delayed.Add(pair.Key + delay, pair.Value);
_eventMap.Remove(pair.Key, pair.Value); _eventMap.Remove(pair.Key, pair.Value);
} }
@@ -233,13 +242,13 @@ namespace Framework.Dynamic
if (group == 0 || group > 8 || Empty()) if (group == 0 || group > 8 || Empty())
return; return;
MultiMap<uint, uint> delayed = new(); MultiMap<TimeSpan, uint> delayed = new();
foreach (var pair in _eventMap.KeyValueList) foreach (var pair in _eventMap.KeyValueList)
{ {
if (Convert.ToBoolean(pair.Value & (1 << (int)(group + 15)))) if (Convert.ToBoolean(pair.Value & (1 << (int)(group + 15))))
{ {
delayed.Add(pair.Key + (uint)delay.TotalMilliseconds, pair.Value); delayed.Add(pair.Key + delay, pair.Value);
_eventMap.Remove(pair.Key, pair.Value); _eventMap.Remove(pair.Key, pair.Value);
} }
} }
@@ -289,7 +298,7 @@ namespace Framework.Dynamic
{ {
foreach (var pair in _eventMap) foreach (var pair in _eventMap)
if (eventId == (pair.Value & 0x0000FFFF)) if (eventId == (pair.Value & 0x0000FFFF))
return TimeSpan.FromMilliseconds(pair.Key - _time); return pair.Key - _time;
return TimeSpan.MaxValue; return TimeSpan.MaxValue;
} }
@@ -311,7 +320,7 @@ namespace Framework.Dynamic
/// it can be resetted and so on. Events occur when this timer /// it can be resetted and so on. Events occur when this timer
/// has reached their time value. Its value is changed in the Update method. /// has reached their time value. Its value is changed in the Update method.
/// </summary> /// </summary>
uint _time; TimeSpan _time;
/// <summary> /// <summary>
/// Phase mask of the event map. /// Phase mask of the event map.
@@ -336,6 +345,6 @@ namespace Framework.Dynamic
/// - Bit 24 - 31: Phase /// - Bit 24 - 31: Phase
/// - Pattern: 0xPPGGEEEE /// - Pattern: 0xPPGGEEEE
/// </summary> /// </summary>
SortedMultiMap<uint, uint> _eventMap = new(); SortedMultiMap<TimeSpan, uint> _eventMap = new();
} }
} }