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