From 7c4fd8ca60c39039466b7a6738c9de0f790aa4b1 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 17 Dec 2018 14:45:06 +0900 Subject: [PATCH] Cleanup room definition --- .../Online/API/Requests/Responses/APIMod.cs | 12 ++ osu.Game/Online/Multiplayer/PlaylistItem.cs | 93 +++++++++++++ osu.Game/Online/Multiplayer/Room.cs | 123 ++++-------------- 3 files changed, 127 insertions(+), 101 deletions(-) create mode 100644 osu.Game/Online/API/Requests/Responses/APIMod.cs create mode 100644 osu.Game/Online/Multiplayer/PlaylistItem.cs diff --git a/osu.Game/Online/API/Requests/Responses/APIMod.cs b/osu.Game/Online/API/Requests/Responses/APIMod.cs new file mode 100644 index 0000000000..af0d0c643f --- /dev/null +++ b/osu.Game/Online/API/Requests/Responses/APIMod.cs @@ -0,0 +1,12 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using osu.Game.Rulesets.Mods; + +namespace osu.Game.Online.API.Requests.Responses +{ + public class APIMod : IMod + { + public string Acronym { get; set; } + } +} diff --git a/osu.Game/Online/Multiplayer/PlaylistItem.cs b/osu.Game/Online/Multiplayer/PlaylistItem.cs new file mode 100644 index 0000000000..e887295f4a --- /dev/null +++ b/osu.Game/Online/Multiplayer/PlaylistItem.cs @@ -0,0 +1,93 @@ +// Copyright (c) 2007-2018 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.Linq; +using Newtonsoft.Json; +using osu.Framework.Configuration; +using osu.Game.Beatmaps; +using osu.Game.Online.API.Requests.Responses; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Mods; + +namespace osu.Game.Online.Multiplayer +{ + public class PlaylistItem + { + [JsonProperty("id")] + public int ID { get; set; } + + [JsonProperty("beatmap_id")] + public int BeatmapID { get; set; } + + [JsonProperty("ruleset_id")] + public int RulesetID { get; set; } + + [JsonIgnore] + public BeatmapInfo Beatmap + { + get => beatmap; + set + { + beatmap = value; + BeatmapID = value?.OnlineBeatmapID ?? 0; + } + } + + [JsonIgnore] + public RulesetInfo Ruleset { get; set; } + + [JsonIgnore] + public readonly BindableCollection AllowedMods = new BindableCollection(); + + [JsonIgnore] + public readonly BindableCollection RequiredMods = new BindableCollection(); + + [JsonProperty("beatmap")] + private APIBeatmap apiBeatmap { get; set; } + + [JsonProperty("allowed_mods")] + private APIMod[] allowedMods + { + get => AllowedMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray(); + set => _allowedMods = value; + } + + [JsonProperty("required_mods")] + private APIMod[] requiredMods + { + get => RequiredMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray(); + set => _requiredMods = value; + } + + private BeatmapInfo beatmap; + private APIMod[] _allowedMods; + private APIMod[] _requiredMods; + + public void MapObjects(BeatmapManager beatmaps, RulesetStore rulesets) + { + // If we don't have an api beatmap, the request occurred as a result of room creation, so we can query the local beatmap instead + // Todo: Is this a bug? + Beatmap = apiBeatmap == null ? beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == BeatmapID) : apiBeatmap.ToBeatmap(rulesets); + Ruleset = rulesets.GetRuleset(RulesetID); + + if (_allowedMods != null) + { + AllowedMods.Clear(); + AllowedMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => _allowedMods.Any(m => m.Acronym == mod.Acronym))); + + _allowedMods = null; + } + + if (_requiredMods != null) + { + RequiredMods.Clear(); + RequiredMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => _requiredMods.Any(m => m.Acronym == mod.Acronym))); + + _requiredMods = null; + } + } + + public bool ShouldSerializeID() => false; + public bool ShouldSerializeapiBeatmap() => false; + } +} diff --git a/osu.Game/Online/Multiplayer/Room.cs b/osu.Game/Online/Multiplayer/Room.cs index 0b10992ada..ed85e6c31c 100644 --- a/osu.Game/Online/Multiplayer/Room.cs +++ b/osu.Game/Online/Multiplayer/Room.cs @@ -1,14 +1,11 @@ // Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE +using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using osu.Framework.Configuration; -using osu.Game.Beatmaps; -using osu.Game.Online.API.Requests.Responses; -using osu.Game.Rulesets; -using osu.Game.Rulesets.Mods; using osu.Game.Users; namespace osu.Game.Online.Multiplayer @@ -16,15 +13,13 @@ namespace osu.Game.Online.Multiplayer public class Room { [JsonProperty("id")] - public Bindable RoomID { get; } = new Bindable(); + public Bindable RoomID { get; private set; } = new Bindable(); [JsonProperty("name")] - public readonly Bindable Name = new Bindable("My awesome room!"); + public Bindable Name { get; private set; } = new Bindable("My awesome room!"); [JsonProperty("host")] - public readonly Bindable Host = new Bindable(); - - public bool ShouldSerializeHost() => false; + public Bindable Host { get; private set; } = new Bindable(); [JsonProperty("playlist")] public readonly BindableCollection Playlist = new BindableCollection(); @@ -35,12 +30,28 @@ namespace osu.Game.Online.Multiplayer [JsonIgnore] public readonly Bindable MaxAttempts = new Bindable(); + [JsonIgnore] + public Bindable Status = new Bindable(new RoomStatusOpen()); + + [JsonIgnore] + public Bindable Availability = new Bindable(); + + [JsonIgnore] + public Bindable Type = new Bindable(new GameTypeTimeshift()); + + [JsonIgnore] + public Bindable MaxParticipants = new Bindable(); + + [JsonIgnore] + public Bindable> Participants = new Bindable>(Enumerable.Empty()); + [JsonProperty("duration")] private int duration { get => (int)Duration.Value.TotalMinutes; set => Duration.Value = TimeSpan.FromMinutes(value); } + // Todo: Find a better way to do this (https://github.com/ppy/osu-framework/issues/1930) [JsonProperty("max_attempts", DefaultValueHandling = DefaultValueHandling.Ignore)] private int? maxAttempts @@ -49,12 +60,6 @@ namespace osu.Game.Online.Multiplayer set => MaxAttempts.Value = value; } - public Bindable Status = new Bindable(new RoomStatusOpen()); - public Bindable Availability = new Bindable(); - public Bindable Type = new Bindable(new GameTypeTimeshift()); - public Bindable MaxParticipants = new Bindable(); - public Bindable> Participants = new Bindable>(Enumerable.Empty()); - public void CopyFrom(Room other) { RoomID.Value = other.RoomID; @@ -69,92 +74,8 @@ namespace osu.Game.Online.Multiplayer Playlist.Clear(); Playlist.AddRange(other.Playlist); } - } - public class PlaylistItem - { - [JsonProperty("id")] - public int ID { get; set; } - - [JsonProperty("beatmap")] - private APIBeatmap apiBeatmap { get; set; } - - public bool ShouldSerializeapiBeatmap() => false; - - private BeatmapInfo beatmap; - - [JsonIgnore] - public BeatmapInfo Beatmap - { - get => beatmap; - set - { - beatmap = value; - BeatmapID = value?.OnlineBeatmapID ?? 0; - } - } - - [JsonProperty("beatmap_id")] - public int BeatmapID { get; set; } - - [JsonProperty("ruleset_id")] - public int RulesetID { get; set; } - - [JsonIgnore] - public readonly BindableCollection AllowedMods = new BindableCollection(); - - [JsonIgnore] - public readonly BindableCollection RequiredMods = new BindableCollection(); - - private APIMod[] _allowedMods; - - [JsonProperty("allowed_mods")] - private APIMod[] allowedMods - { - get => AllowedMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray(); - set => _allowedMods = value; - } - - private APIMod[] _requiredMods; - - [JsonProperty("required_mods")] - private APIMod[] requiredMods - { - get => RequiredMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray(); - set => _requiredMods = value; - } - - [JsonIgnore] - public RulesetInfo Ruleset { get; set; } - - public void MapObjects(BeatmapManager beatmaps, RulesetStore rulesets) - { - // If we don't have an api beatmap, the request occurred as a result of room creation, so we can query the local beatmap instead - // Todo: Is this a bug? - Beatmap = apiBeatmap == null ? beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == BeatmapID) : apiBeatmap.ToBeatmap(rulesets); - Ruleset = rulesets.GetRuleset(RulesetID); - - if (_allowedMods != null) - { - AllowedMods.Clear(); - AllowedMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => _allowedMods.Any(m => m.Acronym == mod.Acronym))); - - _allowedMods = null; - } - - if (_requiredMods != null) - { - RequiredMods.Clear(); - RequiredMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => _requiredMods.Any(m => m.Acronym == mod.Acronym))); - - _requiredMods = null; - } - } - - // Todo: Move this elsewhere for reusability - private class APIMod : IMod - { - public string Acronym { get; set; } - } + public bool ShouldSerializeRoomID() => false; + public bool ShouldSerializeHost() => false; } }