2018-12-11 16:32:01 +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
|
|
|
|
|
2018-12-17 10:51:12 +08:00
|
|
|
using System.Diagnostics;
|
2018-12-14 20:09:17 +08:00
|
|
|
using System.Net.Http;
|
2018-12-17 10:51:12 +08:00
|
|
|
using System.Threading;
|
2018-12-14 20:09:17 +08:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.IO.Network;
|
2018-12-17 10:51:12 +08:00
|
|
|
using osu.Framework.Logging;
|
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;
|
2018-12-14 20:09:17 +08:00
|
|
|
using osu.Game.Scoring;
|
2018-12-21 17:22:13 +08:00
|
|
|
using osu.Game.Screens.Multi.Ranking;
|
2018-12-11 16:32:01 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2018-12-21 17:22:13 +08:00
|
|
|
using osu.Game.Screens.Ranking;
|
2018-12-11 16:32:01 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Multi.Play
|
|
|
|
{
|
|
|
|
public class TimeshiftPlayer : Player
|
|
|
|
{
|
2018-12-21 17:22:13 +08:00
|
|
|
private readonly Room room;
|
2018-12-14 20:09:17 +08:00
|
|
|
private readonly int playlistItemId;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private APIAccess api { get; set; }
|
|
|
|
|
2018-12-21 17:22:13 +08:00
|
|
|
public TimeshiftPlayer(Room room, int playlistItemId)
|
2018-12-14 20:09:17 +08:00
|
|
|
{
|
2018-12-21 17:22:13 +08:00
|
|
|
this.room = room;
|
2018-12-14 20:09:17 +08:00
|
|
|
this.playlistItemId = playlistItemId;
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:51:12 +08:00
|
|
|
private int? token;
|
2018-12-14 20:09:17 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
2018-12-17 10:51:12 +08:00
|
|
|
token = null;
|
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
|
2018-12-21 17:22:13 +08:00
|
|
|
var req = new CreateScoreRequest(room.RoomID.Value ?? 0, playlistItemId);
|
2018-12-14 20:09:17 +08:00
|
|
|
req.Success += r => token = r.ID;
|
2018-12-17 10:51:12 +08:00
|
|
|
req.Failure += e =>
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
|
|
|
|
Logger.Error(e, "Failed to retrieve a score submission token.");
|
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
ValidForResume = false;
|
|
|
|
Exit();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-14 20:09:17 +08:00
|
|
|
api.Queue(req);
|
2018-12-17 10:51:12 +08:00
|
|
|
|
|
|
|
while (!failed && !token.HasValue)
|
|
|
|
Thread.Sleep(1000);
|
2018-12-14 20:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override ScoreInfo CreateScore()
|
|
|
|
{
|
|
|
|
var score = base.CreateScore();
|
|
|
|
|
2018-12-17 10:51:12 +08:00
|
|
|
Debug.Assert(token != null);
|
|
|
|
|
2018-12-21 17:22:13 +08:00
|
|
|
var request = new SubmitScoreRequest(token.Value, room.RoomID.Value ?? 0, playlistItemId, score);
|
2018-12-17 10:51:12 +08:00
|
|
|
request.Failure += e => Logger.Error(e, "Failed to submit score");
|
2018-12-14 20:09:17 +08:00
|
|
|
api.Queue(request);
|
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
2018-12-21 17:22:13 +08:00
|
|
|
|
|
|
|
protected override Results CreateResults(ScoreInfo score) => new MultiResults(score, room);
|
2018-12-14 20:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class SubmitScoreRequest : APIRequest
|
|
|
|
{
|
|
|
|
private readonly int scoreId;
|
|
|
|
private readonly int roomId;
|
|
|
|
private readonly int playlistItemId;
|
|
|
|
private readonly ScoreInfo scoreInfo;
|
|
|
|
|
|
|
|
public SubmitScoreRequest(int scoreId, int roomId, int playlistItemId, ScoreInfo scoreInfo)
|
|
|
|
{
|
|
|
|
this.scoreId = scoreId;
|
|
|
|
this.roomId = roomId;
|
|
|
|
this.playlistItemId = playlistItemId;
|
|
|
|
this.scoreInfo = scoreInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override WebRequest CreateWebRequest()
|
|
|
|
{
|
|
|
|
var req = base.CreateWebRequest();
|
|
|
|
|
|
|
|
req.ContentType = "application/json";
|
|
|
|
req.Method = HttpMethod.Put;
|
|
|
|
|
|
|
|
req.AddRaw(JsonConvert.SerializeObject(scoreInfo));
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string Target => $@"rooms/{roomId}/playlist/{playlistItemId}/scores/{scoreId}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CreateScoreRequest : APIRequest<CreateScoreResult>
|
|
|
|
{
|
|
|
|
private readonly int roomId;
|
|
|
|
private readonly int playlistItemId;
|
|
|
|
|
|
|
|
public CreateScoreRequest(int roomId, int playlistItemId)
|
|
|
|
{
|
|
|
|
this.roomId = roomId;
|
|
|
|
this.playlistItemId = playlistItemId;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override WebRequest CreateWebRequest()
|
|
|
|
{
|
|
|
|
var req = base.CreateWebRequest();
|
|
|
|
req.Method = HttpMethod.Post;
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string Target => $@"rooms/{roomId}/playlist/{playlistItemId}/scores";
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CreateScoreResult
|
|
|
|
{
|
|
|
|
[JsonProperty("id")]
|
|
|
|
public int ID { get; set; }
|
2018-12-11 16:32:01 +08:00
|
|
|
}
|
|
|
|
}
|