
    sojL                     b   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mZmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmc mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z% d dl&m'Z' d dlm(Z(m)Z)m*Z*m+Z+m,Z, d dl-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6  G d de          Z7	 	 	 d-dedej8        dede'dede9de:ddfd Z;dej8        dededede<e<e'                  f
d!Z=dede"fd"Z>ded#e"d$ede?fd%Z@d&e%de?fd'ZAd&e%de?fd(ZBdedefd)ZCdedej8        ded$ed*e:d+eddfd,ZDdS ).    N)datetime)StrEnum)UpdateMessage)
BadRequest
RetryAfter)ContextTypes)Game)User)Emoji)MessageSource)SavedMediaName)Screen)GameOutcome)RockPaperScissors)RockPaperScissorsChoice)Keyboard)get_auto_move_warningenqueue_auto_moveedit_other_player_messageget_global_share_keyboardend_global_game_player)full_message_sendmention_markdown_userfull_media_sendget_message_source)get_belly_formatted)get_current_languageset_current_languagec                       e Zd ZdZdZdZdS )GameRPSReservedKeysz+
    The reserved keys for this screen
    abN)__name__
__module____qualname____doc__GAME_IDCHOICE     //app/src/chat/common/screens/screen_game_rps.pyr!   r!   (   s$          GFFFr+   r!   Fupdatecontextuserinbound_keyboardgameis_auto_moveedit_message_idreturnc                 J  K   t          j        | |||           d{V }|dS t          |          }t          j                    |_        |j        t          |           k    rt          |j	        t          j                           }||j        k    r||_        n||_        |                                |_        |                                 |s't%          |t'          |          | dd           d{V  |                                 |                                r+t-          |||                    |                     d{V  |                                r|                                }	t          j        ||	|| |                                s|r|                    |          nd           d{V  d|_        |}
	 t;          |t=          |||          | t?          ||| |          |                                 d|dd	  	         d{V }|r|j!        }
n=# tD          tF          f$ r)}tI          j%        d|j!         d	|            Y d}~nd}~ww xY w|                                r|j&        |                    |          }tO                      }tQ          |j        )                                           t=          |||j                  }tQ          |j&        )                                           t=          |||j&                  }tQ          |           |j*        +                    tY          ||||
||t?          ||| |                               dS 	 t;          |t=          |||          | t?          ||| |          |                                 tZ          j.        |
           d{V }|                                r|j&        tO                      }tQ          |j        )                                           t=          |||j                  }tQ          |j&        )                                           t=          |||j&                  }tQ          |           |j*        +                    tY          ||||j!        ||t?          ||| |                               t          j/        |||j0                   d{V  |j        t          j1        k    s|j        t          j1        k    rd}d}|                                s|j        t          |           k    rd}d}n`|                    |          r|j        t          j1        k    rd}n3|j&        ,|                    |          s|j        t          j1        k    rd}|r;|j*        +                    te          | |||j        tf          |j!                             |rG|sdnd}|j*        +                    te          | |||j&        tf          |j!        |                     dS dS dS # tF          $ r Y dS w xY w)a  
    Manage the Rock Paper Scissors game screen
    :param update: The update object
    :param context: The context object
    :param user: The user object
    :param inbound_keyboard: The inbound keyboard
    :param game: The game object
    :param is_auto_move: If the interaction is from auto move
    :param edit_message_id: Message to be edited. Useful for when a user does not interact with the private game before
        auto-move is triggered, since the message tied with the update would be the start command
    :return: None
    NT)r-   answer_callback
show_alert)is_challenger)r-   send_outcome_to_userF)captionr-   keyboardauthorized_usersedit_only_caption_and_keyboardr3   ignore_bad_request_exceptionshould_log_ignored_exceptionz1Failed to send auto-move result message for game z: )r:   r-   r;   r<   saved_media_namer3   r      )extra_wait_time)4game_servicevalidate_game	get_boardr   nowlast_interaction_datescreen
get_screen	RPSChoiceinfor!   r)   
challengerchallenger_choiceopponent_choiceget_as_json_stringboardsaver   get_choice_textset_turn	is_globalr   r8   is_finishedget_outcomeend_gameget_other_playershould_update_modelr   get_textget_outbound_keyboardget_playersidr   r   loggingwarningopponentr   r   get_languageapplicationcreate_taskr   r   GAME_ROCK_PAPER_SCISSORSnotify_game_turn	game_turnNONEr   	auto_move)r-   r.   r/   r0   r1   r2   r3   rock_paper_scissors
rps_choicegame_outcomesent_message_idmessageeother_playerplayer_languagechallenger_textopponent_textshould_auto_move_challengershould_auto_move_opponentrB   s                       r,   manageru   1   s@     . +FG=MtTTTTTTTTD|-6t__!)D*V"4"444/45H5OPQQ
4?""4>112</(;;==
		  	#
++ $          	$$&&&>> 	`($dFXFXY]F^F^__________ &&(( <$7$C$C$E$E#040@0@[L[%%d+++W[
 
 
 	
 	
 	
 	
 	
 	
 	
 $) 
 *	%4 ':DAA.wfdKK!%!1!1!3!3/3 /-1-1
& 
& 
& 
  
  
  
  
  
 G  -")*J' 	 	 	ORDGRRqRR       	 >> 	 90066L244O !=!=!?!?@@@&t-@$/RRO !;!;!=!=>>>$T+>NNM 111++)##!)'4NN 
 
 
 	W!0T#6==*7D&$GG!--//+D+"
 "
 "
 
 
 
 
 
 
 >> 	 9244O !=!=!?!?@@@&t-@$/RRO !;!;!=!=>>>$T+>NNM 111++)J#!)'4FF 
 
 
 +GT;N;XYYYYYYYYY  1Y^CC"2inDD*/'(-% >>## 5#*j.@.@@@26/04- &&t,,
5+=OO26//M- ..t44 .+;y~MM04-* #//%t	7:    ) 8?AAa   #//%!
(7  
 
 
 
 
E ED<      s,   AH   I1II"J,X 
X"!X"c           	      >   g }|                                 r|S g }t          j        |j        t          j        t
          j        i}|                    t          t          j        |t          |                               t          j        |j        t          j        t
          j        i}|                    t          t          j        |t          |                               t          j        |j        t          j        t
          j        i}|                    t          t          j        |t          |                               |                    |           ||                                r%|                    |          rt          | |          ng z  }|S )z
    Get the outbound keyboard
    :param context: The context
    :param game: The game object
    :param update: The update object
    :param user: The user
    :return: The outbound keyboard
    rK   rH   )rU   r!   r(   r]   r)   rJ   ROCKappendr   r   rI   PAPERSCISSORSchallenger_has_finishedr8   r   )	r.   r1   r-   r/   outbound_keyboardkeyboard_linebutton_info_rockbutton_info_paperbutton_info_scissorss	            r,   r[   r[     s}    /1 !  $&M#TW"IN %*3CJW]L^L^___```#TW"IO %+4EjY_N`N`aaabbb#TW"I$6 &::fCUCUVVV   ]+++''))	.2.@.@.F.F	!'4000 r+   c                     | j         =t                      }|                                | _         |                                  |S t	          j        | j                   }t          di |S )zO
    Get the board
    :param game: The game object
    :return: The board
    Nr*   )rP   r   rO   rQ   jsonloads)r1   ri   rock_paper_scissors_dicts      r,   rE   rE   2  se     z/11(;;==
		""#z$*55887888r+   ri   playerc           	         |                                 r%|                                t          j        k    r2t          j                            t          | j                            }n`|                                t          j	        k    r2t          j                            t          | j
                            }nt          j        }t          j                            t          | j                  t          |j                  t          | j
                  t          |j                            }||z   }n|                                 s*|j        t"          j        k    r|j        t"          j        k    sh|                                 r|                     |          r|j        t"          j        k    s*|                     |          rG|j        t"          j        k    r2t          j        t-          |                                            z   }n|                                 r|| j        k    }|rAt          j                            t          |j                            }|t          j        z  }nt          j                            t          |j                            }|t          j        z  }nx|j        t"          j        k    r2t          j                            t          | j                            }n1t          j                            t          | j
                            }| j
        It          j                            t          j        t          j        t=          | j                  d|          S t          j                             t          j        t          j        t          | j                  t          | j
                  t=          | j                  d|          S )z
    Get the text
    :param game: The game object
    :param rock_paper_scissors: The rock paper scissors object
    :param player: The player
    :return: The text
    )add_turn_notification_timeN )!rU   rV   r   CHALLENGER_WONphrasesGAME_RESULT_WINformatr   rL   OPPONENT_WONr`   GAME_RESULT_DRAWROCK_PAPER_SCISSORS_CHOICESget_choice_emojirM   rN   rT   rJ   rg   r8   is_opponentGAME_STATUS_AWAITING_CHOICEr   ROCK_PAPER_SCISSORS_CHOICEGAME_GLOBAL_PENDING_CHALLENGER$ROCK_PAPER_SCISSORS_PENDING_OPPONENT GAME_STATUS_AWAITING_USER_CHOICEGAME_TEXT_WITHOUT_PLAYERSROCK_PAPER_SCISSORS_GAME_NAME$ROCK_PAPER_SCISSORS_GAME_DESCRIPTIONr   wager	GAME_TEXT)r1   ri   r   added_ot_textchoices_textr8   s         r,   rZ   rZ   D  sb    &&(( :**,,0JJJ#3::;PQUQ`;a;abbMM ,,..+2JJJ#3::;PQUQ^;_;_``MM#4M:AA!$/220BCC!$-000@AA	
 
 %}4   *	#5GG#3y~EENN F
 &&v.. F ,=OO $$V,, P ,;y~MM $?BW/3~~/?/?+?C C C MM ^^ 	"do5M  	N ' B I I$%8%JKK! ! !GG ' B I I$%8%HII! ! !MM 2inDD#DKK%do66 MM $DKK%dm44 M }07718
++
 
 	
 ##-4do..dm,,DJ''
  r+   rj   c                    | xt           j        k    r* t          j                            t          j                  S xt           j        k    r* t          j                            t          j                  S t           j        k    r)t          j                            t          j	                  S 	 t          d|            )zU
    Get the choice text
    :param rps_choice: The choice
    :return: The text
    $Unknown Rock Paper Scissors choice: )rJ   rx   r   r   r   ROCK_PAPER_SCISSORS_CHOICE_ROCKrz    ROCK_PAPER_SCISSORS_CHOICE_PAPERr{   #ROCK_PAPER_SCISSORS_CHOICE_SCISSORS
ValueErrorrj   s    r,   rR   rR     s     Y^^^^5<<7   Y____5<<8   5<<;   PJPPQQQr+   c                     | xt           j        k    r t          j        S xt           j        k    r t          j        S t           j        k    rt          j        S 	 t          d|            )zW
    Get the choice emoji
    :param rps_choice: The choice
    :return: The emoji
    r   )rJ   rx   r   rz   r{   r   r   s    r,   r   r     s_     Y^^^^:Y____;>!PJPPQQQr+   c                 h    t          |           t          j        u rt          j        S t          j        S )zU
    Get the screen
    :param update: The update object
    :return: The screen
    )r   r   GROUPr   GRP_ROCK_PAPER_SCISSORS_GAMEPVT_ROCK_PAPER_SCISSORS_GAME)r-   s    r,   rI   rI     s,     &!!]%88822..r+   
message_id_previous_gamec           	        K   t          |          }|                    |          r|j        t          j        k    s*|                    |          r|j        t          j        k    rdS t          j        t          j
                                                  t          j        |j        k    t          j        |j        k    z                                            }|dk    rdS t          |                    |          r|j        n|j                  }|t          j        k    r't          j        t#          t                              }	n|                                }	t'          dt(          j        |j        t(          j        |	it/          |                     }
t1          j        |j                  }t5          | |||
|d|           d{V  dS )a?  
    Auto-move for the given player
    :param update: The update
    :param context: The context object
    :param game: The game object
    :param player: The player object
    :param message_id: Identifier of the message to be edited
    :param _previous_game: Game object state during enqueue
    :return: None
    N)rG   r   r   rw   T)r2   r3   )rE   r8   rM   rJ   rg   r   rN   r
   r-   r   rF   wherer]   rG   executerandomchoicelistget_losing_choicer   r!   r(   r)   rI   r   	get_by_idru   )r-   r.   r1   r   r   r   rP   claimed_rowsother_player_choicer   mock_keyboards              r,   rh   rh     s     (  )E			F	#	#(-(?9>(Q(QF## )R!Y^33 	(,..999	47"t'ATE_'_`	a	a	 
 q%.!%!3!3F!;!;XAX& & in,,"M$y//::/AACC '
'&
 &!!  M ^FI&&F
4Yc           r+   )NFN)Er   r^   r   r   enumr   src.tg_compatr   r   src.tg_compat.errorr   r   src.tg_compat.extr	   resources.phrasesr   src.service.game_serviceservicerC   src.model.Gamer
   src.model.Userr   src.model.enums.Emojir   src.model.enums.MessageSourcer   src.model.enums.SavedMediaNamer   src.model.enums.Screenr   src.model.game.GameOutcomer   $src.model.game.rps.RockPaperScissorsr   *src.model.game.rps.RockPaperScissorsChoicer   rJ   src.model.pojo.Keyboardr   r   r   r   r   r   src.service.message_servicer   r   r   r   src.utils.string_utilsr   src.service.language_servicer   r   r!   DEFAULT_TYPEboolintru   r   r[   rE   strrZ   rR   r   rI   rh   r*   r+   r,   <module>r      sI                  ) ) ) ) ) ) ) ) 6 6 6 6 6 6 6 6 * * * * * * # # # # # # / / / / / / / / /             ' ' ' ' ' ' 7 7 7 7 7 7 9 9 9 9 9 9 ) ) ) ) ) ) 2 2 2 2 2 2 B B B B B B [ [ [ [ [ [ , , , , , ,                         7 6 6 6 6 6 S S S S S S S S    '    P PP&P P 	P
 P P P 
P P P Pf+&+.2+<B+JN+	$x.+ + + +\9D 9. 9 9 9 9$V4 V.? V VRU V V V VrR	 Rc R R R R2R Rs R R R R&
/v 
/& 
/ 
/ 
/ 
/BB&B B 	B
 B B 
B B B B B Br+   