From addaefdde73a39325c91cca660fdf38fa203e4ed Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sat, 26 Nov 2022 13:02:44 +0100 Subject: [PATCH] [Raid] Refactor RosterManager into Raid --- Cocotte/Modules/Raids/PlayerInfo.cs | 6 ++-- Cocotte/Modules/Raids/Raid.cs | 27 ++++++++++------ Cocotte/Modules/Raids/RosterManager.cs | 43 -------------------------- Cocotte/Utils/NumberUtils.cs | 9 ++++-- 4 files changed, 28 insertions(+), 57 deletions(-) delete mode 100644 Cocotte/Modules/Raids/RosterManager.cs diff --git a/Cocotte/Modules/Raids/PlayerInfo.cs b/Cocotte/Modules/Raids/PlayerInfo.cs index 3bcb2c3..cd7cb5e 100644 --- a/Cocotte/Modules/Raids/PlayerInfo.cs +++ b/Cocotte/Modules/Raids/PlayerInfo.cs @@ -6,10 +6,11 @@ public class PlayerInfo public ulong Id { get; } + private readonly uint _fc; public uint Fc { get => _fc; - set + init { _fc = value; _lastFcUpdate = DateTime.Today; @@ -18,8 +19,7 @@ public class PlayerInfo public bool IsFcUpdateRequired => DateTime.Today - _lastFcUpdate > FcUpdateInterval; - private uint _fc; - private DateTime _lastFcUpdate; + private readonly DateTime _lastFcUpdate; public PlayerInfo(ulong id, uint fc) { diff --git a/Cocotte/Modules/Raids/Raid.cs b/Cocotte/Modules/Raids/Raid.cs index 8ecf0b3..227fbf9 100644 --- a/Cocotte/Modules/Raids/Raid.cs +++ b/Cocotte/Modules/Raids/Raid.cs @@ -6,10 +6,9 @@ public class Raid { public ulong Id { get; } public DateTime DateTime { get; } + public IEnumerable> Rosters => _players.Select(p => p.Value).GroupBy(p => p.RosterNumber); - private readonly RosterManager _rosterManager = new(); - - public IEnumerable> Rosters => _rosterManager.Rosters; + private readonly IDictionary _players = new Dictionary(); public Raid(ulong id, DateTime dateTime) { @@ -21,29 +20,39 @@ public class Raid #endif } - public bool AddPlayer(RosterPlayer player) + public bool AddPlayer(RosterPlayer rosterPlayer) { - return _rosterManager.AddPlayer(player); + // TODO add logic to split player in multiple rosters + rosterPlayer.RosterNumber = 1; + + return _players.TryAdd(rosterPlayer.Id, rosterPlayer); } public bool UpdatePlayer(RosterPlayer rosterPlayer) { - return _rosterManager.UpdatePlayer(rosterPlayer); + if (!_players.ContainsKey(rosterPlayer.Id)) + { + return false; + } + + _players[rosterPlayer.Id] = rosterPlayer; + + return true; } public RosterPlayer GetPlayer(ulong id) { - return _rosterManager.GetPlayer(id); + return _players[id]; } public bool ContainsPlayer(ulong userId) { - return _rosterManager.ContainsPlayer(userId); + return _players.ContainsKey(userId); } public bool RemovePlayer(ulong id) { - return _rosterManager.RemovePlayer(id); + return _players.Remove(id); } public override bool Equals(object? other) diff --git a/Cocotte/Modules/Raids/RosterManager.cs b/Cocotte/Modules/Raids/RosterManager.cs deleted file mode 100644 index 55dac29..0000000 --- a/Cocotte/Modules/Raids/RosterManager.cs +++ /dev/null @@ -1,43 +0,0 @@ -namespace Cocotte.Modules.Raids; - -public class RosterManager -{ - private readonly IDictionary _players = new Dictionary(); - - public IEnumerable> Rosters => _players.Select(p => p.Value).GroupBy(p => p.RosterNumber); - - public bool AddPlayer(RosterPlayer rosterPlayer) - { - // TODO add logic to split player in multiple rosters - rosterPlayer.RosterNumber = 1; - - return _players.TryAdd(rosterPlayer.Id, rosterPlayer); - } - - public bool UpdatePlayer(RosterPlayer rosterPlayer) - { - if (!_players.ContainsKey(rosterPlayer.Id)) - { - return false; - } - - _players[rosterPlayer.Id] = rosterPlayer; - - return true; - } - - public bool RemovePlayer(ulong id) - { - return _players.Remove(id); - } - - public RosterPlayer GetPlayer(ulong id) - { - return _players[id]; - } - - public bool ContainsPlayer(ulong userId) - { - return _players.ContainsKey(userId); - } -} \ No newline at end of file diff --git a/Cocotte/Utils/NumberUtils.cs b/Cocotte/Utils/NumberUtils.cs index feab3e1..81b1535 100644 --- a/Cocotte/Utils/NumberUtils.cs +++ b/Cocotte/Utils/NumberUtils.cs @@ -1,11 +1,16 @@ using System.Numerics; -using System.Text; namespace Cocotte.Utils; public static class NumberUtils { - public static string FormatSpaced(this INumber number) where T : INumber? + /// + /// Return a string representation of an with digits separated by spaces + /// + /// + /// + /// + public static string FormatSpaced(this IBinaryInteger number) where T : IBinaryInteger? { var stringNumber = number.ToString(null, null);