diff 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
line wrap: on
line diff
--- a/api/Exporters/WarFoundryHtmlExporter.cs	Fri May 21 19:41:02 2010 +0000
+++ b/api/Exporters/WarFoundryHtmlExporter.cs	Sat May 29 20:03:17 2010 +0000
@@ -21,6 +21,7 @@
 	public class WarFoundryHtmlExporter : IWarFoundryExporter
 	{
 		private static WarFoundryHtmlExporter exporter;
+		private delegate string GetStatCellTextDelegate(Stat stat);
 		
 		public static WarFoundryHtmlExporter GetDefault()
 		{
@@ -56,7 +57,8 @@
 			metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
 			head.AppendChild(metaCharset);
 			XmlElement style = doc.CreateElement("style");
-			style.InnerText = "table, th, td { border: 1px solid #000 }";
+			style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" +
+				"table table { width: 100% }";
 			head.AppendChild(style);
 			XmlElement body = doc.CreateElement("body");
 			html.AppendChild(body);
@@ -100,11 +102,22 @@
 			
 			foreach (Unit unit in army.GetUnits())
 			{
-				tables[unit.UnitType.StatsID].AppendChild(CreateUnitRow(unit, doc));
+				tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc));
 			}
 			
 			return DictionaryUtils.ToArray(tables);
 		}
+
+		private static string GetFirstStatType(Unit unit)
+		{
+			string[] unitStatIDs = unit.UnitStatsArrayIDs;
+			return GetFirstStatType(unitStatIDs);
+		}
+		
+		public static string GetFirstStatType(string[] unitStatIDs)
+		{
+			return unitStatIDs[0];
+		}
 		
 		private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
 		{
@@ -115,6 +128,10 @@
 			name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name");
 			headerRow.AppendChild(name);
 			
+			XmlElement unitTypeName = doc.CreateElement("th");
+			unitTypeName.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitTypeName", "type name");
+			headerRow.AppendChild(unitTypeName);
+			
 			foreach (StatSlot stat in stats.StatSlots)
 			{
 				XmlElement statHeader = doc.CreateElement("th");
@@ -139,13 +156,7 @@
 			XmlElement name = doc.CreateElement("td");
 			name.InnerText = unit.Name;
 			row.AppendChild(name);
-			
-			foreach (Stat stat in unit.UnitStatsArray)
-			{
-				XmlElement statCell = doc.CreateElement("td");
-				statCell.InnerText = stat.SlotValueString;
-				row.AppendChild(statCell);
-			}
+			CreateStatsBlock(unit, row);
 
 			StringBuilder sb = new StringBuilder();
 			UnitEquipmentItem[] unitEquipment = unit.GetEquipment ();
@@ -233,6 +244,73 @@
 			return row;
 		}
 		
+		private void CreateStatsBlock(Unit unit, XmlElement unitRow)
+		{
+			XmlDocument doc = unitRow.OwnerDocument;
+			XmlElement statsWrapperCell = doc.CreateElement("td");
+			unitRow.AppendChild(statsWrapperCell);
+			Stat[][] memberStats = unit.UnitStatsArraysWithName;
+			statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString());
+			string[] statTypeIDs = unit.UnitStatsArrayIDs;
+			string defaultStatType = GetFirstStatType(statTypeIDs);
+			Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell);
+			
+			int statCount = statTypeIDs.Length;
+			
+			for (int i = 0; i < statCount; i++)
+			{
+				Stat[] statLine = memberStats[i];
+				string statTypeID = statTypeIDs[i];
+				XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID);
+				int statLineCount = statLine.Length;
+				XmlElement statRow = doc.CreateElement("tr");
+				tableElement.AppendChild(statRow);
+				GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
+				AddStatCell(statLine[0].SlotValueString, statRow);
+				
+				for (int j = 1; j < statLineCount; j++)
+				{
+					string statText = statCellTextDelegate(statLine[j]);
+					AddStatCell(statText, statRow);
+				}
+			}
+		}
+
+		private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell)
+		{
+			Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>();
+			XmlDocument doc = containerCell.OwnerDocument;
+			
+			foreach (string statTypeID in statTypeIDs)
+			{
+				if (!statTables.ContainsKey(statTypeID))
+				{
+					XmlElement tableElement = doc.CreateElement("table");
+					containerCell.AppendChild(tableElement);
+					statTables[statTypeID] = tableElement;
+				}
+			}
+			
+			return statTables;
+		}
+
+		private string GetDefaultStatCellText(Stat stat)
+		{
+			return Translation.GetTranslation("armyHtmlExportDefaultStatCellText", "{0}", stat.SlotValueString, stat.ParentSlotName);
+		}
+
+		private string GetOtherStatCellText(Stat stat)
+		{
+			return Translation.GetTranslation("armyHtmlExportOtherStatCellText", "{1}: {0}", stat.SlotValueString, stat.ParentSlotName);
+		}
+		
+		private static void AddStatCell(string statValue, XmlElement row)
+		{
+			XmlElement statCell = row.OwnerDocument.CreateElement("td");
+			statCell.InnerText = statValue;
+			row.AppendChild(statCell);
+		}
+		
 		private string GetEquipmentAmountRatioTranslation (double amount, int number)
 		{
 			return Translation.GetTranslation ("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);