
    sojC                        d dl Z d dlZd dlT 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 d dlmZ d dlmZ d dlmZ  G d de	          Z e !                                 	 d dl"m#Z#m$Z% d dlm&Z&  e# e&j'                              Z( e%e()                    ddd                     dS # e*$ r Y dS w xY w)    N)*)phrases)	BaseModel)	GroupChat)User)
GameStatus)SavedMediaName)	GameBoard)GameDifficulty)GameOutcome)GameType)	Character)Terminology)get_belly_formattedc                      e Zd ZU dZ eeddd          Zeez  ed<    eedddd          Zeez  ed	<    e	d
          Z
ee	z  ed<    ed
          Zeez  ed<    ed
          Zeez  ed<    e	ej                  Zee	z  ed<    eedddd          Zeez  ed<    ed
          Zeez  ed<    ed
          Zeez  ed<    ed
          Zeez  ed<    ed
          Zeez  ed<    eej        j                  Zej        ez  ed<    ed
          Z ej        ez  ed<    ed
          Z!ej        ez  ed<    ed
          Z"ej        ez  ed<    ed
          Z#ej        ez  ed<    e$d          Z%e&e$z  ed<    ed
          Z' ed
          Z( ed          Z) G d  d!          Z*d"ed#e&fd$Z+d#e,fd%Z-d#e.fd&Z/d#efd'Z0e1d"eded#efd(            Z2e1d"eded#efd)            Z3e1d"eded#d fd*            Z4e1d"ed#eeffd+            Z5e1d"ed#eeffd,            Z6d#efd-Z7d#efd.Z8d#e9e         fd/Z:d#e&fd0Z;e1d#e9d          fd1            Z<d#efd2Z=d#efd3Z>d"ed#e&fd4Z?d"ed#e&fd5Z@d"ed#efd6ZAd#ed7z  fd8ZBd#e&fd9ZCd#eDfd:ZEd#e&fd;ZFdeGd"ed#d7fd<ZHd#e&fd=ZId#e&fd>ZJd#e&fd?ZKd#eegfd@ZLd#e&fdAZMd#e&fdBZNd#efdCZOd#efdDZPd#eQfdEZRd#efdFZSd7S )GGamez
    Game class
    game_challengersCASCADE)backref	on_delete	on_update
challengergame_opponentsT)r   r   r   nullopponent)r   typeboardopponent_board)defaultstatusgame_groups_chatsRESTRICT)r   r   r   r   
group_chat
message_idchallenger_message_idopponent_message_idwagerlast_interaction_dateglobal_challenger_start_dateglobal_challenger_end_dateglobal_opponent_start_dateglobal_opponent_end_dateFglobal_challenger_has_finishedr   c                       e Zd ZdZdS )	Game.MetagameN)__name__
__module____qualname__db_table     /app/src/model/Game.pyMetar/   A   s        r6   r8   userreturnc                 .    | j         |k    p
| j        |k    S )z
        Check if the user is a player of the game
        :param user: The user
        :return: True if the user is a player, False otherwise
        r   r   selfr9   s     r7   	is_playerzGame.is_playerD   s     $&?$-4*??r6   c                 N    t          | j                                                  S )z^
        Get the SavedMediaName for this GameType
        :return: The SavedMediaName
        )r   r   get_saved_media_namer>   s    r7   rA   zGame.get_saved_media_nameM   s     
 	""77999r6   c                 h    t          j        t          t          | j                                      S )z\
        Get the difficulty level of the game
        :return: The difficulty level
        )r   get_from_total_wagerintstrr'   rB   s    r7   get_difficultyzGame.get_difficultyT   s%     23s4:3G3GHHHr6   c                 N    t          | j                                                  S )zD
        Get the name of the game
        :return: The name
        )r   r   get_namerB   s    r7   rI   zGame.get_name\   s      	""++---r6   c                    |t           j        t           j        fv rt                                                                          t          j        | k    t          j        |k    z                                            t                                                                          t          j	        | k    t          j        |
                                k    z                                            z   S t                                                                          t          j        | k    t          j        |k    z                                            S )z
        Get the total amount of wins or losses or draws a user has
        param user: The user
        param status: The status (won or lost or draw)
        return: The total amount of wins or losses or draws
        )r   WONLOSTr   selectwherer   r    countr   get_opposite_statusr9   r    s     r7   get_total_win_or_loss_or_drawz"Game.get_total_win_or_loss_or_drawd   s     jnjo666%%t$'>4;RXCX&YZZ``bb&&-$+A[A[A]A]2]^__	 vv}}$$do&=$+QWBW%XYY__aaar6   c                 (   t                               t                              t           j                                                t           j        | k    t           j        |k    z                                            t                               t                              t           j                                                t           j	        | k    t           j        |
                                k    z                                            z   S )z
        Get the total amount of belly a user has won or lost
        param user: The user
        param status: The status (won or lost)
        return: The total amount of belly
        )r   rM   fnSUMr'   rN   r   r    scalarr   rP   rQ   s     r7   get_total_belly_won_or_lostz Game.get_total_belly_won_or_lostx   s     KKtz**++11D(T[F-BC  &((KKtz**++11$&4;&:T:T:V:V+VW  &(( 	r6   c                    t                                                                          t           j        | k    t           j        |k    z                                t           j                                                                                  }t                                                                          t           j	        | k    t           j        |
                                k    z                                t           j                                                                                  }||S ||S |j        |j        k    r|S |S )z
        Get the game with the max belly won or lost
        param user: The user
        param status: The status (won or lost)
        return: The game
        )r   rM   rN   r   r    order_byr'   descfirstr   rP   )r9   r    max_game_as_challengermax_game_as_opponents       r7   get_max_won_or_lostzGame.get_max_won_or_lost   s     FFVXXUDOt+v0EFGGXdjoo''((UWW 	 FFVXXUDMT)dkV=W=W=Y=Y.YZ[[Xdjoo''((UWW 	 ")'''))!'*>*DDD))##r6   c                    t                               t           j        t                              t           j                                      d                                        t           j        | k                                  t           j                  	                    t          d                                                                                    }|j        }t                               t           j        t                              t           j                                      d                                        t           j        | k                                  t           j                  	                    t          d                                                                                    }|j        }||k    r	|j        |fS |j        |fS )z
        Get the most challenged user and the amount of games
        param user: The user
        return: The most challenged user and the amount of games
        rO   )r   rM   r   rT   COUNTaliasrN   r   group_byrY   SQLrZ   r[   rO   )r9   "most_challenged_user_as_challengeramount_as_challenger most_challenged_user_as_opponentamount_as_opponents        r7   get_most_challenged_userzGame.get_most_challenged_user   sE    KKrxx'>'>'D'DW'M'MNNU4?d*++Xdm$$Xc'll''))**UWW 	+  BG KK$/)B)B)H)H)Q)QRRU4=D())Xdo&&Xc'll''))**UWW 	) >C"4445>@TTT/:<NNNr6   c                    t                               t           j        t                              t           j                                      d                                        t           j        | k                                  t           j                  	                    t          d                                                                                    }|j        }t                               t           j        t                              t           j                                      d                                        t           j        | k                                  t           j                  	                    t          d                                                                                    }|j        }||k    rt          |j                  |fS t          |j                  |fS )z
        Get the most played game and the amount of games
        param user: The user
        return: The most played game and the amount of games
        rO   )r   rM   r   rT   r`   ra   rN   r   rb   rY   rc   rZ   r[   rO   r   r   )r9   most_played_as_challengerre   most_played_as_opponentrg   s        r7   get_most_played_gamezGame.get_most_played_game   sV    KK	288DI#6#6#<#<W#E#EFFU4?d*++Xdi  Xc'll''))**UWW 	"  9> KK	288DI#6#6#<#<W#E#EFFU4=D())Xdi  Xc'll''))**UWW 	  5:"4445:;;=QQQ/4557IIIr6   c                 *    t          | j                  S )z@
        Get the GameType
        :return: The GameType
        )r   r   rB   s    r7   get_typezGame.get_type   s    
 	"""r6   c                 *    t          | j                  S )zT
        Get the status of the game
        :return: The status of the game
        )r   r    rB   s    r7   
get_statuszGame.get_status   s     $+&&&r6   c                     | j         | j        gS )z>
        Get the players
        :return: The players
        r<   rB   s    r7   get_playerszGame.get_players   s     //r6   c                     | j         duS )zj
        Check if the game is global
        :return: True if the game is global, False otherwise
        N)r)   rB   s    r7   	is_globalzGame.is_global  s     0<<r6   c                  |   t                                                               t           j                            d          t           j                                        z  t           j        t          j        k    z            	                    t           j
                                        t           j                  S )z
        Get all global games that are in progress and an opponent has not being found yet,
        first those of which the challenger has finished, then oldest
        :return: The global games
        F)r   rM   rN   r)   is_nullr+   r    r   IN_PROGRESSrY   r-   rZ   r5   r6   r7   get_global_gameszGame.get_global_games  s     KKMMU5==eDD6>>@@B{j&<<>  X388::D<] 	
r6   c                 *    t          | j                  S )zN
        Get the formatted wager
        :return: The formatted wager
        r   r'   rB   s    r7   get_wager_formattedzGame.get_wager_formatted&  s     #4:...r6   c                 0    t          | j        dz            S )zX
        Get the formatted half wager
        :return: The formatted half wager
           rz   rB   s    r7   get_half_wager_formattedzGame.get_half_wager_formatted.  s     #4:?333r6   c                     || j         k    S )z
        Check if the user is the challenger
        :param user: The user
        :return: True if the user is the challenger, False otherwise
        )r   r=   s     r7   is_challengerzGame.is_challenger6  s     t&&r6   c                 .    |                      |           S )z
        Check if the user is the opponent
        :param user: The user
        :return: True if the user is the opponent, False otherwise
        )r   r=   s     r7   is_opponentzGame.is_opponent?  s     %%d++++r6   c                 H    |                      |          r| j        S | j        S )zf
        Get the other player
        :param user: The user
        :return: The other player
        )r   r   r   r=   s     r7   get_other_playerzGame.get_other_playerH  s*     d## 	!= r6   Nc                     |                                  t          j        u r| j        S |                                  t          j        u r| j        S dS )zH
        Get the winner of the game
        :return: The winner
        N)rp   r   rK   r   rL   r   rB   s    r7   
get_winnerzGame.get_winnerT  sG     ??
..?"??
//= tr6   c                 .    |                                  duS )zp
        Check if the game has a winner
        :return: True if the game has a winner, False otherwise
        N)r   rB   s    r7   
has_winnerzGame.has_winnerb  s       ,,r6   c                 "   |                                  t          j        u rt          j        S |                                  t          j        u rt          j        S |                                  t          j        u rt          j        S t          j        S )zJ
        Get the outcome of the game
        :return: The outcome
        )	rp   r   rK   r   CHALLENGER_WONrL   OPPONENT_WONDRAWNONErB   s    r7   get_outcomezGame.get_outcomej  sm     ??
..--??
//++??
//##r6   c                 N    |                                                                  S )zn
        Check if the game is finished
        :return: True if the game is finished, False otherwise
        )rp   is_finishedrB   s    r7   r   zGame.is_finished{  s        ,,...r6   c                     |                      |          s|                                 s|                                | _        dS |                                | _        dS )z
        Set the board for the user
        :param board: The game board
        :param user: The user
        :return: None
        N)r   rt   get_as_json_stringr   r   )r>   r   r9   s      r7   	set_boardzGame.set_board  s\     d## 	=4>>+;+; 	=1133DJJJ"'":":"<"<Dr6   c                 N    |                                                                  S )zr
        Check if the game is turn based
        :return: True if the game is turn based, False otherwise
        )rn   is_turn_basedrB   s    r7   r   zGame.is_turn_based  s     }},,...r6   c                 N    |                                                                  S )zr
        Check if the game is time based
        :return: True if the game is time based, False otherwise
        )rn   is_guess_basedrB   s    r7   r   zGame.is_guess_based  s     }}--///r6   c                     | j         duS )zv
        Check if the game has an opponent
        :return: True if the game has an opponent, False otherwise
        N)r   rB   s    r7   has_opponentzGame.has_opponent  s     }D((r6   c                    ddl m}  || j        r| j        nt          j                                        | j                  } || j        r| j        nt          j                                        | j                  }||fS )z^
        Get the players time in seconds
        :return: The players time in seconds
        r   )get_remaining_time_in_seconds)start_datetime)src.service.date_servicer   r*   datetimenowr)   r,   r+   )r>   r   challenger_total_time_secondsopponent_total_time_secondss       r7   get_players_time_secondszGame.get_players_time_seconds  s     	KJJJJJ(E(E 2-//&**,,<)
 )
 )
% 'D&C 0---&**,,:'
 '
 '
# -.IIIr6   c                 d    |                                  o| j        dup|                                 S )z|
        Check if the challenger has finished
        :return: True if the challenger has finished, False otherwise
        N)rt   r*   r   rB   s    r7   challenger_has_finishedzGame.challenger_has_finished  s7     ~~ 
+47M4;K;K;M;M	
r6   c                 d    |                                  o| j        dup|                                 S )zx
        Check if the opponent has finished
        :return: True if the opponent has finished, False otherwise
        N)rt   r,   r   rB   s    r7   opponent_has_finishedzGame.opponent_has_finished  s7     ~~ 
)5K9I9I9K9K	
r6   c                     dS )zd
        Get the challenger time in seconds
        :return: The challenger time in seconds
        Nr5   rB   s    r7   get_challenger_time_secondsz Game.get_challenger_time_seconds  s      r6   c                    |                                  xt          j        k    r t          j                                        S 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|                                             )z\
        Get the seconds for every hint
        :return: The seconds for every hint
        zUnsupported game type: )rn   r   GUESS_OR_LIFEEnv GUESS_OR_LIFE_NEW_LIFE_WAIT_TIMEget_intPUNK_RECORDS"PUNK_RECORDS_NEXT_DETAIL_WAIT_TIMESHAMBLESSHAMBLES_NEXT_LEVEL_WAIT_TIMEWHOS_WHOWHOS_WHO_NEXT_LEVEL_WAIT_TIME
ValueErrorrB   s    r7   get_seconds_for_every_hintzGame.get_seconds_for_every_hint  s     mmoo''''';CCEEE&&&&&=EEGGG"""""8@@BBB"""8@@BBB !L4==??!L!LMMMr6   c                     t          j        | j                  }d|v r"|                    d          }t	          di |}n5d|v r"|                    d          }t          di |}nt          d          |S )zT
        Get the terminology from the game
        :return: The terminology
        terminology	characterz*No terminology or character in JSON stringr5   )jsonloadsr   popr   r   r   )r>   	json_dict	term_dictr   	char_dicts        r7   get_terminologyzGame.get_terminology  s     Jtz**	I%%!m44I'2'?'?Y'?'?KKI%%!k22I'0'='=9'='=KKIJJJr6   c                     t           j                            |                                                                 |                                           S )zP
        Get the global item text
        :return: The global item text
        )r   GAME_GLOBAL_ITEM_TEXTformatrn   rI   r{   rB   s    r7   get_global_item_textzGame.get_global_item_text  sD     ,33MMOO$$&&(@(@(B(B
 
 	
r6   )Tr1   r2   r3   __doc__ForeignKeyFieldr   r   __annotations__r   SmallIntegerFieldr   r   	TextFieldr   rF   r   r   AWAITING_SELECTIONr    r   r#   IntegerFieldr$   rE   r%   r&   BigIntegerFieldr'   DateTimeFieldr   r   r(   r)   r*   r+   r,   BooleanFieldr-   boollast_hint_datelast_hint_opponent_datethread_restart_countr8   r?   r	   rA   r   rG   rI   staticmethodrR   rW   r^   rh   rl   rn   rp   listrr   rt   rx   r{   r~   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r5   r6   r7   r   r      s          *9(I* * *J&    (7&)yW[( ( (Hd_$    *;):)E)E)ED(&
&EEE 'YD111E3?111&/iT&:&:&:NC)O:::->->-. . .FJ**    />o#/ / /J	O+    &2\t%<%<%<Jl"<<<0<$0G0G0G3-GGG.:l.E.E.E|+EEE#2?#=#=#=E3 ===?L}!%@ @ @8,}<    GTmY]F^F^F^ ("3m"C^^^DQMW[D\D\D\ 1M A\\\DQMW[D\D\D\ 1M A\\\BO-UYBZBZBZh/-?ZZZ:F,u:U:U:U"D<$7UUU"]---N+m666'<222       @d @t @ @ @ @:n : : : :I I I I I.# . . . . bD b* b b b b \b& $ 
 s    \$ #$$ #$
 #$v #$ #$ #$ \#$J Ot Os O O O \O> J4 JXsO J J J \J>#( # # # #'J ' ' ' '0T$Z 0 0 0 0=4 = = = = 
d6l 
 
 
 \
*/S / / / /4# 4 4 4 4'$ '4 ' ' ' ', , , , , ,
T 
d 
 
 
 
D4K    -D - - - - [        "/T / / / /=y = = = = = =/t / / / /0 0 0 0 0)d ) ) ) )J3* J J J J8
 
 
 
 

t 
 
 
 
S    NC N N N N,    &
c 
 
 
 
 
 
r6   r   )MySQLMigratormigrate)db_objr0   )r    F)unique)+r   r   peeweeresources.EnvironmentEnvironmentr   	resourcesr   src.model.BaseModelr   src.model.GroupChatr   src.model.Userr   src.model.enums.GameStatusr   src.model.enums.SavedMediaNamer	   src.model.game.GameBoardr
   src.model.game.GameDifficultyr   src.model.game.GameOutcomer   src.model.game.GameTyper   src.model.wiki.Characterr   src.model.wiki.Terminologyr   src.utils.string_utilsr   r   create_tableplayhouse.migrater   r   
pw_migrater   get_db	_migrator	add_index	Exceptionr5   r6   r7   <module>r      s         # # # # # #       ) ) ) ) ) ) ) ) ) ) ) )       1 1 1 1 1 1 9 9 9 9 9 9 . . . . . . 8 8 8 8 8 8 2 2 2 2 2 2 , , , , , , . . . . . . 2 2 2 2 2 2 6 6 6 6 6 6y
 y
 y
 y
 y
9 y
 y
 y
x      
		FFFFFFFF******mfmoo..IJFK>>      	 	 	DD	s   AC CC