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

feat: split CardDefs by language #30

Merged
merged 2 commits into from
Feb 6, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8</LangVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions HearthDb.CardDefsDownloader/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace HearthDb.CardDefsDownloader
{
class Program
{
static void Main(string[] args)
{
var outdir = args[0];
Directory.CreateDirectory(outdir);
var httpClient = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });

// CardDefs.base.xml contains non localized tags and enUS tag
// Other languages can be found under e.g. CardDefs.deDE.xml
using var request = new HttpRequestMessage(HttpMethod.Get, "https://api.hearthstonejson.com/v1/latest/CardDefs.base.xml");
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

var response = httpClient.SendAsync(request).Result;
var data = response.Content.ReadAsStringAsync().Result;
File.WriteAllText(Path.Combine(outdir, "CardDefs.base.xml"), data);

var etag = response.Headers.ETag.Tag;
var lastModified = response.Content.Headers.LastModified;
File.WriteAllText(Path.Combine(outdir, "CardDefs.base.etag"), $"{etag}\n{lastModified.ToString()}");
}
}
}
51 changes: 51 additions & 0 deletions HearthDb.Tests/CardDefsLoadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using HearthDb.CardDefs;
using HearthDb.Enums;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HearthDb.Tests
{
[TestClass]
public class CardDefsLoadTest
{
[TestInitialize]
public void Init()
{
Cards.LoadBaseData(new CardDefs.CardDefs { Entites = new List<Entity>() });
}

[TestCleanup]
public void Cleanup()
{
Cards.LoadBaseData();
}

[TestMethod]
public void LoadCards_CorrectlyAssemblesData()
{
using var fsBase = File.OpenRead("Data/TestCardDefs.base.xml");
Cards.LoadBaseData(fsBase);

Assert.AreEqual("Nutthapon Petchthai", Cards.All["AT_001"].ArtistName);
Assert.AreEqual("Flame Lance", Cards.All["AT_001"].GetLocName(Locale.enUS));
Assert.AreEqual(null, Cards.All["AT_001"].GetLocName(Locale.deDE));

using var fsLocDe = File.OpenRead("Data/TestCardDefs.deDE.xml");
Cards.LoadLocaleData(fsLocDe, Locale.deDE);

Assert.AreEqual("Nutthapon Petchthai", Cards.All["AT_001"].ArtistName);
Assert.AreEqual("Flame Lance", Cards.All["AT_001"].GetLocName(Locale.enUS));
Assert.AreEqual("Flammenlanze", Cards.All["AT_001"].GetLocName(Locale.deDE));
}

[TestMethod]
public void HasValidETagData()
{
var bundled = Cards.GetBundledCardDefsETag();
Assert.IsNotNull(bundled.ETag);
Assert.IsTrue(DateTime.TryParse(bundled.LastModified, out _));
}
}
}
23 changes: 23 additions & 0 deletions HearthDb.Tests/Data/TestCardDefs.base.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<CardDefs build="212197">
<Entity CardID="AT_001" ID="2539" version="2">
<Tag enumID="185" name="CARDNAME" type="LocString">
<enUS>Flame Lance</enUS>
</Tag>
<Tag enumID="184" name="CARDTEXT" type="LocString">
<enUS>Deal $25 damage
to a minion.</enUS>
</Tag>
<Tag enumID="351" name="FLAVORTEXT" type="LocString">
<enUS>It's on the rack next to ice lance, acid lance, and English muffin lance.</enUS>
</Tag>
<Tag enumID="342" name="ARTISTNAME" type="String">Nutthapon Petchthai</Tag>
<Tag enumID="48" name="COST" type="Int" value="5"/>
<Tag enumID="183" name="CARD_SET" type="Int" value="15"/>
<Tag enumID="199" name="CLASS" type="Int" value="4"/>
<Tag enumID="202" name="CARDTYPE" type="Int" value="5"/>
<Tag enumID="203" name="RARITY" type="Int" value="1"/>
<Tag enumID="321" name="COLLECTIBLE" type="Int" value="1"/>
<Tag enumID="1635" name="SPELL_SCHOOL" type="Int" value="2"/>
</Entity>
</CardDefs>
14 changes: 14 additions & 0 deletions HearthDb.Tests/Data/TestCardDefs.deDE.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<CardDefs build="212197">
<Entity CardID="AT_001" ID="2539" version="2">
<Tag enumID="185" name="CARDNAME" type="LocString">
<deDE>Flammenlanze</deDE>
</Tag>
<Tag enumID="184" name="CARDTEXT" type="LocString">
<deDE>Fügt einem Diener $25 Schaden zu.</deDE>
</Tag>
<Tag enumID="351" name="FLAVORTEXT" type="LocString">
<deDE>Die Sommerversion der Eislanze. Gut gegen chronisch kalte Hände.</deDE>
</Tag>
</Entity>
</CardDefs>
4 changes: 4 additions & 0 deletions HearthDb.Tests/HearthDb.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<ProjectReference Include="..\HearthDb\HearthDb.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Data/*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions HearthDb.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class UnitTest1
public void BasicTest()
{
Assert.AreEqual("Flame Lance", Cards.All["AT_001"].Name);
Assert.AreEqual("Flammenlanze", Cards.All["AT_001"].GetLocName(Locale.deDE));
// non-enUS no longer included by default. CardDefsLoadTest verifies this works.
// Assert.AreEqual("Flammenlanze", Cards.All["AT_001"].GetLocName(Locale.deDE));
Assert.AreEqual("Nutthapon Petchthai", Cards.All["AT_001"].ArtistName);
Assert.AreEqual(CardSet.TGT, Cards.All["AT_001"].Set);
Assert.AreEqual(true, Cards.All["AT_001"].Collectible);
Expand Down Expand Up @@ -80,7 +81,7 @@ public void TestCardText()
Assert.IsTrue(cramSession.Text.Contains("improved by"));

var flameLance = Cards.All[CardIds.Collectible.Mage.FlameLanceTGT];
Assert.IsTrue(flameLance.GetLocText(Locale.frFR).Contains("$25"));
Assert.IsTrue(flameLance.GetLocText(Locale.enUS).Contains("$25"));

var elvenArcher = Cards.All[CardIds.Collectible.Neutral.ElvenArcherVanilla];
Assert.IsTrue(elvenArcher.Text.Contains("Deal 1 damage"));
Expand Down
10 changes: 10 additions & 0 deletions HearthDb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.EnumsGenerator", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HearthDb.Tests", "HearthDb.Tests\HearthDb.Tests.csproj", "{875316D1-C4C3-48BD-BDAE-3727269B2A67}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HearthDb.CardDefsDownloader", "HearthDb.CardDefsDownloader\HearthDb.CardDefsDownloader.csproj", "{92DBA109-DF71-4874-827A-20B00EABB57D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,14 @@ Global
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|Any CPU.Build.0 = Release|Any CPU
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.ActiveCfg = Release|Any CPU
{875316D1-C4C3-48BD-BDAE-3727269B2A67}.Release|x86.Build.0 = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.ActiveCfg = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Debug|x86.Build.0 = Debug|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|Any CPU.Build.0 = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.ActiveCfg = Release|Any CPU
{92DBA109-DF71-4874-827A-20B00EABB57D}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions HearthDb/CardDefs/CardDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public class CardDefs
{
[XmlElement("Entity")]
public List<Entity> Entites { get; set; }


[XmlAttribute("build")]
public string Build { get; set; }
}
}
Loading