Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions osu.Desktop/IPC/Messages/BeatmapMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 Newtonsoft.Json;
using osu.Game.Beatmaps;

namespace osu.Desktop.IPC.Messages
{
public class BeatmapMessage : OsuWebSocketMessage
{
[JsonProperty("beatmap_id")]
public required int BeatmapId { get; init; }

[JsonProperty("beatmap_set_id")]
public required int BeatmapSetId { get; init; }

[JsonProperty("beatmap_hash")]
public required string BeatmapHash { get; init; }

[JsonProperty("metadata")]
public required BeatmapMetadata Metadata { get; init; }

[JsonProperty("difficulty")]
public required BeatmapDifficulty Difficulty { get; init; }

[JsonProperty("difficulty_name")]
public required string DifficultyName { get; init; }

[JsonProperty("ruleset_id")]
public required int RulesetId { get; init; }

[JsonProperty("bpm")]
public required double BPM { get; init; }

[JsonProperty("star_rating")]
public required double StarRating { get; init; }

[JsonProperty("status")]
public required BeatmapOnlineStatus Status { get; init; }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want this enum (and any future cases) to serialize as a string rather than an int, depending on what future documentation for the WebSocket may look like once it reaches a mature state.

Indifferent, would appreciate another opinion.

}

public class BeatmapMetadata
{
[JsonProperty("artist")]
public required string Artist { get; init; }

[JsonProperty("artist_unicode")]
public required string ArtistUnicode { get; init; }

[JsonProperty("title")]
public required string Title { get; init; }

[JsonProperty("title_unicode")]
public required string TitleUnicode { get; init; }

[JsonProperty("author")]
public required string Author { get; init; }

[JsonProperty("source")]
public required string Source { get; init; }

[JsonProperty("tags")]
public required string Tags { get; init; }

[JsonProperty("user_tags")]
public required string[] UserTags { get; init; }
}

public class BeatmapDifficulty
{
[JsonProperty("approach_rate")]
public required double ApproachRate { get; init; }

[JsonProperty("circle_size")]
public required double CircleSize { get; init; }

[JsonProperty("overall_difficulty")]
public required double OverallDifficulty { get; init; }

[JsonProperty("drain_rate")]
public required double DrainRate { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace osu.Desktop.IPC.Messages
{
public class HitCountMessage : OsuWebSocketMessage
public class UserActivityMessage : OsuWebSocketMessage
{
[JsonProperty("new_hits")]
public long NewHits { get; init; }
[JsonProperty("status")]
public required string Status { get; init; }
}
}
69 changes: 62 additions & 7 deletions osu.Desktop/IPC/OsuWebSocketProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,99 @@
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.IPC;
using osu.Game.Online.Multiplayer;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Users;
using osu.Game.Utils;
using BeatmapDifficulty = osu.Desktop.IPC.Messages.BeatmapDifficulty;
using BeatmapMetadata = osu.Desktop.IPC.Messages.BeatmapMetadata;
using JsonConvert = Newtonsoft.Json.JsonConvert;

namespace osu.Desktop.IPC
{
public partial class OsuWebSocketProvider : Component
{
private WebSocketServer? server;
private readonly Bindable<ScoreInfo> lastLocalScore = new Bindable<ScoreInfo>();
private readonly Bindable<UserActivity?> userActivity = new Bindable<UserActivity?>();

[Resolved]
private Bindable<WorkingBeatmap> workingBeatmap { get; set; } = null!;

[BackgroundDependencyLoader]
private void load(SessionStatics sessionStatics)
{
server = new WebSocketServer(49727);
server.StartAsync().FireAndForget(onError: ex => Logger.Error(ex, "Failed to start websocket"));

sessionStatics.BindWith(Static.LastLocalUserScore, lastLocalScore);
sessionStatics.BindWith(Static.UserOnlineActivity, userActivity);
}

protected override void LoadComplete()
{
base.LoadComplete();

lastLocalScore.BindValueChanged(val =>
userActivity.BindValueChanged(val =>
{
if (val.NewValue == null)
return;

if (server?.IsRunning != true)
return;

var msg = new HitCountMessage { NewHits = val.NewValue.Statistics.Where(kv => kv.Key.IsBasic() && kv.Key.IsHit()).Sum(kv => kv.Value) };
var msg = new UserActivityMessage
{
Status = val.NewValue.GetType().Name,
};

broadcast(msg);
}, true);

workingBeatmap.BindValueChanged(val =>
{
// default beatmap on load
if (val.NewValue is DummyWorkingBeatmap)
return;

if (val.NewValue.BeatmapInfo.OnlineID == val.OldValue.BeatmapInfo.OnlineID)
return;

if (server?.IsRunning != true)
return;

var msg = new BeatmapMessage
{
BeatmapId = val.NewValue.BeatmapInfo.OnlineID,
BeatmapSetId = val.NewValue.BeatmapSetInfo.OnlineID,
BeatmapHash = val.NewValue.BeatmapInfo.OnlineMD5Hash,
Metadata = new BeatmapMetadata
{
Artist = val.NewValue.BeatmapInfo.Metadata.Artist,
ArtistUnicode = val.NewValue.BeatmapInfo.Metadata.ArtistUnicode,
Title = val.NewValue.BeatmapInfo.Metadata.Title,
TitleUnicode = val.NewValue.BeatmapInfo.Metadata.TitleUnicode,
Author = val.NewValue.BeatmapInfo.Metadata.Author.Username,
Source = val.NewValue.BeatmapInfo.Metadata.Source,
Tags = val.NewValue.BeatmapInfo.Metadata.Tags,
UserTags = val.NewValue.BeatmapInfo.Metadata.UserTags.ToArray(),
},
Difficulty = new BeatmapDifficulty
{
ApproachRate = val.NewValue.BeatmapInfo.Difficulty.ApproachRate,
CircleSize = val.NewValue.BeatmapInfo.Difficulty.CircleSize,
DrainRate = val.NewValue.BeatmapInfo.Difficulty.DrainRate,
OverallDifficulty = val.NewValue.BeatmapInfo.Difficulty.OverallDifficulty,
},
DifficultyName = val.NewValue.BeatmapInfo.DifficultyName,
RulesetId = val.NewValue.BeatmapInfo.Ruleset.OnlineID,
BPM = Math.Round(val.NewValue.BeatmapInfo.BPM, 2),
StarRating = val.NewValue.BeatmapInfo.StarRating.FloorToDecimalDigits(2),
Status = val.NewValue.BeatmapInfo.Status,
};

broadcast(msg);
});
}, true);
}

private void broadcast(OsuWebSocketMessage message)
Expand Down
Loading