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}~qd}~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   
%