Beispiel #1
0
        /// <summary>
        /// [Call only when all players are ready] Starts the game round
        /// </summary>
        public void StartGame()
        {
            if (_clientsOfThisGame.Count < 4)
            {
                throw new NotEnoughPlayerInGameException();
            }
            else
            {
                roundsPlayed = 0;

                //TODO: Verhindern, dass ein Spiel mehrfach gestartet werden kann.
                while (_clientsOfThisGame[0].TcpClient.Connected)
                {
                    Console.WriteLine("Du bist dran " + _currentClient.Player.PlayerName);
                    Console.WriteLine(_currentClient.Player.PlayerName + " has " + _currentClient.Player.PlayerRemainingMoves + " moves");

                    if (_currentClient.Player.Life > 0)
                    {
                        do
                        {
                            Console.WriteLine("Moves left: " + _currentClient.Player.PlayerRemainingMoves);
                            Console.WriteLine("Position: " + _currentClient.Player.XPosition + ", " + _currentClient.Player.YPosition);
                            CommandGame command = ReceiveCommandFromClient(_currentClient.TcpClient);
                            // command.SourcePlayer = _players.GetValueOrDefault(command.SourceClientID);
                            command.SourcePlayer = _currentClient.Player;
                            command.Level        = _level;

                            try
                            {
                                command.Execute();

                                if (_currentClient.Player.PlayerRemainingMoves > 0)
                                {
                                    SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackOK(_currentClient.Client_ID));
                                }
                                else
                                {
                                    SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackEndOfTurn(_currentClient.Client_ID));
                                }
                            }
                            catch (System.Exception e)
                            {
                                SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackGameException(_currentClient.Client_ID, e));
                            }
                        } while (_currentClient.Player.PlayerRemainingMoves > 0);
                    }

                    NextPlayer();
                }

                throw new NotImplementedException();
            }
            // gcm.AddCommand(ReceiveCommandFromClient(_currentClient));
            // gcm.ProcessPendingTransactions();
        }
        /// <summary>
        /// [Call only when all players are ready] Starts the game round
        /// </summary>
        public void RunGame()
        {
            if (_level.PlayerList.Count == MAX_CLIENTS)
            {
                while (GetTcpClientState(_clientsOfThisGame[0].TcpClient))
                {
                    Console.WriteLine("Du bist dran " + _currentClient.Player.PlayerName);
                    Console.WriteLine(_currentClient.Player.PlayerName + " has " + _currentClient.Player.PlayerRemainingMoves + " moves");

                    if (_currentClient.Player.Life > 0)
                    {
                        while (_currentClient.Player.PlayerRemainingMoves > 0 && GetTcpClientState(_currentClient.TcpClient) && _currentClient.Player.Life > 0)
                        {
                            try
                            {
                                Console.WriteLine("Moves left: " + _currentClient.Player.PlayerRemainingMoves);
                                Console.WriteLine("Position: " + _currentClient.Player.XPosition + ", " + _currentClient.Player.YPosition);
                                CommandGame command = ReceiveCommandFromClient(_currentClient.TcpClient);

                                if (command != null)
                                {
                                    command.SourcePlayer = _currentClient.Player;

                                    if (command is CommandGameAttack)
                                    {
                                        foreach (var item in _clientsOfThisGame)
                                        {
                                            if (item.Client_ID == ((CommandGameAttack)command).TargetClientID)
                                            {
                                                ((CommandGameAttack)command).TargetPlayer = item.Player;
                                            }
                                        }
                                    }

                                    command.Level = _level;

                                    command.Execute();

                                    if (_currentClient.Player.PlayerRemainingMoves > 0 && _currentClient.Player.Life > 0 && command.IsCompleted)
                                    {
                                        SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackOK(_currentClient.Client_ID));
                                        UpdateClients();
                                    }
                                    else if (command.IsCompleted)
                                    {
                                        SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackEndOfTurn(_currentClient.Client_ID));
                                    }
                                }
                            }
                            catch (System.Exception ex)
                            {
                                if (ex is System.InvalidOperationException)
                                {
                                    foreach (var item in _clientsOfThisGame)
                                    {
                                        item.TcpClient.Close();
                                        item.IsInGame = false;
                                    }
                                }
                                else
                                {
                                    SendFeedbackToClient(_currentClient.TcpClient, new CommandFeedbackGameException(_currentClient.Client_ID, ex));
                                }
                            }
                        }
                    }

                    NextPlayer();
                    Console.WriteLine("####################\n Round: " + gameRound + "\n####################");
                }
            }
            else if (GetTcpClientState(_clientsOfThisGame[0].TcpClient) == false)
            {

            }
            else
            {
                throw new NotEnoughPlayerInGameException();
            }
        }