Skip to content

Commit

Permalink
[Bot] Rename Member to Account
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Sep 30, 2024
1 parent c90ced6 commit f1ebff2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Bot/Clash/ClanUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal ClanUtil()
internal static ClanUtil FromInit(Clan clan)
{
ClanUtil c = new(clan, []);
IEnumerable<string> existingMembers = Member.FetchAll().Select(m => m.CocId);
IEnumerable<string> existingMembers = Account.FetchAll().Select(m => m.Id);
foreach (string dbMember in existingMembers)
{
ClanMember? clanMember = clan.MemberList!.FirstOrDefault(m => m.Tag == dbMember);
Expand Down
10 changes: 5 additions & 5 deletions Bot/Clash/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<Alt> alts = fakeMem.GetAltsByMain();
string? altId = null;
if (alts.Any())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions Bot/Clash/Phaser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static Phaser()
Coc.EventInitRaid += InitRaid;
Coc.EventRaidCompleted += ProcessRaid;
Dc.EventBotReady += BotReadyAsync;
Member.EventAltAdded += AltAdded;
Account.EventAltAdded += AltAdded;

Init();
}
Expand All @@ -45,7 +45,7 @@ private static void ProcessRaid(ClanCapitalRaidSeason season)
foreach (ClanCapitalRaidSeasonAttacker atk in Coc.GetRaidAttackers(season))

Check notice on line 45 in Bot/Clash/Phaser.cs

View workflow job for this annotation

GitHub Actions / inspect

"[ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator] Part of loop's body can be converted into LINQ-expression but another 'GetEnumerator' method will be used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Phaser.cs(45,9)
{
// 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();
Expand Down Expand Up @@ -100,7 +100,7 @@ private static void DonationChanged(IEnumerable<Tuple<string, int>> 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();
Expand Down Expand Up @@ -200,7 +200,7 @@ private static async Task CheckQueueAsync()
List<Violator> violators = [];
foreach (string member in node._ids)
{
IEnumerable<Alt> alts = new Member(member).GetAltsByMain();
IEnumerable<Alt> alts = new Account(member).GetAltsByMain();
int accountCount = alts.Count() + 1;
Main main = Main.TryFetch(member)!;
bool violated = false;
Expand Down
20 changes: 10 additions & 10 deletions Bot/Discord/Cmds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!");
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()}`");
Expand Down
2 changes: 1 addition & 1 deletion Bot/Discord/Dc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member>(new MemberConverter());
s_interactionSvc.AddTypeConverter<Account>(new MemberConverter());

Coc.EventDonated += DonationsChangedAsync;
Phaser.EventViolated += PhaseViolatedAsync;
Expand Down
10 changes: 5 additions & 5 deletions Bot/Discord/MemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeConverterResult> ReadAsync(IInteractionContext context, IApplicationCommandInteractionDataOption option, IServiceProvider services)
{
Expand All @@ -32,7 +32,7 @@ public override Task<TypeConverterResult> 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);

/*
Expand All @@ -53,7 +53,7 @@ public override Task<TypeConverterResult> 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.")
Expand All @@ -64,15 +64,15 @@ public override Task<TypeConverterResult> 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);
}

// Check whether input is the tag of a clan member
ClanMember? member = Coc.TryGetMember(input);
if (member != null)
{
Member sqlMember = Member.TryFetch(input)!;
Account sqlMember = Account.TryFetch(input)!;
return TypeConverters.Success(sqlMember);
}

Expand Down
34 changes: 18 additions & 16 deletions Bot/Sql/Member.cs → Bot/Sql/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
using SQLite;

namespace Hyperstellar.Sql;
public class Member(string cocId) : DbObj<Member>
public class Account(string id) : DbObj<Account>
{
internal static event Action<Main, Main>? 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<Member>().FirstOrDefault(m => m.CocId == cocId);
public Account() : this("") { }

public void AddAlt(Member altMember)
internal static Account? TryFetch(string cocId) => s_db.Table<Account>().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)
{
Expand All @@ -30,30 +32,30 @@ public void AddAlt(Member altMember)

public bool IsAlt()
{
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.AltId == CocId);
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.AltId == Id);
return result.Any();
}

public bool IsAltMain()
{
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.MainId == CocId);
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.MainId == Id);
return result.Any();
}

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

public Main? TryToMain() => s_db.Table<Main>().FirstOrDefault(m => m.MainId == CocId);
public Main? TryToMain() => s_db.Table<Main>().FirstOrDefault(m => m.MainId == Id);

public Main ToMain() => s_db.Table<Main>().First(m => m.MainId == CocId);
public Main ToMain() => s_db.Table<Main>().First(m => m.MainId == Id);

public TableQuery<Alt> GetAltsByMain() => s_db.Table<Alt>().Where(a => a.MainId == CocId);
public TableQuery<Alt> GetAltsByMain() => s_db.Table<Alt>().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<Main>().First(m => m.MainId == mainId);
}
}
1 change: 1 addition & 0 deletions Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : Db where T : DbObj<T>, new()
Expand Down

0 comments on commit f1ebff2

Please sign in to comment.