a
    e=iý  ã                   @   s$   d Z G dd„ dƒZdd„ Zeƒ ZdS )z±
WebSocket Registry Module

This module provides efficient O(1) user lookup and management for WebSocket connections.
Replaces O(n) set iteration with dictionary-based lookups.
c                   @   s^   e Zd ZdZdd„ Zddœdd„Zddœdd	„Zed
œdd„Ze	dœdd„Z
edœdd„ZdS )ÚUserRegistryzl
    Efficient O(1) user lookup registry.

    Replaces O(n) set iteration with O(1) dictionary lookup.
    c                 C   s
   i | _ d S )N)Ú_users©Úself© r   ú:/var/www/lichun.app/lichun/ws/server/websocket_registry.pyÚ__init__   s    zUserRegistry.__init__N)Úreturnc                 C   s   t |dƒr|| j|j< dS )zAdd a websocket connectionÚuserIDN)Úhasattrr   r	   ©r   Ú	websocketr   r   r   Úadd   s    
zUserRegistry.addc                 C   s$   t |dƒr |j| jv r | j|j= dS )zRemove a websocket connectionr	   N©r
   r	   r   r   r   r   r   Úremove   s    zUserRegistry.remove)Úuser_idc                 C   s   | j  |¡S )zGet websocket by user ID (O(1)))r   Úget)r   r   r   r   r   r      s    zUserRegistry.getc                 C   s
   t | jƒS )zGet number of connected users)Úlenr   r   r   r   r   Úcount!   s    zUserRegistry.countc                 C   s   t |dƒr|j| jv S dS )z Check if websocket is registeredr	   Fr   r   r   r   r   Ú__contains__%   s    
zUserRegistry.__contains__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   Ústrr   Úintr   Úboolr   r   r   r   r   r   	   s   r   c                 C   s
   t  | ¡S )z¯
    Get websocket connection for a specific player ID.

    Args:
        player_id: The player's user ID

    Returns:
        WebSocket connection or None if not found
    )ÚUSERSr   )Ú	player_idr   r   r   Úget_websocket_for_player,   s    
r   N)r   r   r   r   r   r   r   r   Ú<module>   s   #