diff --git a/Bot/Clash/ClanUtil.cs b/Bot/Clash/ClanUtil.cs index 0acbae6..0463bde 100644 --- a/Bot/Clash/ClanUtil.cs +++ b/Bot/Clash/ClanUtil.cs @@ -26,7 +26,7 @@ internal ClanUtil() internal static ClanUtil FromInit(Clan clan) { ClanUtil c = new(clan, []); - IEnumerable existingMembers = Member.FetchAll().Select(m => m.CocId); + IEnumerable existingMembers = Account.FetchAll().Select(m => m.Id); foreach (string dbMember in existingMembers) { ClanMember? clanMember = clan.MemberList!.FirstOrDefault(m => m.Tag == dbMember); diff --git a/Bot/Clash/Coc.cs b/Bot/Clash/Coc.cs index b5d4f9a..65cc9cb 100644 --- a/Bot/Clash/Coc.cs +++ b/Bot/Clash/Coc.cs @@ -41,7 +41,7 @@ private static void CheckMembersJoined(ClanUtil clan) } string[] members = [.. clan._joiningMembers.Keys]; - Member[] memberObjects = [.. members.Select(static memberId => new Member(memberId))]; + Account[] memberObjects = [.. members.Select(static memberId => new Account(memberId))]; Db.InsertAll(memberObjects); string membersMsg = string.Join(", ", members); Console.WriteLine($"{membersMsg} joined"); @@ -63,7 +63,7 @@ private static void CheckMembersLeft(ClanUtil clan) foreach ((string id, ClanMember member) in clan._leavingMembers) { - Member fakeMem = new(id); + Account fakeMem = new(id); IEnumerable alts = fakeMem.GetAltsByMain(); string? altId = null; if (alts.Any()) @@ -91,7 +91,7 @@ private static void CheckMembersLeft(ClanUtil clan) string[] members = [.. clan._leavingMembers.Keys]; foreach (string member in members) { - new Member(member).Delete(); + new Account(member).Delete(); } string membersMsg = string.Join(", ", members); @@ -242,7 +242,7 @@ private static async Task CheckDonationsAsync(ClanUtil clan) graph.AddVertex(current.Tag); graph.AddEdge(new("s", current.Tag, donated)); - accToMainAcc.TryAdd(current.Tag, new Member(current.Tag).GetEffectiveMain().MainId); + accToMainAcc.TryAdd(current.Tag, new Account(current.Tag).GetEffectiveMain().MainId); } } @@ -274,7 +274,7 @@ private static async Task CheckDonationsAsync(ClanUtil clan) graph.AddEdge(new(vertexName, "t", received)); foreach (string donor in accToMainAcc - .Where(kv => kv.Value != new Member(current.Tag).GetEffectiveMain().MainId) + .Where(kv => kv.Value != new Account(current.Tag).GetEffectiveMain().MainId) .Select(kv => kv.Key)) { graph.AddEdge(new(donor, vertexName, received)); diff --git a/Bot/Clash/Phaser.cs b/Bot/Clash/Phaser.cs index e6a0880..e64c3e5 100644 --- a/Bot/Clash/Phaser.cs +++ b/Bot/Clash/Phaser.cs @@ -25,7 +25,7 @@ static Phaser() Coc.EventInitRaid += InitRaid; Coc.EventRaidCompleted += ProcessRaid; Dc.EventBotReady += BotReadyAsync; - Member.EventAltAdded += AltAdded; + Account.EventAltAdded += AltAdded; Init(); } @@ -45,7 +45,7 @@ private static void ProcessRaid(ClanCapitalRaidSeason season) foreach (ClanCapitalRaidSeasonAttacker atk in Coc.GetRaidAttackers(season)) { // If in the db, mark as raided - Member? member = Member.TryFetch(atk.Tag); + Account? member = Account.TryFetch(atk.Tag); if (member != null) { Main main = member.GetEffectiveMain(); @@ -100,7 +100,7 @@ private static void DonationChanged(IEnumerable> donations) { foreach ((string tag, int donated) in donations) { - Main main = new Member(tag).GetEffectiveMain(); + Main main = new Account(tag).GetEffectiveMain(); main.Donated += (uint)donated; Console.WriteLine($"[Donate25] {tag} {donated}"); main.Update(); @@ -200,7 +200,7 @@ private static async Task CheckQueueAsync() List violators = []; foreach (string member in node._ids) { - IEnumerable alts = new Member(member).GetAltsByMain(); + IEnumerable alts = new Account(member).GetAltsByMain(); int accountCount = alts.Count() + 1; Main main = Main.TryFetch(member)!; bool violated = false; diff --git a/Bot/Discord/Cmds.cs b/Bot/Discord/Cmds.cs index d0cfbc0..c0a02d9 100644 --- a/Bot/Discord/Cmds.cs +++ b/Bot/Discord/Cmds.cs @@ -47,9 +47,9 @@ public async Task AdminAsync(SocketGuildUser user) [RequireAdmin] [SlashCommand("alt", "[Admin] Links an alt to a main")] - public async Task AltAsync(Member alt, Member main) + public async Task AltAsync(Account alt, Account main) { - if (alt.CocId == main.CocId) + if (alt.Id == main.Id) { await RespondAsync("Bro alt must be different from main bruh"); return; @@ -66,16 +66,16 @@ public async Task AltAsync(Member alt, Member main) } main.AddAlt(alt); - ClanMember clanAlt = Coc.GetMember(alt.CocId); - ClanMember clanMain = Coc.GetMember(main.CocId); + ClanMember clanAlt = Coc.GetMember(alt.Id); + ClanMember clanMain = Coc.GetMember(main.Id); await RespondAsync($"`{clanAlt.Name}` is now an alt of `{clanMain.Name}`"); } [RequireAdmin] [SlashCommand("discord", "[Admin] Links a Discord account to a Main")] - public async Task DiscordAsync(Member coc, IGuildUser discord) + public async Task DiscordAsync(Account member, IGuildUser discord) { - Main? main = coc.TryToMain(); + Main? main = member.TryToMain(); if (main == null) { await RespondAsync("`coc` can't be an alt!"); @@ -92,9 +92,9 @@ public async Task DiscordAsync(Member coc, IGuildUser discord) } [SlashCommand("info", "Shows info of a clan member")] - public async Task InfoAsync(Member clanMember) + public async Task InfoAsync(Account clanMember) { - ClanMember cocMem = Coc.GetMember(clanMember.CocId); + ClanMember cocMem = Coc.GetMember(clanMember.Id); EmbedBuilder embed = new() { @@ -132,9 +132,9 @@ public async Task InfoAsync(Member clanMember) [RequireAdmin] [SlashCommand("alias", "[Admin] Sets an alias for a Coc member")] - public async Task AliasAsync(string alias, Member member) + public async Task AliasAsync(string alias, Account member) { - bool success = new CocAlias(alias, member.CocId).Insert() == 1; + bool success = new CocAlias(alias, member.Id).Insert() == 1; if (success) { await RespondAsync($"`{alias}` is now an alias of `{member.GetName()}`"); diff --git a/Bot/Discord/Dc.cs b/Bot/Discord/Dc.cs index 46dfc4b..ea9d430 100644 --- a/Bot/Discord/Dc.cs +++ b/Bot/Discord/Dc.cs @@ -21,7 +21,7 @@ static Dc() #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { s_interactionSvc = new(s_bot); // Dont make it inline instantiate because s_bot.Rest would still be null - s_interactionSvc.AddTypeConverter(new MemberConverter()); + s_interactionSvc.AddTypeConverter(new MemberConverter()); Coc.EventDonated += DonationsChangedAsync; Phaser.EventViolated += PhaseViolatedAsync; diff --git a/Bot/Discord/MemberConverter.cs b/Bot/Discord/MemberConverter.cs index 8f2c39d..934c0a8 100644 --- a/Bot/Discord/MemberConverter.cs +++ b/Bot/Discord/MemberConverter.cs @@ -10,7 +10,7 @@ namespace Hyperstellar.Discord; internal sealed class MemberConverter : TypeConverter { - public override bool CanConvertTo(Type type) => typeof(Member).IsAssignableFrom(type); + public override bool CanConvertTo(Type type) => typeof(Account).IsAssignableFrom(type); public override ApplicationCommandOptionType GetDiscordType() => ApplicationCommandOptionType.String; public override Task ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services) { @@ -32,7 +32,7 @@ public override Task ReadAsync(IInteractionContext context, } // TODO: REMOVE THIS AFTER DB REDESIGN - SKIPPING THE CHECK BELOW BECUZ FOR NOW, IN DB = MUST BE IN CLAN - Member sqlMember = Member.TryFetch(main.MainId)!; + Account sqlMember = Account.TryFetch(main.MainId)!; return TypeConverters.Success(sqlMember); /* @@ -53,7 +53,7 @@ public override Task ReadAsync(IInteractionContext context, // Check whether the coc account of the alias is still in the clan string aliasCocId = dbAlias.CocId; ClanMember? aliasClanMember = Coc.TryGetMember(aliasCocId); - Member sqlMember = Member.TryFetch(aliasCocId)!; + Account sqlMember = Account.TryFetch(aliasCocId)!; return aliasClanMember == null ? TypeConverters.Error("The player of this alias isn't in the clan.") @@ -64,7 +64,7 @@ public override Task ReadAsync(IInteractionContext context, string? cocId = Coc.GetMemberId(input); if (cocId != null) { - Member sqlMember = Member.TryFetch(cocId)!; + Account sqlMember = Account.TryFetch(cocId)!; return TypeConverters.Success(sqlMember); } @@ -72,7 +72,7 @@ public override Task ReadAsync(IInteractionContext context, ClanMember? member = Coc.TryGetMember(input); if (member != null) { - Member sqlMember = Member.TryFetch(input)!; + Account sqlMember = Account.TryFetch(input)!; return TypeConverters.Success(sqlMember); } diff --git a/Bot/Sql/Member.cs b/Bot/Sql/Account.cs similarity index 61% rename from Bot/Sql/Member.cs rename to Bot/Sql/Account.cs index 01aa86c..2d90563 100644 --- a/Bot/Sql/Member.cs +++ b/Bot/Sql/Account.cs @@ -2,23 +2,25 @@ using SQLite; namespace Hyperstellar.Sql; -public class Member(string cocId) : DbObj +public class Account(string id) : DbObj { internal static event Action? EventAltAdded; [PrimaryKey, NotNull] - public string CocId { get; set; } = cocId; + public string Id { get; set; } = id; - public Member() : this("") { } + public long? LeftTime { get; set; } - internal static Member? TryFetch(string cocId) => s_db.Table().FirstOrDefault(m => m.CocId == cocId); + public Account() : this("") { } - public void AddAlt(Member altMember) + internal static Account? TryFetch(string cocId) => s_db.Table().FirstOrDefault(m => m.Id == cocId); + + public void AddAlt(Account altMember) { - Alt alt = new(altMember.CocId, CocId); + Alt alt = new(altMember.Id, Id); alt.Insert(); - Main altMain = Main.TryFetch(altMember.CocId)!; - Main mainMain = Main.TryFetch(CocId)!; + Main altMain = Main.TryFetch(altMember.Id)!; + Main mainMain = Main.TryFetch(Id)!; EventAltAdded!(altMain, mainMain); if (altMain.Discord != null) { @@ -30,30 +32,30 @@ public void AddAlt(Member altMember) public bool IsAlt() { - TableQuery result = s_db.Table().Where(a => a.AltId == CocId); + TableQuery result = s_db.Table().Where(a => a.AltId == Id); return result.Any(); } public bool IsAltMain() { - TableQuery result = s_db.Table().Where(a => a.MainId == CocId); + TableQuery result = s_db.Table().Where(a => a.MainId == Id); return result.Any(); } - public Alt? TryToAlt() => s_db.Table().FirstOrDefault(a => a.AltId == CocId); + public Alt? TryToAlt() => s_db.Table().FirstOrDefault(a => a.AltId == Id); - public Main? TryToMain() => s_db.Table
().FirstOrDefault(m => m.MainId == CocId); + public Main? TryToMain() => s_db.Table
().FirstOrDefault(m => m.MainId == Id); - public Main ToMain() => s_db.Table
().First(m => m.MainId == CocId); + public Main ToMain() => s_db.Table
().First(m => m.MainId == Id); - public TableQuery GetAltsByMain() => s_db.Table().Where(a => a.MainId == CocId); + public TableQuery GetAltsByMain() => s_db.Table().Where(a => a.MainId == Id); - public string GetName() => Coc.GetMember(CocId).Name; + public string GetName() => Coc.GetMember(Id).Name; public Main GetEffectiveMain() { Alt? alt = TryToAlt(); - string mainId = alt == null ? CocId : alt.MainId; + string mainId = alt == null ? Id : alt.MainId; return s_db.Table
().First(m => m.MainId == mainId); } } diff --git a/Bot/Sql/Db.cs b/Bot/Sql/Db.cs index 77d81b2..deb40d9 100644 --- a/Bot/Sql/Db.cs +++ b/Bot/Sql/Db.cs @@ -30,6 +30,7 @@ await Program.TryUntilAsync(static async () => internal int Delete() => s_db.Delete(this); internal int Update() => s_db.Update(this); + } public abstract class DbObj : Db where T : DbObj, new()