Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added endpoint for GetGameMapsPlaytime #93

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Steam.Models/CSGO/GameMapsPlaytimeGameMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Steam.Models.CSGO
{
public enum GameMapsPlaytimeGameMode
{
Competitive,
Casual
}
}
10 changes: 10 additions & 0 deletions src/Steam.Models/CSGO/GameMapsPlaytimeInterval.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace Steam.Models.CSGO
{
public enum GameMapsPlaytimeInterval
{
Day,
Week,
Month
}
}
7 changes: 7 additions & 0 deletions src/Steam.Models/CSGO/GameMapsPlaytimeMapGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Steam.Models.CSGO
{
public enum GameMapsPlaytimeMapGroup
{
Operation
}
}
11 changes: 11 additions & 0 deletions src/Steam.Models/CSGO/GameMapsPlaytimeModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Steam.Models.CSGO
{
public class GameMapsPlaytimeModel
{
public DateTime IntervalStartTimeStamp { get; set; }
public string MapName { get; set; }
public float RelativePercentage { get; set; }
}
}
18 changes: 14 additions & 4 deletions src/Steam.UnitTests/CSGOServersTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
using Microsoft.Extensions.Configuration;
using Steam.Models.CSGO;
using SteamWebAPI2.Interfaces;
using SteamWebAPI2.Utilities;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Steam.UnitTests
{
public class CSGOServersTests : BaseTest
{
[Fact]
public async Task GetGameMapsPlaytimeAsync_Should_Succeed()
{
var steamInterface = factory.CreateSteamWebInterface<CSGOServers>(new HttpClient());
var response = await steamInterface.GetGameMapsPlaytimeAsync(
GameMapsPlaytimeInterval.Week,
GameMapsPlaytimeGameMode.Competitive,
GameMapsPlaytimeMapGroup.Operation
);
Assert.NotNull(response);
Assert.NotNull(response.Data);
}

[Fact]
public async Task GetGameServerStatusAsync_Should_Succeed()
{
Expand Down
7 changes: 7 additions & 0 deletions src/SteamWebAPI2/AutoMapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static void Initialize()
CreateSteamWebResponseMap<SteamServerInfo, SteamServerInfoModel>(x);
CreateSteamWebResponseMap<SteamApiListContainer, IReadOnlyCollection<SteamInterfaceModel>>(x);
CreateSteamWebResponseMap<GoldenWrenchResultContainer, IReadOnlyCollection<GoldenWrenchModel>>(x);
CreateSteamWebResponseMap<GameMapsPlaytimeContainer, IEnumerable<GameMapsPlaytimeModel>>(x);

#region Endpoint: DOTA2Econ

Expand Down Expand Up @@ -551,6 +552,12 @@ public static void Initialize()
src => Mapper.Map<ServerStatusResult, ServerStatusModel>(src.Result)
);

x.CreateMap<GameMapsPlaytimeContainer, IEnumerable<GameMapsPlaytimeModel>>().ConvertUsing(
src => Mapper.Map<IEnumerable<GameMapsPlaytime>, IEnumerable<GameMapsPlaytimeModel>>(src.Result.Playtimes)
);
x.CreateMap<GameMapsPlaytime, GameMapsPlaytimeModel>()
.ForMember(dest => dest.IntervalStartTimeStamp, opts => opts.MapFrom(source => source.IntervalStartTimeStamp.ToDateTime()));

x.CreateMap<EconItem, EconItemModel>();
x.CreateMap<EconItemAttribute, EconItemAttributeModel>();
x.CreateMap<EconItemAttributeAccountInfo, EconItemAttributeAccountInfoModel>();
Expand Down
26 changes: 26 additions & 0 deletions src/SteamWebAPI2/Interfaces/CSGOServers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Steam.Models.CSGO;
using SteamWebAPI2.Models.CSGO;
using SteamWebAPI2.Utilities;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SteamWebAPI2.Interfaces
Expand All @@ -23,6 +24,31 @@ public CSGOServers(ISteamWebRequest steamWebRequest, ISteamWebInterface steamWeb
: steamWebInterface;
}

/// <summary>
/// Maps to the Steam Web API interface/method of ICSGOServers_730/GetGameMapsPlaytime/v1
/// </summary>
/// <returns></returns>
public async Task<ISteamWebResponse<IEnumerable<GameMapsPlaytimeModel>>> GetGameMapsPlaytimeAsync(
GameMapsPlaytimeInterval interval,
GameMapsPlaytimeGameMode gameMode,
GameMapsPlaytimeMapGroup mapGroup
)
{
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();

parameters.AddIfHasValue(interval.ToString().ToLower(), "interval");
parameters.AddIfHasValue(gameMode.ToString().ToLower(), "gamemode");
parameters.AddIfHasValue(mapGroup.ToString().ToLower(), "mapgroup");

var steamWebResponse = await steamWebInterface.GetAsync<GameMapsPlaytimeContainer>("GetGameMapsPlaytime", 1, parameters);

var steamWebResponseModel = AutoMapperConfiguration.Mapper.Map<
ISteamWebResponse<GameMapsPlaytimeContainer>,
ISteamWebResponse<IEnumerable<GameMapsPlaytimeModel>>>(steamWebResponse);

return steamWebResponseModel;
}

/// <summary>
/// Maps to the Steam Web API interface/method of ICSGOServers_730/GetGameServersStatus/v1
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/SteamWebAPI2/Models/CSGO/GameMapsPlaytimeContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using SteamWebAPI2.Utilities.JsonConverters;

namespace SteamWebAPI2.Models.CSGO
{
public class GameMapsPlaytime
{
public ulong IntervalStartTimeStamp { get; set; }
public string MapName { get; set; }
public float RelativePercentage { get; set; }
}

public class GameMapsPlaytimeResult
{
public IEnumerable<GameMapsPlaytime> Playtimes { get; set; }
}

public class GameMapsPlaytimeContainer
{
[JsonConverter(typeof(GameMapsPlaytimeConverter))]
public GameMapsPlaytimeResult Result { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SteamWebAPI2.Models.CSGO;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace SteamWebAPI2.Utilities.JsonConverters
{
internal class GameMapsPlaytimeConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}

/// <summary>
/// Custom deserialization required because raw API response is awful. Instead of returning real JSON objects, the API
/// returns an array of "keys" and a matrix of "rows". We need to match up the keys to the columns within the rows
/// to figure out which value goes with which key.
///
/// Example response:
///
/// "Keys": ["Key 1", "Key 2", "Key 3"],
/// "Rows": [
/// ["A 1", "A 2", "A 3"],
/// ["B 1", "B 2", "B 3"]
/// ]
/// </summary>
/// <param name="reader"></param>
/// <param name="objectType"></param>
/// <param name="existingValue"></param>
/// <param name="serializer"></param>
/// <returns></returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}

List<GameMapsPlaytime> playtimes = new List<GameMapsPlaytime>();

JObject o = JObject.Load(reader);

var keys = o["Keys"];
var rows = o["Rows"];

foreach (var row in rows)
{
int columnIndex = 0;
GameMapsPlaytime playtime = new GameMapsPlaytime();
foreach (var value in row)
{
var key = keys[columnIndex];
if (key.ToString() == "IntervalStartTimeStamp")
{
playtime.IntervalStartTimeStamp = ulong.Parse(value.ToString());
}
else if (key.ToString() == "MapName")
{
playtime.MapName = value.ToString();
}
else if (key.ToString() == "RelativePercentage")
{
playtime.RelativePercentage = float.Parse(value.ToString());
}
columnIndex++;
}
playtimes.Add(playtime);
}

return new GameMapsPlaytimeResult()
{
Playtimes = playtimes
};
}

public override bool CanWrite { get { return false; } }

public override bool CanConvert(Type objectType)
{
return typeof(GameMapsPlaytimeResult).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
}
}
}