From 9d305ba0247bb2444ad74878d5b9eb8574a37e91 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Wed, 26 Dec 2018 20:32:01 +0900 Subject: [PATCH] Reduce code duplication --- osu.Game/Screens/Multi/RoomManager.cs | 45 ++++++++++++++++----------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/osu.Game/Screens/Multi/RoomManager.cs b/osu.Game/Screens/Multi/RoomManager.cs index c5345e5453..54d35db1c7 100644 --- a/osu.Game/Screens/Multi/RoomManager.cs +++ b/osu.Game/Screens/Multi/RoomManager.cs @@ -47,7 +47,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) @@ -114,13 +119,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); @@ -133,22 +133,29 @@ namespace osu.Game.Screens.Multi return tcs.Task; } - private void addRoom(Room local, Room remote) + /// + /// Updates a local with a remote copy. + /// + /// The local to update. + /// The remote to update with. + 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) + /// + /// Adds a to the list of available rooms. + /// + /// The to add.< + 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); } } }