PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/JTacticalSim.DataContext/BaseDataContext.cs

https://github.com/Queztionmark/JTacticalSim
C# | 447 lines | 347 code | 87 blank | 13 comment | 0 complexity | ff18d4ccded5f7d547e5c10506c8a9e4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Dynamic;
  5. using System.Xml.Linq;
  6. using JTacticalSim.API.DTO;
  7. using JTacticalSim.API.Data;
  8. using JTacticalSim.Data.DTO;
  9. using JTacticalSim.API;
  10. using JTacticalSim.API.Game;
  11. using JTacticalSim.API.Component;
  12. using JTacticalSim.API.InfoObjects;
  13. namespace JTacticalSim.DataContext
  14. {
  15. public abstract class BaseDataContext : IDataContext
  16. {
  17. private readonly DataFileFactory _dataFileFactory = DataFileFactory.Instance;
  18. #region Objects/Accessors
  19. // Component Objects
  20. protected IBoardDTO _board;
  21. public IBoardDTO Board
  22. {
  23. get { return this._board; }
  24. protected set { this._board = value; }
  25. }
  26. protected IBasePointValues _basePointValues;
  27. public IBasePointValues BasePointValues
  28. {
  29. get { return this._basePointValues; }
  30. protected set { this._basePointValues = value; }
  31. }
  32. protected TableInfo<IDemographicDTO> _demographicsTable;
  33. [TableRecognizable(typeof(IDemographic))]
  34. public TableInfo<IDemographicDTO> DemographicsTable
  35. {
  36. get { return this._demographicsTable; }
  37. protected set { this._demographicsTable = value; }
  38. }
  39. protected TableInfo<INodeDTO> _nodesTable;
  40. [TableRecognizable(typeof(INode))]
  41. public TableInfo<INodeDTO> NodesTable
  42. {
  43. get { return this._nodesTable; }
  44. protected set { this._nodesTable = value; }
  45. }
  46. protected TableInfo<IUnitDTO> _unitsTable;
  47. [TableRecognizable(typeof(IUnit))]
  48. public TableInfo<IUnitDTO> UnitsTable
  49. {
  50. get { return this._unitsTable; }
  51. protected set { this._unitsTable = value; }
  52. }
  53. protected TableInfo<IFactionDTO> _factionsTable;
  54. [TableRecognizable(typeof(IFaction))]
  55. public TableInfo<IFactionDTO> FactionsTable
  56. {
  57. get { return this._factionsTable; }
  58. protected set { this._factionsTable = value; }
  59. }
  60. protected TableInfo<ICountryDTO> _countriesTable;
  61. [TableRecognizable(typeof(ICountry))]
  62. public TableInfo<ICountryDTO> CountriesTable
  63. {
  64. get { return this._countriesTable; }
  65. protected set { this._countriesTable = value; }
  66. }
  67. protected TableInfo<IPlayerDTO> _playersTable;
  68. [TableRecognizable(typeof(IPlayer))]
  69. public TableInfo<IPlayerDTO> PlayersTable
  70. {
  71. get { return this._playersTable; }
  72. protected set { this._playersTable = value; }
  73. }
  74. protected TableInfo<ITileDTO> _tilesTable;
  75. [TableRecognizable(typeof(ITile))]
  76. public TableInfo<ITileDTO> TilesTable
  77. {
  78. get { return this._tilesTable; }
  79. protected set { this._tilesTable = value; }
  80. }
  81. protected TableInfo<ISavedGameDTO> _savedGames;
  82. [TableRecognizable(typeof(ISavedGame))]
  83. public TableInfo<ISavedGameDTO> SavedGames
  84. {
  85. get { return this._savedGames; }
  86. protected set { this._savedGames = value; }
  87. }
  88. protected TableInfo<IScenarioDTO> _scenarios;
  89. [TableRecognizable(typeof(IScenario))]
  90. public TableInfo<IScenarioDTO> Scenarios
  91. {
  92. get { return this._scenarios; }
  93. protected set { this._scenarios = value; }
  94. }
  95. // Data Objects
  96. protected TableInfo<IUnitBaseTypeDTO> _unitBaseTypesTable;
  97. [TableRecognizable(typeof(IUnitBaseType))]
  98. public TableInfo<IUnitBaseTypeDTO> UnitBaseTypesTable
  99. {
  100. get { return this._unitBaseTypesTable; }
  101. protected set { this._unitBaseTypesTable = value; }
  102. }
  103. protected TableInfo<IUnitClassDTO> _unitClassesTable;
  104. [TableRecognizable(typeof(IUnitClass))]
  105. public TableInfo<IUnitClassDTO> UnitClassesTable
  106. {
  107. get { return this._unitClassesTable; }
  108. protected set { this._unitClassesTable = value; }
  109. }
  110. protected TableInfo<IUnitTypeDTO> _unitTypesTable;
  111. [TableRecognizable(typeof(IUnitType))]
  112. public TableInfo<IUnitTypeDTO> UnitTypesTable
  113. {
  114. get { return this._unitTypesTable; }
  115. protected set { this._unitTypesTable = value; }
  116. }
  117. protected TableInfo<IUnitTaskDTO> _unitTasksTable;
  118. [TableRecognizable(typeof(IUnitTask))]
  119. public TableInfo<IUnitTaskDTO> UnitTasksTable
  120. {
  121. get { return this._unitTasksTable; }
  122. protected set { this._unitTasksTable = value; }
  123. }
  124. protected TableInfo<IMissionObjectiveDTO> _missionObjectivesTable;
  125. [TableRecognizable(typeof(IMissionObjective))]
  126. public TableInfo<IMissionObjectiveDTO> MissionObjectivesTable
  127. {
  128. get { return this._missionObjectivesTable; }
  129. protected set { this._missionObjectivesTable = value; }
  130. }
  131. protected TableInfo<IUnitGroupTypeDTO> _unitGroupTypesTable;
  132. [TableRecognizable(typeof(IUnitGroupType))]
  133. public TableInfo<IUnitGroupTypeDTO> UnitGroupTypesTable
  134. {
  135. get { return this._unitGroupTypesTable; }
  136. protected set { this._unitGroupTypesTable = value; }
  137. }
  138. protected TableInfo<IUnitGeogTypeDTO> _unitGeogTypesTable;
  139. [TableRecognizable(typeof(IUnitGeogType))]
  140. public TableInfo<IUnitGeogTypeDTO> UnitGeogTypesTable
  141. {
  142. get { return this._unitGeogTypesTable; }
  143. protected set { this._unitGeogTypesTable = value; }
  144. }
  145. protected TableInfo<IDemographicClassDTO> _demographicClassesTable;
  146. [TableRecognizable(typeof(IDemographicClass))]
  147. public TableInfo<IDemographicClassDTO> DemographicClassesTable
  148. {
  149. get { return this._demographicClassesTable; }
  150. protected set { this._demographicClassesTable = value; }
  151. }
  152. protected TableInfo<IDemographicTypeDTO> _demographicTypesTable;
  153. [TableRecognizable(typeof(IDemographicType))]
  154. public TableInfo<IDemographicTypeDTO> DemographicTypesTable
  155. {
  156. get { return this._demographicTypesTable; }
  157. protected set { this._demographicTypesTable = value; }
  158. }
  159. protected TableInfo<IVictoryConditionDTO> _victoryConditionsTable;
  160. [TableRecognizable(typeof(IVictoryCondition))]
  161. public TableInfo<IVictoryConditionDTO> VictoryConditionsTable
  162. {
  163. get { return this._victoryConditionsTable; }
  164. protected set { this._victoryConditionsTable = value; }
  165. }
  166. // Lookups
  167. protected List<dynamic> _unitBaseTypeUnitClassesLookup;
  168. public List<dynamic> UnitBaseTypeUnitClassesLookup
  169. {
  170. get { return this._unitBaseTypeUnitClassesLookup; }
  171. protected set { this._unitBaseTypeUnitClassesLookup = value; }
  172. }
  173. protected List<dynamic> _unitBaseTypeUnitGeogTypesLookup;
  174. public List<dynamic> UnitBaseTypeUnitGeogTypesLookup
  175. {
  176. get { return this._unitBaseTypeUnitGeogTypesLookup; }
  177. protected set { this._unitBaseTypeUnitGeogTypesLookup = value; }
  178. }
  179. protected List<dynamic> _unitGeogTypeDemographicClassesLookup;
  180. public List<dynamic> UnitGeogTypeDemographicClassesLookup
  181. {
  182. get { return this._unitGeogTypeDemographicClassesLookup; }
  183. protected set { this._unitGeogTypeDemographicClassesLookup = value; }
  184. }
  185. // Missions
  186. protected List<dynamic> _unitGroupTypeUnitTaskLookup;
  187. public List<dynamic> UnitGroupTypeUnitTaskLookup
  188. {
  189. get { return this._unitGroupTypeUnitTaskLookup; }
  190. protected set { this._unitGroupTypeUnitTaskLookup = value; }
  191. }
  192. protected List<dynamic> _unitTaskUnitClassesLookup;
  193. public List<dynamic> UnitTaskUnitClassesLookup
  194. {
  195. get { return this._unitTaskUnitClassesLookup; }
  196. protected set { this._unitTaskUnitClassesLookup = value; }
  197. }
  198. protected List<dynamic> _missionObjectiveUnitTasks;
  199. public List<dynamic> MissionObjectiveUnitTasks
  200. {
  201. get { return this._missionObjectiveUnitTasks; }
  202. protected set { this._missionObjectiveUnitTasks = value; }
  203. }
  204. // ----------------------------------------------------
  205. protected List<dynamic> _unitAssignments;
  206. public List<dynamic> UnitAssignments
  207. {
  208. get { return this._unitAssignments; }
  209. protected set { this._unitAssignments = value; }
  210. }
  211. protected List<dynamic> _unitTransports;
  212. public List<dynamic> UnitTransports
  213. {
  214. get { return this._unitTransports; }
  215. protected set { this._unitTransports = value; }
  216. }
  217. protected List<dynamic> _unitBattleEffectiveLookup;
  218. public List<dynamic> UnitBattleEffectiveLookup
  219. {
  220. get { return this._unitBattleEffectiveLookup; }
  221. protected set { this._unitBattleEffectiveLookup = value; }
  222. }
  223. protected List<dynamic> _unitGeogTypeMovementOverrides;
  224. public List<dynamic> UnitGeogTypeMovementOverrides
  225. {
  226. get { return this._unitGeogTypeMovementOverrides; }
  227. protected set { this._unitGeogTypeMovementOverrides = value; }
  228. }
  229. protected List<dynamic> _unitTransportUnitTypeUnitClasses;
  230. public List<dynamic> UnitTransportUnitTypeUnitClasses
  231. {
  232. get { return this._unitTransportUnitTypeUnitClasses; }
  233. protected set { this._unitTransportUnitTypeUnitClasses = value; }
  234. }
  235. protected List<dynamic> _factionVictoryConditions;
  236. public List<dynamic> FactionVictoryConditions
  237. {
  238. get { return this._factionVictoryConditions; }
  239. protected set { this._factionVictoryConditions = value; }
  240. }
  241. protected List<int> _hybridDemographicClasses;
  242. public List<int> HybridDemographicClasses
  243. {
  244. get { return this._hybridDemographicClasses; }
  245. protected set { this._hybridDemographicClasses = value; }
  246. }
  247. protected List<dynamic> _movementHinderanceInDirection;
  248. public List<dynamic> MovementHinderanceInDirection
  249. {
  250. get { return this._movementHinderanceInDirection; }
  251. protected set { this._movementHinderanceInDirection = value; }
  252. }
  253. #endregion
  254. protected BaseDataContext()
  255. {
  256. // We don't want saved games refreshed when loading new games
  257. _savedGames = new TableInfo<ISavedGameDTO>();
  258. _scenarios = new TableInfo<IScenarioDTO>();
  259. Reset();
  260. }
  261. public virtual void Reset()
  262. {
  263. // Component Objects
  264. _nodesTable = new TableInfo<INodeDTO>();
  265. _unitsTable = new TableInfo<IUnitDTO>();
  266. _factionsTable = new TableInfo<IFactionDTO>();
  267. _countriesTable = new TableInfo<ICountryDTO>();
  268. _playersTable = new TableInfo<IPlayerDTO>();
  269. _demographicsTable = new TableInfo<IDemographicDTO>();
  270. _tilesTable = new TableInfo<ITileDTO>();
  271. _board = new BoardDTO();
  272. _basePointValues = new BasePointValuesDTO();
  273. // Data Objects
  274. _unitBaseTypesTable = new TableInfo<IUnitBaseTypeDTO>();
  275. _unitClassesTable = new TableInfo<IUnitClassDTO>();
  276. _unitTypesTable = new TableInfo<IUnitTypeDTO>();
  277. _unitGroupTypesTable = new TableInfo<IUnitGroupTypeDTO>();
  278. _unitGeogTypesTable = new TableInfo<IUnitGeogTypeDTO>();
  279. _unitTasksTable = new TableInfo<IUnitTaskDTO>();
  280. _missionObjectivesTable = new TableInfo<IMissionObjectiveDTO>();
  281. _demographicClassesTable = new TableInfo<IDemographicClassDTO>();
  282. _demographicTypesTable = new TableInfo<IDemographicTypeDTO>();
  283. _victoryConditionsTable = new TableInfo<IVictoryConditionDTO>();
  284. // Lookups
  285. _unitBaseTypeUnitClassesLookup = new List<dynamic>();
  286. _unitBaseTypeUnitGeogTypesLookup = new List<dynamic>();
  287. _unitGeogTypeDemographicClassesLookup = new List<dynamic>();
  288. _unitTaskUnitClassesLookup = new List<dynamic>();
  289. _missionObjectiveUnitTasks = new List<dynamic>();
  290. _unitGroupTypeUnitTaskLookup = new List<dynamic>();
  291. _unitAssignments = new List<dynamic>();
  292. _unitTransports = new List<dynamic>();
  293. _unitGeogTypeMovementOverrides = new List<dynamic>();
  294. _unitBattleEffectiveLookup = new List<dynamic>();
  295. _unitTransportUnitTypeUnitClasses = new List<dynamic>();
  296. _factionVictoryConditions = new List<dynamic>();
  297. _hybridDemographicClasses = new List<int>();
  298. _movementHinderanceInDirection = new List<dynamic>();
  299. }
  300. public virtual IResult<string> LoadSavedGameData(string gameDataDirectory, bool IsScenario)
  301. {
  302. var r = new DataResult<string>{Status = ResultStatus.SUCCESS, Result = gameDataDirectory};
  303. // We'll always store this in XML in the GameData root
  304. try
  305. {
  306. IDataFileInfo<XDocument> dataFiles = _dataFileFactory.GetDataFiles<XDocument>(gameDataDirectory, IsScenario);
  307. // Scenarios
  308. var scenarios = dataFiles.ScenarioDataFile.Descendants("Scenario")
  309. .Select(dto => new ScenarioDTO
  310. {
  311. ID = Convert.ToInt32(dto.Attribute("ID").Value),
  312. UID = Guid.NewGuid(),
  313. Name = dto.Attribute("Name").Value,
  314. GameFileDirectory = dto.Attribute("GameFileDirectory").Value,
  315. Author = dto.Attribute("GameFileDirectory").Value
  316. } as IScenarioDTO);
  317. // Games
  318. var savedGames = dataFiles.SavedGameDataFile.Descendants("SavedGame")
  319. .Select(dto => new SavedGameDTO
  320. {
  321. ID = Convert.ToInt32(dto.Attribute("ID").Value),
  322. UID = Guid.NewGuid(),
  323. Name = dto.Attribute("Name").Value,
  324. LastPlayed = Convert.ToBoolean(dto.Attribute("LastPlayed").Value),
  325. GameFileDirectory = dto.Attribute("GameFileDirectory").Value,
  326. Scenario = Convert.ToInt32(dto.Attribute("Scenario").Value)
  327. } as ISavedGameDTO);
  328. // Add to context
  329. this._scenarios.Records = scenarios.ToList();
  330. this._savedGames.Records = savedGames.ToList();
  331. }
  332. catch (Exception ex)
  333. {
  334. r.Status = ResultStatus.EXCEPTION;
  335. r.ex = ex;
  336. r.Messages.Add("Saved game data not loaded.");
  337. }
  338. r.Messages.Add("Saved game data loaded.");
  339. return r;
  340. }
  341. public virtual IResult<string> LoadData(string gameFileDirectory, bool IsScenario)
  342. {
  343. throw new NotImplementedException();
  344. }
  345. public virtual IResult<IGameFileCopyable> SaveData(IGameFileCopyable currentGame)
  346. {
  347. throw new NotImplementedException();
  348. }
  349. public virtual IResult<IGameFileCopyable> SaveDataAs(IGameFileCopyable current, IGameFileCopyable newGame)
  350. {
  351. throw new NotImplementedException();
  352. }
  353. public virtual IResult<IGameFileCopyable> RemoveSavedGameData(IGameFileCopyable delGame)
  354. {
  355. throw new NotImplementedException();
  356. }
  357. public void Dispose()
  358. {}
  359. }
  360. }