Skip to content

Commit

Permalink
[Bot] Refactor and added Donation table
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 21, 2024
1 parent 6d70e12 commit 73a2152
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Only the .NET SDK is essential tbh. This project uses .NET 8

You can also use VSCode + C# extension and just download the .NET 8 SDK separately, tho I haven't tried.
You can also use VSCode + C# extension and just download the .NET 8 SDK separately.

2. You need to create `secrets.json` in the folder.
```json
Expand All @@ -22,7 +22,7 @@
}
```

3. You need the SQLite file `Hyperstellar.db`. Just ask me for that
3. You need the SQLite file `Hyperstellar.db`. Just ask me for that. You need to place it in the debug/release binary output folder, next to the executable.

<a name="overview"></a>
# Structure Overview
Expand Down
31 changes: 23 additions & 8 deletions Bot/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ internal static ClanUtil FromInit(Clan clan)

internal static ClanUtil FromPoll(Clan clan)
{
ClanUtil c = new(clan, new(s_prevClan._members));
ClanUtil c = new(clan, new(s_clan._members));

Check notice on line 51 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[ArrangeObjectCreationWhenTypeNotEvident] Missing type specification" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(51,1691)
foreach (ClanMember member in clan.MemberList!)
{
c._members[member.Tag] = member;
if (s_prevClan.HasMember(member))
if (s_clan.HasMember(member))
{
c._existingMembers[member.Tag] = member;
c._leavingMembers.Remove(member.Tag);
Expand All @@ -77,7 +77,7 @@ internal readonly struct DonationTuple(int donated, int received)
private const string ClanId = "#2QU2UCJJC";
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.
private static ClanUtil s_prevClan;
private static ClanUtil s_clan;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

private static void CheckMembersJoined(ClanUtil clan)
Expand Down Expand Up @@ -137,7 +137,7 @@ private static async Task PollAsync()
await Task.WhenAll([
CheckDonationsAsync(clanUtil),
]);
s_prevClan = clanUtil;
s_clan = clanUtil;
}

private static async Task CheckDonationsAsync(ClanUtil clan)
Expand All @@ -146,7 +146,7 @@ private static async Task CheckDonationsAsync(ClanUtil clan)
foreach (string tag in clan._existingMembers.Keys)
{
ClanMember current = clan._members[tag];
ClanMember previous = s_prevClan._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);
Expand All @@ -158,18 +158,33 @@ private static async Task CheckDonationsAsync(ClanUtil clan)
}
}

private static async Task Donate25Async()

Check warning on line 161 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[UnusedMember.Local] Method 'Donate25Async' is never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(161,5362)
{
while (true)
{
DateTime now = DateTime.Now;
DateTime nextMonday = now.AddDays(((int)DayOfWeek.Monday - (int)now.DayOfWeek + 7) % 7)
.Date;
TimeSpan timeToWait = nextMonday - now;
await Task.Delay(timeToWait);


}
}

Check warning on line 173 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[FunctionNeverReturns] Function never returns" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(173,5717)

internal static string? GetMemberId(string name)
{
ClanMember? result = s_prevClan._clan.MemberList!.FirstOrDefault(m => m.Name == name);
ClanMember? result = s_clan._clan.MemberList!.FirstOrDefault(m => m.Name == name);
return result?.Tag;
}

internal static ClanMember GetMember(string id) => s_prevClan._members[id];
internal static ClanMember GetMember(string id) => s_clan._members[id];

internal static async Task InitAsync() => s_prevClan = ClanUtil.FromInit(await GetClanAsync());
internal static async Task InitAsync() => s_clan = ClanUtil.FromInit(await GetClanAsync());

internal static async Task BotReadyAsync()
{
//_ = Task.Run(Donate25Async);
while (true)
{
await PollAsync();
Expand Down
2 changes: 1 addition & 1 deletion Bot/Dc/Cmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task AltAsync(Member alt, Member main)
await RespondAsync("Main can't be an alt in the database!");
return;
}
if (alt.IsMain())
if (alt.IsAltMain())
{
await RespondAsync("Alt can't be a main in the database!");
return;
Expand Down
14 changes: 4 additions & 10 deletions Bot/Sql/Alt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

namespace Hyperstellar.Sql;

internal sealed class Alt
internal sealed class Alt(string altId, string mainId)
{
[PrimaryKey, NotNull]
public string AltId { get; set; }
public string AltId { get; set; } = altId;

[NotNull]
public string MainId { get; set; }
public string MainId { get; set; } = mainId;

public Alt() => AltId = MainId = "";

public Alt(string altId, string mainId)
{
AltId = altId;
MainId = mainId;
}
public Alt() : this("", "") { }
}
8 changes: 3 additions & 5 deletions Bot/Sql/BotAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Hyperstellar.Sql;

internal sealed class BotAdmin
internal sealed class BotAdmin(ulong id)
{
[PrimaryKey, NotNull]
public ulong Id { get; set; }
public ulong Id { get; set; } = id;

public BotAdmin() => Id = 0;

public BotAdmin(ulong id) => Id = id;
public BotAdmin() : this(0) { }
}
7 changes: 6 additions & 1 deletion Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ internal static void Commit()

internal static IEnumerable<Member> GetMembers() => s_db.Table<Member>();

internal static bool AddMembers(string[] members) => s_db.InsertAll(members.Select(m => new Member(m))) == members.Length;
internal static bool AddMembers(string[] members)
{
int memberCount = s_db.InsertAll(members.Select(m => new Member(m)));
int donationCount = s_db.InsertAll(members.Select(m => new Donation(m)));
return memberCount == donationCount && memberCount == members.Length;
}

internal static bool RemoveMembers(string[] members)
{
Expand Down
17 changes: 17 additions & 0 deletions Bot/Sql/Donation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using SQLite;

namespace Hyperstellar.Sql;

internal class Donation(string id)

Check warning on line 5 in Bot/Sql/Donation.cs

View workflow job for this annotation

GitHub Actions / build

Type 'Donation' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
[PrimaryKey, NotNull]
public string Id { get; set; } = id;

[NotNull]
public uint Donated { get; set; } = 0;

[NotNull]
public long Checked { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

public Donation() : this("") { }
}
10 changes: 5 additions & 5 deletions Bot/Sql/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public class Member
[Unique]
public ulong? DiscordId { get; set; }

public Member() => CocId = "";
public Member() : this("") { }

public Member(string cocId) => CocId = cocId;

public Member(string CocId, ulong DiscordId)
public Member(string cocId, ulong discordId)
{
this.CocId = CocId;
this.DiscordId = DiscordId;
CocId = cocId;
DiscordId = discordId;
}

public void AddAlt(Member altMember)
Expand All @@ -30,7 +30,7 @@ public bool IsAlt()
return result.Count() > 0;
}

public bool IsMain()
public bool IsAltMain()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);
return result.Count() > 0;
Expand Down

0 comments on commit 73a2152

Please sign in to comment.