1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 01:47:25 +08:00
osu-lazer/osu.Game/Online/API/Requests/GetScoresRequest.cs

202 lines
5.6 KiB
C#
Raw Normal View History

2017-03-15 16:50:52 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2017-03-15 16:50:52 +08:00
using System.Collections.Generic;
using System.Linq;
2017-03-15 13:06:05 +08:00
using Newtonsoft.Json;
2017-07-26 12:22:46 +08:00
using osu.Game.Beatmaps;
2017-09-14 19:05:43 +08:00
using osu.Game.Rulesets;
using osu.Game.Users;
using osu.Game.Rulesets.Replays;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Select.Leaderboards;
using System.Collections.Specialized;
2017-03-15 13:06:05 +08:00
namespace osu.Game.Online.API.Requests
{
public class GetScoresRequest : APIRequest<GetScoresResponse>
{
private readonly BeatmapInfo beatmap;
private readonly LeaderboardScope scope;
private readonly RulesetInfo ruleset;
2017-03-15 13:06:05 +08:00
public GetScoresRequest(BeatmapInfo beatmap)
{
if (!beatmap.OnlineBeatmapID.HasValue)
throw new InvalidOperationException($"Cannot lookup a beatmap's scores without having a populated {nameof(BeatmapInfo.OnlineBeatmapID)}.");
this.beatmap = beatmap;
Success += onSuccess;
}
public GetScoresRequest(BeatmapInfo beatmap, LeaderboardScope scope, RulesetInfo ruleset)
2017-03-15 13:06:05 +08:00
{
if (!beatmap.OnlineBeatmapID.HasValue)
throw new InvalidOperationException($"Cannot lookup a beatmap's scores without having a populated {nameof(BeatmapInfo.OnlineBeatmapID)}.");
2017-03-15 13:06:05 +08:00
this.beatmap = beatmap;
this.scope = scope;
this.ruleset = ruleset;
Success += onSuccess;
}
private void onSuccess(GetScoresResponse r)
{
foreach (OnlineScore score in r.Scores)
score.ApplyBeatmap(beatmap);
2017-03-15 13:06:05 +08:00
}
private string mapScopeToQuery()
{
switch(scope)
{
case LeaderboardScope.Global:
return @"type=global";
case LeaderboardScope.Friends:
return @"type=friend";
case LeaderboardScope.Country:
return @"type=country";
default:
return String.Empty;
}
}
private string mapRulesetToQuery()
{
switch(ruleset.Name)
{
case @"osu!":
return @"mode=osu";
case @"osu!taiko":
return @"mode=taiko";
case @"osu!catch":
return @"mode=catch";
case @"osu!mania":
return @"mode=mania";
default:
return String.Empty;
}
}
protected override string Target => $@"beatmaps/{beatmap.OnlineBeatmapID}/scores?{mapScopeToQuery()}&{mapRulesetToQuery()}";
2017-03-15 13:06:05 +08:00
}
public class GetScoresResponse
{
[JsonProperty(@"scores")]
public IEnumerable<OnlineScore> Scores;
2017-03-15 13:06:05 +08:00
}
public class OnlineScore : Score
{
[JsonProperty(@"score")]
private double totalScore
{
set { TotalScore = value; }
}
[JsonProperty(@"max_combo")]
private int maxCombo
{
set { MaxCombo = value; }
}
[JsonProperty(@"user")]
private User user
{
set { User = value; }
}
[JsonProperty(@"replay_data")]
private Replay replay
{
set { Replay = value; }
}
2017-09-14 19:05:43 +08:00
[JsonProperty(@"mode_int")]
public int OnlineRulesetID { get; set; }
[JsonProperty(@"score_id")]
private long onlineScoreID
{
set { OnlineScoreID = value; }
}
[JsonProperty(@"created_at")]
private DateTimeOffset date
{
set { Date = value; }
}
2017-09-14 19:05:43 +08:00
[JsonProperty(@"beatmap")]
private BeatmapInfo beatmap
{
set { Beatmap = value; }
}
[JsonProperty(@"beatmapset")]
private BeatmapMetadata metadata
{
set { Beatmap.Metadata = value; }
}
[JsonProperty(@"statistics")]
private Dictionary<string, object> jsonStats
{
set
{
foreach (var kvp in value)
{
string key = kvp.Key;
switch (key)
{
case @"count_300":
key = @"300";
break;
case @"count_100":
key = @"100";
break;
case @"count_50":
key = @"50";
break;
case @"count_miss":
key = @"x";
break;
default:
continue;
}
Statistics.Add(key, kvp.Value);
}
}
}
[JsonProperty(@"mods")]
private string[] modStrings { get; set; }
public void ApplyBeatmap(BeatmapInfo beatmap)
{
Beatmap = beatmap;
2017-09-14 19:05:43 +08:00
ApplyRuleset(beatmap.Ruleset);
}
public void ApplyRuleset(RulesetInfo ruleset)
{
Ruleset = ruleset;
// Evaluate the mod string
Mods = Ruleset.CreateInstance().GetAllMods().Where(mod => modStrings.Contains(mod.ShortenedName)).ToArray();
}
}
}