1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 22:07:40 +08:00
osu-lazer/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerRoomManager.cs

117 lines
4.3 KiB
C#
Raw Normal View History

2020-12-19 00:17:24 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
2020-12-19 00:22:52 +08:00
using System.Linq;
2020-12-19 00:17:24 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2020-12-19 00:17:24 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
2020-12-19 00:17:24 +08:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
using osu.Game.Screens.OnlinePlay.Multiplayer;
2020-12-19 00:17:24 +08:00
2020-12-25 12:38:11 +08:00
namespace osu.Game.Tests.Visual.Multiplayer
2020-12-19 00:17:24 +08:00
{
2020-12-25 12:38:11 +08:00
public class TestMultiplayerRoomManager : MultiplayerRoomManager
2020-12-19 00:17:24 +08:00
{
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private OsuGameBase game { get; set; }
[Cached]
public readonly Bindable<FilterCriteria> Filter = new Bindable<FilterCriteria>(new FilterCriteria());
2020-12-19 00:17:24 +08:00
private readonly List<Room> rooms = new List<Room>();
protected override void LoadComplete()
{
base.LoadComplete();
int currentScoreId = 0;
int currentRoomId = 0;
2020-12-19 00:17:24 +08:00
((DummyAPIAccess)api).HandleRequest = req =>
{
switch (req)
{
case CreateRoomRequest createRoomRequest:
var createdRoom = new APICreatedRoom();
createdRoom.CopyFrom(createRoomRequest.Room);
createdRoom.RoomID.Value ??= currentRoomId++;
2020-12-19 00:17:24 +08:00
rooms.Add(createdRoom);
createRoomRequest.TriggerSuccess(createdRoom);
break;
case JoinRoomRequest joinRoomRequest:
joinRoomRequest.TriggerSuccess();
break;
case PartRoomRequest partRoomRequest:
partRoomRequest.TriggerSuccess();
break;
case GetRoomsRequest getRoomsRequest:
2020-12-19 00:22:52 +08:00
var roomsWithoutParticipants = new List<Room>();
foreach (var r in rooms)
{
var newRoom = new Room();
newRoom.CopyFrom(r);
newRoom.RecentParticipants.Clear();
roomsWithoutParticipants.Add(newRoom);
}
getRoomsRequest.TriggerSuccess(roomsWithoutParticipants);
break;
case GetRoomRequest getRoomRequest:
getRoomRequest.TriggerSuccess(rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId));
2020-12-19 00:17:24 +08:00
break;
case GetBeatmapSetRequest getBeatmapSetRequest:
var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type);
onlineReq.Success += res => getBeatmapSetRequest.TriggerSuccess(res);
onlineReq.Failure += e => getBeatmapSetRequest.TriggerFailure(e);
// Get the online API from the game's dependencies.
game.Dependencies.Get<IAPIProvider>().Queue(onlineReq);
break;
case CreateRoomScoreRequest createRoomScoreRequest:
createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 });
break;
case SubmitRoomScoreRequest submitRoomScoreRequest:
submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore
{
ID = currentScoreId++,
Accuracy = 1,
EndedAt = DateTimeOffset.Now,
Passed = true,
Rank = ScoreRank.S,
MaxCombo = 1000,
TotalScore = 1000000,
User = api.LocalUser.Value,
Statistics = new Dictionary<HitResult, int>()
});
break;
}
};
}
public new void ClearRooms() => base.ClearRooms();
2020-12-19 00:17:24 +08:00
public new void Schedule(Action action) => base.Schedule(action);
}
}