diff --git a/Bot/Coc.cs b/Bot/Coc.cs index 90828fd..4f5ea40 100644 --- a/Bot/Coc.cs +++ b/Bot/Coc.cs @@ -14,19 +14,41 @@ internal readonly struct DonationTuple(int donated, int received) private class ClanUtil { - internal readonly Dictionary Members = []; internal readonly Clan Clan; - internal readonly IEnumerable existingMembers; + internal readonly Dictionary Members = []; + internal readonly Dictionary ExistingMembers = []; + internal readonly Dictionary JoiningMembers = []; + internal readonly Dictionary LeavingMembers; public ClanUtil(Clan clan) { + LeavingMembers = _prevClan.Members; foreach (var member in clan.MemberList!) { Members[member.Tag] = member; + if (_prevClan.HasMember(member)) + { + ExistingMembers[member.Tag] = member; + LeavingMembers.Remove(member.Tag); + } else + { + JoiningMembers[member.Tag] = member; + } } Clan = clan; - existingMembers = clan.MemberList!.Intersect(_prevClan.Clan.MemberList!, new MemberComparer()); } + + internal ClanUtil(Clan clan, bool init) + { + LeavingMembers = []; + foreach (var member in clan.MemberList!) + { + Members[member.Tag] = member; + } + Clan = clan; + } + + internal bool HasMember(ClanMember member) => Members.ContainsKey(member.Tag); } private class MemberComparer : IEqualityComparer @@ -52,15 +74,13 @@ private static async Task PollAsync() if (clan.MemberList == null) return; ClanUtil clanUtil = new(clan); await CheckDonations(clanUtil); - _prevClan = clanUtil; } private static async Task CheckDonations(ClanUtil clan) { Dictionary donationsDelta = []; - var existingMemberTags = clan.existingMembers.Select(x => x.Tag); - foreach (var tag in existingMemberTags) + foreach (var tag in clan.ExistingMembers.Keys) { var current = clan.Members[tag]; var previous = _prevClan.Members[tag]; @@ -75,7 +95,7 @@ private static async Task CheckDonations(ClanUtil clan) } } - internal static async Task InitAsync() => _prevClan = new(await GetClanAsync()); + internal static async Task InitAsync() => _prevClan = new(await GetClanAsync(), true); internal static async Task BotReadyAsync() {