1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 03:52:55 +08:00
osu-lazer/osu.Game/Online/Chat/NowPlayingCommand.cs

127 lines
3.9 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.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
2022-11-30 13:47:16 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Users;
namespace osu.Game.Online.Chat
{
2022-11-24 13:32:20 +08:00
public partial class NowPlayingCommand : Component
{
[Resolved]
private IChannelPostTarget channelManager { get; set; } = null!;
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private Bindable<WorkingBeatmap> currentBeatmap { get; set; } = null!;
[Resolved]
private Bindable<IReadOnlyList<Mod>> selectedMods { get; set; } = null!;
2022-11-30 13:47:16 +08:00
[Resolved]
private IBindable<RulesetInfo> currentRuleset { get; set; } = null!;
[Resolved]
private LocalisationManager localisation { get; set; } = null!;
private readonly Channel? target;
2021-06-29 15:30:40 +08:00
/// <summary>
/// Creates a new <see cref="NowPlayingCommand"/> to post the currently-playing beatmap to a parenting <see cref="IChannelPostTarget"/>.
/// </summary>
/// <param name="target">The target channel to post to. If <c>null</c>, the currently-selected channel will be posted to.</param>
public NowPlayingCommand(Channel target)
{
this.target = target;
}
protected override void LoadComplete()
{
base.LoadComplete();
string verb;
IBeatmapInfo beatmapInfo;
switch (api.Activity.Value)
{
case UserActivity.InGame game:
verb = "playing";
2021-10-02 23:55:29 +08:00
beatmapInfo = game.BeatmapInfo;
break;
case UserActivity.Editing edit:
verb = "editing";
2021-10-02 23:55:29 +08:00
beatmapInfo = edit.BeatmapInfo;
break;
default:
verb = "listening to";
2021-10-02 23:55:29 +08:00
beatmapInfo = currentBeatmap.Value.BeatmapInfo;
break;
}
string[] pieces =
{
"is",
verb,
getBeatmapPart(),
2022-11-30 13:47:16 +08:00
getRulesetPart(),
getModPart(),
};
channelManager.PostMessage(string.Join(' ', pieces.Where(p => !string.IsNullOrEmpty(p))), true, target);
2022-11-30 13:31:54 +08:00
Expire();
2022-11-27 08:41:08 +08:00
string getBeatmapPart()
{
string beatmapInfoString = localisation.GetLocalisedBindableString(beatmapInfo.GetDisplayTitleRomanisable()).Value;
return beatmapInfo.OnlineID > 0 ? $"[{api.WebsiteRootUrl}/b/{beatmapInfo.OnlineID} {beatmapInfoString}]" : beatmapInfoString;
}
2022-11-30 13:47:16 +08:00
string getRulesetPart()
{
if (api.Activity.Value is not UserActivity.InGame) return string.Empty;
return $"<{currentRuleset.Value.Name}>";
}
2022-11-27 08:41:08 +08:00
string getModPart()
{
if (api.Activity.Value is not UserActivity.InGame) return string.Empty;
2022-11-27 08:41:41 +08:00
if (selectedMods.Value.Count == 0)
{
return string.Empty;
}
2022-11-30 13:31:54 +08:00
StringBuilder modsString = new StringBuilder();
foreach (var mod in selectedMods.Value.Where(mod => mod.Type == ModType.DifficultyIncrease))
{
2022-11-30 13:31:54 +08:00
modsString.Append($"+{mod.Acronym} ");
}
foreach (var mod in selectedMods.Value.Where(mod => mod.Type != ModType.DifficultyIncrease))
{
2022-11-30 13:31:54 +08:00
modsString.Append($"-{mod.Acronym} ");
}
2022-11-30 13:31:54 +08:00
return modsString.ToString().Trim();
}
}
}
}