
    soj3=                        d dl Z d dl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 d d
lmZ d dlmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0 	 	 	 	 	 d)dedede1dedede1de1de2fdZ3dedeegfdZ4dededefdZ5dededefdZ6de7defdZ8dededefdZ9	 	 	 d*d!ej:        deded"e1d#e1d$e;ddfd%Z<d&ed!ej:        dededdf
d'Z=dedede1fd(Z>dS )+    N)DoesNotExist)Update)ContextTypes)Game)User)Emoji)GameOutcome)GuessOrLife
PlayerType
PlayerInfo)SupabaseRest)Terminology)	save_gameget_players'get_guess_game_users_to_send_message_toset_user_private_screenend_gameget_textend_text_based_gameget_global_time_based_text#get_global_text_challenger_finishedend_global_game_playertimeout_opponent_guess_gameget_winner_loser_textget_global_share_keyboard)full_message_sendescape_valid_markdown_chars)get_current_languageset_current_languageFgameguess_or_lifeis_finishedplayer_typeoutcomeis_for_groupis_for_new_lifereturnc                 <   t          |           \  }}|t          j        u r|n|}	|                                 }
g }t          D ]}|                                 r||ur|                    ||p||u           }t          |          }d}|s't          j        |	                    |          j
        z  nd}|t          j        u r|n|}|||u s|rt          j                            |                    |          |                    |                    }||u r#t          j                            |d||          }nt          j                            |                                |d||          }nn|r3t          j                            |                                |          }n9t          j                            |                                ||dnd||          }|                    |           |t          j        u r|                                 |sd                    |          }|                                 rA|dt1          | |	          z   z  }|                     |	          r|
r|t5          | d          z  }|                                rR|sP|sN|                     |	          r|
s7|dt          j                            |                                           z   z  }|S d                    |          dz   }t=          | |||j        |	          S )
a  
    Get the specific text
    :param game: The game object
    :param guess_or_life: The guess or life object
    :param is_finished: If the game is finished
    :param player_type: The player type
    :param outcome: The outcome if the game is finished
    :param is_for_group: If the text is for the group message update
    :param is_for_new_life: If the text is for a new life
    :return: The specific text
    )show_guessed_letters N
z


F)should_add_already_guessed_textz

)game_outcometerminologytext_to_add_before_footer) r   r   
CHALLENGERchallenger_has_finished	is_globalget_word_with_blanksr   r   
BLUE_HEARTget_player_infolivesphrases)GUESS_OR_LIFE_GAME_REMAINING_USED_LETTERSformatget_remaining_lettersget_used_letters_formattedGUESS_OR_LIFE_GAME_WORD_LIVES"GUESS_OR_LIFE_GAME_NAME_WORD_LIVESget_markdown_nameGUESS_OR_LIFE_GAME_NAME_WORDappendOPPONENTreversejoinr   is_challengerr   can_issue_life/GUESS_GAME_INPUT_CAPTION_SECONDS_TO_NEXT_LIFE_2get_seconds_for_every_hintr   r.   )r    r!   r"   r#   r$   r%   r&   
challengeropponentplayerr1   ot_text_listptword_with_blanksadditionhearts_textuserpt_textot_texttext_before_footers                       //app/src/chat/common/screens/screen_game_gol.pyget_specific_textrU   $   s   * 't,,J&**???ZZXF"::<<L /% /%>> 	+ 5 5(==k&FR;5F > 
 
 77GHH  U = =b A A GGG 	
  :#888zzh"k(9(9[(9HOO33B7788<< H [  !?FF$dK  "DKK**,,.>kS[   !>EE**,,.>  "DKK**,,$(0TTb  	G$$$$ j)))  ---->> 	t8vFFFFG!!&)) .E >%   
 ((**		 $	 ''//		 5L		 v W ^ ^//11! !  G   \22V;!-"4       c           	         | j         t          j        t          j                                        dt          j        |                                           t          j	        |                                                     }t          |          }t          | |                                d           |                                 r$t          | |                                d           ||fS t          t          j        | j                             |                                 r&t          t          j        | j                            ndfS )zO
    Get the board
    :param game: The game object
    :return: The board
    NT)max_lenonly_lettersmin_unique_charactersmax_unique_characters)hint_was_issuedis_opponent_board)boardr   get_random_terminologyEnvGUESS_OR_LIFE_MAX_CHARACTERSget_intr
   'get_min_unique_characters_by_difficultyget_difficulty'get_max_unique_characters_by_difficultyr   get_as_json_stringr2   get_board_object_from_jsonjsonloadsopponent_board)r    random_terminologyr_   s      rT   
get_boardsrm      s7    z*6*M4<<>>"-"U##%%# # #."U##%%# #	+
 	+
 	+
 .//$0022DIIII>> 	PdE4466$OOOOe|%dj&<&<==HLHXHX	b	#DJt/B$C$C	D	D	D^b rV   rP   c                     t          |           \  }}|                     |          s|                                 s|n|S )zw
    Get the player board
    :param game: The game object
    :param user: The user object
    :return: The board
    )rm   rD   r2   r    rP   challenger_boardrk   s       rT   get_player_boardrq      sF     (2$'7'7$n#11$77ct~~?O?OcUccrV   c                 X    t          |           \  }}|                     |          r|n|S )zv
    Get the other board
    :param game: The game object
    :param user: The user object
    :return: The board
    )rm   rD   ro   s       rT   get_other_boardrs      s5     (2$'7'7$n!//55K>>;KKrV   	json_dictc                     |                      d          }t          di |}|                      d          }t          di |}|                      d          }t          di |}t          dd|i| ||dS )zh
    Get the board
    :param json_dict: The dictionary containing the board
    :return: The board
    r.   challenger_infoopponent_info)rv   rw    )popr   r   r
   )rt   	term_dicttermchallenger_info_dictrv   opponent_info_dictrw   s          rT   rh   rh      s     m,,I#00i00D %==):;;","D"D/C"D"DO"77 * @ @-? @ @M   %7FVc    rV   c                 b    t          |           \  }}||k    rt          j        S t          j        S )z|
    Get the player type
    :param game: The game object
    :param user: The user object
    :return: The player type
    )r   r   r0   rA   )r    rP   rH   rI   s       rT   get_player_typer      s3     't,,JT$$rV   Tcontextshould_send_to_all_playersschedule_next_sendhint_wait_secondsc           	      n  K   ||n|                                 }t          |||o|                                 |           d{V }t          ||          }|D ]}t	          ||t          ||          d          }	|                                rN|rt          j        nt          j	        }
t          j
                            |	|
                    |                    }n|	}| j                            t          | ||j                             | j                            t!          ||                     |sdS t#          j        |           d{V  	 t'          j        |j                  }n# t,          $ r Y dS w xY w|                                rdS |                    |          rt3          | |           d{V rdS t          ||          }|                    |          r!|                    t8          j                  rdS t=          ||          sdS t?          | ||           d{V  dS )a  
    Issue lives to the players
    :param context: The context
    :param game: The game
    :param user: The user to send the message to
    :param should_send_to_all_players: If the image should be sent to all players
    :param schedule_next_send: If the next send should be scheduled
    :param hint_wait_seconds: How many seconds to wait before issuing next hint. If none, the game default is used
    NT)r#   r&   )chat_id) rG   r   r2   rq   rU   r   rE   r7   /GUESS_GAME_INPUT_CAPTION_SECONDS_TO_NEXT_LIFE_1rF    GUESS_OR_LIFE_GAME_PRIVATE_RECAPr9   applicationcreate_taskr   
tg_user_idr   asynciosleepr   	get_by_ididr   r"   is_opponentr   rD   player_has_guessedr   r0   issue_life_if_possiblerun_game)r   r    rP   r   r   r   usersr_   uspecific_textremaining_time_textrR   s               rT   r   r      s     & /:@_@_@a@a  :	#	<DNN,<,<(<	       E T4((E  J J)%_T1%=%=t
 
 
 !! 	$ .MGGL  
 >EE#**+<== GG
 $G''(9'7TUT`(a(a(abbb 	''(?4(H(HIIII 
-)
*
********^DG,,      (CGT(R(R"R"R"R"R"R"R )$55E $ E$<$<Z=R$S$S  "$-- 
7D$
'
''''''''''s   E2 2
F ?F updatec           
      L  K   	 | j         j        }n# t          $ r Y dS w xY wt          ||          }t	          |          dk    rdS |                                sdS t          ||          }|                    ||          rdS |                    |          j	        dk    rdS |
                    ||           t          ||                                |                    |                     t          |||          }|                    |          s|                    |           t          ||                                |                    |                     t          |||          }t"          j                            t"          j        |          }t+          |||            d{V  dS |                    |t/          ||                    s|t0          j        u r|                    |          rpt7          ||           d{V  t          |||          }t"          j                            |          }t+          ||| t;          ||                     d{V  dS t"          j                            t"          j        |          }t+          |||            d{V  dS t?          |          \  }	}
|                     |          }tC          ||||            d{V  d|_"        ||
k    r|	n|
}tG                      }tI          |%                                           t          ||d	|
          }tM          ||          \  }}tI          |%                                           t          ||d	t          ||          
          }tM          ||          \  }}d}|j'        ?tI          |j'        j(        %                                           t          ||d	|d	          }tI          |           tS          ||||||||           d{V  dS )z
    Validate the answer
    :param update: The update object
    :param context: The context object
    :param game: The game object
    :param user: The user object
    :return: None
    N   r   r]   )r#   )r   )r   keyboardFT)r"   r#   )r"   r$   r%   )*effective_messagetextAttributeErrorrq   lenisalphar   has_used_letterr5   r6   add_used_letterr   rg   r   rU   is_letter_is_in_wordremove_lifer7   r   r9   GUESS_OR_LIFE_GAME_WRONG_LETTERr   r"   rs   r   r0   r   r   GUESS_GAME_CORRECT_ANSWERr   !GUESS_OR_LIFE_GAME_CORRECT_LETTERr   get_outcomer   should_update_modelr   r   get_languager   
group_chatgroupr   )r   r   r    rP   letterr_   r#   r   rR   rH   rI   r$   loserplayer_languagespecific_winner_textwinner_text_specific_loser_text
loser_text
group_texts                       rT   validate_answerr   H  s     ).    *$55E 6{{Q >> -dD99K[&11  [))/144 
+v...dE,,..$BRBRSWBXBXYYYY%dE{KKKM %%f-- +&&&$0022dFVFVW[F\F\]]]])$;OOO:AA3]
 
  @@@@@@@@@@ [/$*E*EFF *///E4L4L[4Y4Y/($777777777-dE{SSSM7>>}MMG#27DAA	          F:AA5}
 
  @@@@@@@@@@ 't,,J ,,[99G
4'&
9
9
99999999$D(**JJE +,,O**,,---,e;   +41EFFNK++--...+e?43O3O   *$0CDDMAzJ"T_2??AABBB&%T7
 
 

 )))
wk5*j          s    
c                     t          | |          }|                                sdS |                                 t          | |                                |                     |          d           dS )z
    Issue a life if possible, saving the game
    :param game: The game
    :param user: The user
    :return: If a life was issued
    FT)r^   r\   )rq   rE   
issue_lifer   rg   r   )r    rP   r_   s      rT   r   r     s     *$55E!! u	  ""**400	    4rV   )FNNFF)TTN)?r   ri   peeweer   src.tg_compatr   src.tg_compat.extr   resources.EnvironmentEnvironmentra   resources.phrasesr7   src.model.Gamer   src.model.Userr   src.model.enums.Emojir   src.model.game.GameOutcomer	   &src.model.game.guessorlife.GuessOrLifer
   r   r   src.model.wiki.SupabaseRestr   src.model.wiki.Terminologyr   src.service.game_servicer   r   r   r   r   r   r   r   r   r   r   r   r   src.service.message_servicer   r   src.service.language_servicer   r   boolstrrU   rm   rq   rs   dictrh   r   DEFAULT_TYPEintr   r   r   rx   rV   rT   <module>r      s                       * * * * * * # # # # # # # # # # # #             ' ' ' ' ' ' 2 2 2 2 2 2 V V V V V V V V V V 4 4 4 4 4 4 2 2 2 2 2 2                              W V V V V V V V S S S S S S S S "!o o
oo o 	o
 o o o 	o o o odT {K8    @	d4 	dt 	d 	d 	d 	d 	d	L$ 	Ld 	L{ 	L 	L 	L 	L$ ;    ,$ d z    ( (,#!Q( Q(&Q(
Q( Q( !%	Q(
 Q( Q( 
Q( Q( Q( Q(hqq)6q>BqJNq	q q q qh T d      rV   