public bool JoinGame(Guid gameId)
        {
            var game = _db.Games.Include(x => x.UserGames).SingleOrDefault(x => x.GameId == gameId);



            if (_activeUser == "")
            {
                return(false);
            }

            UserGame ug = new UserGame {
                GameId      = game.GameId,
                UserId      = _activeUser,
                User        = GetUser(_activeUser),
                PlayerState = Models.Enums.PlayerState.ShipPlacing
            };

            if (game.UserGames != null)
            {
                if (game.UserGames.Count() < game.MaxPlayers)
                {
                    _session.Save("GameId", gameId);

                    game.UserGames.Add(ug);
                    _session.Save("GameId", game.GameId);
                    _db.UserGames.Add(ug);
                    _db.SaveChanges();
                    CreateBattleField(ug);
                    _db.SaveChanges();
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        public void StartGame(int gameSize)
        {
            if (_activeUser == "")
            {
                return;
            }

            Game tempGame = new Game {
                GameId          = Guid.NewGuid(),
                MaxPlayers      = 2,
                GameSize        = gameSize,
                GameState       = Models.Enums.GameState.Setup,
                OwnerId         = _activeUser,
                CurrentPlayerId = _activeUser,
            };

            UserGame ug = new UserGame {
                GameId      = tempGame.GameId,
                UserId      = _activeUser,
                User        = _gameLogicService.GetUser(_activeUser),
                PlayerState = Models.Enums.PlayerState.ShipPlacing
            };

            tempGame.UserGames = new List <UserGame>();
            tempGame.UserGames.Add(ug);

            _session.Save("GameId", tempGame.GameId);

            _db.Games.Add(tempGame);
            _db.UserGames.Add(ug);
            _db.SaveChanges();
            _gameLogicService.CreateBattleField(ug);
            _db.SaveChanges();
        }