
    sojO                        d dl Z d dlZd dl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ZmZ d dlm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mZ d dlm Z  d dl!m"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,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZC d dlDmEZE d d lFmGZG d d!lHmIZI d d"lJmKZK d d#lLmMZMmNZN d d$lOmPZPmQZQmRZR d d%lSmTZT d d&lUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\ d d'l]m^Z^ d d(l_m`Z`maZambZb d d)lcmdZd d d*lemfZf d dlgmhc miZi d d+ljmkZk d d,llmmZn d d-lUmoZo d d.lpmqZqmrZr d d/lsmtZt d d0lumvZv d d1lgmwZwmxZxmyZymzZz d d2l{m|Z|  G d3 d4e|          Z}	 	 	 dNd6ed7ej~        d8ed9eEd:ed;ed<ed=dfd>Zd:ed8ed=e5e5e5e5gfd?Zd7ej~        d:ed@ekd6ed8ed=eeeE                  fdAZd:ed=ekfdBZd:ed=ekfdCZdDed=efdEZd:edFekd8edGekd=ef
dHZd6ed=e-fdIZd6ed7ej~        d:edJedKedLed=dfdMZdS )O    N)datetime	timedelta)TupleCallable)DoesNotExist)Update)
RetryAfter
BadRequest)ContextTypes)phrasesEnvironment)Game)	GroupChat)User)ContextDataKey)Emoji)
GameStatus)GameTurnNotificationGameOutcomeNotification)ReservedKeyboardKeys)
SavedMedia)SavedMediaType)Screen)DevilFruitAbilityType)IncomeTaxEventType)CommonChatException)	GameBoard)GameOutcome)GameTurn)GameType)PunkRecords)Shambles)WhosWho)ContextDataValue)Keyboard)	Character)SupabaseRest)Terminology)add_or_remove_bountyvalidate_amount)convert_seconds_to_durationget_remaining_durationget_elapsed_duration)get_ability_adjusted_datetime)mention_markdown_userdelete_messagefull_media_sendget_message_urlfull_message_send"full_message_or_media_send_or_editget_deeplink)send_notification)get_bot_context_dataset_bot_context_data'get_random_user_context_inner_query_key)get_outcome_text)get_belly_formatted)RussianRoulette)RussianRouletteChamberStatus)get_message_source)get_current_languageset_current_language)MessageSource)SavedMediaName)edit_other_player_messageenqueue_auto_moveget_global_share_keyboardend_global_game_player)StrEnumc                       e Zd ZdZdZdZdS )GameRRReservedKeysz+
    The reserved keys for this screen
    abN)__name__
__module____qualname____doc__GAME_IDPOSITION     ./app/src/chat/common/screens/screen_game_rr.pyrI   rI   R   s$          GHHHrS   rI   Fupdatecontextuserinbound_keyboardgameis_auto_moveedit_message_idreturnc                 R  K   t          j        | |||           d{V }|dS t          ||           d{V }|d         }|d         }	|d         }
|d         }t          j                    |_        |j        t          |           k    r|                                s<|	                    ||          s&t          |t          j        | dd           d{V  dS |j        t          j                 \  }}|j        |         |         t"          j        k    r&t          |t          j        | dd           d{V  dS |                                r&t          |t          j        | dd           d{V  dS t"          j        |j        |         |<   |s8t          |t-          |                                          | d           d{V  |                    ||           |                                r+t3          |||                    |          	           d{V  |                    |	
          r|                    |                    |          |	          }t          j        |||| |                                s|r|                    |          nd           d{V  d|_        |}	 t?          |tA          ||||	          | tC          |||| |          |"                                d|dd	  	         d{V }|r|j#        }n=# tH          tJ          f$ r)}tM          j'        d|j#         d|            Y d}~nd}~ww xY w|                                r|j(        |                    |          }tS                      }tU          |j+        ,                                           tA          ||
|j+        |          }tU          |j(        ,                                           tA          |||j(        |
          }tU          |           |j-        .                    t_          ||||||tC          |||	| |                               dS |j        t          |           k    r(|                                s|0                                 t?          |tA          ||||	          | tC          |||| |          |"                                tb          j2        |d           d{V }|                                r|j(        tS                      }tU          |j+        ,                                           tA          ||
|j+        |          }tU          |j(        ,                                           tA          |||j(        |
          }tU          |           |j-        .                    t_          ||||r|j#        nd||tC          |||	| |                               |                    ||           |3                                 |                                s!t          j4        |||j5                   d{V  |                                sM|rM|j-        .                    tm          | |||7                    |          tp          |j#                             dS dS dS )a  
    Manage the Russian Roulette 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
    Nr            T)rU   answer_callback
show_alert)rU   ra   )is_challengerother_board)rU   send_outcome_to_userF)captionrU   keyboardauthorized_usersedit_only_caption_and_keyboardr[   ignore_bad_request_exceptionshould_log_ignored_exceptionz1Failed to send auto-move result message for game z: )rg   rU   rh   ri   saved_media_namer[   rk   )9game_servicevalidate_gameget_all_boardsr   nowlast_interaction_datescreen
get_screen	is_globalis_user_turnr3   r   GAME_NOT_YOUR_TURNinforI   rQ   cylinderRRChamberStatusFIRED+RUSSIAN_ROULETTE_GAME_CHAMBER_ALREADY_FIREDis_finishedGAME_GLOBAL_WAIT_FOR_OPPONENTget_choice_textbullet_is_fired	set_boardrF   rc   get_outcomeend_gameget_other_playershould_update_modelr1   get_specific_textget_outbound_keyboardget_playersidr	   r
   loggingwarningopponentr?   r@   
challengerget_languageapplicationcreate_taskrC   set_turnrB   GAME_RUSSIAN_ROULETTEsavenotify_game_turn	game_turnrD   get_user_turn	auto_move)rU   rV   rW   rX   rY   rZ   r[   
all_boardsboardre   challenger_boardopponent_boardxygame_outcomesent_message_idmessageeother_playerplayer_languagechallenger_textopponent_texts                         rT   manager   [   s     . +FG=MtTTTTTTTTD|.<T4.H.H(H(H(H(H(H(HJ']E#-a=K(21&0mN!)D *V"4"444~~ 	(:(:4(F(F 	#* $          F$%7%@A1>!Q?#888#C $          F  	#5 $          F.4q!  	# 5 5 7 788 $	          	ud### 	`($dFXFXY]F^F^__________ [11 @$)$5$5d6H6H6N6NP[$\$\#040@0@[L[%%d+++W[
 
 
 	
 	
 	
 	
 	
 	
 	
 $) 
 *	%4)$t[II.weVTRR!%!1!1!3!3/3 /-1-1
& 
& 
& 
  
  
  
  
  
 G  -")*J' 	 	 	ORDGRRqRR       	 >> 	 90066L244O !=!=!?!?@@@/& O !!;!;!=!=>>>-ndm5E M !111++)##!)'4fl[[ 
 
 
 	*V"4"444T^^=M=M4 -!$t[AA&weVTJJ))++'='%)	 	 	 	 	 	 	 	 	G ~~ 
DM5.00T_99;;<<<+D2BDOUcddT]7799:::)$O_``_---''%%/

4%gt[&$OO 
	
 
	
 
	
 	NN5$IIKKK>> L+GT5?KKKKKKKKK  
7 
''u':':4'@'@)WZ 	
 	
 	
 	
 	

 
 
 
s   3AL MMMc                 f  K   |                                  st          |           }d}nf|                     |          rt          |           nt          |           }|                     |          rt          |           nt          |           }|                     |          r|}|}n|}|}||||fS )z
    Get all boards
    :param game: The game object
    :param user: The user object
    :return: Current board, other board, challenger board, opponent board
    N)ru   get_board_challengerrc   get_board_opponent)rY   rW   r   re   r   r   s         rT   rp   rp   +  s       >> 	
$T** +/*<*<T*B*B` &&&HZ[_H`H` 	 )-(:(:4(@(@`t$$$FZ[_F`F` 	 $  $& 		 rS   russian_roulettec                 `   g }t          |j                  D ]W\  }}g }t          |          D ])\  }	}
|                    ||	          r-|                    t	          t
          j                             It          j        |j	        t          j
        ||	fi}||j        k    r6|	|j        k    r+t          j                                        rt
          j        }nL|
t"          j        k    rt
          j        }n/|j        |k    r|j        |	k    rt
          j        }nt
          j        }|                    t	          ||t+          |                               +|                    |           Y||                                r%|                    |          rt1          | |          ng z  }|S )z
    Get the outbound keyboard
    :param context: The context
    :param game: The game object
    :param russian_roulette: The russian roulette object
    :param update: The update
    :param user: The user
    :return: The outbound keyboard
    rx   rs   )	enumeratery   is_chamber_centerappendr%   r   CENTER_CHAMBERrI   rP   r   rQ   bullet_xbullet_yEnv%RUSSIAN_ROULETTE_SHOW_BULLET_LOCATIONget_boolFIRED_BULLET_CHAMBERrz   	NOT_FIREDNOT_FIRED_CHAMBERFIRED_EMPTY_CHAMBERrt   challenger_has_finishedrc   rE   )rV   rY   r   rU   rW   outbound_keyboard	row_indexrowkeyboard_linechamber_indexchamberbutton_infoemojis                rT   r   r   K  s   " /1#$4$=>> 0 0	3(*&/nn 	c 	c"M711)]KK c$$Xe.B%C%CDDDD '.&/)]1K
 !1!:::%)9)BBBAJJLL C "6EE 999!3EE )1Y>>,5FF % : % 9$$Xe+jY_N`N`%a%a%abbbb  ////''))	.2.@.@.F.F	!'4000 rS   c                     | j         =t                      }|                                | _         |                                  |S t	          j        | j                   }t          di |S )zZ
    Get the challenger board
    :param game: The game object
    :return: The board
    NrR   )r   r<   get_as_json_stringr   jsonloadsrY   r   russian_roulette_dicts      rT   r   r     se     z*,,%88::
		 Jtz22332333rS   c                     | j         It          t          j                  }|                                | _         |                                  |S t          j        | j                   }t          di |S )zX
    Get the opponent board
    :param game: The game object
    :return: The board
    N)r   rR   )r   r<   r   OPPONENTr   r   r   r   r   s      rT   r   r     sp     "*X5FGGG.AACC		 Jt':;;332333rS   bullet_shotc                 6    | rt           j        S t           j        S )zb
    Get the choice text
    :param bullet_shot: If the bullet was shot
    :return: The text
    )r   !RUSSIAN_ROULETTE_GAME_BULLET_SHOT%RUSSIAN_ROULETTE_GAME_BULLET_NOT_SHOT)r   s    rT   r   r     s      98888rS   r   re   c           	          |                      |          }|                     |          }d}|                                 rL|t          j                            t          j        |                                |j        |j	        z  dz
            z  }| 
                                s|
                                r|t          j        z  }| j        |dt          j                            |                     |                                          |                                |j        |j	        z  dz
            z   z  }| 
                                s|
                                r|t          j        z  }| 
                                r|dz  }|r|
                                p|o|duo|
                                }|r|
                                p|o|duo|
                                }t          j        | | 
                                p|
                                |                    |                      |          |          |                                 s|                    |           n||||          S )z
    Get the text
    :param game: The game object
    :param board: The russian roulette board
    :param user: The user
    :param other_board: The other board
    :return: The text
     r_   N
z

rd   )text_to_add_before_footerr   opponent_has_finished)rc   is_opponentru   r   GAME_POINTSformat	TEXT_YOURget_fired_correct_countrowscolumnsr}   GAME_POINTS_FINISHEDr   r   get_markdown_mentionrn   get_textr   r   )	rY   r   rW   re   rc   r   
extra_textr   r   s	            rT   r   r     s    &&t,,M""4((KJ~~ %g)00u<<>>emA[_`@`
 
 	

  	7!2!2!4!4 	7'66J =$$!4!;!;%%d++@@BB3355em+q0" "  J
 !! ;[%<%<%>%> ;g::
!! %f$
,D1B1B1D1D M4/MK4K4K4M4M  )@U->->-@-@ O+T1Ok6M6M6O6O   1e//11$,,T22LL)-)9)9CD!!!t", 73   rS   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>   rA   GROUPr   GRP_RUSSIAN_ROULETTE_GAMEPVT_RUSSIAN_ROULETTE_GAME)rU   s    rT   rt   rt     s,     &!!]%888//++rS   player
message_idprevious_gamec           	         K   |                                 r|                    |          rt          |          }t          |          }nt          |          }t          |          }|                                |                                k    rdS t          j        t          j                              	                    t
          j
        |j
        k    t
          j        |j        k    z                                            }|dk    rdS t          dt          j        |j
        t          j        |j        |j        fit'          |                     }	t)          j        |j
                  }t-          | |||	|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)rr   r   r   r   T)rZ   r[   )ru   rc   r   r   r   r   rU   r   rq   wherer   rr   executer%   rI   rP   rQ   r   r   rt   r   	get_by_idr   )
rU   rV   rY   r   r   r   r   previous_boardclaimed_rowsmock_keyboards
             rT   r   r     s     & >> ;t11&99 ;!5d!;!;-m<<!3D!9!9+M:: !!^%F%F%H%HHH 	(,..999	47"t'ATE_'_`	a	a	 
 q '
&'%.%.)I
 &!!  M ^FI&&F
4Yc           rS   )NFN)asyncior   r   r   r   typingr   r   peeweer   src.tg_compatr   src.tg_compat.errorr	   r
   src.tg_compat.extr   	constantsc	resourcesr   r   r   src.model.Gamer   src.model.GroupChatr   src.model.Userr   src.model.enums.ContextDataKeyr   src.model.enums.Emojir   src.model.enums.GameStatusr   src.model.enums.Notificationr   r   $src.model.enums.ReservedKeyboardKeysr   src.model.enums.SavedMediar   src.model.enums.SavedMediaTyper   src.model.enums.Screenr   1src.model.enums.devil_fruit.DevilFruitAbilityTyper   -src.model.enums.income_tax.IncomeTaxEventTyper   src.model.error.CommonChatErrorr   src.model.game.GameBoardr   src.model.game.GameOutcomer   src.model.game.GameTurnr   src.model.game.GameTyper    &src.model.game.punkrecords.PunkRecordsr!    src.model.game.shambles.Shamblesr"   src.model.game.whoswho.WhosWhor#   src.model.pojo.ContextDataValuer$   src.model.pojo.Keyboardr%   src.model.wiki.Characterr&   src.model.wiki.SupabaseRestr'   src.model.wiki.Terminologyr(   src.service.bounty_servicer)   r*   src.service.date_servicer+   r,   r-   src.service.devil_fruit_servicer.   src.service.message_servicer/   r0   r1   r2   r3   r4   r5    src.service.notification_servicer6   src.utils.context_utilsr7   r8   r9   src.utils.phrase_utilsr:   src.utils.string_utilsr;   src.service.game_serviceservicern   .src.model.game.russianroulette.RussianRouletter<   ;src.model.game.russianroulette.RussianRouletteChamberStatusr=   rz   r>   src.service.language_servicer?   r@   src.model.enums.MessageSourcerA   src.model.enums.SavedMediaNamerB   rC   rD   rE   rF   enumrG   rI   DEFAULT_TYPEboolintr   rp   listr   r   r   strr   r   rt   r   rR   rS   rT   <module>r%     s      ( ( ( ( ( ( ( ( " " " " " " " "                   6 6 6 6 6 6 6 6 * * * * * *     < < < < < < < <       ) ) ) ) ) )       9 9 9 9 9 9 ' ' ' ' ' ' 1 1 1 1 1 1 V V V V V V V V E E E E E E 1 1 1 1 1 1 9 9 9 9 9 9 ) ) ) ) ) ) S S S S S S L L L L L L ? ? ? ? ? ? . . . . . . 2 2 2 2 2 2 , , , , , , , , , , , , > > > > > > 5 5 5 5 5 5 2 2 2 2 2 2 < < < < < < , , , , , , . . . . . . 4 4 4 4 4 4 2 2 2 2 2 2 L L L L L L L L         
 J I I I I I                  ? > > > > >         
 4 3 3 3 3 3 6 6 6 6 6 6 / / / / / / / / / J J J J J J      ; : : : : : S S S S S S S S 7 7 7 7 7 7 9 9 9 9 9 9                         M
 M
M
&M
 M
 	M

 M
 M
 M
 
M
 M
 M
 M
`t 4 Y	9V_4`    @:&:
: &: 	:
 : 
$x.: : : :z4t 4 4 4 4 4$4T 4o 4 4 4 4$
9 
9# 
9 
9 
9 
93
3&3.23AP33 3 3 3l
,v 
,& 
, 
, 
, 
,;;&; ; 	;
 ; ; 
; ; ; ; ; ;rS   