comparison api/Exporters/WarFoundryHtmlExporter.cs @ 261:b9b8b0e60c31

Re #281: Export multiple stat lines in HTML * Add initial version of multiple stat lines per unit - still needs some work Also: * Line ending cleanup
author IBBoard <dev@ibboard.co.uk>
date Sat, 29 May 2010 20:03:17 +0000
parents 22509bd03ca2
children 900adf96a915
comparison
equal deleted inserted replaced
260:b72cc74a240b 261:b9b8b0e60c31
19 /// Custom exporter that exports an army as a basic HTML file 19 /// Custom exporter that exports an army as a basic HTML file
20 /// </summary> 20 /// </summary>
21 public class WarFoundryHtmlExporter : IWarFoundryExporter 21 public class WarFoundryHtmlExporter : IWarFoundryExporter
22 { 22 {
23 private static WarFoundryHtmlExporter exporter; 23 private static WarFoundryHtmlExporter exporter;
24 private delegate string GetStatCellTextDelegate(Stat stat);
24 25
25 public static WarFoundryHtmlExporter GetDefault() 26 public static WarFoundryHtmlExporter GetDefault()
26 { 27 {
27 if (exporter == null) 28 if (exporter == null)
28 { 29 {
54 XmlElement metaCharset = doc.CreateElement("meta"); 55 XmlElement metaCharset = doc.CreateElement("meta");
55 metaCharset.SetAttribute("http-equiv", "Content-Type"); 56 metaCharset.SetAttribute("http-equiv", "Content-Type");
56 metaCharset.SetAttribute("content", "text/html;charset=UTF-8"); 57 metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
57 head.AppendChild(metaCharset); 58 head.AppendChild(metaCharset);
58 XmlElement style = doc.CreateElement("style"); 59 XmlElement style = doc.CreateElement("style");
59 style.InnerText = "table, th, td { border: 1px solid #000 }"; 60 style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" +
61 "table table { width: 100% }";
60 head.AppendChild(style); 62 head.AppendChild(style);
61 XmlElement body = doc.CreateElement("body"); 63 XmlElement body = doc.CreateElement("body");
62 html.AppendChild(body); 64 html.AppendChild(body);
63 XmlElement header = doc.CreateElement("h1"); 65 XmlElement header = doc.CreateElement("h1");
64 header.InnerText = Translation.GetTranslation("armyHtmlOutputBodyHeader", "{0} - {1}pts", army.Name, army.Points); 66 header.InnerText = Translation.GetTranslation("armyHtmlOutputBodyHeader", "{0} - {1}pts", army.Name, army.Points);
98 tables[statSets.ID] = CreateTable(statSets, doc); 100 tables[statSets.ID] = CreateTable(statSets, doc);
99 } 101 }
100 102
101 foreach (Unit unit in army.GetUnits()) 103 foreach (Unit unit in army.GetUnits())
102 { 104 {
103 tables[unit.UnitType.StatsID].AppendChild(CreateUnitRow(unit, doc)); 105 tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc));
104 } 106 }
105 107
106 return DictionaryUtils.ToArray(tables); 108 return DictionaryUtils.ToArray(tables);
109 }
110
111 private static string GetFirstStatType(Unit unit)
112 {
113 string[] unitStatIDs = unit.UnitStatsArrayIDs;
114 return GetFirstStatType(unitStatIDs);
115 }
116
117 public static string GetFirstStatType(string[] unitStatIDs)
118 {
119 return unitStatIDs[0];
107 } 120 }
108 121
109 private XmlElement CreateTable(SystemStats stats, XmlDocument doc) 122 private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
110 { 123 {
111 XmlElement table = doc.CreateElement("table"); 124 XmlElement table = doc.CreateElement("table");
113 table.AppendChild(headerRow); 126 table.AppendChild(headerRow);
114 XmlElement name = doc.CreateElement("th"); 127 XmlElement name = doc.CreateElement("th");
115 name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name"); 128 name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name");
116 headerRow.AppendChild(name); 129 headerRow.AppendChild(name);
117 130
131 XmlElement unitTypeName = doc.CreateElement("th");
132 unitTypeName.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitTypeName", "type name");
133 headerRow.AppendChild(unitTypeName);
134
118 foreach (StatSlot stat in stats.StatSlots) 135 foreach (StatSlot stat in stats.StatSlots)
119 { 136 {
120 XmlElement statHeader = doc.CreateElement("th"); 137 XmlElement statHeader = doc.CreateElement("th");
121 statHeader.InnerText = stat.Name; 138 statHeader.InnerText = stat.Name;
122 headerRow.AppendChild(statHeader); 139 headerRow.AppendChild(statHeader);
137 { 154 {
138 XmlElement row = doc.CreateElement("tr"); 155 XmlElement row = doc.CreateElement("tr");
139 XmlElement name = doc.CreateElement("td"); 156 XmlElement name = doc.CreateElement("td");
140 name.InnerText = unit.Name; 157 name.InnerText = unit.Name;
141 row.AppendChild(name); 158 row.AppendChild(name);
142 159 CreateStatsBlock(unit, row);
143 foreach (Stat stat in unit.UnitStatsArray)
144 {
145 XmlElement statCell = doc.CreateElement("td");
146 statCell.InnerText = stat.SlotValueString;
147 row.AppendChild(statCell);
148 }
149 160
150 StringBuilder sb = new StringBuilder(); 161 StringBuilder sb = new StringBuilder();
151 UnitEquipmentItem[] unitEquipment = unit.GetEquipment (); 162 UnitEquipmentItem[] unitEquipment = unit.GetEquipment ();
152 163
153 if (unitEquipment.Length > 0) 164 if (unitEquipment.Length > 0)
231 row.AppendChild(points); 242 row.AppendChild(points);
232 243
233 return row; 244 return row;
234 } 245 }
235 246
247 private void CreateStatsBlock(Unit unit, XmlElement unitRow)
248 {
249 XmlDocument doc = unitRow.OwnerDocument;
250 XmlElement statsWrapperCell = doc.CreateElement("td");
251 unitRow.AppendChild(statsWrapperCell);
252 Stat[][] memberStats = unit.UnitStatsArraysWithName;
253 statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString());
254 string[] statTypeIDs = unit.UnitStatsArrayIDs;
255 string defaultStatType = GetFirstStatType(statTypeIDs);
256 Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell);
257
258 int statCount = statTypeIDs.Length;
259
260 for (int i = 0; i < statCount; i++)
261 {
262 Stat[] statLine = memberStats[i];
263 string statTypeID = statTypeIDs[i];
264 XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID);
265 int statLineCount = statLine.Length;
266 XmlElement statRow = doc.CreateElement("tr");
267 tableElement.AppendChild(statRow);
268 GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
269 AddStatCell(statLine[0].SlotValueString, statRow);
270
271 for (int j = 1; j < statLineCount; j++)
272 {
273 string statText = statCellTextDelegate(statLine[j]);
274 AddStatCell(statText, statRow);
275 }
276 }
277 }
278
279 private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell)
280 {
281 Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>();
282 XmlDocument doc = containerCell.OwnerDocument;
283
284 foreach (string statTypeID in statTypeIDs)
285 {
286 if (!statTables.ContainsKey(statTypeID))
287 {
288 XmlElement tableElement = doc.CreateElement("table");
289 containerCell.AppendChild(tableElement);
290 statTables[statTypeID] = tableElement;
291 }
292 }
293
294 return statTables;
295 }
296
297 private string GetDefaultStatCellText(Stat stat)
298 {
299 return Translation.GetTranslation("armyHtmlExportDefaultStatCellText", "{0}", stat.SlotValueString, stat.ParentSlotName);
300 }
301
302 private string GetOtherStatCellText(Stat stat)
303 {
304 return Translation.GetTranslation("armyHtmlExportOtherStatCellText", "{1}: {0}", stat.SlotValueString, stat.ParentSlotName);
305 }
306
307 private static void AddStatCell(string statValue, XmlElement row)
308 {
309 XmlElement statCell = row.OwnerDocument.CreateElement("td");
310 statCell.InnerText = statValue;
311 row.AppendChild(statCell);
312 }
313
236 private string GetEquipmentAmountRatioTranslation (double amount, int number) 314 private string GetEquipmentAmountRatioTranslation (double amount, int number)
237 { 315 {
238 return Translation.GetTranslation ("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number); 316 return Translation.GetTranslation ("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
239 } 317 }
240 318