o
    [h
"                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ	 d dl
mZ d dlZd dlZd dlmZmZ d dlZd dlmZmZ d dlmZmZ d dlm  mZ d d	lmZ G d
d deejjZdS )    N)Column
ForeignKey)relationship)sqltypes)HTTPException)random_chars_8ADMIN_SECRET_CODE)	MixinVarsCurrencyType)url_i_should_be_onget_page_lookup)settingsc                   @   sB  e Zd ZdZeejedddZe	dddZ
eedd	d
Zeejd	d
ZeeddZeedd	d
Zeedd	d
Zeedd	d
Zeejdd	dZdd Zeedd	d
Zeededd	d	dZeedZeejdd	dZeejd	d
Zeejd	d
ZeejddZeedd	d
Zeedd	d
Zeejd	d
Z eedZ!eejZ"dZ#eejddZ$eejddZ%e&j'( Z)eejZ*eejddZ+eejddZ,eejZ-eejZ.dd Z/dd Z0dd Z1dd Z2d d! Z3d"d# Z4d$d% Z5d&d' Z6d(d) Z7d*d+ Z8d,d- Z9d.d/ Z:d0d1 Z;d2d3 Z<d4d5 Z=d6S )7ParticipantZotree_participantzotree_session.idZCASCADE)ZondeleteSessionZpp_set)Zback_populatesd   T)nullabler   )default2   )r   indexc                 C   s   d | jS )zthe human-readable version.zP{})formatid_in_sessionself r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/models/participant.py_numeric_label'   s   zParticipant._numeric_labeli,     F)r   r   uniquer      i  z5 You can define it in the PARTICIPANT_FIELDS setting.c                 C   s.   |sd S t |dkrtdd| || _d S )Nr   i  z,participant_label is too long or malformed: )lenr   label)r   r    r   r   r   	set_label^   s   

zParticipant.set_labelc                 C   s   | j  d| j S )N/)_index_in_pages_max_page_indexr   r   r   r   _current_pageg   s   zParticipant._current_pagec                 C   s   |   S N)r%   r   r   r   r   current_page_m   s   zParticipant.current_page_c                 C   sL   g }| j jd }|D ]}tj|}|jj| dd}|t	| q
|S )zUsed to calculate payoffsapp_sequence)participantround_number)
sessionconfigotreecommonget_models_modulePlayerZobjects_filterZorder_byextendlist)r   lstr(   Zappmodels_moduleZplayersr   r   r   get_playersp   s   zParticipant.get_playersc                 C   s6   | j s|  S | j| jkrt| j| j| jS d| j S )Nz/OutOfRangeNotification/)visited
_start_urlr#   r$   r   code_session_coder   r   r   r   _url_i_should_be_on|   s   
zParticipant._url_i_should_be_onc                 C   s   t j| jS r&   )r-   r.   Zparticipant_start_urlr8   r   r   r   r   r7         zParticipant._start_urlc                 C      | j | jS r&   )payoffZto_real_world_currencyr+   r   r   r   r   payoff_in_real_world_currency   r;   z)Participant.payoff_in_real_world_currencyc                 C   r<   r&   )r+   Z"_get_payoff_plus_participation_feer=   r   r   r   r   payoff_plus_participation_fee   r;   z)Participant.payoff_plus_participation_feec                 C   s6   t | j| j}tj|j}t|d}|j| |j	dS )Nr0   )r)   r*   )
r   r9   r#   r-   r.   r/   app_namegetattrZobjects_getr*   )r   lookupr4   ZPlayerClassr   r   r   _get_current_player   s   
zParticipant._get_current_playerc                 C   s   | }|j dkrGd|_ d|_|js|| ttj |_tt		 |_
ddlm} |j|j|j|jdddtt		 dddd
}|| dS dS )	zmin a separate function so that we can call it individually,
        e.g. from advance_last_place_participantsr      T)common2 ZInitializeParticipant)
Zsession_codeZparticipant_id_in_sessionZparticipant_codeZ
page_indexr@   Z	page_nameZepoch_time_completedr*   timeout_happenedZis_wait_pageN)r#   r6   r    r!   strdatetimeutcnowtime_started_utcinttime_last_page_timestampr-   rE   ZTimeSpentRowr9   r   r8   Zwrite_row_to_page_buffer)r   Zparticipant_labelpprE   rowr   r   r   
initialize   s.   


zParticipant.initializec                 C   s4   ddl m} tjt| jt|| gdd d S )Nr   )export)rows)groupdata)r-   rR   channel_utilsZsync_group_sendZsession_monitor_group_namer9   dictZget_rows_for_monitor)r   rR   r   r   r   _update_monitor_table   s
   

z!Participant._update_monitor_tablec                 C   s2   | j | jkrd S t| j| j j }||  |S r&   )r#   r$   r   r9   Z
page_classZinstantiate_without_requestZset_attributes)r   pager   r   r   _get_page_instance   s   
zParticipant._get_page_instancec                 C   sT   ddl m} |  }t||r(ddlm} |tjjt	tjj
di|_|  d S d S )Nr   )Page)FormData1)Z	otree.apir[   rZ   
isinstanceZstarlette.datastructuresr\   r-   	constantsZadmin_secret_coder   rG   Z
_form_datapost)r   r[   rY   r\   r   r   r   _submit_current_page   s   
z Participant._submit_current_pagec                 C   sB   t dD ]}|  }|s dS | }t|jds dS qdS )z
        we need the redirect logic because if it's from ensure_pages_visited,
        then the previous page
        was a wait page, so that will not handle redirects.
           N3)rangerZ   getrH   status_code
startswith)r   irY   respr   r   r   _visit_current_page   s   zParticipant._visit_current_pagec                 C   s   | j ddS )NfinishedF)varsre   r   r   r   r   _get_finished   r;   zParticipant._get_finishedN)>__name__
__module____qualname__Z__tablename__r   stIntegerr   Z
session_idr   r+   Stringr    r   r
   r=   rK   Zmturk_assignment_idZmturk_worker_idr#   r   Z_monitor_noter   r8   r9   Booleanr6   rN   Z_last_request_timestampZis_on_wait_pageZ_current_page_nameZ_current_app_nameZ_round_numberZ_current_form_page_urlr$   Z_SETATTR_NO_FIELD_HINTZ_is_botZis_browser_botr-   databaseZ
FloatFieldZ_timeout_expiration_timeZ_timeout_page_indexZ_gbat_is_connectedZ_gbat_tab_hiddenZ_gbat_page_indexZ_gbat_groupedr!   r%   r'   r5   r:   r7   r>   r?   rC   rQ   rX   rZ   ra   rj   rm   r   r   r   r   r      sl    




		!	r   )rI   rM   Z
sqlalchemyr   r   Zsqlalchemy.ormr   Zsqlalchemy.sqlr   rq   Zstarlette.exceptionsr   Zotree.commonr-   Zotree.databaser   r   Zotree.constantsr	   r
   Zotree.lookupr   r   Zotree.channels.utilsZchannelsutilsrV   r   ru   Z
SSPPGModelr   r   r   r   r   <module>   s    