Exemplo n.º 1
0
        /// <summary>
        /// Executes a ChessCommand.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="command">The ChessCommand.</param>
        public void ExecuteCommand(AuthenticatedClient client, ChessCommand command)
        {
            bool executionAllowed = false;

            if (this.PlayerTurn)
            {
                if (this.HostClient == client)
                {
                    executionAllowed = true;
                }
            }
            else
            {
                if (this.GuestClient == client)
                {
                    executionAllowed = true;
                }
            }

            if (executionAllowed)
            {
                // Do stuff
                this.Model.ExecuteCommand(command);
            }

            this.NotifyClients();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Called when a ChessCommand is received.
 /// </summary>
 /// <param name="packetHeader">The packet header.</param>
 /// <param name="connection">The connection.</param>
 /// <param name="command"></param>
 private void HandleChessCommandSent(PacketHeader packetHeader, Connection connection, ChessCommand command)
 {
     if (this.IsClientAuthenticated(connection))
     {
         AuthenticatedClient client = this.AuthenticatedClients.GetAuthenticatedClientFromConnection(connection);
         this.ChessGameRoomList.ExecuteCommandInRoom(client, command);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Used to know if a client is in the room.
        /// </summary>
        /// <param name="authenticatedClient">The client.</param>
        /// <returns>True == the client is in the room, false == the client is NOT in the room.</returns>
        public bool IsClientInRoom(AuthenticatedClient authenticatedClient)
        {
            bool isInRoom = false;

            if (this.HostClient == authenticatedClient || this.GuestClient == authenticatedClient)
            {
                isInRoom = true;
            }
            return(isInRoom);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Removes a client from all game rooms.
 /// </summary>
 /// <param name="authenticatedClient">The client to remove.</param>
 public void RemoveClientFromAllRooms(AuthenticatedClient authenticatedClient)
 {
     lock (this.GameRoomList)
     {
         foreach (ChessGameRoom gameRoom in this.GameRoomList)
         {
             if (gameRoom.IsClientInRoom(authenticatedClient))
             {
                 gameRoom.LeaveRoom(authenticatedClient);
             }
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Get the AuthenticatedClient object reference of a given connection.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <returns>The AuthenticatedClient object reference.</returns>
        public AuthenticatedClient GetAuthenticatedClientFromConnection(Connection connection)
        {
            AuthenticatedClient authClient = new AuthenticatedClient();

            foreach (AuthenticatedClient cli in this.ClientList)
            {
                if (cli.Connection == connection)
                {
                    authClient = cli;
                }
            }

            return(authClient);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Removes a client from a game room.
        /// </summary>
        /// <param name="authenticatedClient">The client to remove.</param>
        /// <param name="roomID">The uniq ID of the room.</param>
        /// <returns>True == the client has been removed, false == an error occured.</returns>
        public bool RemoveClientFromRoom(AuthenticatedClient authenticatedClient, int roomID)
        {
            bool hasBeenRemoved = false;

            lock (this.GameRoomList)
            {
                foreach (ChessGameRoom cgr in this.GameRoomList)
                {
                    if (cgr.ID == roomID)
                    {
                        hasBeenRemoved = cgr.LeaveRoom(authenticatedClient);
                    }
                }
            }

            return(hasBeenRemoved);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Adds a client to a chess game room.
        /// </summary>
        /// <param name="authenticatedClient">The authenticated client.</param>
        /// <param name="roomID">The room's ID.</param>
        /// <returns>True == the client has joined, false == an error occured.</returns>
        public bool AddClientToRoom(AuthenticatedClient authenticatedClient, int roomID)
        {
            bool hasJoined = false;

            lock (this.GameRoomList)
            {
                foreach (ChessGameRoom cgr in this.GameRoomList)
                {
                    if (cgr.ID == roomID)
                    {
                        hasJoined = cgr.JoinRoom(authenticatedClient);
                    }
                }

                //this.GetRoomFromID(roomID).JoinRoom(authenticatedClient);
            }

            return(hasJoined);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Removes a client from the room.
        /// </summary>
        /// <param name="authenticatedClient">The client.</param>
        /// <returns>True == the client has been removed, false == the client has not been removed.</returns>
        public bool LeaveRoom(AuthenticatedClient authenticatedClient)
        {
            bool hasBeenRemoved = false;

            if (this.RoomName == authenticatedClient.Username)
            {
                if (this.HostClient == authenticatedClient)
                {
                    this.HostClient = null;
                    hasBeenRemoved  = true;
                }
            }
            else
            {
                if (this.GuestClient == authenticatedClient)
                {
                    this.GuestClient = null;
                    hasBeenRemoved   = true;
                }
            }

            this.NotifyClients();
            return(hasBeenRemoved);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Adds a client to the room.
        /// </summary>
        /// <param name="authenticatedClient">The client.</param>
        /// <returns>True == the client has been added, false == the client has not been added.</returns>
        public bool JoinRoom(AuthenticatedClient authenticatedClient)
        {
            bool hasJoined = false;

            if (this.RoomName == authenticatedClient.Username)
            {
                if (this.HostClient == null)
                {
                    this.HostClient = authenticatedClient;
                    hasJoined       = true;
                }
            }
            else
            {
                if (this.GuestClient == null)
                {
                    this.GuestClient = authenticatedClient;
                    hasJoined        = true;
                }
            }

            this.NotifyClients();
            return(hasJoined);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Executes a command in the given room.
        /// </summary>
        /// <param name="roomID">The room's ID.</param>
        public void ExecuteCommandInRoom(AuthenticatedClient client, ChessCommand command)
        {
            ChessGameRoom gameRoom = this.GetRoomFromID(command.RoomID);

            gameRoom.ExecuteCommand(client, command);
        }