Exemplo n.º 1
0
        /// <inheritdoc/>
        public void ExpelPlayer(ExpelVoteDto expelVote)
        {
            string       host                  = expelVote.Host;
            ServiceMatch gameMatch             = GetMatch(host);
            string       usernameOfExpelPlayer = expelVote.UsernameOfExpelPlayer;

            int           playerExpelVotes = gameMatch.AddExpelVote(usernameOfExpelPlayer);
            ServicePlayer voterPlayer      = gameMatch.GetPlayer(expelVote.UsernameOfVoterPlayer);

            voterPlayer.AddPlayerVoted(usernameOfExpelPlayer);

            IList <ServicePlayer> playersInMatch = gameMatch.GetPlayersConnectedToMatch();
            int numOfPlayers = playersInMatch.Count;

            if (playerExpelVotes > (numOfPlayers / 2))
            {
                ServicePlayer playerWithActiveTurn = gameMatch.GetPlyerWithActiveTurn();
                ServicePlayer expelPlayer          = gameMatch.GetPlayer(usernameOfExpelPlayer);

                if (playerWithActiveTurn.Username.Equals(usernameOfExpelPlayer))
                {
                    expelPlayer = playerWithActiveTurn;
                    int indexOfPlayerWithCurrentTurn = gameMatch.GetPlayersConnectedToMatch().IndexOf(playerWithActiveTurn);
                    indexOfPlayerWithCurrentTurn = ChangeTurn(gameMatch, indexOfPlayerWithCurrentTurn);

                    ServicePlayer nextPlayer = gameMatch.GetPlayersConnectedToMatch()[indexOfPlayerWithCurrentTurn];
                    playerWithActiveTurn.HasActiveTurn = false;
                    nextPlayer.HasActiveTurn           = true;

                    foreach (var playerInMatch in playersInMatch)
                    {
                        playerInMatch.MatchServiceConnection.EndTurnOfExpelPlayer(nextPlayer.Username);
                    }
                }

                IList <int> cardsUncovered = expelPlayer.GetUncoveredCards();
                foreach (var playerConnected in playersInMatch)
                {
                    var channel = playerConnected.MatchServiceConnection;
                    channel.NotifyPlayerWasExpel(usernameOfExpelPlayer, cardsUncovered);
                }

                RemovePairs(gameMatch, cardsUncovered);
                gameMatch.RemovePlayer(usernameOfExpelPlayer);

                if (playersInMatch.Count == 1)
                {
                    this.NotifyMatchHasEnded(host);
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public void LeaveMatch(string host, string username)
        {
            ServiceMatch gameMatch = GetMatch(host);

            IList <ServicePlayer> playersInMatch       = gameMatch.GetPlayersConnectedToMatch();
            ServicePlayer         playerWithActiveTurn = gameMatch.GetPlyerWithActiveTurn();
            ServicePlayer         leavePlayer          = gameMatch.GetPlayer(username);

            if (playerWithActiveTurn.Username.Equals(username))
            {
                leavePlayer = playerWithActiveTurn;
                int indexOfPlayerWithCurrentTurn = gameMatch.GetPlayersConnectedToMatch().IndexOf(playerWithActiveTurn);
                indexOfPlayerWithCurrentTurn = ChangeTurn(gameMatch, indexOfPlayerWithCurrentTurn);

                ServicePlayer nextPlayer = gameMatch.GetPlayersConnectedToMatch()[indexOfPlayerWithCurrentTurn];
                playerWithActiveTurn.HasActiveTurn = false;
                nextPlayer.HasActiveTurn           = true;

                foreach (var playerInMatch in playersInMatch)
                {
                    playerInMatch.MatchServiceConnection.EndTurnOfExpelPlayer(nextPlayer.Username);
                }
            }

            IList <int> cardsUncovered = leavePlayer.GetUncoveredCards();

            foreach (var playerConnected in playersInMatch)
            {
                var channel = playerConnected.MatchServiceConnection;
                channel.NotifyPlayerLeaveMatch(username, cardsUncovered);
            }

            RemovePairs(gameMatch, cardsUncovered);
            gameMatch.RemovePlayer(username);

            if (playersInMatch.Count == 1)
            {
                this.NotifyMatchHasEnded(host);
            }
        }