From 7f6f74b3e82f202721ae5f246a39a2fb1dbf3370 Mon Sep 17 00:00:00 2001 From: Pythonic-Rainbow Date: Mon, 23 Sep 2024 20:52:04 +0800 Subject: [PATCH] [Bot] Add Donation table to db --- Bot/Clash/Coc.cs | 25 +++++++++++++++++++++++++ Bot/Sql/Donation.cs | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Bot/Sql/Donation.cs diff --git a/Bot/Clash/Coc.cs b/Bot/Clash/Coc.cs index 42bf0c6..63dcf51 100644 --- a/Bot/Clash/Coc.cs +++ b/Bot/Clash/Coc.cs @@ -154,6 +154,9 @@ static async Task WaitRaidAsync() private static async Task CheckDonationsAsync(ClanUtil clan) { + long currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + Dictionary donations = []; + List> donDelta = [], recDelta = []; Dictionary accToMainAcc = []; AdjacencyGraph> graph = new(false); @@ -168,6 +171,12 @@ private static async Task CheckDonationsAsync(ClanUtil clan) { int donated = current.Donations - previous.Donations; + Donation donation = new(currentTime, tag) + { + Donated = donated + }; + donations[tag] = donation; + donDelta.Add(new(current.Tag, donated)); graph.AddVertex(current.Tag); @@ -186,6 +195,20 @@ private static async Task CheckDonationsAsync(ClanUtil clan) { string vertexName = $"#{current.Tag}"; // Double # for received node int received = current.DonationsReceived - previous.DonationsReceived; + if (donations.TryGetValue(tag, out Donation? value)) + { + value.Received = received; + } + else + { + Donation donation = new(currentTime, tag) + { + Received = received + }; + + donations[tag] = donation; + } + recDelta.Add(new(current.Tag, received)); graph.AddVertex(vertexName); graph.AddEdge(new(vertexName, "t", received)); @@ -199,6 +222,8 @@ private static async Task CheckDonationsAsync(ClanUtil clan) } } + Db.InsertAll(donations.Values); + if (graph.VertexCount > 2) { ReversedEdgeAugmentorAlgorithm> reverseAlgo = new( diff --git a/Bot/Sql/Donation.cs b/Bot/Sql/Donation.cs new file mode 100644 index 0000000..13db600 --- /dev/null +++ b/Bot/Sql/Donation.cs @@ -0,0 +1,21 @@ +using SQLite; + +namespace Hyperstellar.Sql; +public class Donation(long time, string cocId, int donated, int received) : DbObj +{ + [PrimaryKey, NotNull] + public long Time { get; set; } = time; + + [PrimaryKey, NotNull] + public string CocID { get; set; } = cocId; + + [NotNull] + public int Donated { get; set; } = donated; + + [NotNull] + public int Received { get; set; } = received; + + public Donation() : this(0, "", 0, 0) { } + + public Donation(long time, string cocId) : this(time, cocId, 0, 0) { } +}