Skip to content

Commit

Permalink
Optimize Donation delta
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Feb 6, 2024
1 parent dfa4d67 commit 758af13
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
49 changes: 18 additions & 31 deletions Bot/Clash/Coc.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
using ClashOfClans;
using ClashOfClans.Models;
using Hyperstellar.Discord;
using Hyperstellar.Sql;
using static Hyperstellar.Discord.Dc;

namespace Hyperstellar.Clash;

internal static class Coc
{

internal readonly struct DonationTuple(int donated, int received)
{
internal readonly int _donated = donated;
internal readonly int _received = received;

internal DonationTuple Add(DonationTuple dt) => new(_donated + dt._donated, _received + dt._received);
}

private const string ClanId = "#2QU2UCJJC"; // 2G8LP8PVV
private static readonly ClashOfClansClient s_client = new(Secrets.s_coc);
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
Expand Down Expand Up @@ -111,57 +102,53 @@ await Task.WhenAll([

private static async Task CheckDonationsAsync(ClanUtil clan)
{
Dictionary<string, DonationTuple> donationsDelta = [];
Dictionary<string, DonationTuple> donationsTagDelta = [];
Dictionary<string, DonationTuple> donDelta = [];
foreach (string tag in clan._existingMembers.Keys)
{
ClanMember current = clan._members[tag];
ClanMember previous = s_clan._members[tag];
if (current.Donations > previous.Donations || current.DonationsReceived > previous.DonationsReceived)
{
donationsDelta[current.Name] = new(current.Donations - previous.Donations, current.DonationsReceived - previous.DonationsReceived);
donationsTagDelta[current.Tag] = new(current.Donations - previous.Donations, current.DonationsReceived - previous.DonationsReceived);
donDelta[current.Tag] = new(current.Donations - previous.Donations, current.DonationsReceived - previous.DonationsReceived);

Check notice on line 112 in Bot/Clash/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[ArrangeObjectCreationWhenTypeNotEvident] Missing type specification" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Coc.cs(112,3397)
}

}


foreach (KeyValuePair<string, DonationTuple> dd in donationsTagDelta)
foreach (KeyValuePair<string, DonationTuple> dd in donDelta)
{
Console.WriteLine($"{dd.Key}: {dd.Value._donated} {dd.Value._received}");
}

// Fold alt data into main
Dictionary<string, DonationTuple> folded = [];
foreach (KeyValuePair<string, DonationTuple> donDelta in donationsTagDelta)
Dictionary<string, DonationTuple> foldedDelta = [];
foreach (KeyValuePair<string, DonationTuple> delta in donDelta)
{
string tag = donDelta.Key;
DonationTuple dt = donDelta.Value;
Member member = new(tag);
Alt? alt = member.TryToAlt();
string tag = delta.Key;
DonationTuple dt = delta.Value;
Alt? alt = new Member(tag).TryToAlt();
if (alt != null)
{
tag = alt.MainId;
}
folded[tag] = folded.TryGetValue(tag, out DonationTuple value) ? value.Add(dt) : dt;
foldedDelta[tag] = foldedDelta.TryGetValue(tag, out DonationTuple value) ? value.Add(dt) : dt;
}
donationsTagDelta = folded;

if (donationsTagDelta.Count > 0)
if (foldedDelta.Count > 0)
{
Console.WriteLine("---");
}

foreach (KeyValuePair<string, DonationTuple> dd in donationsTagDelta)
foreach (KeyValuePair<string, DonationTuple> dd in foldedDelta)
{
Console.WriteLine($"{dd.Key}: {dd.Value._donated} {dd.Value._received}");
}

// Everyone is main now
foreach (KeyValuePair<string, DonationTuple> donDelta in donationsTagDelta)
// Everyone is main now, begin processing Donate25
foreach (KeyValuePair<string, DonationTuple> delta in foldedDelta)

Check notice on line 148 in Bot/Clash/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[UseDeconstruction] Use deconstruction" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Coc.cs(148,4603)
{
string tag = donDelta.Key;
DonationTuple dt = donDelta.Value;
string tag = delta.Key;
DonationTuple dt = delta.Value;
int donated = dt._donated;
int received = dt._received;

Expand All @@ -175,9 +162,9 @@ private static async Task CheckDonationsAsync(ClanUtil clan)
}
}

if (donationsDelta.Count > 0)
if (donDelta.Count > 0)
{
await DonationsChangedAsync(donationsDelta);
await Dc.DonationsChangedAsync(donDelta);
}
}

Expand Down
9 changes: 9 additions & 0 deletions Bot/Clash/DonationTuple.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Hyperstellar.Clash;

internal readonly struct DonationTuple(int donated, int received)
{
internal readonly int _donated = donated;
internal readonly int _received = received;

internal DonationTuple Add(DonationTuple dt) => new(_donated + dt._donated, _received + dt._received);
}
18 changes: 10 additions & 8 deletions Bot/Discord/Dc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Hyperstellar.Clash;
using Hyperstellar.Sql;
using static Hyperstellar.Clash.Coc;

namespace Hyperstellar.Discord;

Expand All @@ -27,7 +27,7 @@ private static Task Log(LogMessage msg)
private static async Task Ready()
{
s_botLog = (SocketTextChannel)s_bot.GetChannel(Secrets.s_botLogId);
_ = Task.Run(BotReadyAsync);
_ = Task.Run(Coc.BotReadyAsync);
await s_interactionSvc.RegisterCommandsGloballyAsync();
}

Expand Down Expand Up @@ -59,26 +59,28 @@ internal static async Task InitAsync()
await s_bot.StartAsync();
}

internal static async Task DonationsChangedAsync(Dictionary<string, DonationTuple> donationsDelta)
internal static async Task DonationsChangedAsync(Dictionary<string, DonationTuple> donDelta)
{
string msg = "[DNT] ";
List<string> items = new(donationsDelta.Count / 2);
foreach (string name in donationsDelta.Keys)
List<string> items = new(donDelta.Count / 2);
foreach (string tag in donDelta.Keys)
{
int donated = donationsDelta[name]._donated;
int donated = donDelta[tag]._donated;
if (donated > 0)
{
string name = Coc.GetMember(tag).Name;
items.Add($"{name}: {donated}");
}
}
msg += string.Join(", ", items);
msg += "\n=> ";
items.Clear();
foreach (string name in donationsDelta.Keys)
foreach (string tag in donDelta.Keys)
{
int received = donationsDelta[name]._received;
int received = donDelta[tag]._received;
if (received > 0)
{
string name = Coc.GetMember(tag).Name;
items.Add($"{name}: {received}");
}
}
Expand Down
4 changes: 3 additions & 1 deletion Bot/Sql/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public bool IsAlt()
public bool IsAltMain()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);
return result.Count() > 0;
return result.Any();
}

public Alt? TryToAlt() => Db.s_db.Table<Alt>().Where(a => a.AltId == CocId).FirstOrDefault();

public TableQuery<Alt> GetAltsByMain() => Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);

public string GetName() => Coc.GetMember(CocId).Name;
}

0 comments on commit 758af13

Please sign in to comment.