view API/Exporters/WarFoundryXMLWithXSLExporter.cs @ 402:3a71f8af5bde

Correct count of unit items
author Dan.Kulinski@dank-laptop.Global.Local
date Mon, 15 Aug 2011 07:53:58 -0600
parents 9a49d34599fb
children 5d7584a73d7f
line source
1 // This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Text;
9 using System.Xml;
10 using System.Xml.Xsl;
11 using System.Xml.XPath;
12 using System.Xml.Schema;
13 using IBBoard.Lang;
14 using IBBoard.Xml;
15 using IBBoard.WarFoundry.API.Objects;
16 using IBBoard.WarFoundry.API.Util;
18 namespace IBBoard.WarFoundry.API.Exporters
19 {
20 /// <summary>
21 /// Custom exporter that exports an army as an XML file with an XSLT applied
22 /// </summary>
23 public class WarFoundryXmlWithXslExporter : IWarFoundryExporter
24 {
25 private static WarFoundryXmlWithXslExporter exporter;
27 // Return the default class associated with this exporter
28 public static WarFoundryXmlWithXslExporter GetDefault()
29 {
30 if (exporter == null)
31 {
32 exporter = new WarFoundryXmlWithXslExporter();
33 }
35 return exporter;
36 }
38 private WarFoundryXmlWithXslExporter()
39 {
40 // Hide constructor
41 }
43 public void ExportArmy(Army army, string path)
44 {
45 XmlDocument armyList = new XmlDocument();
47 // Everything will be a child of the army element
48 XmlElement root = armyList.CreateElement("army");
50 // Basic army information
51 XmlElement armyRace = armyList.CreateElement("race");
52 armyRace.InnerText = army.Race.Name;
53 root.AppendChild(armyRace);
55 XmlElement armyName = armyList.CreateElement("name");
56 armyName.InnerText = army.Name;
57 root.AppendChild(armyName);
59 XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable");
60 armyAvailablePoints.InnerText = army.MaxPoints.ToString();
61 root.AppendChild(armyAvailablePoints);
63 XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed");
64 armyUsedPoints.InnerText = army.Points.ToString();
65 root.AppendChild(armyUsedPoints);
67 // Get Categories and interate through each
68 foreach(ArmyCategory cat in army.Categories)
69 {
70 if (cat.GetUnits().Length == 0)
71 continue;
72 XmlElement armyCategory = armyList.CreateElement("category");
73 armyCategory.SetAttribute("type", cat.Name);
76 // Get units and iterate through each
77 foreach(Unit uni in cat.GetUnits())
78 {
79 XmlElement armyUnit = armyList.CreateElement("unit");
80 armyUnit.SetAttribute("name", uni.UnitType.Name);
82 foreach (Stat[] stat in uni.UnitStatsArraysWithName)
83 {
84 XmlElement armyStatLine = armyList.CreateElement("statLine");
85 foreach (Stat singleStat in stat)
86 {
87 XmlElement armyStat = armyList.CreateElement("stat");
88 armyStat.SetAttribute("name", singleStat.ParentSlotName);
89 armyStat.SetAttribute("value", singleStat.SlotValueString);
90 armyStatLine.AppendChild(armyStat);
91 }
92 armyUnit.AppendChild(armyStatLine);
93 }
94 armyUnit.SetAttribute("points", uni.Points.ToString());
95 armyUnit.SetAttribute("models", uni.Size.ToString());
97 foreach (UnitEquipmentItem equip in uni.GetEquipment())
98 {
99 XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
100 //armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString());
102 // Item Name
103 XmlElement armyEquipmentName = armyList.CreateElement("name");
104 armyEquipmentName.InnerText = equip.Name;
105 armyEquipmentItem.AppendChild(armyEquipmentName);
107 int armyEquipAmount = 0;
109 if (UnitEquipmentUtil.GetEquipmentAmount(uni, equip) == null)
110 {
111 armyEquipAmount = 0;
112 }
113 else
114 {
115 armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
116 }
118 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
119 {
120 float fraction = (float)(armyEquipAmount / 100.0);
121 armyEquipAmount = (int)(fraction * uni.Size);
122 }
124 armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString());
126 armyUnit.AppendChild(armyEquipmentItem);
127 }
129 foreach (Ability abil in uni.Abilities)
130 {
131 XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
133 XmlElement armyAbilityName = armyList.CreateElement("name");
134 armyAbilityName.InnerText = abil.Name;
135 armyAbilityItem.AppendChild(armyAbilityName);
137 XmlElement armyAbilityDesc = armyList.CreateElement("description");
138 armyAbilityDesc.InnerText = abil.Description;
139 armyAbilityItem.AppendChild(armyAbilityDesc);
141 armyUnit.AppendChild(armyAbilityItem);
142 }
144 armyCategory.AppendChild(armyUnit);
145 }
146 root.AppendChild(armyCategory);
147 }
152 // Append all Categories to the XML doc
154 // Append tree to document
155 armyList.AppendChild(root);
157 // Simple XML output settings
158 XmlWriterSettings xmlSettings = new XmlWriterSettings();
159 xmlSettings.Indent = true;
160 xmlSettings.IndentChars = " ";
162 // Write XML to file
163 using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
164 {
165 armyList.Save(writer);
166 writer.Flush();
167 }
168 }
169 private string GetEquipmentAmountRatioTranslation(double amount, int number)
170 {
171 return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
172 }
174 private string GetEquipmentAmountNumberTranslation(int amount)
175 {
176 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
177 }
179 private string GetEquipmentAmountAllTranslation(Unit unit)
180 {
181 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
182 }
183 }
184 }