Skip to content

Commit

Permalink
Rewrite commands to use FafApiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Crotalus committed Nov 30, 2023
1 parent a44692e commit 7654e7e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override async Task DiscordReplyAsync(DiscordCommandContext ctx, MapPool
var embed = new DiscordEmbedBuilder()
.WithTitle($"{pool.MatchmakerQueueMapPool.Name}")
.WithColor(Context.DostyaRed)
.WithThumbnail(pool.MapVersions[0].ThumbnailUrlSmall.Replace(" ", "%20") ?? "");
.WithThumbnail(pool.MapVersions[0].ThumbnailUrlLarge?.ToString().Replace(" ", "%20") ?? "");

int i = 0;
foreach (var map in pool.MapVersions)
Expand Down
41 changes: 22 additions & 19 deletions src/Faforever.Qai.Core/Commands/Dual/Map/GetMapCommand.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System.Threading.Tasks;

using DSharpPlus.Entities;

using Faforever.Qai.Core.Commands.Context;
using Faforever.Qai.Core.Models;
using Faforever.Qai.Core.Operations.Maps;

using Qmmands;

namespace Faforever.Qai.Core.Commands.Dual.Map
{
public class GetMapCommand : DualCommandModule<MapResult>
public class GetMapCommand : DualCommandModule<Operations.FafApi.Map>
{
private readonly ISearchMapOperation _map;

Expand Down Expand Up @@ -41,28 +38,34 @@ public async Task GetMapCommandAsync([Remainder] string map)
}
}

public override async Task DiscordReplyAsync(DiscordCommandContext ctx, MapResult data)
public override async Task DiscordReplyAsync(DiscordCommandContext ctx, Operations.FafApi.Map data)
{
var embed = new DiscordEmbedBuilder();
var version = data.LatestVersion;

embed.WithTitle("Download map")
.WithColor(Context.DostyaRed)
.WithUrl(data.DownloadUrl?.AbsoluteUri.Replace(" ", "%20"))
.WithAuthor($"{data.Title} (ID #{data.Id})")
.WithDescription(data.Description)
.AddField("Size", data.Size, true)
.AddField("Max Players", data.MaxPlayers.ToString(), true)
.AddField("Ranked", data.Ranked.ToString(), true)
.AddField("Created At", data.CreatedAt?.ToString("u"), true)
.AddField("Author", data.Author ?? "Unknown")
.WithImageUrl(data.PreviewUrl?.AbsoluteUri.Replace(" ", "%20"));
.WithUrl(version.DownloadUrl?.AbsoluteUri.Replace(" ", "%20"))
.WithAuthor($"{version.Id} (ID #{data.Id})")
.WithDescription(version.Description)
.AddField("Size", version.Size, true)
.AddField("Max Players", version.MaxPlayers.ToString(), true)
.AddField("Ranked", version.Ranked.ToString(), true)
.AddField("Created At", version.CreateTime.ToString("u"), true)
.AddField("Author", data.Author?.Login ?? "Unknown")
.WithImageUrl(version.ThumbnailUrlLarge?.AbsoluteUri.Replace(" ", "%20"));

await Context.ReplyAsync(embed);
}

public override async Task IrcReplyAsync(IrcCommandContext ctx, MapResult data)
=> await Context.ReplyAsync($"Map: {data.Title}, ID: {data.Id}, Size: {data.Size}," +
$" Players: {data.MaxPlayers}, Ranked: {data.Ranked}, Author: {data.Author}," +
$" Download: {data.DownloadUrl?.AbsoluteUri.Replace(" ", "%20")}," +
$" Preview: {data.PreviewUrl?.AbsoluteUri.Replace(" ", "%20")}");
public override async Task IrcReplyAsync(IrcCommandContext ctx, Operations.FafApi.Map data)
{
var latest = data.LatestVersion;

await Context.ReplyAsync($"Map: {data.DisplayName}, ID: {data.Id}, Size: {latest.Size}," +
$" Players: {latest.MaxPlayers}, Ranked: {latest.Ranked}, Author: {data.Author}," +
$" Download: {latest.DownloadUrl?.AbsoluteUri.Replace(" ", "%20")}," +
$" Preview: {latest.ThumbnailUrlLarge?.AbsoluteUri.Replace(" ", "%20")}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ private DiscordEmbed DiscordResponse(Game game)
var mapName = game.MapVersion != null ? $"{game.MapVersion.Map.DisplayName}" : "Unknown";

string? thumbnail = null;
if (game.MapVersion?.ThumbnailUrlLarge != null && Uri.TryCreate(game.MapVersion?.ThumbnailUrlLarge!, UriKind.Absolute, out var uri))
thumbnail = uri.AbsoluteUri;
if (game.MapVersion?.ThumbnailUrlLarge != null)
thumbnail = game.MapVersion.ThumbnailUrlLarge.AbsoluteUri;

embed
.WithColor(Context.DostyaRed)
Expand Down
9 changes: 5 additions & 4 deletions src/Faforever.Qai.Core/Operations/FafApi/Dto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class MapVersion
public int Id { get; set; }
public DateTime CreateTime { get; set; }
public string Description { get; set; }
public string DownloadUrl { get; set; }
public Uri DownloadUrl { get; set; }
public string Filename { get; set; }
public string FolderName { get; set; }
public short Height { get; set; }
public bool Hidden { get; set; }
public byte MaxPlayers { get; set; }
public bool Ranked { get; set; }
public string ThumbnailUrlLarge { get; set; }
public string ThumbnailUrlSmall { get; set; }
public Uri ThumbnailUrlLarge { get; set; }
public Uri ThumbnailUrlSmall { get; set; }
public DateTime UpdateTime { get; set; }
public short Version { get; set; }
public short Width { get; set; }
Expand All @@ -64,8 +64,9 @@ public class Map
public string MapType { get; set; }
public int NumberOfReviews { get; set; }
public DateTime UpdateTime { get; set; }

public MapVersion LatestVersion { get; set; }
public MapStatistics Statistics { get; set; }
public Player Author { get; set; }
}

public class MapStatistics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,6 @@ public async Task<IEnumerable<MapPool>> FetchLadderPoolAsync()
var mapPools = await _api.GetAsync(fafApiQuery) ?? new List<MapPool>();

return mapPools.Where(mp => !mp.MatchmakerQueueMapPool.MatchmakerQueue.TechnicalName.Contains("tmm4v4_full_share")).ToArray();

/*
string json = await _api.Client.GetStringAsync("/data/mapPool?include=matchmakerQueueMapPool");
var data = JObject.Parse(json);
JToken? allData = data["included"];
if (allData is null) return null;
var maps = allData.Where(x => x["type"]?.ToString() == "map");
var versions = allData.Where(x => x["type"]?.ToString() == "mapVersion");
List<MapResult> res = new();
foreach (var map in maps)
{
var included = versions.FirstOrDefault(x => x["id"]?.ToObject<long>() == map["relationships"]?["latestVersion"]?["data"]?["id"]?.ToObject<long>());
if (included is null) continue;
var size = included["attributes"]?["height"]?.ToObject<long>().GetMapSize() ?? 0;
res.Add(new()
{
Title = map["attributes"]?["displayName"]?.ToString(),
CreatedAt = map["attributes"]?["createTime"]?.ToObject<DateTime>(),
Id = map["id"]?.ToObject<long>() ?? 0,
Ranked = included["attributes"]?["ranked"]?.ToObject<bool>(),
DownloadUrl = included["attributes"]?["downloadUrl"]?.ToObject<Uri>(),
PreviewUrl = included["attributes"]?["thumbnailUrlLarge"]?.ToObject<Uri>(),
Description = included["attributes"]?["description"]?.ToString().RemoveBadContent(),
MaxPlayers = included["attributes"]?["maxPlayers"]?.ToObject<long>(),
Author = null,
Size = $"{size}x{size} km",
Version = map["attributes"]?["version"]?.ToObject<int>()
});
}
return res;
*/
}
}
}
31 changes: 19 additions & 12 deletions src/Faforever.Qai.Core/Operations/Maps/ApiSearchMapOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@

using Faforever.Qai.Core.Models;
using Faforever.Qai.Core.Operations.Clients;
using Faforever.Qai.Core.Operations.FafApi;
using Newtonsoft.Json.Linq;

namespace Faforever.Qai.Core.Operations.Maps
{
public class ApiSearchMapOperation : ISearchMapOperation
{
private readonly ApiHttpClient _api;
private readonly FafApiClient _api;

public ApiSearchMapOperation(ApiHttpClient api)
public ApiSearchMapOperation(FafApiClient api)
{
_api = api;
}

public async Task<MapResult?> GetMapAsync(string map)
public async Task<Map?> GetMapAsync(string map)
{
string data =
await this._api.Client.GetStringAsync(
$"/data/map?page[size]=1&include=versions,author&filter=displayName==\"{map}\"");
var query = new ApiQuery<Map>()
.Include("versions,author")
.Where("displayName", map)
.Sort("-gamesPlayed");

return ParseStringData(data);
var maps = await this._api.GetAsync(query);

return maps.FirstOrDefault();
}

public async Task<MapResult?> GetMapAsync(int mapId)
public async Task<Map?> GetMapAsync(int mapId)
{
string data =
await this._api.Client.GetStringAsync(
$"/data/map?page[size]=1&include=versions,author&filter=id=={mapId}");
var query = new ApiQuery<Map>()
.Include("versions,author")
.Where("id", mapId)
.Sort("-gamesPlayed");

var maps = await this._api.GetAsync(query);

return ParseStringData(data);
return maps.FirstOrDefault();
}

private MapResult? ParseStringData(string data)
Expand Down
5 changes: 3 additions & 2 deletions src/Faforever.Qai.Core/Operations/Maps/ISearchMapOperation.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Threading.Tasks;

using Faforever.Qai.Core.Models;
using Faforever.Qai.Core.Operations.FafApi;

namespace Faforever.Qai.Core.Operations.Maps
{
public interface ISearchMapOperation
{
public Task<MapResult?> GetMapAsync(string map);
public Task<MapResult?> GetMapAsync(int mapId);
public Task<Map?> GetMapAsync(string map);
public Task<Map?> GetMapAsync(int mapId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Faforever.Qai.Core.Operations.Maps;
using Faforever.Qai.Core.Operations.PatchNotes;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Threading.Tasks;

namespace Faforever.Qai.Core.Tests.Operations
{
public class FetchLadderPoolOperationTest : OperationTestBase
{
private IFetchLadderPoolOperation operation { get; set; }
private IServiceScope scope { get; set; }

[OneTimeSetUp]
public void FetchReplaySetUp()
{
scope = Services.CreateScope();
operation = scope.ServiceProvider.GetRequiredService<IFetchLadderPoolOperation>();
}

[TestCase(TestName = "Fetch patch notes")]
public async Task FetchPatchNotes()
{
var res = await operation.FetchLadderPoolAsync();

Assert.NotNull(res);
Assert.IsNotEmpty(res);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Faforever.Qai.Core.Operations.Replays;
using Faforever.Qai.Core.Services;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Faforever.Qai.Core.Operations.Maps;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Threading.Tasks;

namespace Faforever.Qai.Core.Tests.Operations
{
public class SearchMapOperationTest : OperationTestBase
{
private ISearchMapOperation operation { get; set; }
private IServiceScope scope { get; set; }

[OneTimeSetUp]
public void FetchReplaySetUp()
{
scope = Services.CreateScope();
operation = scope.ServiceProvider.GetRequiredService<ISearchMapOperation>();
}

[TestCase("DualGap_fix_adaptive", TestName = "Fetch map by name")]
public async Task GetMapByName(string mapName)
{
var res = await operation.GetMapAsync(mapName);

Assert.NotNull(res);
Assert.IsNotEmpty(res.DisplayName);
}
}
}

0 comments on commit 7654e7e

Please sign in to comment.