public IActionResult Index(Guid?id)
        {
            var vm = new GameStateSpecificationViewModel
            {
                Id            = id,
                AllShips      = CreateShipOptions().ToList(),
                SelectedShips = new List <ShipSelection>()
            };

            GameState existingGameState;

            if (id != null)
            {
                existingGameState = _gsRepo.GetById(id.Value).Result;
                foreach (var p in existingGameState.Pawns)
                {
                    vm.SelectedShips.Add(new ShipSelection(p));
                }

                vm.GameName = existingGameState.Name;
            }

            RemoveEmptyEntriesAndEnsureLastOptionEmpty(vm);

            return(View(vm));
        }
        public IActionResult Index(Guid?id, GameStateSpecificationViewModel vm)
        {
            ModelState.Clear();

            vm.Id       = id;
            vm.AllShips = CreateShipOptions().ToList();
            RemoveEmptyEntriesAndEnsureLastOptionEmpty(vm);



            foreach (var ship in vm.SelectedShips)
            {
                var isShipSelected = !string.IsNullOrWhiteSpace(ship.Code);
                if (!isShipSelected)
                {
                    continue;
                }

                var spec = GameRules.GetShipByCode(ship.Code);
                if (string.IsNullOrWhiteSpace(ship.Name))
                {
                    ship.Name = spec.Registry
                                + GetNextRegistryNumber(vm.SelectedShips, spec.Registry).ToString().PadLeft(3, '0')
                                + " " + spec.Name;
                }
            }

            return(View(vm));
        }
 private static void RemoveEmptyEntriesAndEnsureLastOptionEmpty(GameStateSpecificationViewModel vm)
 {
     vm.SelectedShips.RemoveAll(x => string.IsNullOrWhiteSpace(x.Code));
     if (!vm.SelectedShips.Any() || !string.IsNullOrWhiteSpace(vm.SelectedShips.Last().Code))
     {
         vm.SelectedShips.Add(new ShipSelection());
     }
 }
        public IActionResult BeginGame(Guid?id, GameStateSpecificationViewModel vm)
        {
            var shipsToCreate = vm.SelectedShips.Where(x => x.ShipId != null || !string.IsNullOrWhiteSpace(x.Code));

            GameState gs;

            if (id != null)
            {
                gs = _gsRepo.GetById(id.Value).Result;
            }
            else
            {
                gs = new GameState {
                    Id = Guid.NewGuid()
                }
            };

            gs.Name = vm.GameName;

            foreach (var shipToCreate in shipsToCreate)
            {
                Pawn pawn;
                if (shipToCreate.ShipId == null)
                {
                    var spec = GameRules.GetShipByCode(shipToCreate.Code);
                    pawn             = Pawn.FromSpecification(spec);
                    pawn.GameStateId = gs.Id.Value;
                    gs.Pawns.Add(pawn);
                }
                else
                {
                    pawn = gs.Pawns.First(x => x.Id == shipToCreate.ShipId.Value);
                }

                pawn.Name = shipToCreate.Name;
            }


            _gsRepo.Save(gs);

            return(RedirectToAction("Index", "StrategicView", new { id }));
        }
    }