Esempio n. 1
0
 public override void PlayPolicy(CardPolicy policy)
 {
     base.PlayPolicy(policy);
     if (policy is CardPolicyFascist)
         panel.FascistBoard.AddCard(policy as CardPolicyFascist);
     else if (policy is CardPolicyLiberal)
         panel.LiberalBoard.AddCard(policy as CardPolicyLiberal);
 }
Esempio n. 2
0
 public virtual void PlayPolicy(CardPolicy policy)
 {
     if (policy is CardPolicyFascist)
         PlayedFascistCards[FascistsCardsPlayed++] = policy as CardPolicyFascist;
     else if (policy is CardPolicyLiberal)
         PlayedLiberalCards[LiberalCardsPlayed++] = policy as CardPolicyLiberal;
 }
Esempio n. 3
0
 internal CardPolicy[] GetPolicyCards(int amount = 3)
 {
     if (DrawPile.CardsRemaining < amount)
     {
         while (DiscardPile.CardsRemaining > 0)
             DrawPile.AddCard(DiscardPile.GetCard());
         DrawPile.Shuffle();
     }
     var policies = new CardPolicy[amount];
     for (var i = 0; i < amount; i++)
         policies[i] = DrawPile.GetCard();
     return policies;
 }
        private void PlayPolicy(CardPolicy policy, ServerResponse response)
        {
            gameState.PlayPolicy(policy);
            response.AddObject(new NetworkCardObject(ServerCommands.ChancellorDiscarded));
            response.AddObject(new NetworkCardObject(ServerCommands.CardPlayed, policy));
            if(policy is CardPolicyLiberal)
            {
                if (!CheckCardPlayedWinCondition(response))
                    GetNextPresident(response);
            }
            else
            {
                if (!CheckCardPlayedWinCondition(response))
                {
                    if (gameState.FascistActions[gameState.FascistsCardsPlayed - 1] != null)
                    {
                        var fascistId = gameState.FascistsCardsPlayed - 1;
                        var fascistAction = gameState.FascistActions[fascistId];
                        response.AddObject(new NetworkByteObject(ServerCommands.PresidentAction, (byte)fascistId));
                        response.AddObject(fascistAction.GetPresidentObject(gameState), gameState.President);
                        gameState.AwaitingPresidentAction = fascistAction.ServerResponse;
                    }
                    else
                        GetNextPresident(response);
                }
            }

        }
        internal void HandleMessage(NetworkObject request, TcpClient sentBy, Player player)
        {
            var response = new ServerResponse();
            response.AddObject(new NetworkObject(ServerCommands.OK, ID: request.ID), player);
            switch (request.Command)
            {
                case ServerCommands.Message:
                    var msgResponse = request as NetworkMessageObject;
                    response.AddObject(new NetworkMessageObject(msgResponse.Username, msgResponse.Message, sendToServer: false));
                    break;
                case ServerCommands.GetGameState:
                    response.AddObject(new NetworkGameStateObject(ServerCommands.SendGameState, gameState), player);
                    break;
                case ServerCommands.AnnounceChancellor:
                    var playerResponse = request as NetworkPlayerObject;
                    if (gameState.MayElectPlayer(playerResponse.Player))
                    {
                        gameState.ResetVotes();
                        gameState.AreVoting = true;
                        gameState.SetChancellor(playerResponse.Player);
                        response.AddObject(playerResponse);
                    }
                    break;
                case ServerCommands.CastVote:
                    if (!gameState.AreVoting) break;
                    var boolObj = request as NetworkBoolObject;
                    gameState.SetVote(player, boolObj.Value);
                    var playerVoted = new NetworkPlayerObject(ServerCommands.PlayerVoted, player);
                    response.AddObject(playerVoted);
                    //multObj.AddObject(playerVoted);
                    if (gameState.EveryoneVoted())
                    {
                        gameState.AreVoting = false;
                        //everyone voted
                        AnnounceVotes(response);
                    }
                    break;
                case ServerCommands.PresidentPolicyCardPicked:
                    const int vetoPowerUnlocked  = 5;
                    if (player != gameState.President || !gameState.PresidentPicking) break;
                    gameState.PresidentPicking = false;
                    var pickPolicyCard = request as NetworkByteObject;
                    gameState.DiscardPile.AddCard(gameState.CurrentlyPicked[pickPolicyCard.Value]);
                    response.AddObject(new NetworkObject(ServerCommands.PresidentDiscarded));
                    var chancellorCards = new CardPolicy[gameState.FascistsCardsPlayed >= vetoPowerUnlocked ? 3 : 2];
                    var j = 0;
                    for (var i = 0; i < 3; i++)
                        if (i != pickPolicyCard.Value)
                            chancellorCards[j++] = gameState.CurrentlyPicked[i];
                    if (gameState.FascistsCardsPlayed == vetoPowerUnlocked)
                        chancellorCards[2] = new CardPolicyVeto();
                    gameState.CurrentlyPicked = chancellorCards;
                    response.AddObject(new NetworkCardObject(ServerCommands.ChancellorPickPolicyCard, chancellorCards), gameState.Chancellor);
                    gameState.ChancellorPicking = true;
                    break;
                case ServerCommands.ChancellorPolicyCardPicked:
                    if (player != gameState.Chancellor || !gameState.ChancellorPicking) break;
                    gameState.ChancellorPicking = false;
                    var pickCard = request as NetworkByteObject;
                    gameState.DiscardPile.AddCard(gameState.CurrentlyPicked[pickCard.Value]);
                    var playCard = gameState.CurrentlyPicked[Math.Abs(pickCard.Value - 1)];
                    gameState.CurrentlyPicked = null;
                    PlayPolicy(playCard, response);
                    break;
                case ServerCommands.PresidentActionExamineResponse:
                    if (player != gameState.President || gameState.AwaitingPresidentAction != request.Command) break;
                    GetNextPresident(response);
                    break;
                case ServerCommands.PresidentActionKillResponse:
                    if (player != gameState.President || gameState.AwaitingPresidentAction != request.Command) break;
                    var killPlayer = request as NetworkPlayerObject;
                    response.AddObject(new NetworkPlayerObject(ServerCommands.KillPlayer, killPlayer.Player));
                    gameState.KillPlayer(killPlayer.Player);
                    if (killPlayer.Player.Hand.Role.IsHitler)
                    {
                        AnnounceWin(response, false, "Hitler was killed");
                        break;
                    }
                    else
                        response.AddObject(new NetworkPlayerObject(ServerCommands.NotHitler, killPlayer.Player));
                    GetNextPresident(response);
                    break;

                case ServerCommands.PresidentActionChoosePresidentResponse:
                    if (player != gameState.President || gameState.AwaitingPresidentAction != request.Command) break;
                    var presObj = request as NetworkPlayerObject;
                    if (presObj.Player == gameState.President) break;
                    var presidentAfterChoice = gameState.GetNextPresident();
                    SetPresident(response, presObj.Player);
                    gameState.SetNextPresident(presidentAfterChoice);
                    break;
                case ServerCommands.PresidentActionInvestigatePresidentResponse:
                    if (player != gameState.President || gameState.AwaitingPresidentAction != request.Command) break;
                    var investigate = request as NetworkPlayerObject;
                    if (investigate.Player == player) break;
                    response.AddObject(new NetworkNewPlayerObject(ServerCommands.RevealMembership, investigate.Player, investigate.Player.Hand.Membership.IsFascist ? 1 : 0), player);
                    GetNextPresident(response);
                    break;
                case ServerCommands.ChancellorRequestVeto:
                    if (player != gameState.Chancellor) break;
                    response.AddObject(new NetworkObject(ServerCommands.PresidentConfirmVeto));
                    break;
                case ServerCommands.PresidentRequestVetoAllowed:
                    if (player != gameState.President) break;
                    var vetoAllowed = (request as NetworkBoolObject).Value;
                    response.AddObject(new NetworkBoolObject(ServerCommands.AnnounceVetoResult, vetoAllowed));
                    if (vetoAllowed)
                    {
                        var currentlyPicked = gameState.CurrentlyPicked;

                        //discard all the cards
                        gameState.CurrentlyPicked = null;
                        for (var i = 0; i < currentlyPicked.Length - 1; i++) //the last card is the veto card
                        {
                            gameState.DiscardPile.AddCard(currentlyPicked[i]);
                            response.AddObject(new NetworkCardObject(ServerCommands.ChancellorDiscarded));
                        }
                        //Increment the election tracker, veto passed
                        IncrementElectionTracker(response);
                        //The next president is elected because veto ends the current government
                        GetNextPresident(response);
                    }
                    else
                    {
                        var newArr = new CardPolicy[gameState.CurrentlyPicked.Length - 1];
                        for (var i = 0; i < gameState.CurrentlyPicked.Length - 1; i++)
                            newArr[i] = gameState.CurrentlyPicked[i];
                        gameState.CurrentlyPicked = newArr;
                    }
                    break;
            }
            server.SendResponse(response);
            OnReceive?.Invoke(player, request);
        }
Esempio n. 6
0
 internal void ReturnPolicyCards(CardPolicy[] cards)
 {
     for (var i = cards.Length - 1; i >= 0; i--)
         DrawPile.AddCard(cards[i]);
 }