示例#1
0
        public void StartGame()
        {
            var playingPlayers = GetAllLobbyPlayers().Where(x => x.PlayerPosition != PlayerPosition.Spectator).ToList();

            if (playingPlayers.Count < 2)
            {
                throw new InvalidActionException("Not enough players.");
            }

            _server.OnClientConnected   -= ServerOnOnClientConnected;
            _server.OnClientDisonnected -= ServerOnOnClientDisonnected; // TODO: postarat se o tyto handlery

            foreach (var lobbyPlayer in GetAllLobbyPlayers().Where(x => x.PlayerHandler != null))
            {
                lobbyPlayer.PlayerHandler.GameStarting();
            }

            // Game updaters
            var gameUpdaters = new List <IGameUpdater>();
            var localGame    = new LocalGame(_hostPlayer.PlayerId);

            gameUpdaters.Add(localGame);
            foreach (var lobbyPlayer in GetAllLobbyPlayers().Where(x => x.PlayerHandler != null))
            {
                gameUpdaters.Add(new RemoteGameUpdater(lobbyPlayer.PlayerHandler.Receiver));
            }

            var board        = new BoardFactory().CreateBoard();
            var gameSettings = new GameSettings();

            // Build the game manager
            var gameManager = new GameManager(board, gameSettings, new MultipleGameUpdater(gameUpdaters), GetAllLobbyPlayers());

            gameManager.DiceRolling = new DiceRolling(gameManager);
            gameManager.TurnLogic   = new TurnLogic(gameManager);
            gameManager.Start();

            var hostPlayer = gameManager.Players.Find(x => x.PlayerId == _hostPlayer.PlayerId);

            if (hostPlayer == null)
            {
                throw new FatalGameException("Invalid host player id");
            }

            var hostPlayerController = new LocalPlayerController(hostPlayer);

            localGame.SetPlayerController(hostPlayerController);

            GameStarting?.Invoke(localGame);
        }