Skip to content

Commit e538193

Browse files
authored
Pin progress updating fixes and improvements (#925)
This PR fixes manually awarded pins not having their progress properly incremented when updating them, extends the `AchieveTopFourthOfXLeaderboardsPin` test to test that case aswell, and does some minor improvements like replacing all `Write` calls in `GameDatabaseContext.Pins` with just `SaveChanges` calls, correcting the namespaces of pin tests and renaming the `ManuallyAwardedPins` file to match its class name.
2 parents de483f0 + 3b911c9 commit e538193

File tree

5 files changed

+106
-92
lines changed

5 files changed

+106
-92
lines changed

Refresh.Database/GameDatabaseContext.Pins.cs

Lines changed: 75 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,39 @@ public void UpdateUserPinProgress(Dictionary<long, int> pinProgressUpdates, Game
1818
(long)ServerPins.TopXOfAnyCommunityLevelWithOver50Scores,
1919
];
2020

21-
this.Write(() =>
21+
foreach (KeyValuePair<long, int> pinProgressUpdate in pinProgressUpdates)
2222
{
23-
foreach (KeyValuePair<long, int> pinProgressUpdate in pinProgressUpdates)
24-
{
25-
long pinId = pinProgressUpdate.Key;
26-
int newProgress = pinProgressUpdate.Value;
27-
PinProgressRelation? existingProgress = existingProgresses.FirstOrDefault(p => p.PinId == pinId);
23+
long pinId = pinProgressUpdate.Key;
24+
int newProgress = pinProgressUpdate.Value;
25+
PinProgressRelation? existingProgress = existingProgresses.FirstOrDefault(p => p.PinId == pinId);
2826

29-
if (existingProgress == null)
30-
{
31-
PinProgressRelation newRelation = new()
32-
{
33-
PinId = pinId,
34-
Progress = newProgress,
35-
Publisher = user,
36-
FirstPublished = now,
37-
LastUpdated = now,
38-
IsBeta = isBeta,
39-
};
40-
this.PinProgressRelations.Add(newRelation);
41-
continue;
42-
}
43-
44-
bool isSpecialTreatmentPin = descendingProgressPins.Contains(pinId);
45-
46-
// Only update progress if it's better. For most pins it's better the greater it is, but for the pins in
47-
// specialTreatmentPins, it's better the smaller it is.
48-
if (!isSpecialTreatmentPin && newProgress > existingProgress.Progress
49-
|| isSpecialTreatmentPin && newProgress < existingProgress.Progress)
27+
if (existingProgress == null)
28+
{
29+
PinProgressRelation newRelation = new()
5030
{
51-
existingProgress.Progress = newProgress;
52-
existingProgress.LastUpdated = now;
53-
}
31+
PinId = pinId,
32+
Progress = newProgress,
33+
Publisher = user,
34+
FirstPublished = now,
35+
LastUpdated = now,
36+
IsBeta = isBeta,
37+
};
38+
this.PinProgressRelations.Add(newRelation);
39+
continue;
40+
}
41+
42+
bool isSpecialTreatmentPin = descendingProgressPins.Contains(pinId);
43+
44+
// Only update progress if it's better. For most pins it's better the greater it is, but for the pins in
45+
// specialTreatmentPins, it's better the smaller it is.
46+
if ((!isSpecialTreatmentPin && newProgress > existingProgress.Progress)
47+
|| (isSpecialTreatmentPin && newProgress < existingProgress.Progress))
48+
{
49+
existingProgress.Progress = newProgress;
50+
existingProgress.LastUpdated = now;
5451
}
55-
});
52+
}
53+
this.SaveChanges();
5654
}
5755

5856
public void UpdateUserProfilePins(List<long> pinUpdates, GameUser user, TokenGame game)
@@ -61,40 +59,38 @@ public void UpdateUserProfilePins(List<long> pinUpdates, GameUser user, TokenGam
6159
IEnumerable<ProfilePinRelation> existingProfilePins = this.GetProfilePinsByUser(user, game);
6260
DateTimeOffset now = this._time.Now;
6361

64-
this.Write(() =>
62+
for (int i = 0; i < pinUpdates.Count; i++)
6563
{
66-
for (int i = 0; i < pinUpdates.Count; i++)
67-
{
68-
long progressType = pinUpdates[i];
64+
long progressType = pinUpdates[i];
6965

70-
// Does the user have any progress on the new pin?
71-
if (!existingProgressIds.Contains(progressType)) continue;
66+
// Does the user have any progress on the new pin?
67+
if (!existingProgressIds.Contains(progressType)) continue;
7268

73-
ProfilePinRelation? existingPinAtIndex = existingProfilePins.FirstOrDefault(p => p.Index == i);
69+
ProfilePinRelation? existingPinAtIndex = existingProfilePins.FirstOrDefault(p => p.Index == i);
7470

75-
// If the pin at this position hasn't changed, skip it
76-
if (existingPinAtIndex?.PinId == progressType) continue;
71+
// If the pin at this position hasn't changed, skip it
72+
if (existingPinAtIndex?.PinId == progressType) continue;
7773

78-
if (existingPinAtIndex == null)
79-
{
80-
this.ProfilePinRelations.Add(new()
81-
{
82-
PinId = progressType,
83-
Publisher = user,
84-
PublisherId = user.UserId,
85-
Index = i,
86-
Game = game,
87-
Timestamp = now,
88-
});
89-
}
90-
else
74+
if (existingPinAtIndex == null)
75+
{
76+
this.ProfilePinRelations.Add(new()
9177
{
92-
this.ProfilePinRelations.Update(existingPinAtIndex);
93-
existingPinAtIndex.PinId = progressType;
94-
existingPinAtIndex.Timestamp = now; // New pin at this position: reset timestamp
95-
}
78+
PinId = progressType,
79+
Publisher = user,
80+
PublisherId = user.UserId,
81+
Index = i,
82+
Game = game,
83+
Timestamp = now,
84+
});
9685
}
97-
});
86+
else
87+
{
88+
this.ProfilePinRelations.Update(existingPinAtIndex);
89+
existingPinAtIndex.PinId = progressType;
90+
existingPinAtIndex.Timestamp = now; // New pin at this position: reset timestamp
91+
}
92+
}
93+
this.SaveChanges();
9894
}
9995

10096
public PinProgressRelation UpdateUserPinProgressToLowest(long pinId, int newProgressValue, GameUser user, bool isBeta)
@@ -105,53 +101,43 @@ public PinProgressRelation UpdateUserPinProgressToLowest(long pinId, int newProg
105101

106102
if (progressToUpdate == null)
107103
{
108-
this.Write(() =>
104+
progressToUpdate = new()
109105
{
110-
progressToUpdate = new()
111-
{
112-
PinId = pinId,
113-
Progress = newProgressValue,
114-
Publisher = user,
115-
PublisherId = user.UserId,
116-
FirstPublished = now,
117-
LastUpdated = now,
118-
IsBeta = isBeta,
119-
};
106+
PinId = pinId,
107+
Progress = newProgressValue,
108+
Publisher = user,
109+
PublisherId = user.UserId,
110+
FirstPublished = now,
111+
LastUpdated = now,
112+
IsBeta = isBeta,
113+
};
120114

121-
this.PinProgressRelations.Add(progressToUpdate);
122-
});
115+
this.PinProgressRelations.Add(progressToUpdate);
116+
SaveChanges();
123117
}
124118
// Only update if the final progress value is actually lower to the one already set
125119
else if (newProgressValue < progressToUpdate.Progress)
126120
{
127-
this.Write(() =>
128-
{
129-
progressToUpdate.Progress = newProgressValue;
130-
progressToUpdate.LastUpdated = now;
131-
});
121+
progressToUpdate.Progress = newProgressValue;
122+
progressToUpdate.LastUpdated = now;
123+
SaveChanges();
132124
}
133125

134126
return progressToUpdate!;
135127
}
136128

137129
public void IncrementUserPinProgress(long pinId, int progressToAdd, GameUser user)
138130
{
139-
this.Write(() =>
140-
{
141-
this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, true);
142-
this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, false);
143-
});
144-
131+
this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, true);
132+
this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, false);
133+
134+
this.SaveChanges();
145135
}
146136

147137
public PinProgressRelation IncrementUserPinProgress(long pinId, int progressToAdd, GameUser user, bool isBeta)
148138
{
149-
PinProgressRelation relation = null!;
150-
151-
this.Write(() =>
152-
{
153-
relation = this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, isBeta);
154-
});
139+
PinProgressRelation relation = this.IncrementUserPinProgressInternal(pinId, progressToAdd, user, isBeta);
140+
this.SaveChanges();
155141

156142
return relation;
157143
}
@@ -180,7 +166,7 @@ private PinProgressRelation IncrementUserPinProgressInternal(long pinId, int pro
180166
}
181167
else
182168
{
183-
progressToUpdate.Progress =+ progressToAdd;
169+
progressToUpdate.Progress += progressToAdd;
184170
progressToUpdate.LastUpdated = now;
185171
}
186172

RefreshTests.GameServer/TestContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public void FillLeaderboard(GameLevel level, int count, byte type)
147147
{
148148
for (byte i = 0; i < count; i++)
149149
{
150-
GameUser scoreUser = this.CreateUser("score" + i);
150+
string username = "score" + i;
151+
GameUser scoreUser = this.Database.GetUserByUsername(username) ?? this.CreateUser(username);
151152
this.SubmitScore(i, type, level, scoreUser, TokenGame.LittleBigPlanet2, TokenPlatform.PS3);
152153
}
153154
}

RefreshTests.GameServer/Tests/Pins/PinProgressUpdatingTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Refresh.Interfaces.Game.Types.Pins;
77
using RefreshTests.GameServer.Extensions;
88

9-
namespace RefreshTests.GameServer.Tests.Levels;
9+
namespace RefreshTests.GameServer.Tests.Pins;
1010

1111
public class PinProgressUpdatingTests : GameServerTest
1212
{

RefreshTests.GameServer/Tests/Pins/ScorePinTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Refresh.Interfaces.Game.Types.UserData.Leaderboard;
77
using RefreshTests.GameServer.Extensions;
88

9-
namespace RefreshTests.GameServer.Tests.Levels;
9+
namespace RefreshTests.GameServer.Tests.Pins;
1010

1111
public class ScorePinTests : GameServerTest
1212
{
@@ -82,6 +82,7 @@ public void AchieveTopFourthOfXLeaderboardsPin(byte scoreType, bool isStoryLevel
8282
long pinIdToCheck = isStoryLevel ? (long)ServerPins.TopFourthOfXStoryLevelsWithOver50Scores
8383
: (long)ServerPins.TopFourthOfXCommunityLevelsWithOver50Scores;
8484

85+
// ROUND 1: Adding the pin
8586
// Create a level and spam it with scores by others
8687
GameLevel level = isStoryLevel ? context.Database.GetStoryLevelById(1) : context.CreateLevel(user);
8788
int levelId = isStoryLevel ? level.StoryId : level.LevelId;
@@ -105,6 +106,32 @@ public void AchieveTopFourthOfXLeaderboardsPin(byte scoreType, bool isStoryLevel
105106
PinProgressRelation? relation = context.Database.GetUserPinProgress(pinIdToCheck, user, false);
106107
Assert.That(relation, Is.Not.Null);
107108
Assert.That(relation!.Progress, Is.EqualTo(1));
109+
110+
// ROUND 2: Updating the pin
111+
// Create another level and spam it with scores by others aswell
112+
GameLevel level2 = isStoryLevel ? context.Database.GetStoryLevelById(2) : context.CreateLevel(user);
113+
int levelId2 = isStoryLevel ? level2.StoryId : level2.LevelId;
114+
115+
context.FillLeaderboard(level2, 100, scoreType);
116+
117+
// Now post our score which will definitely make it to the top 25% here aswell
118+
SerializedScore score2 = new()
119+
{
120+
Host = true,
121+
ScoreType = scoreType,
122+
Score = 80,
123+
};
124+
125+
context.Database.PlayLevel(level2, user, 1);
126+
message = client.PostAsync($"/lbp/scoreboard/{slotType}/{levelId2}", new StringContent(score2.AsXML())).Result;
127+
Assert.That(message.StatusCode, Is.EqualTo(OK));
128+
129+
context.Database.Refresh();
130+
131+
// Ensure the pin progress has been incremented
132+
PinProgressRelation? relation2 = context.Database.GetUserPinProgress(pinIdToCheck, user, false);
133+
Assert.That(relation2, Is.Not.Null);
134+
Assert.That(relation2!.Progress, Is.EqualTo(2));
108135
}
109136

110137
[Test]

0 commit comments

Comments
 (0)