Skip to content

Commit f697beb

Browse files
authored
Merge branch 'main' into email-domain
2 parents 68e23bb + 4060ec4 commit f697beb

26 files changed

Lines changed: 892 additions & 172 deletions

Refresh.Database/GameDatabaseContext.Levels.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public GameLevel AddLevel(ISerializedPublishLevel createInfo, TokenGame game, Ga
5252
EnforceMinMaxPlayers = createInfo.EnforceMinMaxPlayers,
5353
SameScreenGame = createInfo.SameScreenGame,
5454
BackgroundGuid = createInfo.BackgroundGuid,
55-
Publisher = publisher,
55+
PublisherUserId = publisher.UserId,
5656
GameVersion = game,
5757
PublishDate = timestamp,
5858
UpdateDate = timestamp,
@@ -63,22 +63,19 @@ public GameLevel AddLevel(ISerializedPublishLevel createInfo, TokenGame game, Ga
6363

6464
this.SaveChanges();
6565

66-
this.CreateRevisionForLevel(level, level.Publisher);
67-
this.GameLevelStatistics.Add(level.Statistics = new GameLevelStatistics
68-
{
69-
LevelId = level.LevelId,
70-
});
71-
72-
this.SaveChanges();
73-
74-
if (level.Publisher != null)
66+
this.WriteEnsuringStatistics(publisher, () =>
7567
{
76-
this.WriteEnsuringStatistics(level.Publisher, () =>
68+
this.GameLevelStatistics.Add(level.Statistics = new GameLevelStatistics
7769
{
78-
level.Publisher.Statistics!.LevelCount++;
70+
LevelId = level.LevelId,
7971
});
80-
}
8172

73+
this.CreateRevisionForLevel(level, level.Publisher);
74+
publisher.Statistics!.LevelCount++;
75+
});
76+
77+
level.Publisher = publisher;
78+
this.Entry(level.Publisher).State = EntityState.Unchanged;
8279
return level;
8380
}
8481

Refresh.Database/GameDatabaseContext.Photos.cs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -119,67 +119,20 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
119119
return newPhoto;
120120
}
121121

122-
/// <remarks>
123-
/// Migration only!!
124-
/// </remarks>
125-
public void MigratePhotoSubjects(GamePhoto photo, bool saveChanges)
122+
public GamePhotoSubject AddSubjectForPhoto(GamePhoto photo, int playerId, string displayName, GameUser? user, List<float> bounds, bool save = true)
126123
{
127-
List<GamePhotoSubject> subjects = [];
128-
129-
#pragma warning disable CS0618 // obsoletion
130-
131-
// If DisplayName is not null, there is a subject in that spot
132-
if (photo.Subject1DisplayName != null)
133-
{
134-
subjects.Add(new()
135-
{
136-
Photo = photo,
137-
PlayerId = 1,
138-
DisplayName = photo.Subject1DisplayName,
139-
User = photo.Subject1User,
140-
Bounds = photo.Subject1Bounds,
141-
});
142-
}
143-
144-
if (photo.Subject2DisplayName != null)
145-
{
146-
subjects.Add(new()
147-
{
148-
Photo = photo,
149-
PlayerId = 2,
150-
DisplayName = photo.Subject2DisplayName,
151-
User = photo.Subject2User,
152-
Bounds = photo.Subject2Bounds,
153-
});
154-
}
155-
156-
if (photo.Subject3DisplayName != null)
157-
{
158-
subjects.Add(new()
159-
{
160-
Photo = photo,
161-
PlayerId = 3,
162-
DisplayName = photo.Subject3DisplayName,
163-
User = photo.Subject3User,
164-
Bounds = photo.Subject3Bounds,
165-
});
166-
}
167-
168-
if (photo.Subject4DisplayName != null)
124+
GamePhotoSubject subject = new()
169125
{
170-
subjects.Add(new()
171-
{
172-
Photo = photo,
173-
PlayerId = 4,
174-
DisplayName = photo.Subject4DisplayName,
175-
User = photo.Subject4User,
176-
Bounds = photo.Subject4Bounds,
177-
});
178-
}
179-
#pragma warning restore CS0618
126+
Photo = photo,
127+
PlayerId = playerId,
128+
DisplayName = displayName,
129+
User = user,
130+
Bounds = bounds,
131+
};
180132

181-
this.GamePhotoSubjects.AddRange(subjects);
182-
if (saveChanges) this.SaveChanges();
133+
this.GamePhotoSubjects.Add(subject);
134+
if (save) this.SaveChanges();
135+
return subject;
183136
}
184137

185138
public void RemovePhoto(GamePhoto photo)
@@ -227,6 +180,9 @@ public IQueryable<GamePhotoSubject> GetSubjectsInPhoto(GamePhoto photo)
227180
.Where(s => s.PhotoId == photo.PhotoId)
228181
.OrderBy(s => s.PlayerId);
229182

183+
public int GetTotalSubjectsInPhoto(GamePhoto photo)
184+
=> this.GamePhotoSubjects.Count(s => s.PhotoId == photo.PhotoId);
185+
230186
public IQueryable<GameUser> GetUsersInPhoto(GamePhoto photo)
231187
=> this.GetSubjectsInPhoto(photo)
232188
.Where(s => s.User != null)

Refresh.Database/GameDatabaseContext.Pins.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ private IEnumerable<PinProgressRelation> GetPinProgressesByUser(GameUser user, b
169169
public DatabaseList<PinProgressRelation> GetPinProgressesByUser(GameUser user, bool isBeta, TokenPlatform platform, int skip, int count)
170170
=> new(this.GetPinProgressesByUser(user, isBeta, platform), skip, count);
171171

172+
public IQueryable<PinProgressRelation> GetAllPinProgressesByUserAndId(GameUser user, long pinId)
173+
=> this.PinProgressRelations.Where(p => p.PublisherId == user.UserId && p.PinId == pinId);
174+
172175
public PinProgressRelation? GetUserPinProgress(long pinId, GameUser user, bool isBeta, TokenPlatform platform)
173176
=> this.PinProgressRelations.FirstOrDefault(p => p.PinId == pinId && p.PublisherId == user.UserId
174177
&& (p.IsBeta == isBeta && p.Platform == platform || p.Platform == TokenPlatform.Website));
@@ -180,20 +183,19 @@ private IEnumerable<ProfilePinRelation> GetProfilePinsByUser(GameUser user, Toke
180183

181184
public DatabaseList<ProfilePinRelation> GetProfilePinsByUser(GameUser user, TokenGame game, TokenPlatform platform, int skip, int count)
182185
=> new(this.GetProfilePinsByUser(user, game, platform), skip, count);
183-
184-
#region Migration Methods
185186

186187
public void AddPinProgress(PinProgressRelation relation, bool save)
187188
{
188189
this.PinProgressRelations.Add(relation);
189190
if (save) this.SaveChanges();
190191
}
191192

192-
public void RemoveAllPinProgressesByIdAndUser(long pinId, ObjectId userId, bool save)
193+
public void RemovePinProgress(PinProgressRelation relation, bool save)
193194
{
194-
this.PinProgressRelations.RemoveRange(p => p.PinId == pinId && p.PublisherId == userId);
195+
this.PinProgressRelations.Remove(relation);
195196
if (save) this.SaveChanges();
196197
}
197198

198-
#endregion
199+
public int GetTotalPinProgresses()
200+
=> this.PinProgressRelations.Count();
199201
}

Refresh.Database/GameDatabaseContext.Relations.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -477,20 +477,6 @@ public GameReview UpdateReview(ISubmitReviewRequest updateInfo, GameReview revie
477477
this.SaveChanges();
478478
return review;
479479
}
480-
481-
public void MigrateReviewLabels(IEnumerable<GameReview> reviews)
482-
{
483-
foreach (GameReview review in reviews)
484-
{
485-
#pragma warning disable CS0618 // LabelsString is obsolete
486-
if (string.IsNullOrWhiteSpace(review.LabelsString)) continue;
487-
488-
review.Labels = LabelExtensions.FromLbpCommaList(review.LabelsString).ToList();
489-
#pragma warning restore CS0618
490-
}
491-
492-
this.SaveChanges();
493-
}
494480

495481
public void DeleteReviewsPostedByUser(GameUser user)
496482
{

Refresh.Database/GameDatabaseContext.Workers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public bool MarkWorkerContacted(int id)
5050
return JsonConvert.DeserializeObject(state.State, type);
5151
}
5252

53+
public IQueryable<string> GetAllJobIds(WorkerClass? workerClass)
54+
{
55+
IQueryable<PersistentJobState> states = this.JobStates;
56+
if (workerClass != null) states = states.Where(s => s.Class == workerClass);
57+
return states.Select(s => s.JobId);
58+
}
59+
5360
public void UpdateOrCreateJobState(string jobId, object state, WorkerClass workerClass)
5461
{
5562
PersistentJobState? jobState = this.JobStates.FirstOrDefault(s => s.JobId == jobId && s.Class == workerClass);
@@ -58,6 +65,7 @@ public void UpdateOrCreateJobState(string jobId, object state, WorkerClass worke
5865
jobState = new PersistentJobState
5966
{
6067
JobId = jobId,
68+
Class = workerClass,
6169
};
6270

6371
this.JobStates.Add(jobState);
@@ -66,4 +74,10 @@ public void UpdateOrCreateJobState(string jobId, object state, WorkerClass worke
6674
jobState.State = JsonConvert.SerializeObject(state, Formatting.None);
6775
this.SaveChanges();
6876
}
77+
78+
public void RemoveJobState(string jobId, bool save = true)
79+
{
80+
this.JobStates.RemoveRange(s => s.JobId == jobId);
81+
if (save) this.SaveChanges();
82+
}
6983
}

Refresh.Interfaces.Workers/Migrations/BackfillLevelAttributesMigration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> que
1212
.OrderBy(l => l.LevelId);
1313
}
1414

15-
protected override void Migrate(WorkContext context, GameLevel[] batch)
15+
protected override int Migrate(WorkContext context, GameLevel[] batch)
1616
{
1717
foreach (GameLevel level in batch)
1818
{
1919
context.Database.ApplyLevelMetadataFromAttributes(level);
2020
}
2121

2222
context.Database.SaveChanges();
23+
return batch.Length;
2324
}
2425
}

Refresh.Interfaces.Workers/Migrations/BackfillModdedPlanetFlagsMigration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ protected override IQueryable<GameUser> SortAndFilter(IQueryable<GameUser> query
1616
|| u.BetaPlanetsHash != "0");
1717
}
1818

19-
protected override void Migrate(WorkContext context, GameUser[] batch)
19+
protected override int Migrate(WorkContext context, GameUser[] batch)
2020
{
2121
foreach (GameUser user in batch)
2222
{
2323
context.Database.UpdatePlanetModdedStatus(user);
2424
}
2525

2626
context.Database.SaveChanges();
27+
return batch.Length;
2728
}
2829
}

Refresh.Interfaces.Workers/Migrations/BackfillReviewLabelsMigration.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
using Refresh.Database.Models.Comments;
1+
using Refresh.Database.Models.Comments;
2+
using Refresh.Database.Models.Levels;
23
using Refresh.Workers;
34

45
namespace Refresh.Interfaces.Workers.Migrations;
56

67
public class BackfillReviewLabelsMigration : MigrationJob<GameReview>
78
{
8-
protected override void Migrate(WorkContext context, GameReview[] batch)
9+
protected override int Migrate(WorkContext context, GameReview[] batch)
910
{
10-
context.Database.MigrateReviewLabels(batch);
11+
foreach (GameReview review in batch)
12+
{
13+
#pragma warning disable CS0618 // LabelsString is obsolete
14+
if (string.IsNullOrWhiteSpace(review.LabelsString)) continue;
15+
16+
context.Database.Update(review);
17+
review.Labels = LabelExtensions.FromLbpCommaList(review.LabelsString).ToList();
18+
#pragma warning restore CS0618
19+
}
20+
21+
context.Database.SaveChanges();
22+
return batch.Length;
1123
}
1224

1325
protected override IQueryable<GameReview> SortAndFilter(IQueryable<GameReview> query)

Refresh.Interfaces.Workers/Migrations/BackfillRevisionMigration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ namespace Refresh.Interfaces.Workers.Migrations;
55

66
public class BackfillRevisionMigration : MigrationJob<GameLevel>
77
{
8-
protected override void Migrate(WorkContext context, GameLevel[] batch)
8+
protected override int Migrate(WorkContext context, GameLevel[] batch)
99
{
1010
foreach (GameLevel level in batch)
1111
{
1212
context.Database.CreateRevisionForLevel(level, null);
1313
}
14+
return batch.Length;
1415
}
1516

1617
protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> query)

Refresh.Interfaces.Workers/Migrations/CalculateScoreRanksMigration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> que
1212
return query.OrderBy(l => l.LevelId);
1313
}
1414

15-
protected override void Migrate(WorkContext context, GameLevel[] batch)
15+
protected override int Migrate(WorkContext context, GameLevel[] batch)
1616
{
1717
foreach (GameLevel level in batch)
1818
{
1919
context.Database.RecalculateScoreStatistics(level);
2020
}
2121
context.Database.SaveChanges();
22+
return batch.Length;
2223
}
2324
}

0 commit comments

Comments
 (0)