forked from LittleBigRefresh/Refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDatabaseContext.Photos.cs
More file actions
255 lines (214 loc) · 9.4 KB
/
Copy pathGameDatabaseContext.Photos.cs
File metadata and controls
255 lines (214 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using JetBrains.Annotations;
using Refresh.Database.Models.Activity;
using Refresh.Database.Models.Users;
using Refresh.Database.Models.Levels;
using Refresh.Database.Models.Photos;
using Refresh.Database.Query;
using Refresh.Database.Helpers;
using Bunkum.Core;
namespace Refresh.Database;
public partial class GameDatabaseContext // Photos
{
private IQueryable<GamePhoto> GamePhotosIncluded => this.GamePhotos
.Include(p => p.LargeAsset)
.Include(p => p.MediumAsset)
.Include(p => p.SmallAsset)
.Include(p => p.Publisher)
.Include(p => p.Publisher.Statistics)
.Include(p => p.Level)
.Include(p => p.Level!.Publisher)
.Include(p => p.Level!.Publisher!.Statistics)
.Include(p => p.Subjects.OrderBy(s => s.PlayerId));
private IQueryable<GamePhotoSubject> GamePhotoSubjectsIncluded => this.GamePhotoSubjects
.Include(p => p.User)
.Include(p => p.User!.Statistics)
.Include(p => p.Photo)
.Include(p => p.Photo!.Publisher)
.Include(p => p.Photo!.Publisher.Statistics)
.Include(p => p.Photo!.Level)
.Include(p => p.Photo!.Level!.Publisher)
.Include(p => p.Photo!.Level!.Publisher!.Statistics)
.Include(p => p.Photo!.LargeAsset)
.Include(p => p.Photo!.MediumAsset)
.Include(p => p.Photo!.SmallAsset)
.Include(p => p.Photo!.Subjects.OrderBy(s => s.PlayerId));
public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject> subjects, GameUser publisher, GameLevel? level)
{
GamePhoto newPhoto = new()
{
SmallAsset = this.GetAssetFromHash(photo.SmallHash) ?? throw new Exception($"Small asset {photo.SmallHash} is missing!"),
MediumAsset = this.GetAssetFromHash(photo.MediumHash) ?? throw new Exception($"Medium asset {photo.MediumHash} is missing!"),
LargeAsset = this.GetAssetFromHash(photo.LargeHash) ?? throw new Exception($"Large asset {photo.LargeHash} is missing!"),
PlanHash = photo.PlanHash,
Publisher = publisher,
Level = level,
LevelType = photo.LevelType ?? "",
OriginalLevelId = photo.LevelId ?? 0,
OriginalLevelName = photo.LevelTitle ?? "",
TakenAt = DateTimeOffset.FromUnixTimeSeconds(Math.Clamp(photo.Timestamp, this._time.EarliestDate, this._time.TimestampSeconds)),
PublishedAt = this._time.Now,
};
this.WriteEnsuringStatistics(publisher, () =>
{
this.GamePhotos.Add(newPhoto);
publisher.Statistics!.PhotosByUserCount++;
});
if (level != null)
{
this.WriteEnsuringStatistics(publisher, level, () =>
{
level.Statistics!.PhotoInLevelCount++;
if (level.Publisher?.UserId == publisher.UserId)
{
level.Statistics!.PhotoByPublisherCount++;
}
});
}
List<IPhotoUploadSubject> subjectsList = subjects.ToList();
List<GamePhotoSubject> finalSubjectsList = [];
// Take care of subjects after saving the photo itself to keep the photo, even if its subjects are malformed
for (int i = 0; i < subjectsList.Count; i++)
{
IPhotoUploadSubject subject = subjectsList[i];
float[] bounds = new float[PhotoHelper.SubjectBoundaryCount];
try
{
bounds = PhotoHelper.ParseBoundsList(subject.BoundsList);
}
catch (Exception ex)
{
this._logger.LogWarning(BunkumCategory.UserPhotos, $"Could not parse {subject.DisplayName}'s photo bounds: {ex.GetType()} - {ex.Message}");
}
GameUser? subjectUser = string.IsNullOrWhiteSpace(subject.Username) ? null : this.GetUserByUsername(subject.Username);
finalSubjectsList.Add(new()
{
Photo = newPhoto,
User = subjectUser,
DisplayName = subject.DisplayName,
PlayerId = i + 1, // Player number 1 - 4
Bounds = bounds
});
if (subjectUser != null)
{
this.WriteEnsuringStatistics(subjectUser, () =>
{
subjectUser.Statistics!.PhotosWithUserCount++;
});
}
}
this.GamePhotoSubjects.AddRange(finalSubjectsList);
this.SaveChanges();
this.CreatePhotoUploadEvent(publisher, newPhoto);
newPhoto.Subjects = finalSubjectsList.ToList();
return newPhoto;
}
public GamePhotoSubject AddSubjectForPhoto(GamePhoto photo, int playerId, string displayName, GameUser? user, List<float> bounds, bool save = true)
{
GamePhotoSubject subject = new()
{
Photo = photo,
PlayerId = playerId,
DisplayName = displayName,
User = user,
Bounds = bounds,
};
this.GamePhotoSubjects.Add(subject);
if (save) this.SaveChanges();
return subject;
}
public void RemovePhoto(GamePhoto photo)
{
foreach (GameUser subjectUser in this.GetUsersInPhoto(photo).ToArray())
{
this.WriteEnsuringStatistics(subjectUser, () =>
{
subjectUser.Statistics!.PhotosWithUserCount--;
});
}
if (photo.Level != null)
{
this.WriteEnsuringStatistics(photo.Publisher, photo.Level, () =>
{
photo.Level.Statistics!.PhotoInLevelCount--;
if (photo.Level.Publisher?.UserId == photo.PublisherId)
{
photo.Level.Statistics!.PhotoByPublisherCount--;
}
});
}
this.WriteEnsuringStatistics(photo.Publisher, () =>
{
IQueryable<Event> photoEvents = this.Events
.Where(e => e.StoredDataType == EventDataType.Photo && e.StoredSequentialId == photo.PhotoId);
// Remove all events referencing the photo
this.Events.RemoveRange(photoEvents);
// Remove all subjects
this.GamePhotoSubjects.RemoveRange(s => s.PhotoId == photo.PhotoId);
// Remove the photo
this.GamePhotos.Remove(photo);
photo.Publisher.Statistics!.PhotosByUserCount--;
});
}
public IQueryable<GamePhotoSubject> GetSubjectsInPhoto(GamePhoto photo)
=> this.GamePhotoSubjectsIncluded
.Where(s => s.PhotoId == photo.PhotoId)
.OrderBy(s => s.PlayerId);
public int GetTotalSubjectsInPhoto(GamePhoto photo)
=> this.GamePhotoSubjects.Count(s => s.PhotoId == photo.PhotoId);
public IQueryable<GameUser> GetUsersInPhoto(GamePhoto photo)
=> this.GetSubjectsInPhoto(photo)
.Where(s => s.User != null)
.OrderBy(s => s.PlayerId)
.Select(s => s.User!);
public int GetTotalPhotoCount() => this.GamePhotos.Count();
[Pure]
public DatabaseList<GamePhoto> GetRecentPhotos(int count, int skip) =>
new(this.GamePhotosIncluded
.OrderByDescending(p => p.PublishedAt), skip, count);
[Pure]
public GamePhoto? GetPhotoById(int id) =>
this.GamePhotosIncluded.FirstOrDefault(p => p.PhotoId == id);
[Pure]
public DatabaseList<GamePhoto> GetPhotosByUser(GameUser user, int count, int skip) =>
new(this.GamePhotosIncluded.Where(p => p.PublisherId == user.UserId)
.OrderByDescending(p => p.TakenAt), skip, count);
[Pure]
public int GetTotalPhotosByUser(GameUser user)
=> this.GamePhotos
.Count(p => p.PublisherId == user.UserId);
[Pure]
public DatabaseList<GamePhoto> GetPhotosWithUser(GameUser user, int count, int skip) =>
new(this.GamePhotoSubjectsIncluded
.Where(s => s.UserId == user.UserId)
.Select(s => s.Photo)
.OrderByDescending(p => p.TakenAt), skip, count);
[Pure]
public int GetTotalPhotosWithUser(GameUser user)
=> this.GamePhotoSubjects.Count(s => s.UserId == user.UserId);
[Pure]
public DatabaseList<GamePhoto> GetPhotosInLevel(GameLevel level, int count, int skip)
=> new(this.GamePhotosIncluded
.Where(p => p.LevelId == level.LevelId)
.OrderByDescending(p => p.TakenAt), skip, count);
[Pure]
public int GetTotalPhotosInLevel(GameLevel level)
=> this.GamePhotos.Count(p => p.LevelId == level.LevelId);
public DatabaseList<GamePhoto> GetPhotosInLevelByUser(GameLevel level, GameUser user, int count, int skip)
=> new(this.GamePhotosIncluded
.Where(p => p.LevelId == level.LevelId && p.PublisherId == user.UserId)
.OrderByDescending(p => p.TakenAt), skip, count);
public int GetTotalPhotosInLevelByUser(GameLevel level, GameUser? user)
{
if (user == null) return 0;
return this.GamePhotos.Count(p => p.LevelId == level.LevelId && p.PublisherId == user.UserId);
}
public void DeletePhotosPostedByUser(GameUser user)
{
IEnumerable<GamePhoto> photos = this.GamePhotos.Where(s => s.PublisherId == user.UserId).ToArray();
foreach (GamePhoto photo in photos)
{
// RemovePhoto already takes care of decrementing (effectively resetting) the publisher's photo count
this.RemovePhoto(photo);
}
}
}