Skip to content

Commit 01ee514

Browse files
committed
Implements known username replacement
1 parent 1eb3032 commit 01ee514

File tree

3 files changed

+135
-59
lines changed

3 files changed

+135
-59
lines changed

Tools/BiblioTech/Rewards/ChainEventsSender.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ public async Task ProcessChainEvents(string[] eventsOverview)
2121
{
2222
await Task.Run(async () =>
2323
{
24+
var users = Program.UserRepo.GetAllUserData();
25+
2426
foreach (var e in eventsOverview)
2527
{
2628
if (!string.IsNullOrEmpty(e))
2729
{
28-
await eventsChannel.SendMessageAsync(e);
30+
var @event = ApplyReplacements(users, e);
31+
await eventsChannel.SendMessageAsync(@event);
2932
await Task.Delay(3000);
3033
}
3134
}
@@ -36,5 +39,26 @@ await Task.Run(async () =>
3639
log.Error("Failed to process chain events: " + ex);
3740
}
3841
}
42+
43+
private string ApplyReplacements(UserData[] users, string msg)
44+
{
45+
var result = ApplyUserAddressReplacements(users, msg);
46+
return result;
47+
}
48+
49+
private string ApplyUserAddressReplacements(UserData[] users, string msg)
50+
{
51+
foreach (var user in users)
52+
{
53+
if (user.CurrentAddress != null &&
54+
!string.IsNullOrEmpty(user.CurrentAddress.Address) &&
55+
!string.IsNullOrEmpty(user.Name))
56+
{
57+
msg = msg.Replace(user.CurrentAddress.Address, user.Name);
58+
}
59+
}
60+
61+
return msg;
62+
}
3963
}
4064
}

Tools/BiblioTech/UserData.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using CodexContractsPlugin;
2+
using GethPlugin;
3+
4+
namespace BiblioTech
5+
{
6+
public class UserData
7+
{
8+
public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List<UserAssociateAddressEvent> associateEvents, List<UserMintEvent> mintEvents, bool notificationsEnabled)
9+
{
10+
DiscordId = discordId;
11+
Name = name;
12+
CreatedUtc = createdUtc;
13+
CurrentAddress = currentAddress;
14+
AssociateEvents = associateEvents;
15+
MintEvents = mintEvents;
16+
NotificationsEnabled = notificationsEnabled;
17+
}
18+
19+
public ulong DiscordId { get; }
20+
public string Name { get; }
21+
public DateTime CreatedUtc { get; }
22+
public EthAddress? CurrentAddress { get; set; }
23+
public List<UserAssociateAddressEvent> AssociateEvents { get; }
24+
public List<UserMintEvent> MintEvents { get; }
25+
public bool NotificationsEnabled { get; set; }
26+
27+
public string[] CreateOverview()
28+
{
29+
return new[]
30+
{
31+
$"name: '{Name}' - id:{DiscordId}",
32+
$"joined: {CreatedUtc.ToString("o")}",
33+
$"current address: {CurrentAddress}",
34+
$"{AssociateEvents.Count + MintEvents.Count} total bot events."
35+
};
36+
}
37+
}
38+
39+
public class UserAssociateAddressEvent
40+
{
41+
public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress)
42+
{
43+
Utc = utc;
44+
NewAddress = newAddress;
45+
}
46+
47+
public DateTime Utc { get; }
48+
public EthAddress? NewAddress { get; }
49+
}
50+
51+
public class UserMintEvent
52+
{
53+
public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction<Ether>? ethReceived, Transaction<TestToken>? testTokensMinted)
54+
{
55+
Utc = utc;
56+
UsedAddress = usedAddress;
57+
EthReceived = ethReceived;
58+
TestTokensMinted = testTokensMinted;
59+
}
60+
61+
public DateTime Utc { get; }
62+
public EthAddress UsedAddress { get; }
63+
public Transaction<Ether>? EthReceived { get; }
64+
public Transaction<TestToken>? TestTokensMinted { get; }
65+
}
66+
}

Tools/BiblioTech/UserRepo.cs

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace BiblioTech
88
public class UserRepo
99
{
1010
private readonly object repoLock = new object();
11+
private readonly Dictionary<ulong, UserData> cache = new Dictionary<ulong, UserData>();
1112

1213
public bool AssociateUserWithAddress(IUser user, EthAddress address)
1314
{
@@ -33,6 +34,12 @@ public void SetUserNotificationPreference(IUser user, bool enableNotifications)
3334
}
3435
}
3536

37+
public UserData[] GetAllUserData()
38+
{
39+
if (cache.Count == 0) LoadAllUserData();
40+
return cache.Values.ToArray();
41+
}
42+
3643
public void AddMintEventForUser(IUser user, EthAddress usedAddress, Transaction<Ether>? eth, Transaction<TestToken>? tokens)
3744
{
3845
lock (repoLock)
@@ -151,12 +158,19 @@ private void SetUserNotification(IUser user, bool notifyEnabled)
151158

152159
private UserData? GetUserData(IUser user)
153160
{
161+
if (cache.ContainsKey(user.Id))
162+
{
163+
return cache[user.Id];
164+
}
165+
154166
var filename = GetFilename(user);
155167
if (!File.Exists(filename))
156168
{
157169
return null;
158170
}
159-
return JsonConvert.DeserializeObject<UserData>(File.ReadAllText(filename))!;
171+
var userData = JsonConvert.DeserializeObject<UserData>(File.ReadAllText(filename))!;
172+
cache.Add(userData.DiscordId, userData);
173+
return userData;
160174
}
161175

162176
private UserData GetOrCreate(IUser user)
@@ -181,6 +195,15 @@ private void SaveUserData(UserData userData)
181195
var filename = GetFilename(userData);
182196
if (File.Exists(filename)) File.Delete(filename);
183197
File.WriteAllText(filename, JsonConvert.SerializeObject(userData));
198+
199+
if (cache.ContainsKey(userData.DiscordId))
200+
{
201+
cache[userData.DiscordId] = userData;
202+
}
203+
else
204+
{
205+
cache.Add(userData.DiscordId, userData);
206+
}
184207
}
185208

186209
private static string GetFilename(IUser user)
@@ -197,66 +220,29 @@ private static string GetFilename(ulong discordId)
197220
{
198221
return Path.Combine(Program.Config.UserDataPath, discordId.ToString() + ".json");
199222
}
200-
}
201-
202-
public class UserData
203-
{
204-
public UserData(ulong discordId, string name, DateTime createdUtc, EthAddress? currentAddress, List<UserAssociateAddressEvent> associateEvents, List<UserMintEvent> mintEvents, bool notificationsEnabled)
205-
{
206-
DiscordId = discordId;
207-
Name = name;
208-
CreatedUtc = createdUtc;
209-
CurrentAddress = currentAddress;
210-
AssociateEvents = associateEvents;
211-
MintEvents = mintEvents;
212-
NotificationsEnabled = notificationsEnabled;
213-
}
214-
215-
public ulong DiscordId { get; }
216-
public string Name { get; }
217-
public DateTime CreatedUtc { get; }
218-
public EthAddress? CurrentAddress { get; set; }
219-
public List<UserAssociateAddressEvent> AssociateEvents { get; }
220-
public List<UserMintEvent> MintEvents { get; }
221-
public bool NotificationsEnabled { get; set; }
222223

223-
public string[] CreateOverview()
224+
private void LoadAllUserData()
224225
{
225-
return new[]
226+
try
226227
{
227-
$"name: '{Name}' - id:{DiscordId}",
228-
$"joined: {CreatedUtc.ToString("o")}",
229-
$"current address: {CurrentAddress}",
230-
$"{AssociateEvents.Count + MintEvents.Count} total bot events."
231-
};
232-
}
233-
}
234-
235-
public class UserAssociateAddressEvent
236-
{
237-
public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress)
238-
{
239-
Utc = utc;
240-
NewAddress = newAddress;
241-
}
242-
243-
public DateTime Utc { get; }
244-
public EthAddress? NewAddress { get; }
245-
}
246-
247-
public class UserMintEvent
248-
{
249-
public UserMintEvent(DateTime utc, EthAddress usedAddress, Transaction<Ether>? ethReceived, Transaction<TestToken>? testTokensMinted)
250-
{
251-
Utc = utc;
252-
UsedAddress = usedAddress;
253-
EthReceived = ethReceived;
254-
TestTokensMinted = testTokensMinted;
228+
var files = Directory.GetFiles(Program.Config.UserDataPath);
229+
foreach (var file in files)
230+
{
231+
try
232+
{
233+
var userData = JsonConvert.DeserializeObject<UserData>(File.ReadAllText(file))!;
234+
if (userData != null && userData.DiscordId > 0)
235+
{
236+
cache.Add(userData.DiscordId, userData);
237+
}
238+
}
239+
catch { }
240+
}
241+
}
242+
catch (Exception ex)
243+
{
244+
Program.Log.Error("Exception while trying to load all user data: " + ex);
245+
}
255246
}
256-
257-
public DateTime Utc { get; }
258-
public EthAddress UsedAddress { get; }
259-
public Transaction<Ether>? EthReceived { get; }
260-
public Transaction<TestToken>? TestTokensMinted { get; }
261247
}
262248
}

0 commit comments

Comments
 (0)