Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Objects/Race.cs @ 11:5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
* Add "add unit type" and "add equipment" methods to Race
* Deprecate old "set unit types" and "set equipment" methods on Race
* Update WarFoundryXmlFactory to use new methods
* Create DuplicateItemException for later use
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 18 Jan 2009 16:24:03 +0000 |
parents | 613bc5eaac59 |
children | ad8eaed12e66 |
comparison
equal
deleted
inserted
replaced
10:607c3232d689 | 11:5a1df00b0359 |
---|---|
128 | 128 |
129 public bool HasCategoryOverrides() | 129 public bool HasCategoryOverrides() |
130 { | 130 { |
131 return categories.Count > 0; | 131 return categories.Count > 0; |
132 } | 132 } |
133 | 133 |
134 public void AddEquipmentItem(EquipmentItem item) | |
135 { | |
136 //TODO: Throw DuplicateItemException | |
137 equipment.Add(item.ID, item); | |
138 } | |
139 | |
140 [Obsolete("Use AddEquipmentItem method instead")] | |
134 public void SetEquipmentItems(Dictionary<string, EquipmentItem> items) | 141 public void SetEquipmentItems(Dictionary<string, EquipmentItem> items) |
135 { | 142 { |
136 equipment = items; | 143 foreach (EquipmentItem item in items.Values) |
144 { | |
145 AddEquipmentItem(item); | |
146 } | |
137 } | 147 } |
138 | 148 |
139 public EquipmentItem GetEquipmentItem(string id) | 149 public EquipmentItem GetEquipmentItem(string id) |
140 { | 150 { |
141 EnsureFullyLoaded(); | 151 EnsureFullyLoaded(); |
151 items.Add(item); | 161 items.Add(item); |
152 } | 162 } |
153 | 163 |
154 return items; | 164 return items; |
155 } | 165 } |
156 | 166 |
167 public void AddUnitType(UnitType type) | |
168 { | |
169 unitTypes.Add(type.ID, type); | |
170 CacheUnitType(type); | |
171 } | |
172 | |
173 [Obsolete("Use AddUnitType method instead")] | |
157 public void SetUnitTypes(Dictionary<string, UnitType> types) | 174 public void SetUnitTypes(Dictionary<string, UnitType> types) |
158 { | 175 { |
159 unitTypes = types; | 176 foreach (UnitType type in types.Values) |
160 unitTypesByCat = null; | 177 { |
178 AddUnitType(type); | |
179 } | |
161 } | 180 } |
162 | 181 |
163 public UnitType[] GetUnitTypes(Category cat) | 182 public UnitType[] GetUnitTypes(Category cat) |
164 { | 183 { |
165 if (unitTypesByCat==null) | 184 BuildUnitTypesByCategoryCache(); |
166 { | |
167 BuildUnitTypesByCategoryCache(); | |
168 } | |
169 | |
170 Dictionary<string, UnitType> unitTypesDictionary; | 185 Dictionary<string, UnitType> unitTypesDictionary; |
171 unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); | 186 unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); |
172 UnitType[] unitTypesArray; | 187 UnitType[] unitTypesArray; |
173 | 188 |
174 if (unitTypesDictionary == null) | 189 if (unitTypesDictionary == null) |
180 unitTypesArray = DictionaryToArrayConverter.Convert<string, UnitType>(unitTypesDictionary); | 195 unitTypesArray = DictionaryToArrayConverter.Convert<string, UnitType>(unitTypesDictionary); |
181 } | 196 } |
182 | 197 |
183 return unitTypesArray; | 198 return unitTypesArray; |
184 } | 199 } |
200 | |
201 private void CacheUnitType(UnitType unit) | |
202 { | |
203 BuildUnitTypesByCategoryCache(); | |
204 Dictionary<string,UnitType> catUnitTypes; | |
205 unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes); | |
206 | |
207 if (catUnitTypes == null) | |
208 { | |
209 throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name)); | |
210 } | |
211 | |
212 catUnitTypes.Add(unit.ID, unit); | |
213 } | |
185 | 214 |
186 private void BuildUnitTypesByCategoryCache() | 215 private void BuildUnitTypesByCategoryCache() |
187 { | 216 { |
217 if (unitTypesByCat == null) | |
218 { | |
219 DoBuildUnitTypesByCategoryCache(); | |
220 } | |
221 } | |
222 | |
223 private void DoBuildUnitTypesByCategoryCache() | |
224 { | |
188 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); | 225 unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>(); |
189 | 226 |
190 foreach (Category category in Categories) | 227 foreach (Category category in Categories) |
191 { | 228 { |
192 unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); | 229 unitTypesByCat.Add(category, new Dictionary<string, UnitType>()); |
193 } | 230 } |
194 | 231 |
195 Dictionary<string,UnitType> catUnitTypes; | 232 foreach (UnitType unit in unitTypes.Values) |
196 | 233 { |
197 foreach (UnitType unit in unitTypes.Values) | 234 CacheUnitType(unit); |
198 { | 235 } |
199 unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes); | |
200 | |
201 if (catUnitTypes == null) | |
202 { | |
203 throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name)); | |
204 } | |
205 | |
206 catUnitTypes.Add(unit.ID, unit); | |
207 } | |
208 } | 236 } |
209 | 237 |
210 public UnitType GetUnitType(string id) | 238 public UnitType GetUnitType(string id) |
211 { | 239 { |
212 return (UnitType)unitTypes[id]; | 240 UnitType type = null; |
241 unitTypes.TryGetValue(id, out type); | |
242 return type; | |
213 } | 243 } |
214 | 244 |
215 public List<Ability> GetAbilityList() | 245 public List<Ability> GetAbilityList() |
216 { | 246 { |
217 List<Ability> items = new List<Ability>(); | 247 List<Ability> items = new List<Ability>(); |