Skip to content

Commit

Permalink
[Bot] Completed Coc.cs refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 15, 2024
1 parent 3ffa551 commit 17c892f
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions Bot/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,41 @@ internal readonly struct DonationTuple(int donated, int received)

private class ClanUtil
{
internal readonly Dictionary<string, ClanMember> Members = [];
internal readonly Clan Clan;
internal readonly IEnumerable<ClanMember> existingMembers;
internal readonly Dictionary<string, ClanMember> Members = [];
internal readonly Dictionary<string, ClanMember> ExistingMembers = [];
internal readonly Dictionary<string, ClanMember> JoiningMembers = [];
internal readonly Dictionary<string, ClanMember> 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<ClanMember>
Expand All @@ -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<string, DonationTuple> 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];
Expand All @@ -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()
{
Expand Down

0 comments on commit 17c892f

Please sign in to comment.