/**
         * Join a game room by player's name (username)
         * @param string Username
         * @return a valid (non-null) player object
         */
        public NetworkBackgammonPlayer Enter(string _playerName, string pw)
        {
            NetworkBackgammonPlayer newPlayer = new NetworkBackgammonPlayer(_playerName);

            // Validate player
            bool goodPlayer = gamePlayerList.VerifyLogin(_playerName, pw);

            // Check the user is already in the list
            if (!connectedPlayers.Contains(newPlayer) && goodPlayer)
            {
                // Add new player object to the game room list
                connectedPlayers.Add(newPlayer);

                // Start listening to this player
                newPlayer.AddListener(this);

                // Have the player start listening to this game room
                AddListener(newPlayer);

                // Broadcast the player connected event to all registered listeners
                Broadcast(new NetworkBackgammonGameRoomEvent(NetworkBackgammonGameRoomEvent.GameRoomEventType.PlayerConnected));
            }
            else
            {
                newPlayer = null;
            }

            return(newPlayer);
        }
Exemplo n.º 2
0
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (player == null)
            {
                if (textBoxPlayerName.Text.Trim() != "")
                {
                    if (gameRoom != null)
                    {
                        string playerName = textBoxPlayerName.Text.Trim();

                        // Register (if not already)
                        gameRoom.RegisterPlayer(playerName, "password");

                        // Enter the game room
                        player = gameRoom.Enter(playerName, "password");

                        if (player != null)
                        {
                            buttonConnect.Text               = "Disconnect";
                            textBoxPlayerName.Enabled        = false;
                            groupBoxGameRoomControls.Enabled = true;

                            player.AddListener(this);

                            UpdateConnectedPlayersList();
                        }
                        else
                        {
                            MessageBox.Show("Login failed!", "Login Error (username)!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Login failed (no game room connection)!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
                else
                {
                    MessageBox.Show("Login failed (no username specified)!", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                if (gameRoom != null)
                {
                    gameRoom.Leave(player);

                    gameRoom.RemoveListener(this);
                    player.RemoveListener(this);

                    player = null;
                }

                buttonConnect.Text               = "Connect";
                textBoxPlayerName.Enabled        = true;
                groupBoxGameRoomControls.Enabled = false;
            }
        }
Exemplo n.º 3
0
        public bool OnConnect(string playerName, string playerPassword, ref string returnMessage)
        {
            bool bRetVal = false;

            if (player == null)
            {
                if (gameRoom != null)
                {
                    // Register (if not already; ignore 'false' return value if already registered)
                    gameRoom.RegisterPlayer(playerName, playerPassword);

                    // Enter the game room
                    player = gameRoom.Enter(playerName, playerPassword);

                    if (player != null)
                    {
                        player.AddListener(this);

                        bRetVal = true;
                    }
                    else
                    {
                        if (returnMessage != null)
                        {
                            returnMessage += "\nPlayer couldn't login with name '" + playerName + "' and password '" + playerPassword + "'.";
                        }
                    }
                }
                else
                {
                    if (returnMessage != null)
                    {
                        returnMessage += "\nNo game room available to connect to!";
                    }
                }
            }
            else
            {
                if (returnMessage != null)
                {
                    returnMessage += "\nPlayer with name '" + playerName + "' is already connected!";
                }

                bRetVal = false;
            }

            return(bRetVal);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Starts the state machine
        /// </summary>
        public void Start()
        {
            // Game Session is listening for events from Player 1
            player1.AddListener(this);
            // Player 1 is listening for events from Game Session
            AddListener(player1);
            // Game Session is listening for events form Player 2
            player2.AddListener(this);
            // Player 2 is listening for events from Game Session
            AddListener(player2);

            bStateMachineKeepRunning = true;

            threadStateMachine = new Thread(new ThreadStart(Run));

            threadStateMachine.Start();
        }