diff --git a/SessionZero/AppManager.cs b/SessionZero/AppManager.cs index bdd656d..14b64b0 100644 --- a/SessionZero/AppManager.cs +++ b/SessionZero/AppManager.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Text.Json; using Cogwheel; using SessionZero.Cogwheel; using SessionZero.Data; @@ -31,98 +33,4 @@ public static class AppManager { MainWindow.ChangePage(pageName); } - - - - - [Command(Name = "test")] - public static void TestParseTemplate() - { - string toml = @" - [metadata] - template_type = ""data"" - id = ""core_item"" - uuid = ""0"" - data_type = ""item"" - version = ""1.0.0"" - description = ""Core SessionZero item template"" - - [name] - type = ""text"" - - [description] - type = ""textblock"" - - [consumable] - type = ""bool"" - default_value = false - - [stats.value] - type = ""number"" - - [stats.weight] - type = ""number"" - - [stats.modifiers.base] - type = ""number"" - default_value = 1 - "; - - try - { - // Parse template - var template = SztlParser.ParseTemplate(toml); - - COGWHEEL.Log($"Template ID: {template.Id}"); - - COGWHEEL.Log("\nTop-Level Fields:"); - foreach (var f in template.Fields) - { - COGWHEEL.Log($"- {f.Key}: Type={f.Value.Type}, Default={f.Value}, TextBlock={f.Value.IsTextBlock}"); - } - - COGWHEEL.Log("\nGroups:"); - foreach (var g in template.SubGroups) - { - PrintGroup(g.Value, ""); - } - - // --- Instantiate SzDataObject --- - var obj = SzObjectFactory.CreateObject(template); - - // Set some values - obj.SetFieldValue("name", "Excalibur"); - obj.SetFieldValue("consumable", true); - obj.SetFieldValue("stats.value", 10); - obj.SetFieldValue("stats.weight", 2); - obj.SetFieldValue("stats.modifiers.base", 5); - - // Get and log values - COGWHEEL.Log("\nInstanced Object Field Values:"); - COGWHEEL.Log($"name = {obj.GetFieldValueAsString("name")}"); - COGWHEEL.Log($"consumable = {obj.GetFieldValue("consumable")}"); - COGWHEEL.Log($"stats.value = {obj.GetFieldValue("stats.value")}"); - COGWHEEL.Log($"stats.weight = {obj.GetFieldValue("stats.weight")}"); - COGWHEEL.Log($"stats.modifiers.base = {obj.GetFieldValue("stats.modifiers.base")}"); - } - catch (Exception ex) - { - COGWHEEL.Log($"Error parsing template: {ex.Message}"); - } - } - - private static void PrintGroup(SztlFieldGroup group, string indent) - { - COGWHEEL.Log($"{indent}Group: {group.Id}"); - - foreach (var f in group.Fields) - { - COGWHEEL.Log($"{indent} Field: {f.Key}, Type={f.Value.Type}, Default={f.Value}"); - } - - foreach (var sg in group.SubGroups) - { - PrintGroup(sg.Value, indent + " "); - } - } } \ No newline at end of file diff --git a/SessionZero/TestingCommands.cs b/SessionZero/TestingCommands.cs new file mode 100644 index 0000000..e30dc9b --- /dev/null +++ b/SessionZero/TestingCommands.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using Cogwheel; +using SessionZero.Data; +using SessionZero.Data.Sztl; + +namespace SessionZero; + +public static class TestingCommands +{ + // TODO: Remove this after testing + [Command(Name = "test")] + private static void TestParseTemplate() + { + string toml = @" + [metadata] + template_type = ""data"" + id = ""core_item"" + uuid = ""0"" + data_type = ""item"" + version = ""1.0.0"" + description = ""Core SessionZero item template"" + + [name] + type = ""text"" + + [description] + type = ""textblock"" + + [consumable] + type = ""bool"" + default_value = false + + [stats.value] + type = ""number"" + + [stats.weight] + type = ""number"" + + [stats.modifiers.base] + type = ""number"" + default_value = 1 + "; + + try + { + var template = SztlParser.ParseTemplate(toml); + + var obj = SzObjectFactory.CreateObject(template) as SzDataObject; + + obj.SetFieldValue("name", "Excalibur"); + obj.SetFieldValue("consumable", true); + obj.SetFieldValue("stats.value", 10); + obj.SetFieldValue("stats.weight", 2); + obj.SetFieldValue("stats.modifiers.base", 5); + + COGWHEEL.Log($"Template ID: {template.Id}"); + + COGWHEEL.Log("\nInstanced Object Field Values:"); + foreach (var fieldPath in new[] + { "name", "consumable", "stats.value", "stats.weight", "stats.modifiers.base" }) + { + COGWHEEL.Log($"> {fieldPath} = {obj.GetFieldValue(fieldPath)}"); + } + + var jsonOptions = new JsonSerializerOptions + { + WriteIndented = true + }; + string json = JsonSerializer.Serialize(new + { + obj.Id, + obj.Uuid, + template_id = obj.TemplateId, + fields = obj.FieldsToDictionary() + }, jsonOptions); + + COGWHEEL.Log("\nJSON Output:"); + COGWHEEL.Log(json); + } + catch (Exception ex) + { + COGWHEEL.Log($"Error parsing template: {ex.Message}"); + } + } + + private static Dictionary FieldsToDictionary(this SzObject obj) + { + var result = new Dictionary(); + foreach (var kvp in obj.Fields) + { + result[kvp.Key] = kvp.Value.Value; + } + + foreach (var group in obj.FieldGroups) + { + result[group.Key] = GroupToDict(group.Value); + } + + return result; + } + + private static Dictionary GroupToDict(SzFieldGroup group) + { + var dict = new Dictionary(); + foreach (var kvp in group.Fields) + { + dict[kvp.Key] = kvp.Value.Value; + } + + foreach (var subGroup in group.SubGroups) + { + dict[subGroup.Key] = GroupToDict(subGroup.Value); + } + + return dict; + } +} \ No newline at end of file diff --git a/SessionZero/data.md b/SessionZero/data.md index a554955..db761ec 100644 --- a/SessionZero/data.md +++ b/SessionZero/data.md @@ -109,22 +109,18 @@ An instanced DatasetObject should only contain simple fields and values, no obje ```json { - "id": "some_id", - "uuid": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", + "Id": "", + "Uuid": "0", "template_id": "core_item", - "parent_dataset_id": "core_items", - "compatible_systems": ["sz_core", "d20"], - "fields": { - "name": "Human readable name", - "description": "A description", - "consumable": false, - + "name": "Excalibur", + "description": null, + "consumable": true, "stats": { "value": 10, "weight": 2, "modifiers": { - "base": 1 + "base": 5 } } }