PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/JTacticalSim.Console/CommandProcessor/ConsoleCommandHandler.cs

https://github.com/Queztionmark/JTacticalSim
C# | 859 lines | 611 code | 201 blank | 47 comment | 141 complexity | 713901f87959dd8636b95bc4e466913d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Diagnostics;
  5. using System.Configuration;
  6. using System.Transactions;
  7. using System.IO;
  8. using JTacticalSim.API;
  9. using JTacticalSim.API.Component;
  10. using JTacticalSim.API.Game;
  11. using JTacticalSim.API.InfoObjects;
  12. using JTacticalSim.API.DTO;
  13. using JTacticalSim.API.Service;
  14. using JTacticalSim.Utility;
  15. using JTacticalSim.World;
  16. namespace JTacticalSim.Console
  17. {
  18. internal class ConsoleCommandHandler : BaseConsoleIOHandler, IInputCommandHandler
  19. {
  20. public ConsoleCommandHandler(IGame theGame)
  21. : base(theGame)
  22. {}
  23. #region Interface
  24. // Game
  25. public void Exit()
  26. {
  27. System.Environment.Exit(1);
  28. }
  29. public void Play()
  30. {
  31. // TODO: Check for loaded game
  32. if (_theGame.SavedGame == null)
  33. {
  34. SetInputError("No game is loaded. Please load a game from the list.");
  35. return;
  36. }
  37. _theGame.Start();
  38. DisplayMainBoardScreen();
  39. }
  40. public void LoadGame()
  41. {
  42. if (_commandArgs.Args.Length < 1)
  43. {
  44. SetInputError("Command requires a game title.");
  45. return;
  46. }
  47. var r = _theGame.LoadGame(_commandArgs.Args[0]);
  48. if (r.Status == ResultStatus.SUCCESS) System.Console.WriteLine(r.Message);
  49. if (r.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(r.Message, r.ex);
  50. // TODO: Handle other results
  51. }
  52. public void NewGame()
  53. {
  54. if (_commandArgs.Args.Length < 2)
  55. {
  56. SetInputError("Command requires a game title and a scenario.");
  57. return;
  58. }
  59. // Handle the scenario input
  60. var scenarioTitle = _commandArgs.Args[0];
  61. var scResult = IsValidScenarioTitleEntered(scenarioTitle);
  62. if (scResult.Status == ResultStatus.FAILURE)
  63. {
  64. SetInputError(scResult.Message);
  65. return;
  66. }
  67. // Handle the new game input
  68. var gTitle = _commandArgs.Args[1];
  69. var gResult = IsValidGameTitleEntered(gTitle);
  70. if (gResult.Status == ResultStatus.FAILURE)
  71. {
  72. SetInputError(gResult.Message);
  73. return;
  74. }
  75. var scenario = _theGame.JTSServices.GenericComponentService.GetByName<IScenario, IScenarioDTO>(scenarioTitle);
  76. var newGame = _theGame.SavedGame.ShallowCopy();
  77. newGame.Name = gTitle;
  78. newGame.GameFileDirectory = gTitle;
  79. newGame.LastPlayed = true;
  80. newGame.Scenario = scenario;
  81. using (var txn = new TransactionScope())
  82. {
  83. // Save the saved game record.
  84. var sResult = _theGame.JTSServices.GameService.SaveSavedGame(newGame);
  85. if (sResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(sResult.Message);
  86. if (sResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(sResult.Message, sResult.ex);
  87. // Save the saved game data.
  88. var cResult = _theGame.SaveAs(scenario, newGame);
  89. if (cResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(cResult.Message);
  90. if (cResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(cResult.Message, cResult.ex);
  91. // Load the newly saved game
  92. _theGame.LoadGame(newGame.GameFileDirectory);
  93. Play();
  94. txn.Complete();
  95. }
  96. }
  97. public void SaveGame()
  98. {
  99. var cResult = _theGame.Save();
  100. if (cResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(cResult.Message);
  101. if (cResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(cResult.Message, cResult.ex);
  102. }
  103. public void SaveGameAs()
  104. {
  105. if (_commandArgs.Args.Length < 1)
  106. {
  107. SetInputError("Command requires a game name.");
  108. return;
  109. }
  110. var title = _commandArgs.Args[0];
  111. var result = IsValidGameTitleEntered(title);
  112. if (result.Status == ResultStatus.FAILURE)
  113. {
  114. SetInputError(result.Message);
  115. return;
  116. }
  117. var newGame = _theGame.SavedGame.ShallowCopy();
  118. newGame.Name = title;
  119. newGame.GameFileDirectory = title;
  120. newGame.LastPlayed = true;
  121. using (var txn = new TransactionScope())
  122. {
  123. // Save the saved game record.
  124. var sResult = _theGame.JTSServices.GameService.SaveSavedGame(newGame);
  125. if (sResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(sResult.Message);
  126. if (sResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(sResult.Message, sResult.ex);
  127. // Save the saved game data.
  128. var cResult = _theGame.SaveAs(_theGame.SavedGame, newGame);
  129. if (cResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(cResult.Message);
  130. if (cResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(cResult.Message, cResult.ex);
  131. // Load the newly saved game
  132. _theGame.LoadGame(newGame.GameFileDirectory);
  133. Play();
  134. txn.Complete();
  135. }
  136. }
  137. public void DeleteGame()
  138. {
  139. if (_commandArgs.Args.Length < 1)
  140. {
  141. SetInputError("Command requires a game name.");
  142. return;
  143. }
  144. var title = _commandArgs.Args[0];
  145. ISavedGame delGame = null;
  146. try
  147. {
  148. delGame = _theGame.JTSServices.GameService.GetSavedGameByName(title);
  149. if (_theGame.SavedGame.Equals(delGame)) _theGame.LoadGame(null);
  150. }
  151. catch (ComponentNotFoundException cnfex)
  152. {
  153. SetInputError(cnfex.Message);
  154. return;
  155. }
  156. // Verify the delete
  157. System.Console.Write("Delete Game {0}? ".F(delGame.Name));
  158. if (!CommandLineUtil.ConsoleUtils.GetYesNoInputAsBool()) return;
  159. using (var txn = new TransactionScope())
  160. {
  161. // Delete the saved game record.
  162. var sResult = _theGame.JTSServices.GameService.RemoveSavedGame(delGame);
  163. if (sResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(sResult.Message);
  164. if (sResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(sResult.Message, sResult.ex);
  165. // Remove the saved game directory and files
  166. var dResult = _theGame.JTSServices.DataService.RemoveSavedGameData(delGame);
  167. if (dResult.Status == ResultStatus.SUCCESS) System.Console.WriteLine(dResult.Message);
  168. if (dResult.Status == ResultStatus.EXCEPTION) _theGame.Renderer.HandleErrorUI(dResult.Message, dResult.ex);
  169. txn.Complete();
  170. }
  171. }
  172. public void DisplayLegend()
  173. {
  174. _theGame.Renderer.DisplayLegend();
  175. }
  176. // Screens
  177. public void DisplayMainBoardScreen()
  178. {
  179. RenderBoard();
  180. }
  181. public void DisplayReinforcementsScreen()
  182. {
  183. GetReinforcements();
  184. }
  185. public void DisplayTitleScreen()
  186. {
  187. // Enter Title Screen state ...
  188. _theGame.StateSystem.ChangeState(StateType.TITLE_MENU);
  189. }
  190. public void DisplayCommandList()
  191. {
  192. foreach (KeyValuePair<CommandType, List<string>> kvp in CommandInterface.GetInputCommands(_theGame.StateSystem.CurrentStateType))
  193. {
  194. System.Console.WriteLine("{0} : ", kvp.Key.ToString());
  195. kvp.Value.ForEach(c => System.Console.WriteLine("\t{0}", c));
  196. System.Console.WriteLine();
  197. }
  198. }
  199. public void EndTurn()
  200. {
  201. _theGame.CurrentTurn.End();
  202. }
  203. public void DisplayUnit()
  204. {
  205. if (_commandArgs.Args.Length < 1 && (_theGame.GameBoard.SelectedUnits == null || _theGame.GameBoard.SelectedUnits.Count == 0))
  206. {
  207. SetInputError("Command requires a unit name.");
  208. return;
  209. }
  210. IUnit unit = (_theGame.GameBoard.SelectedUnits == null || _theGame.GameBoard.SelectedUnits.Count == 0)
  211. ? GetValidUnit(_commandArgs.Args[0])
  212. : _theGame.GameBoard.SelectedUnits.First();
  213. INode node = _theGame.JTSServices.NodeService.GetNodeAt(unit.Location);
  214. unit.DisplayInfo();
  215. node.DisplayInfo();
  216. }
  217. public void DisplayPlayer()
  218. {
  219. _theGame.CurrentTurn.Player.DisplayInfo();
  220. }
  221. public void DisplayAssignedUnits()
  222. {
  223. if (_commandArgs.Args.Length < 1)
  224. {
  225. SetInputError("Command requires a unit name.");
  226. return;
  227. }
  228. IUnit unit = GetValidUnit(_commandArgs.Args[0]);
  229. List<IUnit> assignedUnits = _theGame.JTSServices.UnitService.GetUnitsByUnitAssignment(unit.ID);
  230. if (assignedUnits != null)
  231. assignedUnits.ForEach(m => m.DisplayInfo());
  232. DrawLine();
  233. }
  234. public void DisplayUnits()
  235. {
  236. var atCurrentLocation = _commandArgs.Switches.Contains("--currentnode");
  237. INode n = null;
  238. if (atCurrentLocation)
  239. {
  240. n = GetSelectedNode();
  241. if (n == null) return;
  242. }
  243. var tmp = (atCurrentLocation)
  244. ? _theGame.JTSServices.UnitService.GetAllUnitsAt(n.Location)
  245. : _theGame.JTSServices.UnitService.GetAllUnits(_theGame.CurrentTurn.Player.Country);
  246. foreach (var o in tmp)
  247. {
  248. o.DisplayName();
  249. System.Console.WriteLine();
  250. }
  251. DrawLine();
  252. }
  253. public void SetCurrentNode()
  254. {
  255. if (_commandArgs.Args.Length < 2)
  256. {
  257. SetInputError("Command requires a Row and column value.");
  258. return;
  259. }
  260. if (!IsValidLocationEntered())
  261. return;
  262. var buttonClicked = (_commandArgs.Args.Length == 3) ? _commandArgs.Args[2] : null;
  263. var l = _theGame.JTSServices.TileService.CreateCoordinate(Convert.ToInt32(_commandArgs.Args[0]), Convert.ToInt32(_commandArgs.Args[1]), 0);
  264. // Fire events
  265. _theGame.JTSServices.NodeService.GetNodeAt(l).On_ComponentClicked(this, new ComponentClickedEventArgs(GetValidMouseButtonClick(buttonClicked)));
  266. }
  267. public void SetSelectedUnit()
  268. {
  269. _theGame.GameBoard.ClearSelectedItems();
  270. if (_commandArgs.Args.Length < 1)
  271. {
  272. SetInputError("Command requires unit name.");
  273. return;
  274. }
  275. var selectAttachedUnits = _commandArgs.Switches.Contains("--attachedunits");
  276. var unit = GetValidUnit(_commandArgs.Args[0]);
  277. var buttonClicked = (_commandArgs.Args.Length == 2) ? _commandArgs.Args[1] : null;
  278. // Fire unit clicked event
  279. // This will handle selecting the correct unit - the next cycled unit (displayed after cycle) for LL/currently displayed unit for others
  280. unit.On_ComponentClicked(this, new ComponentClickedEventArgs(GetValidMouseButtonClick(buttonClicked)));
  281. if (selectAttachedUnits)
  282. {
  283. var attached = unit.GetAllAttachedUnits().Where(u => u.LocationEquals(unit.Location));
  284. _theGame.GameBoard.SelectedUnits.AddRange(attached);
  285. }
  286. }
  287. public void SetSelectedUnits()
  288. {
  289. _theGame.GameBoard.ClearSelectedItems();
  290. if (_commandArgs.Args.Length < 2)
  291. {
  292. SetInputError("Command requires a Row and column value.");
  293. return;
  294. }
  295. if (!IsValidLocationEntered())
  296. return;
  297. var l = _theGame.JTSServices.TileService.CreateCoordinate(Convert.ToInt32(_commandArgs.Args[0]), Convert.ToInt32(_commandArgs.Args[1]), 0);
  298. // Only the units for the current player
  299. var countries = new List<ICountry>{_theGame.CurrentTurn.Player.Country};
  300. var units = _theGame.JTSServices.UnitService.GetUnitsAt(l, countries);
  301. if (units == null || units.Count == 0)
  302. {
  303. SetInputError("{0} has no units at the selected location.".F(_theGame.CurrentTurn.Player.Country.Name));
  304. return;
  305. }
  306. // var buttonClicked = (_commandArgs.Args.Length == 3) ? _commandArgs.Args[2] : null;
  307. // Fire unit clicked event
  308. // This will handle selecting the correct unit - the next cycled unit (displayed after cycle) for LL/currently displayed unit for others
  309. units.ForEach(u => u.Select());
  310. }
  311. public void DisplayCurrentNode()
  312. {
  313. var n = GetSelectedNode();
  314. if (n == null) return;
  315. n.DisplayInfo();
  316. var factions = _theGame.JTSServices.GenericComponentService.GetAll<IFaction, IFactionDTO>();
  317. var units = _theGame.JTSServices.UnitService.GetUnitsAt(_theGame.GameBoard.SelectedNode.Location, factions);
  318. if (units != null && units.Any())
  319. units.DisplayInfo();
  320. DrawLine();
  321. }
  322. public void MoveUnitsToSelectedNode()
  323. {
  324. var units = _theGame.GameBoard.SelectedUnits;
  325. if (units == null || units.Count == 0)
  326. {
  327. SetInputError("You must first select units to move.");
  328. return;
  329. }
  330. var results = new List<bool>();
  331. units.ForEach(u => results.Add(MoveUnitToSelectedNode(u)));
  332. if (results.Any(r => r))
  333. {
  334. var node = _theGame.GameBoard.SelectedNode;
  335. _theGame.GameBoard.ClearSelectedItems();
  336. // Reset the selected node
  337. _theGame.GameBoard.SelectedNode = node;
  338. }
  339. }
  340. public void MoveUnitToSelectedNode()
  341. {
  342. if (_theGame.GameBoard.SelectedUnits.Count > 1)
  343. {
  344. SetInputError("You have multiple units selected. Use command 'MoveUnits'.");
  345. return;
  346. }
  347. var unit = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits.Single() : null;
  348. if (unit == null)
  349. {
  350. SetInputError("You must first select a unit to move.");
  351. return;
  352. }
  353. var success = MoveUnitToSelectedNode(unit);
  354. if (success)
  355. {
  356. var node = _theGame.GameBoard.SelectedNode;
  357. _theGame.GameBoard.ClearSelectedItems();
  358. // Reset the selected node
  359. _theGame.GameBoard.SelectedNode = node;
  360. }
  361. }
  362. public void CycleUnits()
  363. {
  364. var n = GetSelectedNode();
  365. if (n == null) return;
  366. n.DefaultTile().GetCountryComponentStack(_theGame.CurrentTurn.Player.Country).CycleUnits();
  367. }
  368. public void AddUnit()
  369. {
  370. // Can only add units to a friendly node
  371. if (_theGame.GameBoard.SelectedNode == null || !_theGame.GameBoard.SelectedNode.IsFriendly())
  372. {
  373. SetInputError("You must first select a friendly space to add the unit to.");
  374. return;
  375. }
  376. var unitType = GetValidUnitType(true);
  377. var unitName = GetValidUnitName();
  378. var unitClass = GetValidUnitClass(unitType);
  379. var unitGroupType = GetValidUnitGroupType();
  380. var subNodeLocation = new SubNodeLocation(5);
  381. var ui = new UnitInfo(unitType, unitClass, unitGroupType);
  382. IUnit unit = _theGame.JTSServices.UnitService.CreateUnit(unitName,
  383. _theGame.GameBoard.SelectedNode.Location,
  384. _theGame.GameBoard.SelectedNode.Country,
  385. ui,
  386. subNodeLocation).SuccessfulObjects.First();
  387. unit.UnitInfo = ui;
  388. unit.SetNextID();
  389. unit.Description = unit.Name;
  390. INode node = _theGame.JTSServices.NodeService.GetNodeAt(_theGame.GameBoard.SelectedNode.Location);
  391. unit.StackOrder = node.VisibleUnitCount() + 1;
  392. _theGame.JTSServices.UnitService.SaveUnits(new List<IUnit> { unit });
  393. node.DefaultTile().AddComponentsToStacks(new List<IUnit> {unit});
  394. node.ResetUnitStackOrder();
  395. }
  396. public void RemoveUnit()
  397. {
  398. if (_commandArgs.Args.Length < 1)
  399. {
  400. SetInputError("Command requires a unit name.");
  401. return;
  402. }
  403. IUnit unit = GetValidUnit(_commandArgs.Args[0]);
  404. //From node - reset the unit stack order for all units at node AFTER move
  405. INode node = _theGame.JTSServices.NodeService.GetNodeAt(unit.Location);
  406. // Remove the unit object
  407. unit.RemoveFromGame();
  408. node.ResetUnitStackOrder();
  409. }
  410. public void AttachUnit()
  411. {
  412. var unitToAttach = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits.Single() : null;
  413. if (unitToAttach == null)
  414. {
  415. SetInputError("You must first select a unit to attach.");
  416. return;
  417. }
  418. if (_commandArgs.Args.Length < 1)
  419. {
  420. SetInputError("Command requires unit name.");
  421. return;
  422. }
  423. var attachToUnit = GetValidUnit(_commandArgs.Args[0]);
  424. var result = unitToAttach.AttachToUnit(attachToUnit);
  425. if (result.Status != ResultStatus.SUCCESS)
  426. SetInputError(result.Message);
  427. }
  428. public void DetachUnit()
  429. {
  430. var unitToDetach = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits.Single() : null;
  431. if (unitToDetach == null)
  432. {
  433. SetInputError("You must first select a unit to detach.");
  434. return;
  435. }
  436. var result = unitToDetach.DetachFromUnit();
  437. if (result.Status != ResultStatus.SUCCESS)
  438. SetInputError(result.Message);
  439. }
  440. public void LoadUnit()
  441. {
  442. var unitToLoad = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits.Single() : null;
  443. if (_commandArgs.Args.Length < 1)
  444. {
  445. SetInputError("Command requires a unit transport unit name.");
  446. return;
  447. }
  448. if (unitToLoad == null)
  449. {
  450. SetInputError("You must first select a unit to load.");
  451. return;
  452. }
  453. var transport = GetValidUnit(_commandArgs.Args[0]);
  454. var fromNode = unitToLoad.GetNode();
  455. var toNode = transport.GetNode();
  456. var result = transport.LoadUnits(new List<IUnit>{unitToLoad});
  457. if (result.Status != ResultStatus.SUCCESS)
  458. {
  459. SetInputError(result.Message);
  460. }
  461. else
  462. {
  463. fromNode.ResetUnitStackOrder();
  464. toNode.ResetUnitStackOrder();
  465. _theGame.GameBoard.ClearSelectedItems();
  466. }
  467. }
  468. public void DeployUnit()
  469. {
  470. var transportUnit = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits.Single() : null;
  471. var selectedNode = _theGame.GameBoard.SelectedNode ?? ((transportUnit == null) ? null : transportUnit.GetNode());
  472. if (transportUnit == null)
  473. {
  474. SetInputError("You must first select a transport unit.");
  475. return;
  476. }
  477. if (selectedNode == null)
  478. {
  479. SetInputError("You must first select a node to deploy to.");
  480. return;
  481. }
  482. if (_commandArgs.Args.Length < 1)
  483. {
  484. SetInputError("Command requires a unit name to deploy.");
  485. return;
  486. }
  487. var unitToDeploy = GetValidUnit(_commandArgs.Args[0]);
  488. var result = transportUnit.DeployUnits(new List<IUnit> {unitToDeploy}, selectedNode);
  489. if (result.Status != ResultStatus.SUCCESS)
  490. {
  491. SetInputError(result.Message);
  492. }
  493. else
  494. {
  495. _theGame.GameBoard.ClearSelectedItems();
  496. selectedNode.ResetUnitStackOrder();
  497. }
  498. }
  499. // Battle
  500. public void BarrageNode()
  501. {
  502. var units = (_theGame.GameBoard.SelectedUnits.Any()) ? _theGame.GameBoard.SelectedUnits : null;
  503. if (units == null)
  504. {
  505. SetInputError("You must first select attacking units.");
  506. return;
  507. }
  508. if (_theGame.GameBoard.SelectedNode == null)
  509. {
  510. SetInputError("You must first select an attack location.");
  511. return;
  512. }
  513. // Enter battle state ...
  514. _theGame.StateSystem.ChangeState(StateType.BATTLE);
  515. var sourceNode = units.First().GetNode();
  516. var targetNode = _theGame.GameBoard.SelectedNode;
  517. var r = _theGame.BattleHandler.BarrageUnitsAtLocation(_theGame.GameBoard.SelectedNode);
  518. // Handle issues with creating and doing battle
  519. if (r.Status == ResultStatus.FAILURE)
  520. {
  521. // Exit battle state ....
  522. _theGame.StateSystem.ChangeState(StateType.GAME_IN_PLAY);
  523. SetInputError(r.Message);
  524. return;
  525. }
  526. sourceNode.ResetUnitStackOrder();
  527. targetNode.ResetUnitStackOrder();
  528. _theGame.GameBoard.ClearSelectedItems();
  529. // Reset the selected node
  530. _theGame.GameBoard.SelectedNode = targetNode;
  531. // Exit battle state ...
  532. _theGame.StateSystem.ChangeState(StateType.GAME_IN_PLAY);
  533. }
  534. public void DoBattleAtLocation()
  535. {
  536. if (_theGame.GameBoard.SelectedNode == null)
  537. {
  538. SetInputError("You must first select a battle location.");
  539. return;
  540. }
  541. // Enter battle state ...
  542. _theGame.StateSystem.ChangeState(StateType.BATTLE);
  543. var targetNode = _theGame.GameBoard.SelectedNode;
  544. var r = _theGame.BattleHandler.DoBattleAtLocation(_theGame.GameBoard.SelectedNode);
  545. // Handle issues with creating and doing battle
  546. if (r.Status == ResultStatus.FAILURE)
  547. {
  548. // Exit battle state ...
  549. _theGame.StateSystem.ChangeState(StateType.GAME_IN_PLAY);
  550. SetInputError(r.Message);
  551. return;
  552. }
  553. targetNode.ResetUnitStackOrder();
  554. _theGame.GameBoard.ClearSelectedItems();
  555. // Reset the selected node
  556. _theGame.GameBoard.SelectedNode = targetNode;
  557. // Exit battle state ...
  558. _theGame.StateSystem.ChangeState(StateType.GAME_IN_PLAY);
  559. }
  560. // Other
  561. public void ScenarioEditor()
  562. {
  563. var curDrive = Directory.GetDirectoryRoot(Directory.GetCurrentDirectory());
  564. var scenarioExecutablePath = ConfigurationManager.AppSettings["gameeditor_executablepath"].ToString();
  565. Process.Start("{0}{1}{2}".F(curDrive, scenarioExecutablePath,"\\JTacticalSimEditor.exe"));
  566. }
  567. // Reinforcements
  568. public void GetReinforcements()
  569. {
  570. // Enter reinforcement state ...
  571. _theGame.StateSystem.ChangeState(StateType.REINFORCE);
  572. }
  573. public void AddReinforcementUnit()
  574. {
  575. var unitType = GetValidUnitType(false);
  576. var unitName = GetValidUnitName();
  577. var unitClass = GetValidUnitClass(unitType);
  578. var unitGroupType = GetValidUnitGroupType();
  579. // Validate the reinforcement points
  580. var subNodeLocation = new SubNodeLocation(5);
  581. var ui = new UnitInfo(unitType, unitClass, unitGroupType);
  582. IUnit unit = _theGame.JTSServices.UnitService.CreateUnit(unitName,
  583. null,
  584. _theGame.CurrentTurn.Player.Country,
  585. ui,
  586. subNodeLocation).SuccessfulObjects.First();
  587. // Remove points and update player
  588. unit.UnitInfo = ui;
  589. unit.SetNextID();
  590. unit.Description = unit.Name;
  591. _theGame.JTSServices.UnitService.SaveUnits(new List<IUnit> { unit });
  592. var r = _theGame.CurrentTurn.Player.AddReinforcementUnit(unit);
  593. if (r.Status == ResultStatus.EXCEPTION)
  594. {
  595. SetInputError(r.ex.Message);
  596. }
  597. if (r.Status == ResultStatus.FAILURE)
  598. {
  599. SetInputError(r.Message);
  600. }
  601. }
  602. public void PlaceReinforcementUnit()
  603. {
  604. // Can only add units to a friendly node
  605. if (_theGame.GameBoard.SelectedNode == null || !_theGame.GameBoard.SelectedNode.IsFriendly())
  606. {
  607. SetInputError("You must first select a friendly space to add the unit to.");
  608. return;
  609. }
  610. if (_commandArgs.Args.Length < 1)
  611. {
  612. SetInputError("Command requires unit name.");
  613. return;
  614. }
  615. var unit = GetValidReinforcementUnit(_commandArgs.Args[0]);
  616. INode node = _theGame.JTSServices.NodeService.GetNodeAt(_theGame.GameBoard.SelectedNode.Location);
  617. var placeResult = unit.PlaceAtLocation(node);
  618. // Handle issues
  619. if (placeResult.Status == ResultStatus.FAILURE)
  620. {
  621. SetInputError(placeResult.Message);
  622. return;
  623. }
  624. if (placeResult.Status == ResultStatus.EXCEPTION)
  625. {
  626. SetInputError(placeResult.ex.Message);
  627. return;
  628. }
  629. // Remove from player and handle result
  630. var removeResult = _theGame.CurrentTurn.Player.RemoveReinforcementUnit(unit);
  631. if (removeResult.Status == ResultStatus.EXCEPTION)
  632. {
  633. SetInputError(removeResult.ex.Message);
  634. }
  635. if (removeResult.Status == ResultStatus.FAILURE)
  636. {
  637. SetInputError(removeResult.Message);
  638. }
  639. }
  640. #region base overrides
  641. public override void GetCommandInput(ICommandInterface ci)
  642. {
  643. System.Console.ResetColor();
  644. System.Console.WriteLine("");
  645. System.Console.BackgroundColor = ConsoleColor.DarkYellow;
  646. System.Console.ForegroundColor = ConsoleColor.Black;
  647. System.Console.Write("Command: ");
  648. System.Console.ResetColor();
  649. System.Console.Write(" ");
  650. var command = System.Console.ReadLine();
  651. if (string.IsNullOrEmpty(command)) return;
  652. ParseCommandArgs(command);
  653. RunCommand(ci);
  654. }
  655. #endregion
  656. #endregion
  657. }
  658. }