1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 05:27:26 +08:00
osu-lazer/osu.Game/Screens/Multi/Play/TimeshiftPlayer.cs

121 lines
3.7 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-27 20:12:32 +08:00
using System;
using System.Diagnostics;
2019-02-28 13:58:44 +08:00
using System.Linq;
using System.Threading;
2018-12-14 20:09:17 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Logging;
2019-01-23 19:52:00 +08:00
using osu.Framework.Screens;
2018-12-14 20:09:17 +08:00
using osu.Game.Online.API;
2018-12-21 17:22:13 +08:00
using osu.Game.Online.Multiplayer;
using osu.Game.Rulesets;
2018-12-14 20:09:17 +08:00
using osu.Game.Scoring;
using osu.Game.Screens.Multi.Ranking;
using osu.Game.Screens.Play;
using osu.Game.Screens.Ranking;
namespace osu.Game.Screens.Multi.Play
{
public class TimeshiftPlayer : Player
{
2019-01-23 19:52:00 +08:00
public Action Exited;
2019-02-05 18:00:01 +08:00
[Resolved(typeof(Room), nameof(Room.RoomID))]
private Bindable<int?> roomId { get; set; }
2019-02-08 17:33:49 +08:00
private readonly PlaylistItem playlistItem;
2018-12-14 20:09:17 +08:00
[Resolved]
private IAPIProvider api { get; set; }
2018-12-14 20:09:17 +08:00
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; }
2019-02-08 17:33:49 +08:00
public TimeshiftPlayer(PlaylistItem playlistItem)
2018-12-14 20:09:17 +08:00
{
2019-02-08 17:33:49 +08:00
this.playlistItem = playlistItem;
2018-12-14 20:09:17 +08:00
}
private int? token;
2018-12-14 20:09:17 +08:00
[BackgroundDependencyLoader]
private void load()
{
token = null;
bool failed = false;
// Sanity checks to ensure that TimeshiftPlayer matches the settings for the current PlaylistItem
if (Beatmap.Value.BeatmapInfo.OnlineBeatmapID != playlistItem.Beatmap.Value.OnlineBeatmapID)
throw new InvalidOperationException("Current Beatmap does not match PlaylistItem's Beatmap");
if (ruleset.Value.ID != playlistItem.Ruleset.Value.ID)
throw new InvalidOperationException("Current Ruleset does not match PlaylistItem's Ruleset");
if (!playlistItem.RequiredMods.All(m => Mods.Value.Any(m.Equals)))
throw new InvalidOperationException("Current Mods do not match PlaylistItem's RequiredMods");
2020-07-29 12:18:40 +08:00
var req = new CreateRoomScoreRequest(roomId.Value ?? 0, playlistItem.ID, Game.VersionHash);
2018-12-14 20:09:17 +08:00
req.Success += r => token = r.ID;
req.Failure += e =>
{
failed = true;
Logger.Error(e, "Failed to retrieve a score submission token.\n\nThis may happen if you are running an old or non-official release of osu! (ie. you are self-compiling).");
Schedule(() =>
{
ValidForResume = false;
2019-01-23 19:52:00 +08:00
this.Exit();
});
};
2018-12-14 20:09:17 +08:00
api.Queue(req);
while (!failed && !token.HasValue)
Thread.Sleep(1000);
2018-12-14 20:09:17 +08:00
}
2019-01-23 19:52:00 +08:00
public override bool OnExiting(IScreen next)
{
if (base.OnExiting(next))
return true;
Exited?.Invoke();
return false;
}
protected override ResultsScreen CreateResults(ScoreInfo score)
{
Debug.Assert(roomId.Value != null);
return new TimeshiftResultsScreen(score, roomId.Value.Value, playlistItem, true);
}
2018-12-14 20:09:17 +08:00
protected override ScoreInfo CreateScore()
{
var score = base.CreateScore();
2018-12-27 20:12:32 +08:00
score.TotalScore = (int)Math.Round(ScoreProcessor.GetStandardisedScore());
Debug.Assert(token != null);
2019-02-08 17:33:49 +08:00
var request = new SubmitRoomScoreRequest(token.Value, roomId.Value ?? 0, playlistItem.ID, score);
2020-06-09 17:21:37 +08:00
request.Success += s => score.OnlineScoreID = s.ID;
request.Failure += e => Logger.Error(e, "Failed to submit score");
2018-12-14 20:09:17 +08:00
api.Queue(request);
2020-06-09 17:21:37 +08:00
return score;
2018-12-14 20:09:17 +08:00
}
2018-12-21 17:22:13 +08:00
2019-01-23 19:52:00 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Exited = null;
}
2018-12-14 20:09:17 +08:00
}
}