Skip to content

Commit 52ddd5a

Browse files
author
babelshift
committed
Added wrapper and manual deserialization for GetGlobalStatsForGame.
1 parent cc07721 commit 52ddd5a

9 files changed

Lines changed: 163 additions & 19 deletions

SteamWebAPI2.Models/AchievementPercentage.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Generated by Xamasoft JSON Class Generator
2+
// http://www.xamasoft.com/json-class-generator
3+
4+
using Newtonsoft.Json;
5+
using System.Collections.Generic;
6+
7+
namespace SteamWebAPI2.Models
8+
{
9+
public class GlobalAchievementPercentage
10+
{
11+
[JsonProperty("name")]
12+
public string Name { get; set; }
13+
14+
[JsonProperty("percent")]
15+
public double Percent { get; set; }
16+
}
17+
18+
public class GlobalAchievementPercentagesResult
19+
{
20+
[JsonProperty("achievements")]
21+
public IList<GlobalAchievementPercentage> AchievementPercentages { get; set; }
22+
}
23+
24+
public class GlobalAchievementPercentagesResultContainer
25+
{
26+
[JsonProperty("achievementpercentages")]
27+
public GlobalAchievementPercentagesResult Result { get; set; }
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Generated by Xamasoft JSON Class Generator
2+
// http://www.xamasoft.com/json-class-generator
3+
4+
using Newtonsoft.Json;
5+
using SteamWebAPI2.Models.Utilities;
6+
using System.Collections.Generic;
7+
8+
namespace SteamWebAPI2.Models
9+
{
10+
public class GlobalStat
11+
{
12+
public string Name { get; set; }
13+
public int Total { get; set; }
14+
}
15+
16+
public class GlobalStatsForGameResult
17+
{
18+
[JsonConverter(typeof(GlobalStatJsonConverter))]
19+
[JsonProperty("globalstats")]
20+
public IList<GlobalStat> GlobalStats { get; set; }
21+
22+
[JsonProperty("result")]
23+
public int Status { get; set; }
24+
}
25+
26+
public class GlobalStatsForGameResultContainer
27+
{
28+
[JsonProperty("response")]
29+
public GlobalStatsForGameResult Result { get; set; }
30+
}
31+
}

SteamWebAPI2.Models/SteamWebAPI2.Models.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="Achievement.cs" />
49-
<Compile Include="AchievementPercentage.cs" />
5049
<Compile Include="FriendListResultContainer.cs" />
50+
<Compile Include="GlobalAchievementPercentagesResultContainer.cs" />
51+
<Compile Include="GlobalStatsForGameResultContainer.cs" />
5152
<Compile Include="PlayerBansContainer.cs" />
5253
<Compile Include="ResolveVanityUrlResultContainer.cs" />
5354
<Compile Include="SteamAppUpToDateCheckResultContainer.cs" />
@@ -98,6 +99,7 @@
9899
<Compile Include="Utilities\AssetClassInfoJsonConverter.cs" />
99100
<Compile Include="Utilities\CSGODataCenterJsonConverter.cs" />
100101
<Compile Include="Utilities\DateTimeExtensions.cs" />
102+
<Compile Include="Utilities\GlobalStatJsonConverter.cs" />
101103
<Compile Include="Utilities\TeamInfoJsonConverter.cs" />
102104
<Compile Include="Utilities\UnixTimeJsonConverter.cs" />
103105
</ItemGroup>

SteamWebAPI2.Models/UserAchievements.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ public class UserAchievements
1010
{
1111
public long SteamID { get; set; }
1212
public string GameName { get; set; }
13-
public IList<Achievement> Achievements { get; set; }
13+
public IList<GlobalAchievementPercentage> Achievements { get; set; }
1414

1515
public UserAchievements()
1616
{
17-
Achievements = new List<Achievement>();
17+
Achievements = new List<GlobalAchievementPercentage>();
1818
}
1919
}
2020
}

SteamWebAPI2.Models/UserStats.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class UserStats
1010
{
1111
public long SteamID { get; set; }
1212
public string GameName { get; set; }
13-
public IList<Achievement> Achievements { get; set; }
13+
public IList<GlobalAchievementPercentage> Achievements { get; set; }
1414
public IList<PlayerStat> Stats { get; set; }
1515

1616
public UserStats()
1717
{
18-
Achievements = new List<Achievement>();
18+
Achievements = new List<GlobalAchievementPercentage>();
1919
Stats = new List<PlayerStat>();
2020
}
2121
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SteamWebAPI2.Models.Utilities
10+
{
11+
internal class GlobalStatJsonConverter : JsonConverter
12+
{
13+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
14+
{
15+
throw new NotImplementedException();
16+
}
17+
18+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
19+
{
20+
if (reader.TokenType == JsonToken.Null)
21+
{
22+
return null;
23+
}
24+
25+
JObject globalStatsObject = JObject.Load(reader);
26+
27+
List<GlobalStat> globalStats = new List<GlobalStat>();
28+
29+
foreach (var globalStatProperty in globalStatsObject.Children<JProperty>())
30+
{
31+
GlobalStat globalStat = new GlobalStat();
32+
33+
globalStat.Name = globalStatProperty.Name;
34+
35+
foreach (var globalStatDetailsProperty in globalStatProperty.Value.Children<JProperty>())
36+
{
37+
string value = globalStatDetailsProperty.Value.ToString();
38+
39+
if (globalStatDetailsProperty.Name == "total") { globalStat.Total = Int32.Parse(value); }
40+
}
41+
42+
globalStats.Add(globalStat);
43+
}
44+
45+
return globalStats;
46+
}
47+
48+
public override bool CanWrite { get { return false; } }
49+
50+
public override bool CanConvert(Type objectType)
51+
{
52+
return typeof(IList<GlobalStat>).IsAssignableFrom(objectType);
53+
}
54+
}
55+
}

SteamWebAPI2/SteamUserStats.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using SteamWebAPI2.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SteamWebAPI2
10+
{
11+
public class SteamUserStats : SteamWebInterface
12+
{
13+
public SteamUserStats(string steamWebApiKey)
14+
: base(steamWebApiKey, "ISteamUserStats")
15+
{ }
16+
17+
public async Task<IReadOnlyCollection<GlobalAchievementPercentage>> GetGlobalAchievementPercentagesForAppAsync(long appId)
18+
{
19+
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
20+
AddToParametersIfHasValue("gameid", appId, parameters);
21+
var achievementPercentagesResult = await CallMethodAsync<GlobalAchievementPercentagesResultContainer>("GetGlobalAchievementPercentagesForApp", 2, parameters);
22+
return new ReadOnlyCollection<GlobalAchievementPercentage>(achievementPercentagesResult.Result.AchievementPercentages);
23+
}
24+
25+
public async Task<GlobalStatsForGameResult> GetGlobalStatsForGameAsync(long appId, IReadOnlyList<string> statNames)
26+
{
27+
List<SteamWebRequestParameter> parameters = new List<SteamWebRequestParameter>();
28+
AddToParametersIfHasValue("appid", appId, parameters);
29+
AddToParametersIfHasValue("count", statNames.Count, parameters);
30+
31+
for (int i = 0; i < statNames.Count; i++)
32+
{
33+
AddToParametersIfHasValue(String.Format("name[{0}]", i), statNames[i], parameters);
34+
}
35+
36+
var globalStatsResult = await CallMethodAsync<GlobalStatsForGameResultContainer>("GetGlobalStatsForGame", 1, parameters);
37+
return globalStatsResult.Result;
38+
}
39+
}
40+
}

SteamWebAPI2/SteamWebAPI2.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="SteamApps.cs" />
5757
<Compile Include="SteamNews.cs" />
5858
<Compile Include="SteamUser.cs" />
59+
<Compile Include="SteamUserStats.cs" />
5960
<Compile Include="SteamWebAPIUtil.cs" />
6061
<Compile Include="Properties\AssemblyInfo.cs" />
6162
<Compile Include="SteamWebInterface.cs" />

0 commit comments

Comments
 (0)