public void repopulate_property(List <string> path) { List <string> parent_path = path.GetRange(0, path.Count - 1); CharDictProperty parent_prop = this.character.properties; ObservableCollection <PropertyRow> parent_rows = this.property_rows; foreach (string token in parent_path) { if (!parent_prop.value.ContainsKey(token)) { throw new ArgumentOutOfRangeException(nameof(path)); } parent_prop = parent_prop.value[token] as CharDictProperty; bool need_row = true; foreach (PropertyRow row in parent_rows) { if (row.name == token) { parent_rows = row.children; need_row = false; break; } } if (need_row) { throw new ArgumentOutOfRangeException(nameof(path)); } if ((parent_prop is null) || (parent_rows is null)) { throw new ArgumentOutOfRangeException(nameof(path)); } } parent_rows.Clear(); this.populate_property_rows(parent_rows, parent_path, parent_prop); }
public override void merge_to(List <EntryAction> actions) { for (int i = actions.Count - 1; i >= 0; i--) { if (actions[i] is ActionCharacterSet ext_char_set) { if (ext_char_set.guid != this.guid) { continue; } // existing ActionCharacterSet with this guid; update its "to" field based on ours and we're done if (this.to is null) { ext_char_set.to.remove_property(this.path); } else { ext_char_set.to.set_property(this.path, this.to); } return; } if (actions[i] is ActionCharacterPropertySet ext_prop_set) { if (ext_prop_set.guid != this.guid) { continue; } //TODO: maybe handle ancestor path which contains this path or its immediate parent if (PropertyUtil.path_common_prefix_length(ext_prop_set.path, this.path) < this.path.Count) { continue; } // existing ActionCharacterPropertySet with this guid and this or a child path if (ext_prop_set.path.Count == this.path.Count) { // action refers to this path; update its "to" field based on ours and we're done actions[i] = new ActionCharacterPropertySet(ext_prop_set.guid, ext_prop_set.path, ext_prop_set.from, this.to); return; } // action refers to a child path; update our "from" field based on it then delete it CharDictProperty dict_prop = this.from as CharDictProperty; for (int j = this.path.Count; j < ext_prop_set.path.Count - 1; j++) { if ((dict_prop is null) || (!dict_prop.value.ContainsKey(ext_prop_set.path[j]))) { dict_prop = null; break; } dict_prop = dict_prop.value[ext_prop_set.path[j]] as CharDictProperty; } if (dict_prop is not null) { if (ext_prop_set.from is null) { dict_prop.value.Remove(ext_prop_set.path[^ 1]);
public void test_subtract() { CharDictProperty foo = new CharDictProperty(), bar = new CharDictProperty(); CharTextProperty tprop = new CharTextProperty("blah"); CharNumProperty nprop = new CharNumProperty(1.23m); foo.value["Some Text"] = tprop; foo.value["Some Number"] = nprop; bar.value["Some Number"] = nprop; foo.subtract(bar); Assert.AreEqual(foo.value.Count, 1); Assert.IsTrue(foo.value.ContainsKey("Some Text")); }
public void test_constructor_argument() { CharTextProperty tprop = new CharTextProperty("bloh"), subprop; CharDictProperty foo = new CharDictProperty(new Dictionary <string, CharProperty>() { ["blah"] = tprop }); Assert.AreEqual(foo.value.Count, 1); Assert.IsTrue(foo.value.ContainsKey("blah")); subprop = foo.value["blah"] as CharTextProperty; Assert.IsNotNull(subprop); Assert.AreEqual(subprop.value, tprop.value); }
public void test_add() { CharDictProperty foo = new CharDictProperty(), bar = new CharDictProperty(); CharTextProperty tprop = new CharTextProperty("blah"); CharNumProperty nprop = new CharNumProperty(1.23m); foo.value["Some Text"] = tprop; bar.value["Some Number"] = nprop; foo.add(bar); Assert.AreEqual(foo.value.Count, 2); Assert.IsTrue(foo.value.ContainsKey("Some Text")); Assert.IsTrue(foo.value.ContainsKey("Some Number")); Assert.IsFalse(ReferenceEquals(foo.value["Some Number"], bar.value["Some Number"])); Assert.AreEqual((foo.value["Some Number"] as CharNumProperty).value, (bar.value["Some Number"] as CharNumProperty).value); }
private Character get_test_character() { Character c = new Character("Krakrox the Barbarian"); c.properties.value["Player"] = new CharTextProperty("Philip"); c.properties.value["XP"] = new CharNumProperty(42); CharSetProperty feats = new CharSetProperty(); feats.value.Add("Power Attack"); feats.value.Add("Cleave"); c.properties.value["Feats"] = feats; CharDictProperty skills = new CharDictProperty(); skills.value["Diplomacy"] = new CharNumProperty(0); skills.value["Seal Clubbing"] = new CharNumProperty(9001); c.properties.value["Skills"] = skills; return(c); }
public void test_equals() { CharDictProperty foo = new CharDictProperty(), bar, baz = new CharDictProperty(); CharTextProperty tprop = new CharTextProperty("blah"); CharNumProperty nprop = new CharNumProperty(1.23m); CharSetProperty sprop = new CharSetProperty(); sprop.value.Add("bloh"); sprop.value.Add("bleh"); foo.value["Some Text"] = tprop; foo.value["Some Number"] = nprop; foo.value["Some Collection"] = sprop; bar = foo.copy(); baz.value["Some Text"] = tprop; Assert.IsTrue(foo.equals(bar)); Assert.IsTrue(bar.equals(foo)); Assert.IsFalse(foo.equals(baz)); Assert.IsFalse(baz.equals(foo)); }
public void test_serialization() { CharDictProperty foo = new CharDictProperty(), bar; CharTextProperty tprop = new CharTextProperty("blah"); CharNumProperty nprop = new CharNumProperty(1.23m); CharSetProperty sprop = new CharSetProperty(); sprop.value.Add("bloh"); sprop.value.Add("bleh"); foo.value["Some Text"] = tprop; foo.value["Some Number"] = nprop; foo.value["Some Collection"] = sprop; DataContractSerializer fmt = new DataContractSerializer(typeof(CharDictProperty)); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { fmt.WriteObject(ms, foo); ms.Seek(0, System.IO.SeekOrigin.Begin); System.Xml.XmlDictionaryReader xr = System.Xml.XmlDictionaryReader.CreateTextReader(ms, new System.Xml.XmlDictionaryReaderQuotas()); bar = (CharDictProperty)(fmt.ReadObject(xr, true)); } Assert.AreEqual(foo.value.Count, bar.value.Count); foreach (string s in foo.value.Keys) { Assert.IsTrue(bar.value.ContainsKey(s)); } CharTextProperty tp = bar.value["Some Text"] as CharTextProperty; Assert.IsFalse(tp is null); Assert.AreEqual(tp.value, "blah"); CharNumProperty np = bar.value["Some Number"] as CharNumProperty; Assert.IsFalse(np is null); Assert.AreEqual(np.value, 1.23m); CharSetProperty sp = bar.value["Some Collection"] as CharSetProperty; Assert.IsFalse(sp is null); Assert.AreEqual(sp.value.Count, 2); Assert.IsTrue(sp.value.Contains("bloh")); Assert.IsTrue(sp.value.Contains("bleh")); }
public void test_set_property_replace() { Character c = this.get_test_character(); CharTextProperty new_prop = new CharTextProperty("Krakrox has no time for skills!"); CharDictProperty old_prop = c.set_property(new List <string>() { "Skills" }, new_prop) as CharDictProperty; Assert.IsFalse(old_prop is null); Assert.AreEqual(old_prop.value.Count, 2); Assert.IsTrue(old_prop.value.ContainsKey("Diplomacy")); Assert.IsTrue(old_prop.value.ContainsKey("Seal Clubbing")); CharTextProperty prop = c.get_property(new List <string>() { "Skills" }) as CharTextProperty; Assert.IsFalse(prop is null); Assert.AreEqual(prop.value, new_prop.value); }
public void test_copy() { CharDictProperty foo = new CharDictProperty(), bar; CharTextProperty tprop = new CharTextProperty("blah"); CharNumProperty nprop = new CharNumProperty(1.23m); CharSetProperty sprop = new CharSetProperty(); sprop.value.Add("bloh"); sprop.value.Add("bleh"); foo.value["Some Text"] = tprop; foo.value["Some Number"] = nprop; foo.value["Some Collection"] = sprop; bar = foo.copy(); Assert.IsFalse(ReferenceEquals(foo, bar)); Assert.IsFalse(ReferenceEquals(foo.value, bar.value)); Assert.AreEqual(foo.value.Count, bar.value.Count); foreach (string s in foo.value.Keys) { Assert.IsTrue(bar.value.ContainsKey(s)); Assert.IsFalse(ReferenceEquals(foo.value[s], bar.value[s])); } CharTextProperty tp = bar.value["Some Text"] as CharTextProperty; Assert.IsFalse(tp is null); Assert.AreEqual(tp.value, "blah"); CharNumProperty np = bar.value["Some Number"] as CharNumProperty; Assert.IsFalse(np is null); Assert.AreEqual(np.value, 1.23m); CharSetProperty sp = bar.value["Some Collection"] as CharSetProperty; Assert.IsFalse(sp is null); Assert.AreEqual(sp.value.Count, 2); Assert.IsTrue(sp.value.Contains("bloh")); Assert.IsTrue(sp.value.Contains("bleh")); }
private void populate_property_rows(ObservableCollection <PropertyRow> prop_rows, List <string> path, CharDictProperty prop) { List <string> prop_names = new List <string>(prop.value.Keys); prop_names.Sort(); foreach (string prop_name in prop_names) { List <string> row_path = new List <string>(path); row_path.Add(prop_name); PropertyRow row = null; if (prop.value[prop_name] is CharTextProperty text_prop) { row = new PropertyRow(prop_name, row_path, text_prop.value); } else if (prop.value[prop_name] is CharNumProperty num_prop) { row = new PropertyRow(prop_name, row_path, num_prop.value.ToString()); } else if (prop.value[prop_name] is CharSetProperty set_prop) { row = new PropertyRow(prop_name, row_path); List <string> members = new List <string>(set_prop.value); members.Sort(); foreach (string member in members) { List <string> member_path = new List <string>(row_path); member_path.Add(member); row.children.Add(new PropertyRow(member, member_path)); } } else if (prop.value[prop_name] is CharDictProperty dict_prop) { row = new PropertyRow(prop_name, row_path); this.populate_property_rows(row.children, row_path, dict_prop); } if (row is not null) { prop_rows.Add(row); } } }