o
    [h9                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlZd dl	Zd dl
Zd dlmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZmZ d d	l
mZ d
dlmZ eeZG dd dZdee fddZdddZdd ZdS )    N)Path)List)settings)get_bots_moduleget_models_module)AUTO_NAME_BOTS_EXPORT_FOLDER)values_flatdb)SessionParticipant)SESSION_CONFIGS_DICT   )ParticipantBotc                   @   s.   e Zd Zdee fddZdd Zdd ZdS )	SessionBotRunnerbotsc                 C   s    i | _ |D ]}|| j |j< qd S N)r   participant_code)selfr   bot r   x/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/bots/runner.py__init__   s   zSessionBotRunner.__init__c              	   C   s   |    d}	 t| jdkrdS |dkrtdt| j }d}|D ]+}| j| }| r/q#z| }W n tyD   | j	| Y nw |
| d}d}q#|sU|d7 }q)zround-robinr   TN
   zBots got stuckFr   )open_start_urlslenr   AssertionErrorlistkeysZon_wait_pageZget_next_submitStopIterationpopZsubmit)r   Zloops_without_progressZplayable_idsZprogress_madeidr   Z
submissionr   r   r   play   s0   

zSessionBotRunner.playc                 C   s   | j  D ]}|  qd S r   )r   valuesZopen_start_url)r   r   r   r   r   r   =   s   
z SessionBotRunner.open_start_urlsN)__name__
__module____qualname__r   r   r   r!   r   r   r   r   r   r      s     r   returnc              
   C   s  t jdi}|rd|t j< t j| d| g }tj| d}t|j	dt j
}dd |D }|jd D ];}t|}	t|}
|
j}|j| dt 	d|jt j
|j}|D ]\}}}|	j|||||| d	}|| | qVq3t }| D ]\}}t|||d
}|| qv|S )NT)
session_idr    r    c                 S   s   i | ]}|g qS r   r   ).0Zpcoder   r   r   
<dictcomp>P   s    zmake_bots.<locals>.<dictcomp>Zapp_sequenceZround_number)case_numberapp_nameZ	player_pkZsubsession_pkr   
session_pk)player_botsexecuted_live_methods)r   Z_is_botZis_browser_botZobjects_filterupdater
   objects_getr   Zpp_setZorder_bycodeconfigr   r   PlayerjoinZwith_entitiesr    subsession_idZ	PlayerBotappendsetitemsr   )r-   r+   use_browser_botsZupdate_kwargsr   sessionZparticipant_codesZplayer_bots_dictr,   Zbots_moduleZmodels_moduler4   ZplayersZ	player_idr   r6   Z
player_botr/   r.   r   r   r   r   	make_botsB   sJ   


r<   c                 C   sN   t j| d}t|j|dd}| d u r|  t  t|d}|	  d S )Nr(   F)r-   r+   r:   )r   )
r
   r1   r<   r    Zget_roomZmock_exogenous_datar	   commitr   r!   )r'   r+   r;   Zbot_listrunnerr   r   r   run_botst   s   
r?   c              	   C   s  | r| g}nt  }|D ]I}zt | }W n ty&   d| d}t|dw | }t|D ]%}td|| t	j
j||pC|d d}	|	j}
t|
|d td q/q|rtj }|tkrl|d	dd
 d }tj|dd tjD ]4}t	j|}|j rt|d|}|jdddd}t	j|| W d   n1 sw   Y  qvt|d}|jddd}t	j| W d   n1 sw   Y  td| dS td dS )zk
    this means all test cases are in 1 big test case.
    so if 1 fails, the others will not get run.
    zNo session config with name 'z'.Nz$Creating '{}' session (test case {})Znum_demo_participants)session_config_namenum_participants)r+   zBots completed sessionz__temp_bots_%b%d_%Hh%Mm%S.%fsT)exist_okz{}.csvw utf8)newlineencodingzall_apps_wide.csv)rI   zExported CSV to folder "{}"zPTip: Run this command with the --export flag to save the data generated by bots.)r   r   KeyError	ExceptionZget_num_bot_casesrangeloggerinfoformatotreer;   Zcreate_sessionr    r?   datetimenowr   strftimeosmakedirsr   Z
OTREE_APPScommonr   r4   Zobjects_existsr   openZexportZ
export_appZexport_wide)r@   rA   Zexport_pathZsession_config_namesZconfig_namer3   msgZnum_bot_casesr+   r;   r'   rR   ZappZmodel_moduleZfpathfpr   r   r   run_all_bots_for_session_config   sX   






rZ   r   ) rQ   loggingrT   pathlibr   typingr   Zotree.commonrP   Zotree.exportZotree.sessionr   r   r   Zotree.constantsr   Zotree.databaser   r	   Zotree.modelsr
   r   r   r   r   	getLoggerr#   rM   r   r<   r?   rZ   r   r   r   r   <module>   s(    
,
2