Data changes and vibe UI theme
This commit is contained in:
+97
-44
@@ -1,16 +1,15 @@
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text.Json.Serialization;
|
||||
using Chrono.Model;
|
||||
|
||||
var repoRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", ".."));
|
||||
var docsDir = Path.Combine(repoRoot, "chrono.docs");
|
||||
var webWwwRoot = Path.Combine(repoRoot, "Chrono", "Web", "wwwroot");
|
||||
var generatedFile = Path.Combine(repoRoot, "Chrono", "Web", "Generated", "Cards.g.cs");
|
||||
|
||||
Console.WriteLine($"Repo root: {repoRoot}");
|
||||
Console.WriteLine($"Docs dir: {docsDir}");
|
||||
Console.WriteLine($"Web wwwroot: {webWwwRoot}");
|
||||
Console.WriteLine($"Generated: {generatedFile}");
|
||||
|
||||
if (!Directory.Exists(docsDir))
|
||||
{
|
||||
@@ -38,6 +37,10 @@ foreach (var file in mdFiles)
|
||||
var category = yaml.GetValueOrDefault("category");
|
||||
if (category == null) continue;
|
||||
|
||||
var imageFile = StripWikiLink(yaml.GetValueOrDefault("imageLink"));
|
||||
if (imageFile != null && !imageFile.EndsWith(".png"))
|
||||
imageFile += ".png";
|
||||
|
||||
var card = new CardData
|
||||
{
|
||||
Name = name,
|
||||
@@ -53,12 +56,9 @@ foreach (var file in mdFiles)
|
||||
ImmortalizeTo = yaml.ContainsKey("immortalizeTo") ? ParseListOrScalar(yaml, "immortalizeTo").Select(s => StripWikiLink(s) ?? "").Where(s => s != "").ToList() : null,
|
||||
ImmortalizeFrom = StripWikiLink(yaml.GetValueOrDefault("immortalizeFrom")),
|
||||
ImmortalizeWhen = NullIfNa(StripWikiLinks(yaml.GetValueOrDefault("immortalizeWhen"))),
|
||||
ImageFile = StripWikiLink(yaml.GetValueOrDefault("imageLink")),
|
||||
ImageFile = imageFile,
|
||||
};
|
||||
|
||||
if (card.ImageFile != null && !card.ImageFile.EndsWith(".png"))
|
||||
card.ImageFile += ".png";
|
||||
|
||||
cards.Add(card);
|
||||
}
|
||||
|
||||
@@ -74,20 +74,47 @@ foreach (var card in cards)
|
||||
File.Copy(src, dst, overwrite: true);
|
||||
}
|
||||
|
||||
// Write cards.json
|
||||
var output = new { cards };
|
||||
var json = JsonSerializer.Serialize(output, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
});
|
||||
var jsonPath = Path.Combine(webWwwRoot, "sample-data", "cards.json");
|
||||
File.WriteAllText(jsonPath, json);
|
||||
// Generate C# source file
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(generatedFile)!);
|
||||
using var writer = new StreamWriter(generatedFile, false, Encoding.UTF8);
|
||||
writer.WriteLine("// <auto-generated/>");
|
||||
writer.WriteLine("#nullable enable");
|
||||
writer.WriteLine();
|
||||
writer.WriteLine("namespace Chrono.Model;");
|
||||
writer.WriteLine();
|
||||
writer.WriteLine("public static class CardDatabase");
|
||||
writer.WriteLine("{");
|
||||
writer.WriteLine(" public static readonly System.Collections.Generic.List<CardData> Cards =");
|
||||
writer.WriteLine(" [");
|
||||
|
||||
Console.WriteLine($"Processed {cards.Count} cards");
|
||||
Console.WriteLine($"Written to {jsonPath}");
|
||||
for (int i = 0; i < cards.Count; i++)
|
||||
{
|
||||
var c = cards[i];
|
||||
writer.WriteLine(" new()");
|
||||
writer.WriteLine(" {");
|
||||
WriteProp(writer, "Name", c.Name, 3);
|
||||
WriteProp(writer, "Category", c.Category, 3);
|
||||
WriteNullProp(writer, "Cost", c.Cost, 3);
|
||||
WriteNullProp(writer, "Attack", c.Attack, 3);
|
||||
WriteNullProp(writer, "Health", c.Health, 3);
|
||||
WriteStrProp(writer, "Description", c.Description, 3);
|
||||
WriteStrProp(writer, "Faction", c.Faction, 3);
|
||||
WriteStrProp(writer, "Set", c.Set, 3);
|
||||
WriteStrProp(writer, "Speed", c.Speed, 3);
|
||||
WriteListProp(writer, "Archetypes", c.Archetypes, 3);
|
||||
WriteListProp(writer, "ImmortalizeTo", c.ImmortalizeTo, 3);
|
||||
WriteStrProp(writer, "ImmortalizeFrom", c.ImmortalizeFrom, 3);
|
||||
WriteStrProp(writer, "ImmortalizeWhen", c.ImmortalizeWhen, 3);
|
||||
WriteStrProp(writer, "ImageFile", c.ImageFile, 3);
|
||||
|
||||
var comma = i < cards.Count - 1 ? "," : "";
|
||||
writer.WriteLine($" }}{comma}");
|
||||
}
|
||||
|
||||
writer.WriteLine(" ];");
|
||||
writer.WriteLine("}");
|
||||
|
||||
Console.WriteLine($"Generated {cards.Count} cards in {generatedFile}");
|
||||
return 0;
|
||||
|
||||
// --- Helpers ---
|
||||
@@ -105,14 +132,14 @@ static string? StripWikiLink(string? s)
|
||||
return Regex.Replace(s, @"\[\[([^\]]*)\]\]", "$1").Trim('"');
|
||||
}
|
||||
|
||||
static string? NullIfNa(string? s) => s is "N/A" or null ? null : s.Trim('"');
|
||||
|
||||
static string? StripWikiLinks(string? s)
|
||||
{
|
||||
if (s == null || s == "N/A") return null;
|
||||
return Regex.Replace(s.Trim('"'), @"\[\[([^\]]*)\]\]", "$1").Trim();
|
||||
}
|
||||
|
||||
static string? NullIfNa(string? s) => s is "N/A" or null ? null : s.Trim('"');
|
||||
|
||||
static List<string> ParseList(Dictionary<string, string> yaml, string key)
|
||||
{
|
||||
if (!yaml.TryGetValue(key, out var raw)) return [];
|
||||
@@ -171,12 +198,7 @@ static Dictionary<string, string> ParseYaml(string yaml)
|
||||
currentKey = trimmed[..colonIndex].Trim();
|
||||
var value = trimmed[(colonIndex + 1)..].Trim();
|
||||
|
||||
if (value.Length == 0)
|
||||
{
|
||||
// Could be a list or multi-line value starting on next line
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value.Length == 0) continue;
|
||||
dict[currentKey] = value;
|
||||
}
|
||||
|
||||
@@ -186,20 +208,51 @@ static Dictionary<string, string> ParseYaml(string yaml)
|
||||
return dict;
|
||||
}
|
||||
|
||||
record CardData
|
||||
static void WriteProp(StreamWriter w, string name, string value, int indent)
|
||||
{
|
||||
[JsonPropertyName("name")] public string Name { get; set; } = "";
|
||||
[JsonPropertyName("category")] public string Category { get; set; } = "";
|
||||
[JsonPropertyName("cost")] public int? Cost { get; set; }
|
||||
[JsonPropertyName("attack")] public int? Attack { get; set; }
|
||||
[JsonPropertyName("health")] public int? Health { get; set; }
|
||||
[JsonPropertyName("description")] public string? Description { get; set; }
|
||||
[JsonPropertyName("faction")] public string? Faction { get; set; }
|
||||
[JsonPropertyName("set")] public string? Set { get; set; }
|
||||
[JsonPropertyName("speed")] public string? Speed { get; set; }
|
||||
[JsonPropertyName("archetypes")] public List<string>? Archetypes { get; set; }
|
||||
[JsonPropertyName("immortalizeTo")] public List<string>? ImmortalizeTo { get; set; }
|
||||
[JsonPropertyName("immortalizeFrom")] public string? ImmortalizeFrom { get; set; }
|
||||
[JsonPropertyName("immortalizeWhen")] public string? ImmortalizeWhen { get; set; }
|
||||
[JsonPropertyName("imageFile")] public string? ImageFile { get; set; }
|
||||
w.WriteLine($"{new string(' ', indent * 4)}{name} = {ToLiteral(value)},");
|
||||
}
|
||||
|
||||
static void WriteStrProp(StreamWriter w, string name, string? value, int indent)
|
||||
{
|
||||
if (value == null) return;
|
||||
w.WriteLine($"{new string(' ', indent * 4)}{name} = {ToLiteral(value)},");
|
||||
}
|
||||
|
||||
static void WriteNullProp(StreamWriter w, string name, int? value, int indent)
|
||||
{
|
||||
if (value == null) return;
|
||||
w.WriteLine($"{new string(' ', indent * 4)}{name} = {value},");
|
||||
}
|
||||
|
||||
static void WriteListProp(StreamWriter w, string name, List<string>? values, int indent)
|
||||
{
|
||||
if (values == null) return;
|
||||
var pad = new string(' ', indent * 4);
|
||||
w.WriteLine($"{pad}{name} = [");
|
||||
foreach (var v in values)
|
||||
w.WriteLine($"{pad} {ToLiteral(v)},");
|
||||
w.WriteLine($"{pad}],");
|
||||
}
|
||||
|
||||
static string ToLiteral(string? s)
|
||||
{
|
||||
if (s == null) return "null";
|
||||
var sb = new StringBuilder();
|
||||
sb.Append('"');
|
||||
foreach (char c in s)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '"': sb.Append("\\\""); break;
|
||||
case '\\': sb.Append("\\\\"); break;
|
||||
case '\n': sb.Append("\\n"); break;
|
||||
case '\r': sb.Append("\\r"); break;
|
||||
case '\t': sb.Append("\\t"); break;
|
||||
case '\0': sb.Append("\\0"); break;
|
||||
default: sb.Append(c); break;
|
||||
}
|
||||
}
|
||||
sb.Append('"');
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user