Example #1
0
        public void ChangeTurn()
        {
            if (currentTurn == "")
            {
                currentTurn = player1Side.ToString();
            }
            else
            {
                currentTurn = (CurrentTurn == player1Side.ToString()) ? player2Side.ToString() : player1Side.ToString();
            }

            if (gameMode == GameMode.TwoPlayer)
            {
                if (EmptyTiles != null && EmptyTiles.Count == 0)
                {
                    EndGame();
                }
                else
                {
                    string feedback = (CurrentTurn == player1Side.ToString()) ? "Player 1 turn" : "Player 2 turn";
                    uiManager.Feedback(feedback);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Asks host if player can change sides. Should only be called during Lobby stage.
        /// </summary>
        /// <param name="NewSide"></param>
        public void AttemptSideChange(PlayerSide NewSide)
        {
            Debug.Assert(Stage == GameStage.Lobby, "Wrong Game stage to change side!");

            int SideSize = 0;

            if (NewSide != PlayerSide.Spectator)
            {
                foreach (KeyValuePair <string, Player> Player in Players)
                {
                    if (Player.Value.Side == NewSide)
                    {
                        SideSize++;
                    }
                }
                if (SideSize > 4)
                {
                    System.Windows.Forms.MessageBox.Show(NewSide.ToString() + " is full!");
                    return;
                }
            }

            //Players[MyName].Side = NewSide;

            /*if (UpdateUI != null)
             * {
             *  UpdateUI(this, new EventArgs());
             * }*/

            if (SendPlayerNotice != null)
            {
                SendPlayerNotice(this, new SendMessageEventArgs(HostName, GAME_SIDE_REQUEST + NewSide.ToString()));
            }
            if (GameEvent != null)
            {
                GameEvent(this, new GameEventArgs("Requesting Side Change...", false));
            }
        }
Example #3
0
        public virtual void ReceivePlayerNotice(string Nickname, string Message)
        {
            if (isHost)
            {
                if (Message.StartsWith(GAME_JOIN_REQUEST)) // A player wants to join the lobby
                {
                    if (Stage != GameStage.Lobby)          // Too late
                    {
                        SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_JOIN_REFUSE_STARTED));
                    }
                    else if (Players.Count >= MaxLobbySize) // Full
                    {
                        SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_JOIN_REFUSE_FULL));
                    }
                    else if (Blacklist.Contains(Nickname)) // KickBanned
                    {
                        SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_JOIN_REFUSE_KICKED));
                    }
                    else if (Bans != null && HostName.StartsWith("[") && (Bans.Contains(Nickname)))
                    {
                        SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_JOIN_REFUSE_BANNED));
                    }
                    else // Can join :D
                    {
                        Whitelist.Add(Nickname);

                        // Append Sync list with player in it

                        StringBuilder OutMsg = new StringBuilder();
                        bool          First  = true;
                        foreach (KeyValuePair <string, Player> Player in Players)
                        {
                            if (First)
                            {
                                First = false;
                            }
                            else
                            {
                                OutMsg.Append("&");
                            }
                            OutMsg.Append(Player.Value.Name);
                            OutMsg.Append(",");
                            OutMsg.Append(((int)(Player.Value.Side)).ToString());
                            OutMsg.Append(",");
                            OutMsg.Append(Player.Value.Ready ? "1" : "0");
                        }
                        // Stick them in it so they don't crap out after connecting
                        OutMsg.Append("&");
                        OutMsg.Append(Nickname);
                        OutMsg.Append(",");
                        OutMsg.Append(PlayerSide.Spectator);
                        OutMsg.Append(",");
                        OutMsg.Append("0");

                        if (SendPlayerNotice != null)
                        {
                            SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_JOIN_ACCEPTED + OutMsg.ToString())); // Invite new player to lobby
                        }


                        //SendChannelNotice(this, new SendMessageEventArgs(Channel, GAME_PLAYER_NEW + Nickname)); // Lets actually do this when they join the channel
                    }
                }
                else if (Message.StartsWith(GAME_SIDE_REQUEST)) // A player wants to change sides
                {
                    Debug.Assert(Enum.IsDefined(typeof(PlayerSide), Message.Substring(GAME_SIDE_REQUEST.Length)), "Player attempting to change to invalid side: " + Message + "\nYou may safely ignore this message, but please report that it happened.");
                    PlayerSide DesiredSide = (PlayerSide)Enum.Parse(typeof(PlayerSide), Message.Substring(GAME_SIDE_REQUEST.Length));

                    if (DesiredSide != PlayerSide.Spectator)
                    {
                        int SideSize = 0;

                        foreach (KeyValuePair <string, Player> Player in Players)
                        {
                            if (Player.Key != Nickname && Player.Value.Side == DesiredSide)
                            {
                                SideSize++;
                            }
                        }

                        if (SideSize > 4 && SendPlayerNotice != null)                                                                                      // Side is full
                        {
                            SendPlayerNotice(this, new SendMessageEventArgs(Nickname, GAME_SIDE_REFUSED_FULL + ((int)Players[Nickname].Side).ToString())); // Tell player to piss off and stay where they are
                            return;
                        }
                    }
                    Players[Nickname].Side = DesiredSide;
                    if (SendChannelNotice != null)
                    {
                        SendChannelNotice(this, new SendMessageEventArgs(Channel, GAME_SIDE_ACCEPTED + Nickname + "&" + DesiredSide.ToString())); // Change player side
                    }
                    if (UpdateUI != null)
                    {
                        UpdateUI(this, new EventArgs());
                    }
                    if (DisplayUserMessage != null)
                    {
                        DisplayUserMessage(this, new SendMessageEventArgs("", Nickname + " changed side to " + DesiredSide.ToString()));
                    }
                    if (HostName == Nickname && GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Changed Side!", true));
                    }
                }
                else if (Message.StartsWith(GAME_SYNC_PLAYERLIST_REQUEST))
                {
                    SyncPlayerList();
                }
            }
            else if (Nickname == HostName)                  // Ensure its coming from the appropriate host
            {
                if (Message.StartsWith(GAME_JOIN_ACCEPTED)) // Ensure the GAME_JOIN_ACCEPTED is from the host for this game instance
                {
                    EventfulDictionary <string, Player> newPlayerList = new EventfulDictionary <string, Player>();
                    string[] PlayersArray = Message.Substring(GAME_JOIN_ACCEPTED.Length).Split('&');
                    foreach (string PlayerData in PlayersArray)
                    {
                        string[] PlayerSettings = PlayerData.Split(',');
                        Player   Dude;
                        if (Players.ContainsKey(PlayerSettings[0]))
                        {
                            Dude = Players[PlayerSettings[0]];
                            newPlayerList.Add(PlayerSettings[0], Dude, false);
                        }
                        else
                        {
                            Dude      = new Player();
                            Dude.Name = PlayerSettings[0];
                            newPlayerList.Add(PlayerSettings[0], Dude, false);
                        }
                        //Debug.Assert(Enum.IsDefined(typeof(PlayerSide), PlayerSettings[1]), "Invalid GAME_SYNC_PLAYERLIST Message from Host: " + Message);
                        Dude.Side  = (PlayerSide)Enum.Parse(typeof(PlayerSide), PlayerSettings[1]);
                        Dude.Ready = (PlayerSettings[2] != "0" ? true : false);
                    }

                    Players = newPlayerList;

                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Join success!", true));
                        GameEvent(this, new GameEventArgs(GAME_JOIN_ACCEPTED, true));
                    }
                    if (UpdateUI != null)
                    {
                        UpdateUI(this, new EventArgs());
                    }
                    SyncPlayerList();
                }
                else if (Message.StartsWith(GAME_JOIN_REFUSE_FULL))
                {
                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Join failed - Lobby full", true));
                        GameEvent(this, new GameEventArgs(GAME_JOIN_REFUSE_FULL, true));
                    }
                }
                else if (Message.StartsWith(GAME_JOIN_REFUSE_STARTED))
                {
                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Join failed - Started", true));
                        GameEvent(this, new GameEventArgs(GAME_JOIN_REFUSE_STARTED, true));
                    }
                }
                else if (Message.StartsWith(GAME_JOIN_REFUSE_KICKED))
                {
                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Join failed - Kicked", true));
                        GameEvent(this, new GameEventArgs(GAME_JOIN_REFUSE_KICKED, true));
                    }
                }
                else if (Message.StartsWith(GAME_JOIN_REFUSE_BANNED))
                {
                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Join failed - Banned", true));
                        GameEvent(this, new GameEventArgs(GAME_JOIN_REFUSE_BANNED, true));
                    }
                }
                else if (Message.StartsWith(GAME_SIDE_REFUSED_FULL))
                {
                    Players[MyName].Side = (PlayerSide)Enum.Parse(typeof(PlayerSide), Message.Substring(GAME_SIDE_REFUSED_FULL.Length));
                    if (GameEvent != null)
                    {
                        GameEvent(this, new GameEventArgs("Can't Change Side", true));
                        GameEvent(this, new GameEventArgs(GAME_SIDE_REFUSED_FULL, true));
                    }
                    if (UpdateUI != null)
                    {
                        UpdateUI(this, new EventArgs());
                    }
                }
                else if (Message.StartsWith(GAME_CUSTOMMOD_CHECK))
                {
                    CustomMod = Message.Substring(GAME_CUSTOMMOD_CHECK.Length);
                    Players[MyName].HasMod = Directory.Exists("data\\custom\\" + CustomMod) && Directory.GetFiles("data\\custom\\" + CustomMod).Count() >= 2;
                    RaiseSendChannelNotice(this, new SendMessageEventArgs(Channel, Players[MyName].HasMod ? GAME_CUSTOMMOD_YES : GAME_CUSTOMMOD_NO));
                    RaiseUpdateUI(this, new EventArgs());
                }
            }
        }