Skip to content

Commit

Permalink
Merge pull request #196 from Xian55/refactor/valuetuple
Browse files Browse the repository at this point in the history
Core: replace Tuple with `ValueTuple`
  • Loading branch information
Xian55 authored Dec 22, 2021
2 parents 805615d + b6c22a2 commit 948de42
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
11 changes: 6 additions & 5 deletions Core/Actionbar/ActionBarCooldownReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ActionBarCooldownReader
private readonly float MAX_ACTION_IDX = 100000f;
private readonly float MAX_VALUE_MUL = 100f;

private readonly Dictionary<int, Tuple<int, DateTime>> dict = new Dictionary<int, Tuple<int, DateTime>>();
private readonly Dictionary<int, (int duration, DateTime startTime)> dict = new Dictionary<int, (int, DateTime)>();

public ActionBarCooldownReader(ISquareReader reader, int cActionbarNum)
{
Expand All @@ -31,12 +31,12 @@ public void Read()

newCooldown /= MAX_VALUE_MUL;

if (dict.TryGetValue(index, out var tuple) && tuple.Item1 != (int)newCooldown)
if (dict.TryGetValue(index, out var tuple) && tuple.duration != (int)newCooldown)
{
dict.Remove(index);
}

dict.TryAdd(index, Tuple.Create((int)newCooldown, DateTime.Now));
dict.TryAdd(index, ((int)newCooldown, DateTime.Now));
}

public void Reset()
Expand All @@ -55,8 +55,9 @@ public int GetRemainingCooldown(PlayerReader playerReader, KeyAction keyAction)

if (dict.TryGetValue(slot, out var tuple))
{
if (tuple.Item1 == 0) return 0;
return Math.Clamp((int)(tuple.Item2.AddSeconds(tuple.Item1) - DateTime.Now).TotalMilliseconds, 0, int.MaxValue);
return tuple.duration == 0
? 0
: Math.Clamp((int)(tuple.startTime.AddSeconds(tuple.duration) - DateTime.Now).TotalMilliseconds, 0, int.MaxValue);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Core/Actionbar/ActionBarCostReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class ActionBarCostReader
private readonly float MAX_ACTION_IDX = 1000f;

//https://wowwiki-archive.fandom.com/wiki/ActionSlot
private readonly Dictionary<int, Tuple<PowerType, int>> dict = new Dictionary<int, Tuple<PowerType, int>>();
private readonly Dictionary<int, (PowerType type, int cost)> dict = new Dictionary<int, (PowerType, int)>();

private readonly Tuple<PowerType, int> empty = new Tuple<PowerType, int>(PowerType.Mana,0);
private readonly (PowerType type, int cost) empty = (PowerType.Mana, 0);

public int MaxCount { get; } = 108; // maximum amount of actionbar slot which tracked

Expand Down Expand Up @@ -57,12 +57,12 @@ public void Read()

int cost = data;

if (dict.TryGetValue(index, out var tuple) && tuple.Item2 != cost)
if (dict.TryGetValue(index, out var tuple) && tuple.cost != cost)
{
dict.Remove(index);
}

if (dict.TryAdd(index, Tuple.Create((PowerType)type, cost)))
if (dict.TryAdd(index, ((PowerType)type, cost)))
{
OnActionCostChanged?.Invoke(this, new ActionBarCostEventArgs(index, (PowerType)type, cost));
}
Expand All @@ -73,7 +73,7 @@ public void Reset()
dict.Clear();
}

public Tuple<PowerType, int> GetCostByActionBarSlot(PlayerReader playerReader, KeyAction keyAction)
public (PowerType type, int cost) GetCostByActionBarSlot(PlayerReader playerReader, KeyAction keyAction)
{
if (KeyReader.ActionBarSlotMap.TryGetValue(keyAction.Key, out int slot))
{
Expand Down
10 changes: 5 additions & 5 deletions Core/ClassConfig/KeyAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,22 @@ public bool HasFormRequirement()
private void UpdateMinResourceRequirement(PlayerReader playerReader, ActionBarCostReader actionBarCostReader)
{
var tuple = actionBarCostReader.GetCostByActionBarSlot(playerReader, this);
if (tuple.Item2 != 0)
if (tuple.cost != 0)
{
int oldValue = 0;
switch (tuple.Item1)
switch (tuple.type)
{
case PowerType.Mana:
oldValue = MinMana;
MinMana = tuple.Item2;
MinMana = tuple.cost;
break;
case PowerType.Rage:
oldValue = MinRage;
MinRage = tuple.Item2;
MinRage = tuple.cost;
break;
case PowerType.Energy:
oldValue = MinEnergy;
MinEnergy = tuple.Item2;
MinEnergy = tuple.cost;
break;
}

Expand Down
6 changes: 3 additions & 3 deletions Core/Goals/CombatUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public bool IsPlayerMoving(Vector3 lastPos)
return distance > 0.01f;
}

public async ValueTask<Tuple<bool, bool>> FoundTargetWhileMoved()
public async ValueTask<(bool foundTarget, bool hadToMove)> FoundTargetWhileMoved()
{
bool hadToMove = false;
var startedMoving = await wait.InterruptTask(200, () => lastPosition != playerReader.PlayerLocation);
Expand All @@ -113,12 +113,12 @@ public async ValueTask<Tuple<bool, bool>> FoundTargetWhileMoved()
if (!await Wait(100, EnteredCombat()))
{
if (await AquiredTarget())
return Tuple.Create(true, hadToMove);
return (true, hadToMove);
}
}


return Tuple.Create(false, hadToMove);
return (false, hadToMove);
}


Expand Down
4 changes: 2 additions & 2 deletions Core/Path/DirectionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public static float CalculateHeading(Vector3 from, Vector3 to)
return MathF.PI + target;
}

public static Tuple<float, float> ToNormalRadian(float wowRadian)
public static (float, float) ToNormalRadian(float wowRadian)
{
// wow origo is north side - shifted 90 degree
return new Tuple<float, float>(
return (
MathF.Cos(wowRadian + (MathF.PI / 2)),
MathF.Sin(wowRadian - (MathF.PI / 2)));
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Requirement/RequirementFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private void BindMinCost(KeyAction item)
if (!valueDictionary.ContainsKey(key))
{
valueDictionary.Add(key,
() => addonReader.ActionBarCostReader.GetCostByActionBarSlot(playerReader, item).Item2);
() => addonReader.ActionBarCostReader.GetCostByActionBarSlot(playerReader, item).cost);
}
}

Expand Down

0 comments on commit 948de42

Please sign in to comment.