Skip to content

Commit

Permalink
Include ClientVersion in Recipes API
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Jan 11, 2025
1 parent a756587 commit aaaca73
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 136 deletions.
71 changes: 23 additions & 48 deletions CS2Interface/GameData/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace CS2Interface {
internal static class GameData {
internal static GameDataItems ItemsGame = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/scripts/items/items_game.txt");
internal static GameDataItemsCDN ItemsGameCdn = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/scripts/items/items_game_cdn.txt");
internal static GameDataText CsgoEnglish = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/resource/csgo_english.txt");
internal static GameDataItems ItemsGame {get; private set;} = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/scripts/items/items_game.txt");
internal static GameDataDictionary ItemsGameCdn {get; private set;} = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/scripts/items/items_game_cdn.txt");
internal static GameDataText CsgoEnglish {get; private set;} = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/pak01_dir/resource/csgo_english.txt");
internal static GameDataDictionary GameVersion {get; private set;} = new("https://raw.githubusercontent.com/SteamDatabase/GameTracking-CS2/master/game/csgo/steam.inf");
internal static uint? ClientVersion {get; private set;}

private static bool IsUpdating = false;
private static SemaphoreSlim UpdateSemaphore = new SemaphoreSlim(1, 1);
Expand All @@ -21,11 +23,12 @@ internal static void Update(bool forceUpdate = false) {
ItemsGame.Updated = false;
ItemsGameCdn.Updated = false;
CsgoEnglish.Updated = false;
GameVersion.Updated = false;
DoNotUpdateUntil = DateTime.Now.AddMinutes(15);
ASF.ArchiLogger.LogGenericInfo(Strings.GameDataRefreshing);
}

if (!ItemsGame.Updated || !ItemsGameCdn.Updated || !CsgoEnglish.Updated) {
if (!ItemsGame.Updated || !ItemsGameCdn.Updated || !CsgoEnglish.Updated || !GameVersion.Updated) {
IsUpdating = true;
}

Expand Down Expand Up @@ -70,60 +73,32 @@ private static async Task DoUpdate() {
if (!CsgoEnglish.Updated) {
tasks.Add(CsgoEnglish.Update());
}
if (!GameVersion.Updated) {
tasks.Add(GameVersion.Update());
}
await Task.WhenAll(tasks).ConfigureAwait(false);

if (ItemsGame.Updated && ItemsGameCdn.Updated && CsgoEnglish.Updated) {
if (ItemsGame.Updated && ItemsGameCdn.Updated && CsgoEnglish.Updated && GameVersion.Updated) {
UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
IsUpdating = false;
ASF.ArchiLogger.LogGenericInfo(Strings.GameDataLoadingSuccess);

string? clientVersionString = GameVersion["ClientVersion"];
if (clientVersionString != null) {
if (uint.TryParse(clientVersionString, out uint clientVersion)) {
ClientVersion = clientVersion;
} else {
ASF.ArchiLogger.LogGenericInfo(Strings.ClientVersionFail);
}
} else {
ASF.ArchiLogger.LogGenericInfo(Strings.ClientVersionFail);
}
}
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
} finally {
UpdateSemaphore.Release();
}
}

internal static string? GetWearName(double wear) {
return wear switch {
<= .07 => CsgoEnglish["SFUI_InvTooltip_Wear_Amount_0"], // Factory New
<= .15 => CsgoEnglish["SFUI_InvTooltip_Wear_Amount_1"], // Minimal Wear
<= .38 => CsgoEnglish["SFUI_InvTooltip_Wear_Amount_2"], // Field-Tested
<= .45 => CsgoEnglish["SFUI_InvTooltip_Wear_Amount_3"], // Well-Worn
_ => CsgoEnglish["SFUI_InvTooltip_Wear_Amount_4"] // Battle-Scarred
};
}

internal static string? GetOriginName(uint origin) {
// https://raw.githubusercontent.com/SteamDatabase/SteamTracking/b5cba7a22ab899d6d423380cff21cec707b7c947/ItemSchema/CounterStrikeGlobalOffensive.json
return origin switch {
0 => Strings.ItemOrigin0, // Timed Drop
1 => Strings.ItemOrigin1, // Achievement
2 => Strings.ItemOrigin2, // Purchased
3 => Strings.ItemOrigin3, // Traded
4 => Strings.ItemOrigin4, // Crafted
5 => Strings.ItemOrigin5, // Store Promotion
6 => Strings.ItemOrigin6, // Gifted
7 => Strings.ItemOrigin7, // Support Granted
8 => Strings.ItemOrigin8, // Found in Crate
9 => Strings.ItemOrigin9, // Earned
10 => Strings.ItemOrigin10, // "Third-Party Promotion
11 => Strings.ItemOrigin11, // "Wrapped Gift
12 => Strings.ItemOrigin12, // "Halloween Drop
13 => Strings.ItemOrigin13, // "Steam Purchase
14 => Strings.ItemOrigin14, // "Foreign Item
15 => Strings.ItemOrigin15, // "CD Key
16 => Strings.ItemOrigin16, // "Collection Reward
17 => Strings.ItemOrigin17, // "Preview Item
18 => Strings.ItemOrigin18, // "Steam Workshop Contribution
19 => Strings.ItemOrigin19, // "Periodic Score Reward
20 => Strings.ItemOrigin20, // "Recycling
21 => Strings.ItemOrigin21, // "Tournament Drop
22 => Strings.ItemOrigin22, // "Stock Item
23 => Strings.ItemOrigin23, // "Quest Reward
24 => Strings.ItemOrigin24, // "Level Up Reward
_ => null
};
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using CS2Interface.Localization;

namespace CS2Interface {
internal class GameDataItemsCDN : GameDataResource {
internal class GameDataDictionary : GameDataResource {
private Dictionary<string, string>? Data;

internal GameDataItemsCDN(string url) : base(url) {}
internal GameDataDictionary(string url) : base(url) {}

internal async Task<bool> Update() {
internal override async Task<bool> Update() {
Dictionary<string, string>? data = await FetchCDNResource().ConfigureAwait(false);
if (data == null) {
ASF.ArchiLogger.LogGenericError(String.Format("Couldn't load game data from: {0}", Url));
ASF.ArchiLogger.LogGenericError(String.Format(Strings.GameDataSourceFailed, Url));

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion CS2Interface/GameData/GameDataItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class GameDataItems : GameDataResource {

internal GameDataItems(string url) : base(url) {}

internal async Task<bool> Update() {
internal override async Task<bool> Update() {
KeyValue? data = await FetchKVResource().ConfigureAwait(false);
if (data == null) {
ASF.ArchiLogger.LogGenericError(String.Format(Strings.GameDataSourceFailed, Url));
Expand Down
4 changes: 3 additions & 1 deletion CS2Interface/GameData/GameDataResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
using SteamKit2;

namespace CS2Interface {
internal class GameDataResource {
internal abstract class GameDataResource {
protected Uri Url;
internal bool Updated {get; set;} = false;

internal GameDataResource(string url) {
Url = new Uri(url);
}

internal abstract Task<bool> Update();

protected async Task<KeyValue?> FetchKVResource() {
HttpClient httpClient = new();
using (Stream response = await httpClient.GetStreamAsync(Url).ConfigureAwait(false)) {
Expand Down
45 changes: 44 additions & 1 deletion CS2Interface/GameData/GameDataText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using CS2Interface.Localization;
using SteamKit2;

namespace CS2Interface {
Expand All @@ -12,7 +13,7 @@ internal class GameDataText : GameDataResource {

internal GameDataText(string url) : base(url) {}

internal async Task<bool> Update() {
internal override async Task<bool> Update() {
KeyValue? data = await FetchKVResource().ConfigureAwait(false);
if (data == null) {
ASF.ArchiLogger.LogGenericError(String.Format("Couldn't load game data from: {0}", Url));
Expand Down Expand Up @@ -64,5 +65,47 @@ internal string? this[string? key] {
return null;
}
}

internal string? GetWearName(double wear) {
return wear switch {
<= .07 => this["SFUI_InvTooltip_Wear_Amount_0"], // Factory New
<= .15 => this["SFUI_InvTooltip_Wear_Amount_1"], // Minimal Wear
<= .38 => this["SFUI_InvTooltip_Wear_Amount_2"], // Field-Tested
<= .45 => this["SFUI_InvTooltip_Wear_Amount_3"], // Well-Worn
_ => this["SFUI_InvTooltip_Wear_Amount_4"] // Battle-Scarred
};
}

internal static string? GetOriginName(uint origin) {
// https://raw.githubusercontent.com/SteamDatabase/SteamTracking/b5cba7a22ab899d6d423380cff21cec707b7c947/ItemSchema/CounterStrikeGlobalOffensive.json
return origin switch {
0 => Strings.ItemOrigin0, // Timed Drop
1 => Strings.ItemOrigin1, // Achievement
2 => Strings.ItemOrigin2, // Purchased
3 => Strings.ItemOrigin3, // Traded
4 => Strings.ItemOrigin4, // Crafted
5 => Strings.ItemOrigin5, // Store Promotion
6 => Strings.ItemOrigin6, // Gifted
7 => Strings.ItemOrigin7, // Support Granted
8 => Strings.ItemOrigin8, // Found in Crate
9 => Strings.ItemOrigin9, // Earned
10 => Strings.ItemOrigin10, // "Third-Party Promotion
11 => Strings.ItemOrigin11, // "Wrapped Gift
12 => Strings.ItemOrigin12, // "Halloween Drop
13 => Strings.ItemOrigin13, // "Steam Purchase
14 => Strings.ItemOrigin14, // "Foreign Item
15 => Strings.ItemOrigin15, // "CD Key
16 => Strings.ItemOrigin16, // "Collection Reward
17 => Strings.ItemOrigin17, // "Preview Item
18 => Strings.ItemOrigin18, // "Steam Workshop Contribution
19 => Strings.ItemOrigin19, // "Periodic Score Reward
20 => Strings.ItemOrigin20, // "Recycling
21 => Strings.ItemOrigin21, // "Tournament Drop
22 => Strings.ItemOrigin22, // "Stock Item
23 => Strings.ItemOrigin23, // "Quest Reward
24 => Strings.ItemOrigin24, // "Level Up Reward
_ => null
};
}
}
}
4 changes: 2 additions & 2 deletions CS2Interface/GameData/GameObjects/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected override bool SetAdditionalProperties() {

TypeName = GameData.CsgoEnglish[ItemData.ItemDef["item_type_name"].Value?.Substring(1)];
QualityName = GameData.CsgoEnglish[GameData.ItemsGame["qualities"]?.FirstOrDefault(x => x["value"].Value == Quality.ToString())?.Name];
OriginName = GameData.GetOriginName(Origin);
OriginName = GameDataText.GetOriginName(Origin);

// Set the item name, which will be something like: what kind of sticker it is, or the name of the weapon skin, or the type of pin/coin
// If an item has a wear value, but uses the default paint_kit (vanilla knives for example), this will be "-"
Expand All @@ -177,7 +177,7 @@ protected override bool SetAdditionalProperties() {
WeaponName = GameData.CsgoEnglish[ItemData.ItemDef["item_name"].Value?.Substring(1)];

if (Wear != null) {
WearName = GameData.GetWearName(Wear.Value);
WearName = GameData.CsgoEnglish.GetWearName(Wear.Value);
string? wearRemapMinValue = ItemData.PaintKitDef!["wear_remap_min"].Value;
string? wearRemapMaxValue = ItemData.PaintKitDef!["wear_remap_max"].Value;
WearMin = wearRemapMinValue == null ? null : float.Parse(wearRemapMinValue, NumberStyles.Float, CultureInfo.InvariantCulture);
Expand Down
4 changes: 2 additions & 2 deletions CS2Interface/IPC/Api/CS2InterfaceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public async Task<ActionResult<GenericResponse>> CraftItem(string botName, ushor

[HttpGet("Recipes")]
[EndpointSummary("Get a list of crafting recipes")]
[ProducesResponseType(typeof(GenericResponse<List<Recipe>>), (int) HttpStatusCode.OK)]
[ProducesResponseType(typeof(GenericResponse<GameData<List<Recipe>>>), (int) HttpStatusCode.OK)]
[ProducesResponseType(typeof(GenericResponse), (int) HttpStatusCode.BadRequest)]
public async Task<ActionResult<GenericResponse>> Recipes([FromQuery] bool showDefs = false) {
List<Recipe> recipes;
Expand All @@ -344,7 +344,7 @@ public async Task<ActionResult<GenericResponse>> Recipes([FromQuery] bool showDe

GameObject.SetSerializationProperties(true, showDefs);

return Ok(new GenericResponse<List<Recipe>>(true, recipes));
return Ok(new GenericResponse<GameData<List<Recipe>>>(true, new GameData<List<Recipe>>(recipes)));
}

private async Task<ActionResult<GenericResponse>> HandleClientException(Bot bot, ClientException e) {
Expand Down
Loading

0 comments on commit aaaca73

Please sign in to comment.