o
    Ín~bÍ  ã                   @   s€   d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZ ddlmZ G d	d
„ d
eƒZdS )a-  
:class:`GameCoordinator` is used to proxy messages from/to GC.
It takes care of the encapsulation details, but on its own is not
enough to communicate with a given GC.

Example implementation of Dota 2 GC client with inheritance.

.. code:: python

    import myDotaModule
    from steam.client import SteamClient
    from steam.core.msg import GCMsgHdrProto
    from steam.client.gc import GameCoordinator

    class ExampleDotaClient(GameCoordinator):
        def __init__(self, steam):
            GameCoordinator.__init__(self, steam, 570)

        def _process_gc_message(self, emsg, header, body):
            if emsg == 4004: # EMsgGCClientWelcome
                message = myDotaModule.gcsdk_gcmessages_pb2.CMsgClientWelcome()
                message.ParseFromString(body)
                print message

        def send_hello(self):
            header = GCMsgHdrProto(4006)  # EMsgGCClientHello
            body = myDotaModule.gcsdk_gcmessages_pb2.CMsgClientHello()
            self.send(header, body.SerializeToString())

    client = SteamClient()
    dota = ExampleDotaClient(client)

    client.login()
    client.games_played([570])
    dota.send_hello()

The above code assumes that we have a ``myDotaModule`` that contains the appropriate
protobufs needed to (de)serialize message for communication with GC.
é    N)ÚEventEmitter)Úset_proto_bitÚclear_proto_bitÚis_proto)ÚEMsg)ÚEResult)ÚGCMsgHdrÚGCMsgHdrProtoÚMsgProto)ÚSteamClientc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚGameCoordinatoraé  
    ``GameCoordinator`` is used to proxy messages from/to GC

    :param steam_client: steam client instance
    :type steam_client: :class:`steam.client.SteamClient`
    :param app_id: app id of the application
    :type app_id: :class:`int`

    Incoming messages are emitted as events using their ``EMsg`` as an event identifier.

    :param header: message header
    :type header: :class:`steam.core.msg.GCMsgHdr`
    :param body: raw message body
    :type body: :class:`bytes`
    c                 C   sD   t |tƒs	tdƒ‚|| _|| _t d| ¡| _| j t	j
| j¡ d S )Nz5Expected an instance of SteamClient as first argumentzGC(appid:%d))Ú
isinstancer   Ú
ValueErrorÚsteamÚapp_idÚloggingÚ	getLoggerÚ_LOGÚonr   ZClientFromGCÚ_handle_from_gc)ÚselfZsteam_clientr   © r   ú:/usr/local/lib/python3.10/dist-packages/steam/client/gc.pyÚ__init__C   s   
zGameCoordinator.__init__c                 G   s4   |d ur| j  dt|ƒ ¡ tj| |g|¢R Ž  d S )NzEmit event: %s)r   ÚdebugÚreprr   Úemit)r   ÚeventÚargsr   r   r   r   N   s   zGameCoordinator.emitc                 C   sX   t tjƒ}| j|j_| j|j_|jrt	|j
ƒn|j
|j_| ¡ | |j_| j |¡ dS )zÛ
        Send a message to GC

        :param header: message header
        :type header: :class:`steam.core.msg.GCMsgHdr`
        :param body: serialized body of the message
        :type body: :class:`bytes`
        N)r
   r   Z
ClientToGCr   ÚheaderZrouting_appidÚbodyÚappidÚprotor   ÚmsgÚmsgtypeÚ	serializeÚpayloadr   Úsend)r   r   r    Úmessager   r   r   r'   S   s   
	

ÿþzGameCoordinator.sendc                 C   sx   |j j| jkr	d S |j j}t|ƒrt||j jƒ}tj|j }n
t	||j jƒ}t	j}|j j|d … }|  
t|ƒ||¡ d S ©N)r    r!   r   r$   r   r	   r&   Ú_sizeZheaderLengthr   Ú_process_gc_messager   )r   r#   Úemsgr   Úheader_sizer    r   r   r   r   f   s   zGameCoordinator._handle_from_gcc                 C   s   |   |||¡ d S r)   )r   )r   r,   r   r    r   r   r   r+   w   s   z#GameCoordinator._process_gc_messageN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r'   r   r+   r   r   r   r   r   2   s    r   )r1   r   ZgeventZeventemitterr   Zsteam.utils.protor   r   r   Zsteam.enums.emsgr   Zsteam.enumsr   Zsteam.core.msgr   r	   r
   Zsteam.clientr   r   r   r   r   r   Ú<module>   s    '