Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement count query parameter for play/user/{id} #197

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Refresh.GameServer/Database/GameDatabaseContext.Levels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public DatabaseList<GameLevel> GetMostReplayedLevels(int count, int skip, TokenG
IEnumerable<GameLevel> mostPlayed = plays
.AsEnumerable()
.GroupBy(r => r.Level)
.Select(g => new { Level = g.Key, Count = g.Count() })
.Select(g => new { Level = g.Key, Count = g.Sum(p => p.Count) })
.OrderByDescending(x => x.Count)
.Select(x => x.Level)
.FilterByGameVersion(gameVersion);
Expand Down
3 changes: 2 additions & 1 deletion Refresh.GameServer/Database/GameDatabaseContext.Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,14 @@ public bool RateLevel(GameLevel level, GameUser user, RatingType type)

#region Playing

public void PlayLevel(GameLevel level, GameUser user)
public void PlayLevel(GameLevel level, GameUser user, int count)
{
PlayLevelRelation relation = new()
{
Level = level,
User = user,
Timestamp = this._time.TimestampMilliseconds,
Count = count,
};

UniquePlayLevelRelation? uniqueRelation = this._realm.All<UniquePlayLevelRelation>()
Expand Down
18 changes: 16 additions & 2 deletions Refresh.GameServer/Database/GameDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected GameDatabaseProvider(IDateTimeProvider time)
this._time = time;
}

protected override ulong SchemaVersion => 92;
protected override ulong SchemaVersion => 93;

protected override string Filename => "refreshGameServer.realm";

Expand Down Expand Up @@ -309,7 +309,6 @@ protected override void Migrate(Migration migration, ulong oldVersion)
IQueryable<dynamic>? oldScores = migration.OldRealm.DynamicApi.All("GameSubmittedScore");
IQueryable<GameSubmittedScore>? newScores = migration.NewRealm.All<GameSubmittedScore>();


for (int i = 0; i < newScores.Count(); i++)
{
dynamic oldScore = oldScores.ElementAt(i);
Expand All @@ -320,5 +319,20 @@ protected override void Migrate(Migration migration, ulong oldVersion)
newScore.Game = newScore.Level.GameVersion;
}
}

IQueryable<dynamic>? oldPlayLevelRelations = migration.OldRealm.DynamicApi.All("PlayLevelRelation");
IQueryable<PlayLevelRelation>? newPlayLevelRelations = migration.NewRealm.All<PlayLevelRelation>();

for (int i = 0; i < newPlayLevelRelations.Count(); i++)
{
dynamic oldPlayLevelRelation = oldPlayLevelRelations.ElementAt(i);
PlayLevelRelation newPlayLevelRelation = newPlayLevelRelations.ElementAt(i);

//In version 93, we added a count to PlayLevelRelation
if (oldVersion < 93)
{
newPlayLevelRelation.Count = 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class GameLevelResponse : IDataConvertableFrom<GameLevelResponse, GameLev
EnforceMinMaxPlayers = old.EnforceMinMaxPlayers,
SameScreenGame = old.SameScreenGame,
HeartCount = old.FavouriteRelations.Count(),
TotalPlayCount = old.AllPlays.Count(),
TotalPlayCount = old.AllPlays.Sum(p => p.Count),
UniquePlayCount = old.UniquePlays.Count(),
YayCount = old.Ratings.Count(r => r._RatingType == (int)RatingType.Yay),
BooCount = old.Ratings.Count(r => r._RatingType == (int)RatingType.Boo),
Expand Down
14 changes: 13 additions & 1 deletion Refresh.GameServer/Endpoints/Game/Levels/LeaderboardEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ public Response PlayLevel(RequestContext context, GameUser user, GameDatabaseCon
GameLevel? level = database.GetLevelById(id);
if (level == null) return NotFound;

database.PlayLevel(level, user);
int count = 1;
//If we are on PSP and it has sent a `count` parameter...
if (context.IsPSP() && context.QueryString.Get("count") != null)
{
//Parse the count
if (!int.TryParse(context.QueryString["count"], out count))
{
//If it fails, send a bad request back to the client
return BadRequest;
}
}

database.PlayLevel(level, user, count);
return OK;
}

Expand Down
2 changes: 1 addition & 1 deletion Refresh.GameServer/Types/Legacy/LegacyGameLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class LegacyGameLevel : IDataConvertableFrom<LegacyGameLevel, GameLevel>
LastUpdated = old.UpdateDate,
TeamPick = old.TeamPicked,
GameVersion = 1,
Plays = old.AllPlays.Count(),
Plays = old.AllPlays.Sum(p => p.Count),
PlaysUnique = old.UniquePlays.Count(),
PlaysComplete = old.Scores.Count(),
CommentsEnabled = true,
Expand Down
1 change: 1 addition & 0 deletions Refresh.GameServer/Types/Relations/PlayLevelRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public partial class PlayLevelRelation : IRealmObject
public GameLevel Level { get; set; }
public GameUser User { get; set; }
public long Timestamp { get; set; }
public int Count { get; set; }
}