view api/DefaultWarFoundryLoader.cs @ 275:10fb73206bd2

Re #296: Fix handling of equipment slot with no limit * Take the easy way out - slots are to limit things and the code never worked without a limit before, even if the file validated, so just require a limit Also: * Arbitrary line ending clean-up from using Geany
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jul 2010 13:52:26 +0000
parents a36a0e9cc05d
children 5ed39187b0e3
line wrap: on
line source

// This file (DefaultWarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
// 
// 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.

using System;
using System.Collections.Generic;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.API
{
	/// <summary>
	/// The default implementation of a <see cref="AbstractWarFoundryLoader"/>
	/// </summary>
	public class DefaultWarFoundryLoader : AbstractWarFoundryLoader
	{
		private Dictionary<string, GameSystem> systemsTable;
		private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace

		public DefaultWarFoundryLoader ()
		{
		}
		
		protected override void PrepareForFileLoad()
		{
			//Just set up blank dictionaries for now - may try different and more complex handling in future
			systemsTable = new Dictionary<string,GameSystem>();
			racesTable = new Dictionary<string,Dictionary<string,Dictionary<string,Race>>>();
		}
		
		protected override GameSystem GetExistingSystemForSystem (GameSystem system)
		{
			return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower());
		}
		
		protected override void DoStoreGameSystem (GameSystem system)
		{
			systemsTable[system.ID.ToLower()] = system;
		}

		protected override void DoStoreRace (Race race)
		{
			Dictionary<string, Dictionary<string, Race>> systemRaces;
			
			string systemID = race.GameSystem.ID.ToLower();
			racesTable.TryGetValue(systemID, out systemRaces);
			
			if (systemRaces==null)
			{
				systemRaces = new Dictionary<string,Dictionary<string,Race>>();
				racesTable.Add(systemID, systemRaces);
			}
			
			Dictionary<string, Race> subRaces;
			systemRaces.TryGetValue(race.ID.ToLower(), out subRaces);
			
			if (subRaces==null)
			{
				subRaces = new Dictionary<string,Race>();
				systemRaces.Add(race.ID.ToLower(), subRaces);
			}
			
			string subID = race.SubID.ToLower();

			if (subRaces.ContainsKey(subID))
			{
				Race existingRace = subRaces[subID];
				
				if (!race.Equals(existingRace))
				{
					//TODO: Raise an event to say we got a different duplicate
					//We can't just fail, because failing is for completely unhandled files, not for objects in a file
				}
			}
			else
			{
				subRaces.Add(race.SubID.ToLower(), race);
			}
		}
		
		public override GameSystem[] GetGameSystems()
		{
			if (systemsTable==null)
			{
				LoadFiles();
			}
			
			return DictionaryUtils.ToArray<string, GameSystem>(systemsTable);
		}
		
		public override GameSystem GetGameSystem(string systemID)
		{
			if (systemsTable==null)
			{
				LoadFiles();
			}
			
			GameSystem system;
			systemsTable.TryGetValue(systemID.ToLower(), out system);
			return system;
		}
		
		protected internal override void RemoveGameSystem(GameSystem system)
		{
			systemsTable.Remove(system.ID.ToLower());
		}
		
		public override Race[] GetRaces(GameSystem system)
		{
			return GetRaces(system.ID);
		}

		/// <summary>
		/// Gets an array of the races for a game system by ID.
		/// </summary>
		/// <param name="systemID">
		/// The <see cref="System.String"/> ID of the game system to get races for
		/// </param>
		/// <returns>
		/// An array of <see cref="Race"/>s for the specified game system
		/// </returns>
		public Race[] GetRaces(string systemID)
		{
			if (racesTable==null)
			{
				LoadFiles();
			}
			
			systemID = systemID.ToLower();
			Dictionary<string, Dictionary<string, Race>> system;
			racesTable.TryGetValue(systemID, out system);
			
			if (system==null)
			{
				return new Race[0];
			}

			int count = 0;

			foreach (Dictionary<string, Race> racesDict in system.Values)
			{
				count+= racesDict.Count;
			}

			Race[] races = new Race[count];
			int i = 0;

			foreach (string raceID in system.Keys)
			{
				foreach (string raceSubId in system[raceID].Keys)
				{
					races[i++] = GetRace(systemID, raceID, raceSubId);
				}
			}

			return races;
		}
		
		public override Race GetRace(GameSystem system, string raceID)
		{
			return GetRace(system.ID, raceID);
		}

		/// <summary>
		/// Gets a single race for a given game system by ID of the game system and race.
		/// </summary>
		/// <param name="systemID">
		/// The <see cref="System.String"/> ID of the game system that the race is part of.
		/// </param>
		/// <param name="raceID">
		/// The <see cref="System.String"/> ID for the race to load.
		/// </param>
		/// <returns>
		/// A <see cref="Race"/> with the specified ID from the game system with the specified ID, or <code>null</code> if there is no race or game system with those IDs.
		/// </returns>
		public Race GetRace(string systemID, string raceID)
		{
			return GetRace(systemID, raceID, "");
		}

		public override Race GetRace(GameSystem system, string raceID, string raceSubID)
		{
			return GetRace(system.ID, raceID, raceSubID);
		}

		/// <summary>
		/// Gets a single race for a given game system by the game system's ID and the race's ID and sub-race ID.
		/// </summary>
		/// <param name="systemID">
		/// The <see cref="System.String"/> ID of the game system that the race is part of.
		/// </param>
		/// <param name="raceID">
		/// The <see cref="System.String"/> ID for the race to load.
		/// </param>
		/// <param name="raceSubID">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="Race"/>
		/// </returns>
		public Race GetRace(string systemID, string raceID, string raceSubID)
		{
			if (racesTable==null)
			{
				LoadFiles();
			}
			
			Race race = null;
			
			Dictionary<string, Race> subraces = GetRaceTable(systemID, raceID);

			if (subraces != null)
			{
				subraces.TryGetValue(raceSubID.ToLower(), out race);
			}
			
			return race;
		}

		private Dictionary<string, Race> GetRaceTable(string systemID, string raceID)
		{
			Dictionary<string, Dictionary<string, Race>> races;
			racesTable.TryGetValue(systemID.ToLower(), out races);
			Dictionary<string, Race> subraces = null;

			if (races != null)
			{
				races.TryGetValue(raceID.ToLower(), out subraces);
			}

			return subraces;
		}

		protected internal override void RemoveRace(Race race)
		{
			Dictionary<string, Race> subraces = GetRaceTable(race.GameSystem.ID, race.ID);

			if (subraces != null)
			{
				subraces.Remove(race.SubID.ToLower());
			}
		}
		
		public override string[] GetGameSystemIDs()
		{
			if (systemsTable==null)
			{
				LoadFiles();
			}

			return DictionaryUtils.ToKeyArray(systemsTable);
		}
		
		public override string[] GetSystemRaceIDs(GameSystem system)
		{
			return GetSystemRaceIDs(system.ID);
		}

		/// <summary>
		/// Gets the IDs of all of the races of a specified game system.
		/// </summary>
		/// <param name="systemID">
		/// The <see cref="System.String"/> ID of the game system to get the available races for.
		/// </param>
		/// <returns>
		/// An array of <see cref="System.String"/>s representing the IDs of the races of the specified game system.
		/// </returns>
		public string[] GetSystemRaceIDs(string systemID)
		{
			if (racesTable == null)
			{
				LoadFiles();
			}

			Dictionary<string, Dictionary<string, Race>> races = racesTable[systemID.ToLower()];

			if (races==null)
			{
				return new string[0];
			}
			else
			{
				string[] keys = new string[races.Keys.Count];
				int i = 0;

				foreach (string key in races.Keys)
				{
					keys[i++] = key;
				}

				return keys;
			}
		}
	}
}