diff --git a/Tools/BiblioTech/Rewards/ChainEventsSender.cs b/Tools/BiblioTech/Rewards/ChainEventsSender.cs index 2e1e9ec..bbad804 100644 --- a/Tools/BiblioTech/Rewards/ChainEventsSender.cs +++ b/Tools/BiblioTech/Rewards/ChainEventsSender.cs @@ -21,11 +21,14 @@ namespace BiblioTech.Rewards { await Task.Run(async () => { + var users = Program.UserRepo.GetAllUserData(); + foreach (var e in eventsOverview) { if (!string.IsNullOrEmpty(e)) { - await eventsChannel.SendMessageAsync(e); + var @event = ApplyReplacements(users, e); + await eventsChannel.SendMessageAsync(@event); await Task.Delay(3000); } } @@ -36,5 +39,26 @@ namespace BiblioTech.Rewards log.Error("Failed to process chain events: " + ex); } } + + private string ApplyReplacements(UserData[] users, string msg) + { + var result = ApplyUserAddressReplacements(users, msg); + return result; + } + + private string ApplyUserAddressReplacements(UserData[] users, string msg) + { + foreach (var user in users) + { + if (user.CurrentAddress != null && + !string.IsNullOrEmpty(user.CurrentAddress.Address) && + !string.IsNullOrEmpty(user.Name)) + { + msg = msg.Replace(user.CurrentAddress.Address, user.Name); + } + } + + return msg; + } } } diff --git a/Tools/BiblioTech/UserData.cs b/Tools/BiblioTech/UserData.cs new file mode 100644 index 0000000..540c326 --- /dev/null +++ b/Tools/BiblioTech/UserData.cs @@ -0,0 +1,66 @@ +using CodexContractsPlugin; +using GethPlugin; + +namespace BiblioTech +{ + public class UserData + { + public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List associateEvents, List mintEvents, bool notificationsEnabled) + { + DiscordId = discordId; + Name = name; + CreatedUtc = createdUtc; + CurrentAddress = currentAddress; + AssociateEvents = associateEvents; + MintEvents = mintEvents; + NotificationsEnabled = notificationsEnabled; + } + + public ulong DiscordId { get; } + public string Name { get; } + public DateTime CreatedUtc { get; } + public EthAddress? CurrentAddress { get; set; } + public List AssociateEvents { get; } + public List MintEvents { get; } + public bool NotificationsEnabled { get; set; } + + public string[] CreateOverview() + { + return new[] + { + $"name: '{Name}' - id:{DiscordId}", + $"joined: {CreatedUtc.ToString("o")}", + $"current address: {CurrentAddress}", + $"{AssociateEvents.Count + MintEvents.Count} total bot events." + }; + } + } + + public class UserAssociateAddressEvent + { + public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress) + { + Utc = utc; + NewAddress = newAddress; + } + + public DateTime Utc { get; } + public EthAddress? NewAddress { get; } + } + + public class UserMintEvent + { + public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction? ethReceived, Transaction? testTokensMinted) + { + Utc = utc; + UsedAddress = usedAddress; + EthReceived = ethReceived; + TestTokensMinted = testTokensMinted; + } + + public DateTime Utc { get; } + public EthAddress UsedAddress { get; } + public Transaction? EthReceived { get; } + public Transaction? TestTokensMinted { get; } + } +} diff --git a/Tools/BiblioTech/UserRepo.cs b/Tools/BiblioTech/UserRepo.cs index 01afbe9..7cd03fd 100644 --- a/Tools/BiblioTech/UserRepo.cs +++ b/Tools/BiblioTech/UserRepo.cs @@ -8,6 +8,7 @@ namespace BiblioTech public class UserRepo { private readonly object repoLock = new object(); + private readonly Dictionary cache = new Dictionary(); public bool AssociateUserWithAddress(IUser user, EthAddress address) { @@ -33,6 +34,12 @@ namespace BiblioTech } } + public UserData[] GetAllUserData() + { + if (cache.Count == 0) LoadAllUserData(); + return cache.Values.ToArray(); + } + public void AddMintEventForUser(IUser user, EthAddress usedAddress, Transaction? eth, Transaction? tokens) { lock (repoLock) @@ -151,12 +158,19 @@ namespace BiblioTech private UserData? GetUserData(IUser user) { + if (cache.ContainsKey(user.Id)) + { + return cache[user.Id]; + } + var filename = GetFilename(user); if (!File.Exists(filename)) { return null; } - return JsonConvert.DeserializeObject(File.ReadAllText(filename))!; + var userData = JsonConvert.DeserializeObject(File.ReadAllText(filename))!; + cache.Add(userData.DiscordId, userData); + return userData; } private UserData GetOrCreate(IUser user) @@ -181,6 +195,15 @@ namespace BiblioTech var filename = GetFilename(userData); if (File.Exists(filename)) File.Delete(filename); File.WriteAllText(filename, JsonConvert.SerializeObject(userData)); + + if (cache.ContainsKey(userData.DiscordId)) + { + cache[userData.DiscordId] = userData; + } + else + { + cache.Add(userData.DiscordId, userData); + } } private static string GetFilename(IUser user) @@ -197,66 +220,29 @@ namespace BiblioTech { return Path.Combine(Program.Config.UserDataPath, discordId.ToString() + ".json"); } - } - public class UserData - { - public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List associateEvents, List mintEvents, bool notificationsEnabled) + private void LoadAllUserData() { - DiscordId = discordId; - Name = name; - CreatedUtc = createdUtc; - CurrentAddress = currentAddress; - AssociateEvents = associateEvents; - MintEvents = mintEvents; - NotificationsEnabled = notificationsEnabled; - } - - public ulong DiscordId { get; } - public string Name { get; } - public DateTime CreatedUtc { get; } - public EthAddress? CurrentAddress { get; set; } - public List AssociateEvents { get; } - public List MintEvents { get; } - public bool NotificationsEnabled { get; set; } - - public string[] CreateOverview() - { - return new[] + try { - $"name: '{Name}' - id:{DiscordId}", - $"joined: {CreatedUtc.ToString("o")}", - $"current address: {CurrentAddress}", - $"{AssociateEvents.Count + MintEvents.Count} total bot events." - }; + var files = Directory.GetFiles(Program.Config.UserDataPath); + foreach (var file in files) + { + try + { + var userData = JsonConvert.DeserializeObject(File.ReadAllText(file))!; + if (userData != null && userData.DiscordId > 0) + { + cache.Add(userData.DiscordId, userData); + } + } + catch { } + } + } + catch (Exception ex) + { + Program.Log.Error("Exception while trying to load all user data: " + ex); + } } } - - public class UserAssociateAddressEvent - { - public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress) - { - Utc = utc; - NewAddress = newAddress; - } - - public DateTime Utc { get; } - public EthAddress? NewAddress { get; } - } - - public class UserMintEvent - { - public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction? ethReceived, Transaction? testTokensMinted) - { - Utc = utc; - UsedAddress = usedAddress; - EthReceived = ethReceived; - TestTokensMinted = testTokensMinted; - } - - public DateTime Utc { get; } - public EthAddress UsedAddress { get; } - public Transaction? EthReceived { get; } - public Transaction? TestTokensMinted { get; } - } }