1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 17:47:29 +08:00
osu-lazer/osu.Game/Online/Multiplayer/Room.cs

149 lines
4.7 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
2018-12-03 19:50:40 +08:00
using System.Linq;
using Newtonsoft.Json;
2018-04-13 17:19:50 +08:00
using osu.Framework.Configuration;
using osu.Game.Beatmaps;
2018-12-12 18:04:11 +08:00
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
2018-04-13 17:19:50 +08:00
using osu.Game.Users;
namespace osu.Game.Online.Multiplayer
{
public class Room
{
2018-12-12 18:04:11 +08:00
[JsonProperty("id")]
2018-12-12 15:20:11 +08:00
public Bindable<int?> RoomID { get; } = new Bindable<int?>();
2018-12-12 18:04:11 +08:00
[JsonIgnore]
public readonly Bindable<BeatmapInfo> Beatmap = new Bindable<BeatmapInfo>();
[JsonProperty("name")]
public readonly Bindable<string> Name = new Bindable<string>("My awesome room!");
[JsonProperty("host")]
public readonly Bindable<User> Host = new Bindable<User>();
2018-12-12 18:04:11 +08:00
public bool ShouldSerializeHost() => false;
[JsonProperty("playlist")]
public readonly BindableCollection<PlaylistItem> Playlist = new BindableCollection<PlaylistItem>();
2018-12-12 18:04:11 +08:00
[JsonProperty("duration")]
public readonly Bindable<int> Duration = new Bindable<int>(100);
[JsonProperty("max_attempts")]
public readonly Bindable<int?> MaxAttempts = new Bindable<int?>(null);
2018-12-03 17:30:26 +08:00
public Bindable<RoomStatus> Status = new Bindable<RoomStatus>(new RoomStatusOpen());
2018-05-22 11:24:39 +08:00
public Bindable<RoomAvailability> Availability = new Bindable<RoomAvailability>();
2018-12-04 17:58:45 +08:00
public Bindable<GameType> Type = new Bindable<GameType>(new GameTypeTimeshift());
2018-04-13 17:19:50 +08:00
public Bindable<int?> MaxParticipants = new Bindable<int?>();
2018-12-03 19:50:40 +08:00
public Bindable<IEnumerable<User>> Participants = new Bindable<IEnumerable<User>>(Enumerable.Empty<User>());
2018-12-12 18:04:11 +08:00
public Room()
{
Beatmap.BindValueChanged(b =>
{
Playlist.Clear();
Playlist.Add(new PlaylistItem { Beatmap = b });
});
}
public void CopyFrom(Room other)
{
2018-12-12 15:20:11 +08:00
RoomID.Value = other.RoomID;
Name.Value = other.Name;
Host.Value = other.Host;
Status.Value = other.Status;
Availability.Value = other.Availability;
Type.Value = other.Type;
MaxParticipants.Value = other.MaxParticipants;
Participants.Value = other.Participants.Value.ToArray();
}
2018-04-13 17:19:50 +08:00
}
public class PlaylistItem
{
[JsonProperty("beatmap")]
2018-12-12 18:04:11 +08:00
private APIBeatmap beatmap { get; set; }
public bool ShouldSerializebeatmap() => false;
[JsonIgnore]
public BeatmapInfo Beatmap { get; set; }
[JsonProperty("beatmap_id")]
public int BeatmapID => 847296; //Beatmap.OnlineBeatmapID ?? 0;
[JsonProperty("ruleset_id")]
public int RulesetID { get; set; }
2018-12-12 18:04:11 +08:00
[JsonIgnore]
public readonly BindableCollection<Mod> AllowedMods = new BindableCollection<Mod>();
2018-12-12 18:04:11 +08:00
[JsonIgnore]
public readonly BindableCollection<Mod> RequiredMods = new BindableCollection<Mod>();
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;
}
private RulesetInfo ruleset;
2018-12-12 18:04:11 +08:00
[JsonIgnore]
public RulesetInfo Ruleset
{
get => ruleset;
set
{
ruleset = value;
if (_allowedMods != null)
{
AllowedMods.Clear();
AllowedMods.AddRange(value.CreateInstance().GetAllMods().Where(mod => _allowedMods.Any(m => m.Acronym == mod.Acronym)));
_allowedMods = null;
}
if (_requiredMods != null)
{
RequiredMods.Clear();
RequiredMods.AddRange(value.CreateInstance().GetAllMods().Where(mod => _requiredMods.Any(m => m.Acronym == mod.Acronym)));
_requiredMods = null;
}
}
}
2018-12-12 18:04:11 +08:00
public void SetRulesets(RulesetStore rulesets)
{
Beatmap = beatmap.ToBeatmap(rulesets);
}
// Todo: Move this elsewhere for reusability
private class APIMod : IMod
{
public string Acronym { get; set; }
}
}
2018-04-13 17:19:50 +08:00
}