Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Exporters/WarFoundryXMLWithXSLExporter.cs @ 402:3a71f8af5bde xml-basic
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 |
rev | line source |
---|---|
399 | 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. | |
4 | |
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; | |
17 | |
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; | |
26 | |
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 } | |
34 | |
35 return exporter; | |
36 } | |
37 | |
38 private WarFoundryXmlWithXslExporter() | |
39 { | |
40 // Hide constructor | |
41 } | |
42 | |
43 public void ExportArmy(Army army, string path) | |
44 { | |
45 XmlDocument armyList = new XmlDocument(); | |
46 | |
47 // Everything will be a child of the army element | |
48 XmlElement root = armyList.CreateElement("army"); | |
49 | |
50 // Basic army information | |
51 XmlElement armyRace = armyList.CreateElement("race"); | |
52 armyRace.InnerText = army.Race.Name; | |
53 root.AppendChild(armyRace); | |
54 | |
55 XmlElement armyName = armyList.CreateElement("name"); | |
56 armyName.InnerText = army.Name; | |
57 root.AppendChild(armyName); | |
58 | |
59 XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable"); | |
60 armyAvailablePoints.InnerText = army.MaxPoints.ToString(); | |
61 root.AppendChild(armyAvailablePoints); | |
62 | |
63 XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed"); | |
64 armyUsedPoints.InnerText = army.Points.ToString(); | |
65 root.AppendChild(armyUsedPoints); | |
66 | |
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); | |
74 | |
75 | |
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); | |
81 | |
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()); | |
96 | |
97 foreach (UnitEquipmentItem equip in uni.GetEquipment()) | |
98 { | |
99 XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); | |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
100 //armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString()); |
399 | 101 |
102 // Item Name | |
103 XmlElement armyEquipmentName = armyList.CreateElement("name"); | |
104 armyEquipmentName.InnerText = equip.Name; | |
105 armyEquipmentItem.AppendChild(armyEquipmentName); | |
106 | |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
107 int armyEquipAmount = 0; |
399 | 108 |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
109 if (UnitEquipmentUtil.GetEquipmentAmount(uni, equip) == null) |
399 | 110 { |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
111 armyEquipAmount = 0; |
399 | 112 } |
113 else | |
114 { | |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
115 armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip); |
399 | 116 } |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
117 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
118 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip)) |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
119 { |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
120 float fraction = (float)(armyEquipAmount / 100.0); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
121 armyEquipAmount = (int)(fraction * uni.Size); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
122 } |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
123 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
124 armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString()); |
399 | 125 |
126 armyUnit.AppendChild(armyEquipmentItem); | |
127 } | |
128 | |
129 foreach (Ability abil in uni.Abilities) | |
130 { | |
131 XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); | |
132 | |
133 XmlElement armyAbilityName = armyList.CreateElement("name"); | |
134 armyAbilityName.InnerText = abil.Name; | |
135 armyAbilityItem.AppendChild(armyAbilityName); | |
136 | |
137 XmlElement armyAbilityDesc = armyList.CreateElement("description"); | |
138 armyAbilityDesc.InnerText = abil.Description; | |
139 armyAbilityItem.AppendChild(armyAbilityDesc); | |
140 | |
141 armyUnit.AppendChild(armyAbilityItem); | |
142 } | |
143 | |
144 armyCategory.AppendChild(armyUnit); | |
145 } | |
146 root.AppendChild(armyCategory); | |
147 } | |
148 | |
149 | |
150 | |
151 | |
152 // Append all Categories to the XML doc | |
153 | |
154 // Append tree to document | |
155 armyList.AppendChild(root); | |
156 | |
157 // Simple XML output settings | |
158 XmlWriterSettings xmlSettings = new XmlWriterSettings(); | |
159 xmlSettings.Indent = true; | |
160 xmlSettings.IndentChars = " "; | |
161 | |
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 } | |
173 | |
174 private string GetEquipmentAmountNumberTranslation(int amount) | |
175 { | |
176 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount); | |
177 } | |
178 | |
179 private string GetEquipmentAmountAllTranslation(Unit unit) | |
180 { | |
181 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size); | |
182 } | |
183 } | |
184 } |