1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:07:44 +08:00

Reduce code duplication

This commit is contained in:
smoogipoo 2018-12-26 20:32:01 +09:00
parent 940d4a16bb
commit 5af29f8384

View File

@ -53,7 +53,12 @@ namespace osu.Game.Screens.Multi
room.Host.Value = api.LocalUser;
var req = new CreateRoomRequest(room);
req.Success += result => addRoom(room, result);
req.Success += result =>
{
update(room, result);
addRoom(room);
};
req.Failure += exception =>
{
if (req.Result != null)
@ -120,13 +125,8 @@ namespace osu.Game.Screens.Multi
// Add new matches, or update existing
foreach (var r in result)
{
processPlaylist(r);
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == r.RoomID.Value);
if (existing == null)
rooms.Add(r);
else
existing.CopyFrom(r);
update(r, r);
addRoom(r);
}
tcs.SetResult(true);
@ -139,22 +139,29 @@ namespace osu.Game.Screens.Multi
return tcs.Task;
}
private void addRoom(Room local, Room remote)
/// <summary>
/// Updates a local <see cref="Room"/> with a remote copy.
/// </summary>
/// <param name="local">The local <see cref="Room"/> to update.</param>
/// <param name="remote">The remote <see cref="Room"/> to update with.</param>
private void update(Room local, Room remote)
{
processPlaylist(remote);
foreach (var pi in remote.Playlist)
pi.MapObjects(beatmaps, rulesets);
local.CopyFrom(remote);
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == local.RoomID.Value);
if (existing != null)
rooms.Remove(existing);
rooms.Add(local);
}
private void processPlaylist(Room room)
/// <summary>
/// Adds a <see cref="Room"/> to the list of available rooms.
/// </summary>
/// <param name="room">The <see cref="Room"/> to add.<</param>
private void addRoom(Room room)
{
foreach (var pi in room.Playlist)
pi.MapObjects(beatmaps, rulesets);
var existing = rooms.FirstOrDefault(e => e.RoomID.Value == room.RoomID.Value);
if (existing == null)
rooms.Add(room);
else
existing.CopyFrom(room);
}
}
}