o
    [h                     @   s   d dl m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mZ d dlmZ d dlmZ G dd	 d	ZG d
d deZG dd deZdeeef fddZe ZdS )    )Path)RoomToSession)add_params_to_url	make_hashvalidate_alphanumeric)settings)dbNoResultFound)defaultdict)Dictc                   @   s^   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdefddZdS )BaseRoomFc                 C   s   t |dd| _|| _d S )Nzsettings.ROOMS room nameZidentifier_description)r   namedisplay_name)selfr   r    r   q/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/room.py__init__   s   
zBaseRoom.__init__c                 C   s   |   d uS N)get_sessionr   r   r   r   has_session   s   zBaseRoom.has_sessionc                 C      t r   NotImplementedErrorr   r   r   r   presence_add      zBaseRoom.presence_addc                 C   r   r   r   r   r   r   r   presence_remove   r   zBaseRoom.presence_removec                 C   s(   z	t j| jdjW S  ty   Y d S w )N	room_name)r   Zobjects_getr   sessionr	   r   r   r   r   r      s
   zBaseRoom.get_sessionc                 C   s.   t j| jd  |rt j| j|d d S d S )Nr   )r   r    )r   Zobjects_filterr   deleteZobjects_create)r   r    r   r   r   set_session$   s   zBaseRoom.set_sessionc                 C   s   |j d| jdS )NAssignVisitorToRoomr   )url_forr   r   requestr   r   r   get_room_wide_url)      zBaseRoom.get_room_wide_urlc                 C   s   g S r   r   r%   r   r   r   get_participant_urls,   r   zBaseRoom.get_participant_urlsreturnc                 C   s.   |   }|r
|j}nd }t|| j| |dS )N)session_coder   url)r   codedictr   r'   )r   r&   r    r+   r   r   r   rest_api_dict/   s   zBaseRoom.rest_api_dictN)__name__
__module____qualname__use_secure_urlsr   r   r   r   r   r"   r'   r)   r.   r/   r   r   r   r   r   
   s    r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )NoLabelRoomFr   c                 C   s   |  j d7  _ d S N   present_countr   labelr   r   r   r   C      zNoLabelRoom.presence_addc                 C   s   |  j d8  _ d S r5   r7   r9   r   r   r   r   F   r;   zNoLabelRoom.presence_removeN)r0   r1   r2   has_participant_labelsr8   r   r   r   r   r   r   r4   ?   s
    r4   c                       sN   e Zd ZU dZeed< 	d fdd	Zdd Zdd	 Zd
d Z	dd Z
  ZS )	LabelRoomTpresent_listFc                    s$   t  || || _|| _g | _d S r   )superr   participant_label_filer3   r>   )r   r   r   r@   r3   	__class__r   r   r   N   s   
zLabelRoom.__init__c                 C      | j | d S r   )r>   appendr9   r   r   r   r   V   r(   zLabelRoom.presence_addc                 C   rC   r   )r>   remover9   r   r   r   r   Y   r(   zLabelRoom.presence_removec                 C   sT   g }|j d| jd}|  D ]}d|i}| jrt||d< t||}|| q|S )Nr#   r   Zparticipant_labelhash)r$   r   get_participant_labelsr3   r   r   rD   )r   r&   Zparticipant_urlsZroom_base_urlr:   paramsZparticipant_urlr   r   r   r)   \   s   
zLabelRoom.get_participant_urlsc                 C   s:   t | jjdd }|D ]}t|dd qtt|S )Nutf8)encodingzparticipant labelr   )r   r@   	read_textsplitr   listr.   fromkeys)r   labelsr:   r   r   r   rG   i   s   z LabelRoom.get_participant_labels)F)r0   r1   r2   r<   rM   __annotations__r   r   r   r)   rG   __classcell__r   r   rA   r   r=   J   s   
 r=   r*   c                  C   sp   t tdg } i }| D ]+}|drtdi |}n|dr)d|d }t|tdi |}|||j< q
|S )NROOMSr@   r3   zSRoom "{}": you must either set participant_label_file, or set use_secure_urls=Falser   r   )getattrr   getr=   format
ValueErrorr4   r   )rR   	ROOM_DICTroomZroom_objectmsgr   r   r   get_room_dictq   s   


rZ   N)pathlibr   Zotree.models_concreter   Zotree.commonr   r   r   Zotreer   Zotree.databaser   r	   collectionsr
   typingr   r   r4   r=   strrZ   rW   r   r   r   r   <module>   s    5'
