22using Refresh . Database . Models . Users ;
33using Refresh . Database . Models . Relations ;
44using Refresh . Database . Models . Pins ;
5+ using MongoDB . Bson ;
56
67namespace Refresh . Database ;
78
89public partial class GameDatabaseContext // Pins
910{
10- public void UpdateUserPinProgress ( Dictionary < long , int > pinProgressUpdates , GameUser user , TokenGame game )
11+ public void UpdateUserPinProgress ( Dictionary < long , int > pinProgressUpdates , GameUser user , bool isBeta , TokenPlatform platform )
1112 {
1213 DateTimeOffset now = this . _time . Now ;
13- bool isBeta = game == TokenGame . BetaBuild ;
14- IEnumerable < PinProgressRelation > existingProgresses = this . GetPinProgressesByUser ( user , isBeta ) ;
14+ IEnumerable < PinProgressRelation > existingProgresses = this . GetPinProgressesByUser ( user , isBeta , platform ) ;
1515 List < long > descendingProgressPins =
1616 [
1717 ( long ) ServerPins . TopXOfAnyStoryLevelWithOver50Scores ,
@@ -34,6 +34,7 @@ public void UpdateUserPinProgress(Dictionary<long, int> pinProgressUpdates, Game
3434 FirstPublished = now ,
3535 LastUpdated = now ,
3636 IsBeta = isBeta ,
37+ Platform = platform ,
3738 } ;
3839 this . PinProgressRelations . Add ( newRelation ) ;
3940 continue ;
@@ -53,10 +54,12 @@ public void UpdateUserPinProgress(Dictionary<long, int> pinProgressUpdates, Game
5354 this . SaveChanges ( ) ;
5455 }
5556
56- public void UpdateUserProfilePins ( List < long > pinUpdates , GameUser user , TokenGame game )
57+ public void UpdateUserProfilePins ( List < long > pinUpdates , GameUser user , TokenGame game , TokenPlatform platform )
5758 {
58- IEnumerable < long > existingProgressIds = this . GetPinProgressesByUser ( user , game == TokenGame . BetaBuild ) . Select ( p => p . PinId ) ;
59- IEnumerable < ProfilePinRelation > existingProfilePins = this . GetProfilePinsByUser ( user , game ) ;
59+ IEnumerable < long > existingProgressIds = this
60+ . GetPinProgressesByUser ( user , game == TokenGame . BetaBuild , platform )
61+ . Select ( p => p . PinId ) ;
62+ IEnumerable < ProfilePinRelation > existingProfilePins = this . GetProfilePinsByUser ( user , game , platform ) ;
6063 DateTimeOffset now = this . _time . Now ;
6164
6265 for ( int i = 0 ; i < pinUpdates . Count ; i ++ )
@@ -80,6 +83,7 @@ public void UpdateUserProfilePins(List<long> pinUpdates, GameUser user, TokenGam
8083 PublisherId = user . UserId ,
8184 Index = i ,
8285 Game = game ,
86+ Platform = platform ,
8387 Timestamp = now ,
8488 } ) ;
8589 }
@@ -93,10 +97,10 @@ public void UpdateUserProfilePins(List<long> pinUpdates, GameUser user, TokenGam
9397 this . SaveChanges ( ) ;
9498 }
9599
96- public PinProgressRelation UpdateUserPinProgressToLowest ( long pinId , int newProgressValue , GameUser user , bool isBeta )
100+ public PinProgressRelation UpdateUserPinProgressToLowest ( long pinId , int newProgressValue , GameUser user , bool isBeta , TokenPlatform platform )
97101 {
98102 // Get pin progress if it exists already
99- PinProgressRelation ? progressToUpdate = this . GetUserPinProgress ( pinId , user , isBeta ) ;
103+ PinProgressRelation ? progressToUpdate = this . GetUserPinProgress ( pinId , user , isBeta , platform ) ;
100104 DateTimeOffset now = this . _time . Now ;
101105
102106 if ( progressToUpdate == null )
@@ -126,31 +130,15 @@ public PinProgressRelation UpdateUserPinProgressToLowest(long pinId, int newProg
126130 return progressToUpdate ! ;
127131 }
128132
129- public void IncrementUserPinProgress ( long pinId , int progressToAdd , GameUser user )
130- {
131- this . IncrementUserPinProgressInternal ( pinId , progressToAdd , user , true ) ;
132- this . IncrementUserPinProgressInternal ( pinId , progressToAdd , user , false ) ;
133-
134- this . SaveChanges ( ) ;
135- }
136-
137- public PinProgressRelation IncrementUserPinProgress ( long pinId , int progressToAdd , GameUser user , bool isBeta )
138- {
139- PinProgressRelation relation = this . IncrementUserPinProgressInternal ( pinId , progressToAdd , user , isBeta ) ;
140- this . SaveChanges ( ) ;
141-
142- return relation ;
143- }
144-
145- private PinProgressRelation IncrementUserPinProgressInternal ( long pinId , int progressToAdd , GameUser user , bool isBeta )
133+ public PinProgressRelation IncrementUserPinProgress ( long pinId , int progressToAdd , GameUser user , bool isBeta , TokenPlatform platform )
146134 {
147135 // Get pin progress if it exists already
148- PinProgressRelation ? progressToUpdate = this . GetUserPinProgress ( pinId , user , isBeta ) ;
136+ PinProgressRelation ? progressToUpdate = this . GetUserPinProgress ( pinId , user , isBeta , platform ) ;
149137 DateTimeOffset now = this . _time . Now ;
150138
151139 if ( progressToUpdate == null )
152140 {
153- PinProgressRelation newRelation = new ( )
141+ progressToUpdate = new ( )
154142 {
155143 PinId = pinId ,
156144 Progress = progressToAdd ,
@@ -159,36 +147,53 @@ private PinProgressRelation IncrementUserPinProgressInternal(long pinId, int pro
159147 FirstPublished = now ,
160148 LastUpdated = now ,
161149 IsBeta = isBeta ,
150+ Platform = platform ,
162151 } ;
163-
164- this . PinProgressRelations . Add ( newRelation ) ;
165- return newRelation ;
152+ this . PinProgressRelations . Add ( progressToUpdate ) ;
166153 }
167154 else
168155 {
169156 progressToUpdate . Progress += progressToAdd ;
170157 progressToUpdate . LastUpdated = now ;
171158 }
172159
160+ this . SaveChanges ( ) ;
173161 return progressToUpdate ;
174162 }
175163
176- private IEnumerable < PinProgressRelation > GetPinProgressesByUser ( GameUser user , bool isBeta )
164+ private IEnumerable < PinProgressRelation > GetPinProgressesByUser ( GameUser user , bool isBeta , TokenPlatform platform )
177165 => this . PinProgressRelations
178- . Where ( p => p . PublisherId == user . UserId && p . IsBeta == isBeta )
166+ . Where ( p => p . PublisherId == user . UserId && ( p . IsBeta == isBeta && p . Platform == platform || p . Platform == TokenPlatform . Website ) )
179167 . OrderByDescending ( p => p . LastUpdated ) ;
180168
181- public DatabaseList < PinProgressRelation > GetPinProgressesByUser ( GameUser user , TokenGame game , int skip , int count )
182- => new ( this . GetPinProgressesByUser ( user , game == TokenGame . BetaBuild ) , skip , count ) ;
169+ public DatabaseList < PinProgressRelation > GetPinProgressesByUser ( GameUser user , bool isBeta , TokenPlatform platform , int skip , int count )
170+ => new ( this . GetPinProgressesByUser ( user , isBeta , platform ) , skip , count ) ;
183171
184- public PinProgressRelation ? GetUserPinProgress ( long pinId , GameUser user , bool isBeta )
185- => this . PinProgressRelations . FirstOrDefault ( p => p . PinId == pinId && p . PublisherId == user . UserId && p . IsBeta == isBeta ) ;
172+ public PinProgressRelation ? GetUserPinProgress ( long pinId , GameUser user , bool isBeta , TokenPlatform platform )
173+ => this . PinProgressRelations . FirstOrDefault ( p => p . PinId == pinId && p . PublisherId == user . UserId
174+ && ( p . IsBeta == isBeta && p . Platform == platform || p . Platform == TokenPlatform . Website ) ) ;
186175
187- private IEnumerable < ProfilePinRelation > GetProfilePinsByUser ( GameUser user , TokenGame game )
176+ private IEnumerable < ProfilePinRelation > GetProfilePinsByUser ( GameUser user , TokenGame game , TokenPlatform platform )
188177 => this . ProfilePinRelations
189- . Where ( p => p . PublisherId == user . UserId && p . Game == game )
178+ . Where ( p => p . PublisherId == user . UserId && p . Game == game && p . Platform == platform )
190179 . OrderBy ( p => p . Index ) ;
191180
192- public DatabaseList < ProfilePinRelation > GetProfilePinsByUser ( GameUser user , TokenGame game , int skip , int count )
193- => new ( this . GetProfilePinsByUser ( user , game ) , skip , count ) ;
181+ public DatabaseList < ProfilePinRelation > GetProfilePinsByUser ( GameUser user , TokenGame game , TokenPlatform platform , int skip , int count )
182+ => new ( this . GetProfilePinsByUser ( user , game , platform ) , skip , count ) ;
183+
184+ #region Migration Methods
185+
186+ public void AddPinProgress ( PinProgressRelation relation , bool save )
187+ {
188+ this . PinProgressRelations . Add ( relation ) ;
189+ if ( save ) this . SaveChanges ( ) ;
190+ }
191+
192+ public void RemoveAllPinProgressesByIdAndUser ( long pinId , ObjectId userId , bool save )
193+ {
194+ this . PinProgressRelations . RemoveRange ( p => p . PinId == pinId && p . PublisherId == userId ) ;
195+ if ( save ) this . SaveChanges ( ) ;
196+ }
197+
198+ #endregion
194199}
0 commit comments