Mercurial > repos > snowblizz-super-API-ideas
comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 9:6ad505b6c36e
Re #10 - Refactor for readability
* Refactor out repeated number parsing in to a separate method
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jan 2009 20:06:28 +0000 |
parents | 613bc5eaac59 |
children | 607c3232d689 |
comparison
equal
deleted
inserted
replaced
8:613bc5eaac59 | 9:6ad505b6c36e |
---|---|
354 private Category CreateCategoryFromElement(XmlElement elem) | 354 private Category CreateCategoryFromElement(XmlElement elem) |
355 { | 355 { |
356 string id = elem.GetAttribute("id"); | 356 string id = elem.GetAttribute("id"); |
357 string name = elem.GetAttribute("name"); | 357 string name = elem.GetAttribute("name"); |
358 int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount; | 358 int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount; |
359 | 359 minPc = GetIntValueFromAttribute(elem, "minPercentage"); |
360 try | 360 maxPc = GetIntValueFromAttribute(elem, "maxPercentage"); |
361 { | 361 minPts = GetIntValueFromAttribute(elem, "minPoints"); |
362 minPc = int.Parse(elem.GetAttribute("minPercentage")); | 362 maxPts = GetIntValueFromAttribute(elem, "maxPoints"); |
363 minChoices = GetIntValueFromAttribute(elem, "minChoices"); | |
364 maxChoices = GetIntValueFromAttribute(elem, "maxChoices"); | |
365 baseValue = GetIntValueFromAttribute(elem, "baseValue"); | |
366 incValue = GetIntValueFromAttribute(elem, "incValue"); | |
367 incAmount = GetIntValueFromAttribute(elem, "incAmount"); | |
368 | |
369 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount); | |
370 } | |
371 | |
372 private GetIntValueFromAttribute(XmlElement elem, string attributeName) | |
373 { | |
374 try | |
375 { | |
376 return int.Parse(elem.GetAttribute(attributeName)); | |
363 } | 377 } |
364 catch(FormatException) | 378 catch(FormatException) |
365 { | 379 { |
366 throw new FormatException("Attribute 'minPercentage' of category "+id+" was not a valid number"); | 380 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
367 } | 381 } |
368 | |
369 try | |
370 { | |
371 maxPc = int.Parse(elem.GetAttribute("maxPercentage")); | |
372 } | |
373 catch(FormatException) | |
374 { | |
375 throw new FormatException("Attribute 'maxPercentage' of category "+id+" was not a valid number"); | |
376 } | |
377 | |
378 try | |
379 { | |
380 minPts = int.Parse(elem.GetAttribute("minPoints")); | |
381 } | |
382 catch(FormatException) | |
383 { | |
384 throw new FormatException("Attribute 'minPoints' of category "+id+" was not a valid number"); | |
385 } | |
386 | |
387 try | |
388 { | |
389 maxPts = int.Parse(elem.GetAttribute("maxPoints")); | |
390 } | |
391 catch(FormatException) | |
392 { | |
393 throw new FormatException("Attribute 'maxPoints' of category "+id+" was not a valid number"); | |
394 } | |
395 | |
396 try | |
397 { | |
398 minChoices = int.Parse(elem.GetAttribute("minChoices")); | |
399 } | |
400 catch(FormatException) | |
401 { | |
402 throw new FormatException("Attribute 'minChoices' of category "+id+" was not a valid number"); | |
403 } | |
404 | |
405 try | |
406 { | |
407 maxChoices = int.Parse(elem.GetAttribute("maxChoices")); | |
408 } | |
409 catch(FormatException) | |
410 { | |
411 throw new FormatException("Attribute 'maxChoices' of category "+id+" was not a valid number"); | |
412 } | |
413 | |
414 try | |
415 { | |
416 baseValue = int.Parse(elem.GetAttribute("baseValue")); | |
417 | |
418 } | |
419 catch(FormatException) | |
420 { | |
421 throw new FormatException("Attribute 'baseValue' of category "+id+" was not a valid number"); | |
422 } | |
423 | |
424 try | |
425 { | |
426 incValue = int.Parse(elem.GetAttribute("incValue")); | |
427 } | |
428 catch(FormatException) | |
429 { | |
430 throw new FormatException("Attribute 'incValue' of category "+id+" was not a valid number"); | |
431 } | |
432 | |
433 try | |
434 { | |
435 incAmount = int.Parse(elem.GetAttribute("incAmount")); | |
436 } | |
437 catch(FormatException) | |
438 { | |
439 throw new FormatException("Attribute 'incAmount' of category "+id+" was not a valid number"); | |
440 } | |
441 | |
442 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount); | |
443 } | 382 } |
444 | 383 |
445 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) | 384 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) |
446 { | 385 { |
447 string id = elem.GetAttribute("id"); | 386 string id = elem.GetAttribute("id"); |
452 Stats stats; | 391 Stats stats; |
453 List<UnitRequirement> unitRequirements = new List<UnitRequirement>(); | 392 List<UnitRequirement> unitRequirements = new List<UnitRequirement>(); |
454 bool found = false; | 393 bool found = false; |
455 List<string> catIDs = new List<string>(); | 394 List<string> catIDs = new List<string>(); |
456 string catID; | 395 string catID; |
457 | 396 minNum = GetIntValueFromAttribute(elem, "minNum"); |
458 try | 397 maxNum = GetIntValueFromAttribute(elem, "maxNum"); |
459 { | 398 minSize = GetIntValueFromAttribute(elem, "minSize"); |
460 minNum = int.Parse(elem.GetAttribute("minNum")); | 399 maxSize = GetIntValueFromAttribute(elem, "maxSize"); |
461 } | |
462 catch(FormatException) | |
463 { | |
464 throw new FormatException("Attribute 'minNum' of unit "+id+" was not a valid number"); | |
465 } | |
466 | |
467 try | |
468 { | |
469 maxNum = int.Parse(elem.GetAttribute("maxNum")); | |
470 } | |
471 catch(FormatException) | |
472 { | |
473 throw new FormatException("Attribute 'maxNum' of unit "+id+" was not a valid number"); | |
474 } | |
475 | |
476 try | |
477 { | |
478 minSize = int.Parse(elem.GetAttribute("minSize")); | |
479 } | |
480 catch(FormatException) | |
481 { | |
482 throw new FormatException("Attribute 'minSize' of unit "+id+" was not a valid number"); | |
483 } | |
484 | |
485 try | |
486 { | |
487 maxSize = int.Parse(elem.GetAttribute("maxSize")); | |
488 } | |
489 catch(FormatException) | |
490 { | |
491 throw new FormatException("Attribute 'maxSize' of unit "+id+" was not a valid number"); | |
492 } | |
493 | 400 |
494 if (minSize > maxSize && maxSize!=-1) | 401 if (minSize > maxSize && maxSize!=-1) |
495 { | 402 { |
496 minSize = maxSize; | 403 minSize = maxSize; |
497 } | 404 } |
498 | 405 |
499 try | 406 points = GetIntValueFromAttribute(elem, "points"); |
500 { | 407 unitPoints = GetIntValueFromAttribute(elem, "unitPoints"); |
501 points = int.Parse(elem.GetAttribute("points")); | |
502 } | |
503 catch(FormatException) | |
504 { | |
505 throw new FormatException("Attribute 'points' of unit "+id+" was not a valid number"); | |
506 } | |
507 | |
508 try | |
509 { | |
510 unitPoints = int.Parse(elem.GetAttribute("unitPoints")); | |
511 } | |
512 catch(FormatException) | |
513 { | |
514 throw new FormatException("Attribute 'trooperPoints' of unit "+id+" was not a valid number"); | |
515 } | |
516 | 408 |
517 XmlNode node = elem.FirstChild; | 409 XmlNode node = elem.FirstChild; |
518 | 410 |
519 foreach(XmlElement cat in node.ChildNodes) | 411 foreach(XmlElement cat in node.ChildNodes) |
520 { | 412 { |