@@ -8,6 +8,7 @@ namespace BiblioTech
8
8
public class UserRepo
9
9
{
10
10
private readonly object repoLock = new object ( ) ;
11
+ private readonly Dictionary < ulong , UserData > cache = new Dictionary < ulong , UserData > ( ) ;
11
12
12
13
public bool AssociateUserWithAddress ( IUser user , EthAddress address )
13
14
{
@@ -33,6 +34,12 @@ public void SetUserNotificationPreference(IUser user, bool enableNotifications)
33
34
}
34
35
}
35
36
37
+ public UserData [ ] GetAllUserData ( )
38
+ {
39
+ if ( cache . Count == 0 ) LoadAllUserData ( ) ;
40
+ return cache . Values . ToArray ( ) ;
41
+ }
42
+
36
43
public void AddMintEventForUser ( IUser user , EthAddress usedAddress , Transaction < Ether > ? eth , Transaction < TestToken > ? tokens )
37
44
{
38
45
lock ( repoLock )
@@ -151,12 +158,19 @@ private void SetUserNotification(IUser user, bool notifyEnabled)
151
158
152
159
private UserData ? GetUserData ( IUser user )
153
160
{
161
+ if ( cache . ContainsKey ( user . Id ) )
162
+ {
163
+ return cache [ user . Id ] ;
164
+ }
165
+
154
166
var filename = GetFilename ( user ) ;
155
167
if ( ! File . Exists ( filename ) )
156
168
{
157
169
return null ;
158
170
}
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 ;
160
174
}
161
175
162
176
private UserData GetOrCreate ( IUser user )
@@ -181,6 +195,15 @@ private void SaveUserData(UserData userData)
181
195
var filename = GetFilename ( userData ) ;
182
196
if ( File . Exists ( filename ) ) File . Delete ( filename ) ;
183
197
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
+ }
184
207
}
185
208
186
209
private static string GetFilename ( IUser user )
@@ -197,66 +220,29 @@ private static string GetFilename(ulong discordId)
197
220
{
198
221
return Path . Combine ( Program . Config . UserDataPath , discordId . ToString ( ) + ".json" ) ;
199
222
}
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 ; }
222
223
223
- public string [ ] CreateOverview ( )
224
+ private void LoadAllUserData ( )
224
225
{
225
- return new [ ]
226
+ try
226
227
{
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
+ }
255
246
}
256
-
257
- public DateTime Utc { get ; }
258
- public EthAddress UsedAddress { get ; }
259
- public Transaction < Ether > ? EthReceived { get ; }
260
- public Transaction < TestToken > ? TestTokensMinted { get ; }
261
247
}
262
248
}
0 commit comments