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

Refactor LevelCategory to use DataContext internally #596

Merged
merged 1 commit into from
Jul 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static IEnumerable<ApiLevelCategoryResponse> FromOldList(IEnumerable<Leve
{
return oldList.Select(category =>
{
DatabaseList<GameLevel>? list = category.Fetch(context, 0, 1, dataContext.Match, dataContext.Database, dataContext.User, new LevelFilterSettings(context, TokenGame.Website), dataContext.User);
DatabaseList<GameLevel>? list = category.Fetch(context, 0, 1, dataContext, new LevelFilterSettings(context, TokenGame.Website), dataContext.User);
GameLevel? level = list?.Items.FirstOrDefault();

return FromOld(category, level, dataContext);
Expand Down
2 changes: 1 addition & 1 deletion Refresh.GameServer/Endpoints/ApiV3/LevelApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ApiListResponse<ApiGameLevelResponse> GetLevels(RequestContext context, G

DatabaseList<GameLevel>? list = categories.Categories
.FirstOrDefault(c => c.ApiRoute.StartsWith(route))?
.Fetch(context, skip, count, matchService, database, user, new LevelFilterSettings(context, TokenGame.Website), user);
.Fetch(context, skip, count, dataContext, new LevelFilterSettings(context, TokenGame.Website), user);

if (list == null) return ApiNotFoundError.Instance;

Expand Down
33 changes: 13 additions & 20 deletions Refresh.GameServer/Endpoints/Game/Levels/LevelEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ public class LevelEndpoints : EndpointGroup
public SerializedMinimalLevelList? GetLevels(RequestContext context,
GameDatabaseContext database,
CategoryService categoryService,
MatchService matchService,
LevelListOverrideService overrideService,
GameUser user,
Token token,
IDataStore dataStore,
DataContext dataContext,
string route)
{
Expand Down Expand Up @@ -63,7 +61,7 @@ public class LevelEndpoints : EndpointGroup

DatabaseList<GameLevel>? levels = categoryService.Categories
.FirstOrDefault(c => c.GameRoutes.Any(r => r.StartsWith(route)))?
.Fetch(context, skip, count, matchService, database, user, new LevelFilterSettings(context, token.TokenGame), user);
.Fetch(context, skip, count, dataContext, new LevelFilterSettings(context, token.TokenGame), user);

if (levels == null) return null;

Expand All @@ -79,25 +77,23 @@ public class LevelEndpoints : EndpointGroup
public SerializedMinimalLevelList? GetLevelsWithPlayer(RequestContext context,
GameDatabaseContext database,
CategoryService categories,
MatchService matchService,
LevelListOverrideService overrideService,
Token token,
IDataStore dataStore,
DataContext dataContext,
string route,
string username)
{
GameUser? user = database.GetUserByUsername(username);
if (user == null) return null;

return this.GetLevels(context, database, categories, matchService, overrideService, user, token, dataStore, dataContext, route);
return this.GetLevels(context, database, categories, overrideService, user, token, dataContext, route);
}

[GameEndpoint("s/{slotType}/{id}", ContentType.Xml)]
[NullStatusCode(NotFound)]
[MinimumRole(GameUserRole.Restricted)]
public GameLevelResponse? LevelById(RequestContext context, GameDatabaseContext database, MatchService matchService,
GameUser user, string slotType, int id, IDataStore dataStore, Token token,
public GameLevelResponse? LevelById(RequestContext context, GameDatabaseContext database, Token token,
string slotType, int id,
LevelListOverrideService overrideService, DataContext dataContext)
{
// If the user has had a hash override in the past, and the level id they requested matches the level ID associated with that hash
Expand All @@ -112,12 +108,12 @@ public class LevelEndpoints : EndpointGroup
[NullStatusCode(BadRequest)]
[MinimumRole(GameUserRole.Restricted)]
public SerializedLevelList? GetMultipleLevels(RequestContext context, GameDatabaseContext database,
MatchService matchService, GameUser user, IDataStore dataStore, Token token, DataContext dataContext)
GameUser user, Token token, DataContext dataContext)
{
string[]? levelIds = context.QueryString.GetValues("s");
if (levelIds == null) return null;

List<GameLevelResponse> levels = new();
List<GameLevelResponse> levels = [];

foreach (string levelIdStr in levelIds)
{
Expand All @@ -140,15 +136,13 @@ public class LevelEndpoints : EndpointGroup
[GameEndpoint("searches", ContentType.Xml)]
[GameEndpoint("genres", ContentType.Xml)]
[MinimumRole(GameUserRole.Restricted)]
public SerializedCategoryList GetModernCategories(RequestContext context, GameDatabaseContext database,
CategoryService categoryService, MatchService matchService, GameUser user, Token token, IDataStore dataStore,
DataContext dataContext)
public SerializedCategoryList GetModernCategories(RequestContext context, CategoryService categoryService, DataContext dataContext)
{
(int skip, int count) = context.GetPageData();

IEnumerable<SerializedCategory> categories = categoryService.Categories
.Where(c => !c.Hidden)
.Select(c => SerializedCategory.FromLevelCategory(c, context, database, dataStore, user, token, matchService, dataContext, 0, 1))
.Select(c => SerializedCategory.FromLevelCategory(c, context, dataContext, 0, 1))
.ToList();

int total = categories.Count();
Expand All @@ -163,15 +157,14 @@ public SerializedCategoryList GetModernCategories(RequestContext context, GameDa

[GameEndpoint("searches/{apiRoute}", ContentType.Xml)]
[MinimumRole(GameUserRole.Restricted)]
public SerializedMinimalLevelResultsList GetLevelsFromCategory(RequestContext context, GameDatabaseContext database,
CategoryService categories, MatchService matchService, GameUser user, Token token, IDataStore dataStore,
string apiRoute, DataContext dataContext)
public SerializedMinimalLevelResultsList GetLevelsFromCategory(RequestContext context,
CategoryService categories, GameUser user, Token token, string apiRoute, DataContext dataContext)
{
(int skip, int count) = context.GetPageData();

DatabaseList<GameLevel>? levels = categories.Categories
.FirstOrDefault(c => c.ApiRoute.StartsWith(apiRoute))?
.Fetch(context, skip, count, matchService, database, user, new LevelFilterSettings(context, token.TokenGame), user);
.Fetch(context, skip, count, dataContext, new LevelFilterSettings(context, token.TokenGame), user);

return new SerializedMinimalLevelResultsList(levels?.Items
.Select(l => GameMinimalLevelResponse.FromOld(l, dataContext))!, levels?.TotalItems ?? 0, skip + count);
Expand All @@ -192,7 +185,7 @@ public SerializedMinimalLevelResultsList GetLevelsFromCategory(RequestContext co
IDataStore dataStore,
Token token,
DataContext dataContext)
=> this.GetLevels(context, database, categories, matchService, overrideService, user, token, dataStore, dataContext, "newest");
=> this.GetLevels(context, database, categories, overrideService, user, token, dataContext, "newest");

[GameEndpoint("favouriteSlots/{username}", ContentType.Xml)]
[NullStatusCode(NotFound)]
Expand All @@ -210,7 +203,7 @@ public SerializedMinimalLevelResultsList GetLevelsFromCategory(RequestContext co
GameUser? user = database.GetUserByUsername(username);
if (user == null) return null;

SerializedMinimalLevelList? levels = this.GetLevels(context, database, categories, matchService, overrideService, user, token, dataStore, dataContext, "favouriteSlots");
SerializedMinimalLevelList? levels = this.GetLevels(context, database, categories, overrideService, user, token, dataContext, "favouriteSlots");

return new SerializedMinimalFavouriteLevelList(levels);
}
Expand Down
5 changes: 3 additions & 2 deletions Refresh.GameServer/Types/Levels/Categories/ByTagCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -19,7 +20,7 @@ internal ByTagCategory() : base("tag", "tag", false)
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? user)
{
string? tagStr = context.QueryString["tag"];
Expand All @@ -32,6 +33,6 @@ internal ByTagCategory() : base("tag", "tag", false)
if (tag == null)
return null;

return database.GetLevelsByTag(count, skip, user, tag.Value, levelFilterSettings);
return dataContext.Database.GetLevelsByTag(count, skip, user, tag.Value, levelFilterSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -19,15 +20,15 @@ internal ByUserLevelCategory() : base("byUser", "by", true)
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? user)
{
// Prefer username from query, but fallback to user passed into this category if it's missing
string? username = context.QueryString["u"];
if (username != null) user = database.GetUserByUsername(username);
if (username != null) user = dataContext.Database.GetUserByUsername(username);

if (user == null) return null;

return database.GetLevelsByUser(user, count, skip, levelFilterSettings, accessor);
return dataContext.Database.GetLevelsByUser(user, count, skip, levelFilterSettings, dataContext.User);
}
}
11 changes: 6 additions & 5 deletions Refresh.GameServer/Types/Levels/Categories/ContestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Contests;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -17,20 +18,20 @@ public ContestCategory() : base("contest", Array.Empty<string>(), false)
this.IconHash = "g820608";
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, MatchService matchService,
GameDatabaseContext database, GameUser? accessor, LevelFilterSettings levelFilterSettings, GameUser? _)
public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
{
// try to find a contest by the query parameter
string? contestId = context.QueryString["contest"];
GameContest? contest = database.GetContestById(contestId);
GameContest? contest = dataContext.Database.GetContestById(contestId);

// if we cant find one by a query param try getting an active contest instead
contest ??= database.GetNewestActiveContest();
contest ??= dataContext.Database.GetNewestActiveContest();

// if not, then fail
if (contest == null)
return null;

return database.GetLevelsFromContest(contest, count, skip, accessor, levelFilterSettings);
return dataContext.Database.GetLevelsFromContest(contest, count, skip, dataContext.User, levelFilterSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -16,9 +17,9 @@ public class CoolLevelsCategory : LevelCategory
this.IconHash = "g820625";
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, MatchService matchService,
GameDatabaseContext database, GameUser? accessor, LevelFilterSettings levelFilterSettings, GameUser? _)
public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
{
return database.GetCoolLevels(count, skip, accessor, levelFilterSettings);
return dataContext.Database.GetCoolLevels(count, skip, dataContext.User, levelFilterSettings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -18,7 +19,7 @@ internal CurrentlyPlayingCategory() : base("currentlyPlaying", "busiest", false)
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
=> database.GetBusiestLevels(count, skip, matchService, accessor, levelFilterSettings);
=> dataContext.Database.GetBusiestLevels(count, skip, dataContext.Match, dataContext.User, levelFilterSettings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -19,7 +20,7 @@ internal DeveloperLevelsCategory() : base("developer", Array.Empty<string>(), fa
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
=> database.GetDeveloperLevels(count, skip, levelFilterSettings);
=> dataContext.Database.GetDeveloperLevels(count, skip, levelFilterSettings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -17,11 +18,11 @@ internal FavouriteSlotsByUserCategory() : base("hearted", "favouriteSlots", true
this.IconHash = "g820611";
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, MatchService matchService, GameDatabaseContext database, GameUser? accessor,
public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? user)
{
if (user == null) return null;

return database.GetLevelsFavouritedByUser(user, count, skip, levelFilterSettings, accessor);
return dataContext.Database.GetLevelsFavouritedByUser(user, count, skip, levelFilterSettings, dataContext.User);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -18,7 +19,7 @@ internal HighestRatedLevelsCategory() : base("mostLiked", new[] { "thumbs", "hig
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
=> database.GetHighestRatedLevels(count, skip, accessor, levelFilterSettings);
=> dataContext.Database.GetHighestRatedLevels(count, skip, dataContext.User, levelFilterSettings);
}
3 changes: 2 additions & 1 deletion Refresh.GameServer/Types/Levels/Categories/LevelCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand Down Expand Up @@ -34,6 +35,6 @@ internal LevelCategory(string apiRoute, string[] gameRoutes, bool requiresUser)
[JsonProperty] public readonly bool RequiresUser;

[Pure]
public abstract DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, MatchService matchService, GameDatabaseContext database, GameUser? accessor,
public abstract DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count, DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -18,7 +19,7 @@ internal MostHeartedLevelsCategory() : base("mostHearted", "mostHearted", false)
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
=> database.GetMostHeartedLevels(count, skip, accessor, levelFilterSettings);
=> dataContext.Database.GetMostHeartedLevels(count, skip, dataContext.User, levelFilterSettings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.Game.Levels.FilterSettings;
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Data;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Types.Levels.Categories;
Expand All @@ -18,7 +19,7 @@ internal MostReplayedLevelsCategory() : base("mostReplayed", "mostPlays", false)
}

public override DatabaseList<GameLevel>? Fetch(RequestContext context, int skip, int count,
MatchService matchService, GameDatabaseContext database, GameUser? accessor,
DataContext dataContext,
LevelFilterSettings levelFilterSettings, GameUser? _)
=> database.GetMostReplayedLevels(count, skip, accessor, levelFilterSettings);
=> dataContext.Database.GetMostReplayedLevels(count, skip, dataContext.User, levelFilterSettings);
}
Loading
Loading