Skip to content

Commit

Permalink
Continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Nov 10, 2024
1 parent 946f8de commit 788e58c
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 198 deletions.
14 changes: 0 additions & 14 deletions Bot/Clash/ClanUtil.cs

This file was deleted.

329 changes: 227 additions & 102 deletions Bot/Clash/Coc.cs

Large diffs are not rendered by default.

89 changes: 30 additions & 59 deletions Bot/Clash/Phaser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ private sealed class Node(long time)

static Phaser()
{
Coc.EventMemberJoined += MemberAdded;
Coc.EventDonatedMaxFlow += DonationChanged;
Coc.EventInitRaid += InitRaid;
Coc.EventRaidCompleted += ProcessRaid;
Dc.EventBotReady += BotReadyAsync;
Expand Down Expand Up @@ -56,6 +54,33 @@ private static void ProcessRaid(ClanCapitalRaidSeason season)

private static void Init()
{
// For each MainID in table MainRequirement, get the row with the latest EndTime
IEnumerable<IGrouping<long, MainRequirement>> eachLatestReq = MainRequirement.FetchEachLatest()
.GroupBy(req => req.EndTime);
long currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

foreach (IGrouping<long, MainRequirement> reqGroup in eachLatestReq)
{
// If overdue, update the req
if (reqGroup.Key < currentTime)
{
foreach (MainRequirement req in reqGroup)
{
req.Passed = Pass.Overdue;
req.Update();
}

continue;
}

if (reqGroup.Key == currentTime)
{

}
}

Task.Delay(-1);

IEnumerable<IGrouping<long, Main>> donationGroups = Main.FetchAll()
.GroupBy(static d => d.Checked)
.OrderBy(static g => g.Key);
Expand All @@ -71,7 +96,7 @@ private static void Init()
{
foreach (Main main in group)
{
expiredNode._ids.Add(main.MainId);
expiredNode._ids.Add(main.AccountId);
main.Checked = expiredNode._checkTime;
main.Donated = 0;
main.Update();
Expand All @@ -82,7 +107,7 @@ private static void Init()
Node node = new(group.Key);
foreach (Main main in group)
{
node._ids.Add(main.MainId);
node._ids.Add(main.AccountId);
}
s_queue.Enqueue(node);
}
Expand All @@ -95,20 +120,9 @@ private static void Init()
Console.WriteLine("[Donate25] Inited");
}

private static void DonationChanged(IEnumerable<Tuple<string, int>> donations)
{
foreach ((string tag, int donated) in donations)
{
Main main = new Account(tag).GetEffectiveMain();
main.Donated += (uint)donated;
Console.WriteLine($"[Donate25] {tag} {donated}");
main.Update();
}
}

private static void AltAdded(Main altMain, Main mainMain)
{
string altId = altMain.MainId, mainId = mainMain.MainId;
string altId = altMain.AccountId, mainId = mainMain.AccountId;
Console.WriteLine($"[Donate25] Removing {altId} -> {mainId} (addalt)");
Node? node = s_queue.FirstOrDefault(n => n._ids.Remove(altId));
if (node != null)
Expand All @@ -120,49 +134,6 @@ private static void AltAdded(Main altMain, Main mainMain)
}
}

private static void MemberAdded(ClanMember member, Main main)
{
main.Checked = GetNowNextTime();
string id = member.Tag;
Console.WriteLine($"[Donate25] Adding {id}");
long targetTime = GetNowNextTime();
Node node = s_queue.Last(); // We expect at least 1 member in the db
if (targetTime == node._checkTime)
{
node._ids.Add(id);
Console.WriteLine($"[Donate25] Added {id} in {node._checkTime} (last node)");
}
else if (targetTime > node._checkTime)
{
node = new(targetTime);
node._ids.Add(id);
s_queue.Enqueue(node);
Console.WriteLine($"[Donate25] Added {id} in {node._checkTime} (new node). New queue len: {s_queue.Count}");
}
else
{
throw new InvalidOperationException($"New member targetTime < last node check time. targetTime: {targetTime} Last node checktime: {node._checkTime}");
}
}

private static void MemberLeft(Account[] leftMembers)
{
/*
string id = member.Tag;
Console.WriteLine($"[Donate25] Removing {id} -> {newMainId}");
Node? node = s_queue.FirstOrDefault(n => n._ids.Remove(id));
if (node != null)
{
Console.WriteLine($"[Donate25] Removed {id} in {node._checkTime}");
if (newMainId != null)
{
node._ids.Add(newMainId);
Console.WriteLine($"[Donate25] Added {newMainId} because it replaced {id} as main");
}
}
*/
}

private static void InitRaid(ClanCapitalRaidSeason season)
{
if (!IsDatetimeExpired(season.StartTime))
Expand Down
26 changes: 13 additions & 13 deletions Bot/Discord/Dc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ static string ProcessViolator(Violator v)
}
}

private static async Task DonationsChangedAsync(IEnumerable<Tuple<string, int>> donDelta, IEnumerable<Tuple<string, int>> recDelta)
private static async Task DonationsChangedAsync(IDictionary<string, DonRecv> donations)
{
string msg = "[DNT] ";
msg += string.Join(", ", donDelta.Select(static t =>
List<string> donors = [], receivers = [];
foreach ((string tag, DonRecv dr) in donations)
{
(string tag, int donated) = t;
string name = Coc.GetMember(tag).Name;
return $"{name}: {donated}";
}));
msg += "\n=> ";
msg += string.Join(", ", recDelta.Select(static t =>
{
(string tag, int received) = t;
string name = Coc.GetMember(tag).Name;
return $"{name}: {received}";
}));
if (dr._donated > 0)
{
donors.Add($"{name}: {dr._donated}");
}
if (dr._received > 0)
{
receivers.Add($"{name}: {dr._received}");
}
}
string msg = $"[DNT] {string.Join(", ", donors)}\n=> {string.Join(", ", receivers)}";
await s_botLog.SendMessageAsync(msg);
}

Expand Down
2 changes: 1 addition & 1 deletion Bot/Discord/MemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Account sqlMember = Account.TryFetch(main.MainId)!;
Account sqlMember = Account.TryFetch(main.AccountId)!;
return TypeConverters.Success(sqlMember);

/*
Expand Down
6 changes: 3 additions & 3 deletions Bot/Sql/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public bool IsAltMain()

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

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

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

public TableQuery<Alt> GetAltsByMain() => s_db.Table<Alt>().Where(a => a.MainId == Id);

Expand All @@ -60,7 +60,7 @@ public Main GetEffectiveMain()
{
Alt? alt = TryToAlt();
string mainId = alt == null ? Id : alt.MainId;
return s_db.Table<Main>().First(m => m.MainId == mainId);
return s_db.Table<Main>().First(m => m.AccountId == mainId);
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion Bot/Sql/Alt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Alt() : this("", "") { }

public TableQuery<Alt> GetOtherAlts() => FetchAll().Where(a => a.MainId == MainId && a.AltId != AltId);

public Main GetMain() => Main.FetchAll().First(m => m.MainId == MainId);
public Main GetMain() => Main.FetchAll().First(m => m.AccountId == MainId);

public bool UpdateMain(string id)
{
Expand Down
2 changes: 1 addition & 1 deletion Bot/Sql/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await Program.TryUntilAsync(static async () =>

internal int Delete() => s_db.Delete(this);

internal int Update() => s_db.Update(this);
internal virtual int Update() => s_db.Update(this);

}

Expand Down
4 changes: 2 additions & 2 deletions Bot/Sql/Donation.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using SQLite;

namespace Hyperstellar.Sql;
public class Donation(long time, string cocId, int donated = 0, int received = 0) : DbObj<Donation>
public class Donation(long time, string accountId, int donated = 0, int received = 0) : DbObj<Donation>
{
[PrimaryKey, NotNull]
public long Time { get; set; } = time;

[PrimaryKey, NotNull]
public string CocId { get; set; } = cocId;
public string AccountId { get; set; } = accountId;

[NotNull]
public int Donated { get; set; } = donated;
Expand Down
4 changes: 2 additions & 2 deletions Bot/Sql/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Hyperstellar.Sql;
public sealed class Main(string id) : DbObj<Main>
{
[PrimaryKey, NotNull]
public string MainId { get; set; } = id;
public string AccountId { get; set; } = id;

[NotNull]
public uint Donated { get; set; }
Expand All @@ -20,7 +20,7 @@ public sealed class Main(string id) : DbObj<Main>

public Main() : this("") { }

internal static Main? TryFetch(string id) => FetchAll().FirstOrDefault(d => d.MainId == id);
internal static Main? TryFetch(string id) => FetchAll().FirstOrDefault(d => d.AccountId == id);

internal static Main? TryFetchByDiscord(ulong uid) => FetchAll().FirstOrDefault(m => m.Discord == uid);
}
62 changes: 62 additions & 0 deletions Bot/Sql/MainRequirement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using SQLite;

namespace Hyperstellar.Sql;

public sealed class MainRequirement(string mainId, long endTime) : DbObj<MainRequirement>
{
[PrimaryKey, NotNull]
public string MainId { get; set; } = mainId;

[PrimaryKey, NotNull]
public long EndTime { get; set; } = endTime;

[NotNull]
public Pass Passed { get; set; }

[NotNull]
public int Donated { get; set; }

[NotNull]
public int Raided { get; set; }

Check warning on line 20 in Bot/Sql/MainRequirement.cs

View workflow job for this annotation

GitHub Actions / inspect

"[UnusedAutoPropertyAccessor.Global] Auto-property accessor 'Raided.set' is never used" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/MainRequirement.cs(20,30)

public MainRequirement() : this("", 0) { }

internal override int Update()
{
List<MainRequirement>? result = s_db.Query<MainRequirement>("UPDATE MainRequirement SET Pass = ?, Donated = ?, Raided = ? WHERE MainId = ? AND EndTime = ?", Passed, Donated, Raided, MainId, EndTime);
return result.Count;
}

public static MainRequirement Fetch(string mainId, long endTime) => s_db.Table<MainRequirement>()
.First(req => req.MainId == mainId && req.EndTime == endTime);

public static MainRequirement FetchLatest(string mainId) => s_db.Table<MainRequirement>()
.Where(req => req.MainId == mainId)
.OrderByDescending(req => req.EndTime)
.First();

public static List<MainRequirement> FetchEachLatest() => s_db.Query<MainRequirement>(
"""
SELECT * FROM MainRequirement AS a
WHERE EndTime = (
SELECT MAX(EndTime) FROM MainRequirement AS b WHERE a.MainID = b.MainID
)
ORDER BY EndTime;
"""
);

/* Is it possible to use TableQuery thru out each filter? */
public static IEnumerable<MainRequirement> FetchLatest(IEnumerable<string> ids) => s_db.Table<MainRequirement>()
.OrderByDescending(req => req.EndTime)
.Where(req => ids.Contains(req.MainId))
.GroupBy(req => req.MainId)
.Select(grp => grp.First());
}

public enum Pass
{
Pending,
Passed,
Failed,
Overdue
}

0 comments on commit 788e58c

Please sign in to comment.