a
    �TiÛ  ã                   @   sn   d Z ddlZddlZddlZe e¡ZG dd„ dƒZG dd„ dƒZdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ ZdS )z´
WebSocket Messaging Module

This module provides utilities for sending messages to WebSocket clients,
including batched updates, serialization helpers, and typed message senders.
é    Nc                   @   sT   e Zd ZdZdd„ Zeeddœdd„Zedœd	d
„Z	edœdd„Z
edœdd„ZdS )ÚBatchedUpdatezš
    Accumulator for batched game state updates.

    Instead of sending 14+ separate messages per tick,
    batch all updates into a single message.
    c                 C   s
   i | _ d S )N©Ú_updates©Úself© r   ú;/var/www/lichun.app/lichun/ws/server/websocket_messaging.pyÚ__init__   s    zBatchedUpdate.__init__N)ÚkeyÚvalueÚreturnc                 C   s   || j |< dS )zAdd an update to the batchNr   )r   r
   r   r   r   r   Úadd   s    zBatchedUpdate.add)r   c                 C   s   | j s
dS d| j dœS )z!Convert to dictionary for sendingNZbatch_update)ÚtypeZupdatesr   r   r   r   r   Úto_dict   s
    þzBatchedUpdate.to_dictc                 C   s"   |   ¡ }|du rdS tj|td�S )zConvert to JSON stringN©Údefault)r   ÚjsonÚdumpsÚComplexHandler)r   Údatar   r   r   Úto_json)   s    zBatchedUpdate.to_jsonc                 C   s   t | jƒdkS )zCheck if batch is emptyr   )Úlenr   r   r   r   r   Úis_empty0   s    zBatchedUpdate.is_empty)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   ÚstrÚanyr   Údictr   r   Úboolr   r   r   r   r   r      s   
r   c                   @   s   e Zd ZdZdd„ ZdS )ÚserverClassz&
    Server state tracking class.
    c                 C   s
   d| _ d S )Nr   )Úticksr   r   r   r   r	   9   s    zserverClass.__init__N)r   r   r   r   r	   r   r   r   r   r!   5   s   r!   c                 C   sN   t | dƒr|  ¡ S t| tƒr$t| ƒS t | dƒr4| jS tdt| ƒj› d�ƒ‚dS )z 
    JSON serialization handler for complex objects.

    Args:
        Obj: Object to serialize

    Returns:
        Jsonable representation if available
    ÚjsonableÚ__dict__zObject of type z is not JSON serializableN)	Úhasattrr#   Ú
isinstanceÚsetÚlistr$   Ú	TypeErrorr   r   )ÚObjr   r   r   r   =   s    



r   c              
   Ã   s’   ddl m} | | j¡}|r~z| |¡I dH  W qŽ tyz } z2td| j› d|› �ƒ ddl}| ¡  W Y d}~qŽd}~0 0 ntd| j› �ƒ dS )zž
    Send message to user (O(1) lookup).

    Args:
        websocket: WebSocket connection with userID attribute
        message: Message string to send
    r   )ÚUSERSNzError sending to z: z,Warning: No active websocket found for user )	Úserver.websocket_registryr+   ÚgetZuserIDÚsendÚ	ExceptionÚprintÚ	tracebackÚ	print_exc)Ú	websocketÚmessager+   ÚuserÚer1   r   r   r   Ú
sendToUserQ   s     r7   c                 Ã   s$   | r t | tj|jtd�ƒI dH  dS )z�
    Send event message to websocket.

    Args:
        websocket: WebSocket connection
        m: Event object with __dict__ attribute
    r   N©r7   r   r   r$   r   )r3   Úmr   r   r   ÚsendEventMessageg   s    r:   c                 Ã   s$   |r t |tj| jtd�ƒI dH  dS )z˜
    Send player information to websocket.

    Args:
        player: Player object with __dict__ attribute
        websocket: WebSocket connection
    r   Nr8   )Úplayerr3   r   r   r   ÚsendUserInfos   s    r<   c                 Ã   s"   | rt | tj|td�ƒI dH  dS )z‹
    Send dictionary object to websocket.

    Args:
        websocket: WebSocket connection
        obj: Object to serialize and send
    r   N)r7   r   r   r   )r3   Úobjr   r   r   ÚsendDict   s    r>   )r   Úasyncior   ÚloggingÚ	getLoggerr   Úloggerr   r!   r   r7   r:   r<   r>   r   r   r   r   Ú<module>   s   
%