comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 12:ac232763858b

Re #9 - Make WarFoundry API use smaller methods * Obselete old Category and UnitType constructors and add minimal constructors * Add setters to properties where needed * Add value sanity checking to Category and UnitType * Set default values for percentages, choices and points for Category * Make XML factory use new constructor and setter properties * Add DuplicateItemException to project file
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 20:52:29 +0000
parents 5a1df00b0359
children 306558904c2a
comparison
equal deleted inserted replaced
11:5a1df00b0359 12:ac232763858b
383 383
384 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) 384 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
385 { 385 {
386 string id = elem.GetAttribute("id"); 386 string id = elem.GetAttribute("id");
387 string name = elem.GetAttribute("typeName"); 387 string name = elem.GetAttribute("typeName");
388 UnitType type = new UnitType(id, name, parentRace);
389 type.MinNumber = GetIntValueFromAttribute(elem, "minNum");
390 type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum");
391 type.MinSize = GetIntValueFromAttribute(elem, "minSize");
392 type.MaxSize = GetIntValueFromAttribute(elem, "maxSize");
393 //TODO: Add base size
394 type.CostPerTrooper = GetIntValueFromAttribute(elem, "points");
395 type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints");
396
397 XmlNode node = elem.FirstChild;
398
399 foreach(XmlElement cat in node.ChildNodes)
400 {
401 string catID = cat.GetAttribute("catID");
402 type.AddCategory(parentRace.GetCategory(catID));
403 }
404
388 string mainCatID = elem.GetAttribute("cat"); 405 string mainCatID = elem.GetAttribute("cat");
389 int minNum, maxNum, minSize, maxSize, baseSize;//TODO: Add base size 406 type.MainCategory = parentRace.GetCategory(mainCatID);
390 float points, unitPoints;
391 Stats stats;
392 List<UnitRequirement> unitRequirements = new List<UnitRequirement>();
393 bool found = false;
394 List<string> catIDs = new List<string>();
395 string catID;
396 minNum = GetIntValueFromAttribute(elem, "minNum");
397 maxNum = GetIntValueFromAttribute(elem, "maxNum");
398 minSize = GetIntValueFromAttribute(elem, "minSize");
399 maxSize = GetIntValueFromAttribute(elem, "maxSize");
400
401 if (minSize > maxSize && maxSize!=-1)
402 {
403 minSize = maxSize;
404 }
405
406 points = GetIntValueFromAttribute(elem, "points");
407 unitPoints = GetIntValueFromAttribute(elem, "unitPoints");
408
409 XmlNode node = elem.FirstChild;
410
411 foreach(XmlElement cat in node.ChildNodes)
412 {
413 catID = cat.GetAttribute("catID");
414 catIDs.Add(catID);
415
416 if (catID == mainCatID)
417 {
418 found = true;
419 }
420 }
421
422 if (!found)
423 {
424 throw new InvalidFileException("The main cat "+mainCatID+" was not found in the list of categories for unit "+id);
425 }
426
427 node = node.NextSibling; 407 node = node.NextSibling;
428 stats = ParseUnitStats((XmlElement)node, system); 408 type.UnitStats = ParseUnitStats((XmlElement)node, system);
429 //TODO: Add unit requirements 409 //TODO: Add unit requirements
430 UnitType type = new UnitType(id, name, mainCatID, catIDs.ToArray(), minNum, maxNum, minSize, maxSize, unitPoints, points, stats, unitRequirements.ToArray(), parentRace);
431 410
432 return type; 411 return type;
433 } 412 }
434 413
435 private Stats ParseUnitStats(XmlElement elem, GameSystem system) 414 private Stats ParseUnitStats(XmlElement elem, GameSystem system)