Beispiel #1
0
 public GameState()
 {
     SeatedPlayers = new Player[10];
     DiscardPile = new Deck<CardPolicy>(17);
     DrawPile = new Deck<CardPolicy>(17);
     PlayedLiberalCards = new CardPolicyLiberal[5];
     PlayedFascistCards = new CardPolicyFascist[6];
 }
 public void SetVote(Player player, bool yes)
 {
     var playerPos = GetPlayerPos(player.Name);
     if (player.Dead)
         votes[playerPos] = Vote.Dead;
     else if (playerPos != -1)
         votes[playerPos] = yes ? Vote.Ja : Vote.Nein;
 }
 private void PlayerButtonClick(object sender, EventArgs args)
 {
     if(awaitingPlayerSelection != null)
     {
         awaitingPlayerSelection((sender as Button).Tag as Player);
         awaitingPlayerSelection = null;
         return;
     }
     if (selectedButton != null)
         selectedButton.Enabled = true;
     var btn = sender as Button;
     btn.Enabled = false;
     selectedButton = btn;
     selectedPlayer = btn.Tag as Player;
     membershipLabel.Text = selectedPlayer.Hand.Membership.IsFascist ? "Fascist" : "Liberal";
     roleLabel.Text = selectedPlayer.Hand.Role.IsHitler ? "Hitler" : selectedPlayer.Hand.Role.IsFascist ? "Fascist" : "Liberal";
 }
Beispiel #4
0
 public virtual void KillPlayer(Player player)
 {
     player.Dead = true;
 }
Beispiel #5
0
 internal virtual void SetPreviousChancellor(Player player)
 {
     PreviousChancellor = player;
 }
Beispiel #6
0
 public bool MayElectPlayer(Player player)
 {
     if (player == PreviousChancellor) return false;
     if (player == PreviousPresident && PlayerCount > 5) return false;
     if (player == President) return false;
     if (player.Dead) return false;
     return true;
 }
 internal override void SetPreviousPresident(Player player)
 {
     base.SetPreviousPresident(player);
     SetPlacardValue(PlayArea.PlacardPrevPresident, PreviousPresident, false);
 }
Beispiel #8
0
 private void ServerSent(Player player, NetworkObject obj)
 {
     AddMessage("Server", player.Name, obj);
 }
 private void SeatPlayer(Player player, int pos)
 {
     SeatedPlayers[pos] = player;
     player.PlayArea = PlayAreas[pos];
 }
 internal override void SetPresident(Player player)
 {
     base.SetPresident(player);
     SetPlacardValue(PlayArea.PlacardPresident, President, true);
 }
Beispiel #11
0
 private PlayArea FindPlayArea(Player player)
 {
     int index = state.GetPlayerPos(player.Name);
     return index == -1 ? null : state.PlayAreas[index];
 }
Beispiel #12
0
 internal virtual void SetPresident(Player player)
 {
     if (PreviousGovernmentElected)
         SetPreviousPresident(President);
     President = player;
 }
        public Player GetNextPresident()
        {
            var i = 0;
            bool nextIsPresident = false;
            if(nextPresident != null)
                if (!nextPresident.Dead)
                {
                    var @return = nextPresident;
                    nextPresident = null;
                    return @return;
                }
                else
                {
                    var playerSeat = Array.IndexOf(SeatedPlayers, nextPresident);
                    i = (Array.IndexOf(presidentOrder, playerSeat) + 1) % presidentOrder.Length;
                    nextPresident = null;
                    nextIsPresident = true;
                }
            while(true)
            {
                if(SeatedPlayers[presidentOrder[i]] != null && !SeatedPlayers[presidentOrder[i]].Dead)
                {
                    if (nextIsPresident)
                        return SeatedPlayers[presidentOrder[i]];

                    if (SeatedPlayers[presidentOrder[i]] == President)
                        nextIsPresident = true;
                }
                i = (i + 1) % presidentOrder.Length;
            }
        }
 public void SetNextPresident(Player player)
 {
     nextPresident = player;
 }
 public int SeatPlayer(Player player)
 {
     for (var i = 0; i < SeatedPlayers.Length; i++)
         if (SeatedPlayers[i] == null)
         {
             SeatedPlayers[i] = player;
             return i;
         }
     return -1;
 }
Beispiel #16
0
 public TextObject(string str, Player player)
 {
     Player = player;
     Text = str;
     DetonateTimer = new TimeSpan(0, 0, 0, 2, 500);
 }
Beispiel #17
0
 internal void CreatePlayerBubble(Player player, string message)
 {
     if (!TextObjects.ContainsKey(player))
         TextObjects.Add(player, new PlayerArgs(FindPlayArea(player)));
     var args = TextObjects[player];
     if(args.Object != null)
         args.QueuedMessages.Push(message);
     else
     {
         var textObj = new TextObject(message, player);
         if (!args.DrawLocation.IsEmpty)
             textObj.Location = TextObjects[player].DrawLocation;
         textObj.OnDetonate += TextObj_OnDetonate;
         lock (state.GameObjects)
             state.GameObjects.AddLast(textObj);
         args.Object = textObj;
         MessageHistory.Instance.AddHistory($"[{player.Name}]: {message}");
     }
 }
Beispiel #18
0
 internal virtual void SetChancellor(Player player)
 {
     if (PreviousGovernmentElected)
         SetPreviousChancellor(Chancellor);
     Chancellor = player;
 }
 internal void SetPresident(ServerResponse response, Player president)
 {
     gameState.SetPresident(president);
     gameState.SetChancellor(null);
     response.AddObject(new NetworkPlayerObject(ServerCommands.AnnouncePresident, president));
 }
Beispiel #20
0
 internal virtual void SetPreviousPresident(Player player)
 {
     PreviousPresident = player;
 }
        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);
        }
 internal override void SetChancellor(Player player)
 {
     base.SetChancellor(player);
     SetPlacardValue(PlayArea.PlacardChancellor, Chancellor, true);
 }
 public void AddObject(NetworkObject obj, Player player = null, bool deadPlayers = true)
 {
     if (responsePrepared) throw new InvalidOperationException("Can't add objects when the object has been sent");
     if(player != null)
     {
         if (objects == null)
             objects = new Dictionary<Player, NetworkMultipleObject>();
         if (!objects.ContainsKey(player))
             objects.Add(player, obj is NetworkMultipleObject ? (obj as NetworkMultipleObject) : new NetworkMultipleObject(obj));
         else
             objects[player].AddObject(obj);
     }
     else if (deadPlayers)
     {
         if (all == null)
             all = obj is NetworkMultipleObject ? (obj  as NetworkMultipleObject) : new NetworkMultipleObject(obj);
         else
             all.AddObject(obj);
     }
     else
     {
         if (noDead == null)
             noDead = obj is NetworkMultipleObject ? (obj as NetworkMultipleObject) : new NetworkMultipleObject(obj);
         else
             all.AddObject(obj);
     }
 }
 internal override void SetPreviousChancellor(Player player)
 {
     base.SetPreviousChancellor(player);
     SetPlacardValue(PlayArea.PlacardPrevChancellor, PreviousChancellor, false);
 }
 public NetworkMultipleObject GetResponse(Player player)
 {
     if (!responsePrepared)
         PrepareResponses();
     if (objects != null && objects.ContainsKey(player))
         return objects[player];
     return player.Dead ? all : noDead;
 }
 private void SetPlacardValue(Placard placard, Player value, bool current)
 {
     if (placard.CurrentArea != null)
     {
         if (current && placard.CurrentArea.Current == placard)
             placard.CurrentArea.Current = null;
         else if (!current && placard.CurrentArea.Previous == placard)
             placard.CurrentArea.Previous = null;
     }
     if (value?.PlayArea != null)
     {
         placard.CurrentArea = value.PlayArea;
         if (current)
             placard.CurrentArea.Current = placard;
         else
             placard.CurrentArea.Previous = placard;
     }
 }
Beispiel #27
0
 private void ServerReceive(Player player, NetworkObject obj)
 {
     AddMessage(player.Name, "Server", obj);
 }