Skip to content

Commit

Permalink
[Bot] Read members from db on init
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Jan 17, 2024
1 parent 2109fee commit fe8c39d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 40 deletions.
37 changes: 17 additions & 20 deletions Bot/Coc.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
using ClashOfClans;
using ClashOfClans.Models;
using Hyperstellar.Sql;

namespace Hyperstellar;

internal sealed class Coc

Check notice on line 7 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[ClassNeverInstantiated.Global] Class 'Coc' is never instantiated" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(7,119)
{
private sealed class ClanUtil
{
internal readonly Clan _clan;
internal Clan _clan;

Check warning on line 11 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[NotAccessedField.Local] Field '_clan' is assigned but its value is never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(11,187)
internal readonly Dictionary<string, ClanMember> _members = [];
internal readonly Dictionary<string, ClanMember> _existingMembers = [];
internal readonly Dictionary<string, ClanMember> _joiningMembers = [];

Check notice on line 14 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[MemberCanBePrivate.Local] Field '_joiningMembers' can be made private" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(14,354)

Check warning on line 14 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[CollectionNeverQueried.Local] Content of collection '_joiningMembers' is only updated but never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(14,403)
internal readonly Dictionary<string, ClanMember> _leavingMembers;
internal Dictionary<string, ClanMember> _leavingMembers;

Check notice on line 15 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[MemberCanBePrivate.Local] Field '_leavingMembers' can be made private" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(15,433)

Check notice on line 15 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[FieldCanBeMadeReadOnly.Local] Field can be made readonly" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(15,473)

internal ClanUtil()
{
IEnumerable<string> existingMembers = Db.GetMembers();
foreach (string member in existingMembers)
{
_members[member] = new(); // PLACEHOLDER

Check notice on line 22 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(22,700)
}
_clan = new(); // PLACEHOLDER

Check notice on line 24 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(24,757)
_leavingMembers = [];
}

internal ClanUtil(Clan clan)
{
Expand All @@ -22,7 +34,7 @@ internal ClanUtil(Clan clan)
if (s_prevClan.HasMember(member))
{
_existingMembers[member.Tag] = member;
_ = _leavingMembers.Remove(member.Tag);
_leavingMembers.Remove(member.Tag);

Check warning on line 37 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[IDE0058] Expression value is never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(37,1191)

Check warning on line 37 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / build

Expression value is never used (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0058)
}
else
{
Expand All @@ -32,16 +44,6 @@ internal ClanUtil(Clan clan)
_clan = clan;
}

internal ClanUtil(Clan clan, bool init)
{
_leavingMembers = [];
foreach (ClanMember member in clan.MemberList!)
{
_members[member.Tag] = member;
}
_clan = clan;
}

internal bool HasMember(ClanMember member) => _members.ContainsKey(member.Tag);

Check notice on line 47 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[MemberCanBePrivate.Local] Method 'HasMember' can be made private" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(47,1419)

Check notice on line 47 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[SuggestBaseTypeForParameter] Parameter can be of type 'ClashOfClans.Models.Identity'" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(47,1443)
}

Expand All @@ -53,10 +55,7 @@ internal readonly struct DonationTuple(int donated, int received)

private const string ClanId = "#2QU2UCJJC";
internal static readonly ClashOfClansClient s_client = new(Secrets.s_coc);

Check notice on line 57 in Bot/Coc.cs

View workflow job for this annotation

GitHub Actions / inspect

"[MemberCanBePrivate.Global] Field 's_client' can be made private" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Coc.cs(57,1743)

#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;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static ClanUtil s_prevClan = new();

private static async Task<Clan> GetClanAsync() => await s_client.Clans.GetClanAsync(ClanId);

Expand Down Expand Up @@ -96,8 +95,6 @@ private static async Task CheckDonations(ClanUtil clan)
}
}

internal static async Task InitAsync() => s_prevClan = new(await GetClanAsync(), true);

internal static async Task BotReadyAsync()
{
while (true)
Expand All @@ -106,4 +103,4 @@ internal static async Task BotReadyAsync()
await Task.Delay(5000);
}
}

Check warning on line 105 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(105,3223)
}
}
2 changes: 0 additions & 2 deletions Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ public class Program
public static async Task Main()
{
await Discord.InitAsync();
await Coc.InitAsync();
Sql.Db.Init();
await Task.Delay(-1);
}
}
19 changes: 19 additions & 0 deletions Bot/Sql/Alt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SQLite;

namespace Hyperstellar.Sql;
internal class Alt

Check warning on line 4 in Bot/Sql/Alt.cs

View workflow job for this annotation

GitHub Actions / inspect

"[CA1852] Type 'Alt' can be sealed because it has no subtypes in its containing assembly and is not externally visible" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/Alt.cs(4,58)

Check warning on line 4 in Bot/Sql/Alt.cs

View workflow job for this annotation

GitHub Actions / build

Type 'Alt' 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 AltId { get; set; }

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

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

public Alt(string altId, string mainId)
{
AltId = altId;
MainId = mainId;
}
}
17 changes: 8 additions & 9 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ namespace Hyperstellar.Sql;

internal sealed class Db
{
private static readonly SQLiteConnection s_db = new("Hyperstellar.db");
internal static readonly SQLiteConnection s_db = new("Hyperstellar.db");

internal static void Init()
{
var x = s_db.Table<User>();
foreach (var y in x)
{
//Console.WriteLine(y.CocId);
}
}
internal static IEnumerable<string> GetMembers() => s_db.Table<Alt>()
.Select(a => a.AltId)
.Union(
s_db.Table<Alt>()
.Select(a => a.MainId)
.Distinct()
);
}
8 changes: 8 additions & 0 deletions Bot/Sql/Member.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Hyperstellar.Sql;

internal class Member

Check warning on line 3 in Bot/Sql/Member.cs

View workflow job for this annotation

GitHub Actions / inspect

"[CA1852] Type 'Member' can be sealed because it has no subtypes in its containing assembly and is not externally visible" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/Member.cs(3,44)

Check warning on line 3 in Bot/Sql/Member.cs

View workflow job for this annotation

GitHub Actions / build

Type 'Member' 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)
{
internal string Id { get; set; }

internal Member(string id) => Id = id;
}
25 changes: 25 additions & 0 deletions Bot/Sql/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using SQLite;

namespace Hyperstellar.Sql;
internal class Person

Check warning on line 4 in Bot/Sql/Person.cs

View workflow job for this annotation

GitHub Actions / build

Type 'Person' 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]
[Column("CocId")]
public string CocId { get; set; }
[Unique]
public ulong? DiscordId { get; set; }

public Person() => CocId = "";

public Person(string CocId, ulong? DiscordId)
{
this.CocId = CocId;
this.DiscordId = DiscordId;
}

public void AddAlt(string altId)
{
Alt alt = new(altId, CocId);
_ = Db.s_db.Insert(alt);
}
}
9 changes: 0 additions & 9 deletions Bot/Sql/User.cs

This file was deleted.

0 comments on commit fe8c39d

Please sign in to comment.