1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 10:12:54 +08:00
osu-lazer/osu.Game/Online/Multiplayer/PlaylistItem.cs

96 lines
3.0 KiB
C#
Raw Normal View History

// 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.
2018-12-17 13:45:06 +08:00
using System.Collections.Generic;
2018-12-17 13:45:06 +08:00
using System.Linq;
using Newtonsoft.Json;
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 List<Mod> AllowedMods = new List<Mod>();
2018-12-17 13:45:06 +08:00
[JsonIgnore]
public readonly List<Mod> RequiredMods = new List<Mod>();
2018-12-17 13:45:06 +08:00
[JsonProperty("beatmap")]
private APIBeatmap apiBeatmap { get; set; }
2019-12-11 13:10:35 +08:00
private APIMod[] allowedModsBacking;
2018-12-17 13:45:06 +08:00
[JsonProperty("allowed_mods")]
private APIMod[] allowedMods
{
get => AllowedMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray();
2019-12-11 13:10:35 +08:00
set => allowedModsBacking = value;
2018-12-17 13:45:06 +08:00
}
2019-12-11 13:10:35 +08:00
private APIMod[] requiredModsBacking;
2018-12-17 13:45:06 +08:00
[JsonProperty("required_mods")]
private APIMod[] requiredMods
{
get => RequiredMods.Select(m => new APIMod { Acronym = m.Acronym }).ToArray();
2019-12-11 13:10:35 +08:00
set => requiredModsBacking = value;
2018-12-17 13:45:06 +08:00
}
private BeatmapInfo beatmap;
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
2018-12-26 21:14:49 +08:00
// Todo: Is this a bug? Room creation only returns the beatmap ID
2018-12-17 13:45:06 +08:00
Beatmap = apiBeatmap == null ? beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == BeatmapID) : apiBeatmap.ToBeatmap(rulesets);
Ruleset = rulesets.GetRuleset(RulesetID);
2019-12-11 13:10:35 +08:00
if (allowedModsBacking != null)
2018-12-17 13:45:06 +08:00
{
AllowedMods.Clear();
2019-12-11 13:10:35 +08:00
AllowedMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => allowedModsBacking.Any(m => m.Acronym == mod.Acronym)));
2018-12-17 13:45:06 +08:00
2019-12-11 13:10:35 +08:00
allowedModsBacking = null;
2018-12-17 13:45:06 +08:00
}
2019-12-11 13:10:35 +08:00
if (requiredModsBacking != null)
2018-12-17 13:45:06 +08:00
{
RequiredMods.Clear();
2019-12-11 13:10:35 +08:00
RequiredMods.AddRange(Ruleset.CreateInstance().GetAllMods().Where(mod => requiredModsBacking.Any(m => m.Acronym == mod.Acronym)));
2018-12-17 13:45:06 +08:00
2019-12-11 13:10:35 +08:00
requiredModsBacking = null;
2018-12-17 13:45:06 +08:00
}
}
public bool ShouldSerializeID() => false;
public bool ShouldSerializeapiBeatmap() => false;
}
}