1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 07:38:21 +08:00
osu-lazer/osu.Game/Online/API/Requests/SearchBeatmapSetsRequest.cs
2020-10-27 21:30:53 +03:00

110 lines
3.5 KiB
C#

// 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 osu.Framework.IO.Network;
using osu.Game.Extensions;
using osu.Game.Overlays;
using osu.Game.Overlays.BeatmapListing;
using osu.Game.Rulesets;
namespace osu.Game.Online.API.Requests
{
public class SearchBeatmapSetsRequest : APIRequest<SearchBeatmapSetsResponse>
{
public SearchCategory SearchCategory { get; }
public SortCriteria SortCriteria { get; }
public SortDirection SortDirection { get; }
public SearchGenre Genre { get; }
public SearchLanguage Language { get; }
public SearchExtra Extra { get; }
public SearchPlayed Played { get; }
private readonly string query;
private readonly RulesetInfo ruleset;
private readonly Cursor cursor;
private string directionString => SortDirection == SortDirection.Descending ? @"desc" : @"asc";
public SearchBeatmapSetsRequest(
string query,
RulesetInfo ruleset,
Cursor cursor = null,
SearchCategory searchCategory = SearchCategory.Any,
SortCriteria sortCriteria = SortCriteria.Ranked,
SortDirection sortDirection = SortDirection.Descending,
SearchGenre genre = SearchGenre.Any,
SearchLanguage language = SearchLanguage.Any,
SearchExtra extra = SearchExtra.Any,
SearchPlayed played = SearchPlayed.Any)
{
this.query = string.IsNullOrEmpty(query) ? string.Empty : System.Uri.EscapeDataString(query);
this.ruleset = ruleset;
this.cursor = cursor;
SearchCategory = searchCategory;
SortCriteria = sortCriteria;
SortDirection = sortDirection;
Genre = genre;
Language = language;
Extra = extra;
Played = played;
}
protected override WebRequest CreateWebRequest()
{
var req = base.CreateWebRequest();
req.AddParameter("q", query);
if (ruleset.ID.HasValue)
req.AddParameter("m", ruleset.ID.Value.ToString());
req.AddParameter("s", SearchCategory.ToString().ToLowerInvariant());
if (Genre != SearchGenre.Any)
req.AddParameter("g", ((int)Genre).ToString());
if (Language != SearchLanguage.Any)
req.AddParameter("l", ((int)Language).ToString());
req.AddParameter("sort", $"{SortCriteria.ToString().ToLowerInvariant()}_{directionString}");
req.AddCursor(cursor);
if (Extra != SearchExtra.Any)
{
string extraString = string.Empty;
switch (Extra)
{
case SearchExtra.Both:
extraString = "video.storyboard";
break;
case SearchExtra.Storyboard:
extraString = "storyboard";
break;
case SearchExtra.Video:
extraString = "video";
break;
}
req.AddParameter("e", extraString);
}
if (Played != SearchPlayed.Any)
req.AddParameter("played", Played.ToString().ToLowerInvariant());
return req;
}
protected override string Target => @"beatmapsets/search";
}
}