Handle beatmap lookup requests in TestRoomRequestsHandler

This commit is contained in:
Dan Balasescu 2022-02-15 22:02:33 +09:00
parent a5183cec77
commit ccd265ebe7
4 changed files with 42 additions and 11 deletions

View File

@ -5,6 +5,7 @@
using osu.Framework.Graphics;
using osu.Framework.Screens;
using osu.Game.Database;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.Multiplayer;
using osu.Game.Screens;
@ -46,6 +47,9 @@ public class TestMultiplayerComponents : OsuScreen
[Cached]
private readonly BeatmapLookupCache beatmapLookupCache = new BeatmapLookupCache();
[Resolved]
private BeatmapManager beatmapManager { get; set; }
private readonly OsuScreenStack screenStack;
private readonly TestMultiplayer multiplayerScreen;
@ -69,9 +73,9 @@ public TestMultiplayerComponents()
}
[BackgroundDependencyLoader]
private void load(IAPIProvider api, OsuGameBase game)
private void load(IAPIProvider api)
{
((DummyAPIAccess)api).HandleRequest = request => multiplayerScreen.RequestsHandler.HandleRequest(request, api.LocalUser.Value, game);
((DummyAPIAccess)api).HandleRequest = request => multiplayerScreen.RequestsHandler.HandleRequest(request, api.LocalUser.Value, beatmapManager);
}
public override bool OnBackButton() => (screenStack.CurrentScreen as OsuScreen)?.OnBackButton() ?? base.OnBackButton();

View File

@ -7,7 +7,7 @@ namespace osu.Game.Online.API.Requests
{
public class GetBeatmapsRequest : APIRequest<GetBeatmapsResponse>
{
private readonly int[] beatmapIds;
public readonly int[] BeatmapIds;
private const int max_ids_per_request = 50;
@ -16,9 +16,9 @@ public GetBeatmapsRequest(int[] beatmapIds)
if (beatmapIds.Length > max_ids_per_request)
throw new ArgumentException($"{nameof(GetBeatmapsRequest)} calls only support up to {max_ids_per_request} IDs at once");
this.beatmapIds = beatmapIds;
BeatmapIds = beatmapIds;
}
protected override string Target => "beatmaps/?ids[]=" + string.Join("&ids[]=", beatmapIds);
protected override string Target => "beatmaps/?ids[]=" + string.Join("&ids[]=", BeatmapIds);
}
}

View File

@ -8,6 +8,7 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Database;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay;
@ -33,9 +34,6 @@ public abstract class OnlinePlayTestScene : ScreenTestScene, IOnlinePlayTestScen
protected override Container<Drawable> Content => content;
[Resolved]
private OsuGameBase game { get; set; }
private readonly Container content;
private readonly Container drawableDependenciesContainer;
private DelegatedDependencyContainer dependencies;
@ -71,7 +69,12 @@ public override void SetUpSteps()
AddStep("setup API", () =>
{
var handler = OnlinePlayDependencies.RequestsHandler;
((DummyAPIAccess)API).HandleRequest = request => handler.HandleRequest(request, API.LocalUser.Value, game);
// Resolving the BeatmapManager in the test scene will inject the game-wide BeatmapManager, while many test scenes cache their own BeatmapManager instead.
// To get around this, the BeatmapManager is looked up from the dependencies provided to the children of the test scene instead.
var beatmapManager = dependencies.Get<BeatmapManager>();
((DummyAPIAccess)API).HandleRequest = request => handler.HandleRequest(request, API.LocalUser.Value, beatmapManager);
});
}

View File

@ -4,12 +4,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Rooms;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Tests.Beatmaps;
namespace osu.Game.Tests.Visual.OnlinePlay
{
@ -33,9 +37,9 @@ public class TestRoomRequestsHandler
/// </summary>
/// <param name="request">The API request to handle.</param>
/// <param name="localUser">The local user to store in responses where required.</param>
/// <param name="game">The game base for cases where actual online requests need to be sent.</param>
/// <param name="beatmapManager">The beatmap manager to attempt to retrieve beatmaps from, prior to returning dummy beatmaps.</param>
/// <returns>Whether the request was successfully handled.</returns>
public bool HandleRequest(APIRequest request, APIUser localUser, OsuGameBase game)
public bool HandleRequest(APIRequest request, APIUser localUser, BeatmapManager beatmapManager)
{
switch (request)
{
@ -128,6 +132,26 @@ public bool HandleRequest(APIRequest request, APIUser localUser, OsuGameBase gam
Statistics = new Dictionary<HitResult, int>()
});
return true;
case GetBeatmapsRequest getBeatmapsRequest:
var result = new List<APIBeatmap>();
foreach (int id in getBeatmapsRequest.BeatmapIds)
{
var baseBeatmap = beatmapManager.QueryBeatmap(b => b.OnlineID == id);
if (baseBeatmap == null)
{
baseBeatmap = new TestBeatmap(new RulesetInfo { OnlineID = 0 }).BeatmapInfo;
baseBeatmap.OnlineID = id;
baseBeatmap.BeatmapSet!.OnlineID = id;
}
result.Add(OsuTestScene.CreateAPIBeatmap(baseBeatmap));
}
getBeatmapsRequest.TriggerSuccess(new GetBeatmapsResponse { Beatmaps = result });
return true;
}
return false;