
    soj                          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mZmZ  G d
 de          Ze                                 dS )    )*N)	BaseModel)
BountyLoan)	GroupChat)User)BountyLoanSource)
GameStatus)get_elapsed_hoursget_remaining_durationget_datetime_in_future_hoursc                      e Zd ZU dZ eeddd          Zeez  ed<    eeddd          Zeez  ed<    e	            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ddddd          ZdZ G d d          ZdedefdZeded
edefd            Zeded
edefd            Zeded
edd fd            Z ededeeffd            Z!ededd fd            Z"dedefd Z#defd!Z$de%dz  fd"Z&d&d#Z'd'dedefd$Z(de)fd%Z*dS )(Plunderz
    Plunder class
    plunder_challengersCASCADE)backref	on_delete	on_update
challengerplunder_opponentsopponentwin_probability)defaultstatusTplunder_groups_chatsRESTRICT)nullr   r   r   
group_chat)r   
message_idbellyselfrevenge_attack_plundersNc                       e Zd ZdZdS )Plunder.MetaplunderN)__name__
__module____qualname__db_table     /app/src/model/Plunder.pyMetar#   4   s        r*   r,   userreturnc                     |j         | j        j         k    r!t          t          | j                            S d| j        z
  S )zz
        Get the win probability given a user

        :param user: The user
        :return: The win probability
        d   )idr   floatstrr   r    r-   s     r+   get_win_probabilityzPlunder.get_win_probability7   s=     7do(((T122333---r*   c                    t                                                                          t           j        | k    t           j        |k    z                                            t                                                                          t           j        | k    t           j        |                                k    z                                            z   S )z
        Get the total amount of wins or losses a user has
        param user: The user
        param status: The status (won or lost)
        return: The total amount of wins or losses
        )r   selectwherer   r   countr   get_opposite_statusr-   r   s     r+   get_total_win_or_losszPlunder.get_total_win_or_lossD   s     IIVXXUG&$.7>V3KLMMUWWiiVXXUG$,6C]C]C_C_1_`aaUWW		
r*   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   r7   fnSUMr   r8   r   r   scalarr   r:   r;   s     r+   get_total_belly_won_or_lostz#Plunder.get_total_belly_won_or_lostX   s     NN266'-001177#t+&0HI  &((NN266'-001177!T)gn@Z@Z@\@\.\]  &(( 	r*   c                    t                                                                          t           j        | k    t           j        |k    z                                t           j                                                                                  }t                                                                          t           j	        | k    t           j        |
                                k    z                                t           j                                                                                  }t          ||d           S )z
        Get the plunder with the max belly won or lost
        param user: The user
        param status: The status (won or lost)
        return: The plunder
        c                     | j         S N)r   )fs    r+   <lambda>z-Plunder.get_max_won_or_lost.<locals>.<lambda>   s
    UVU\ r*   )key)r   r7   r8   r   r   order_byr   descfirstr   r:   max)r-   r   max_plunder_as_challengermax_plunder_as_opponents       r+   get_max_won_or_lostzPlunder.get_max_won_or_lostk   s     IIVXXUG&$.7>V3KLMMXgm((**++UWW 	" IIVXXUG$,6C]C]C_C_1_`aaXgm((**++UWW 	  ,.EK\K\]]]]r*   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 plundered user and the amount of plunders
        param user: The user
        return: The most plundered user and the amount of plunders
        r9   )r   r7   r   r>   COUNTaliasr8   r   group_byrH   SQLrI   rJ   r9   )r-   !most_plundered_user_as_challengeramount_as_challengermost_plundered_user_as_opponentamount_as_opponents        r+   get_most_plundered_userzPlunder.get_most_plundered_user   sW    IIVG$bhhw/?&@&@&F&Fw&O&OPPU7%-..Xg&''Xc'll''))**UWW 	*  AF IIVG&1C(D(D(J(J7(S(STTU7#t+,,Xg())Xc'll''))**UWW 	( =B 4442=?QQQ09;OOOr*   c                 <   t                                                                          t           j        | k    t           j        t
          j        k    z                                t           j        	                                          
                                S )zp
        Get the plunder with the max sentence
        param user: The user
        return: The plunder
        )r   r7   r8   r   r   r	   LOSTrH   sentence_durationrI   rJ   )r-   s    r+   get_max_sentencezPlunder.get_max_sentence   sa     IIVXXUW'4/GNjo4UVXXXg/446677UWW	
r*   c                 4    | j         |k    r| j         n| j        S )zb
        Get the other opponent
        param user: The user
        return: The opponent
        )r   r   r4   s     r+   get_opponentzPlunder.get_opponent   s     #'/T"9"9tt}Lr*   c                 *    t          | j                  S )zT
        Get the status of the game
        :return: The status of the game
        )r	   r   r    s    r+   
get_statuszPlunder.get_status   s     $+&&&r*   c                     t          j        t           j        t          j        k    t           j        | j        k              S )z
        Get the loan
        :return: The loan, or None if no loan was created for this plunder (e.g. the loser
        had combat-loss immunity, so nothing was owed)
        )r   get_or_nonesourcer   PLUNDERexternal_idr1   r`   s    r+   get_loanzPlunder.get_loan   s6     %!1!99:;QUYU\;\
 
 	
r*   c                 R    t                               t           j        | k              S )zN
        Get the revenge plunder
        :return: The revenge plunder
        )r   rc   in_revenge_to_plunderr`   s    r+   get_revenge_plunderzPlunder.get_revenge_plunder   s      ""7#@D#HIIIr*   c                     | j         |k    rdS t          | j                  t          j                                        k    rdS | j        dS |                                 dS dS )a  
        Check if a plunder can be revenged.
        User can revenge a plunder if they were attacked less than x time ago and have not
        already revenged that plunder
        :param user: The user
        :return: True if the user can revenge the plunder, False otherwise
        FNT)r   r
   dateEnv$FIGHT_PLUNDER_REVENGE_DURATION_HOURSget_intri   rj   r4   s     r+   can_revengezPlunder.can_revenge   so     ?d""5 TY''#*R*Z*Z*\*\\\5 %15 ##%%15tr*   c                 ~    t          t          t          j                                        | j                            S )zd
        Get the remaining duration for the revenge
        :return: The remaining duration
        )r   r   rm   rn   ro   rl   r`   s    r+   get_revenge_remaining_durationz&Plunder.get_revenge_remaining_duration   s9     &(8@@BBDI 
 
 	
r*   )r.   r   rD   )+r%   r&   r'   __doc__ForeignKeyFieldr   r   __annotations__r   
FloatFieldr   r2   SmallIntegerFieldr	   IN_PROGRESSr   boolr   r   IntegerFieldr   intBigIntegerFieldr   r[   ri   r!   r,   r5   staticmethodr<   rA   rN   rX   r\   r^   ra   r   rg   rj   rp   r3   rr   r)   r*   r+   r   r      s          *9+yI* * *J&    (7)Y)( ( (Hd_$    +5*,,OUZ'666'8'8AW'X'X'XFD$$XXX.=o&/ / /J	O+    &2\t%<%<%<Jl"<<<#2?#=#=#=E3 ===$$///+O)   #       . . . . . . 
D 
* 
 
 
 
 \
& $ 
 s    \$ ^$ ^
 ^y ^ ^ ^ \^: Pd Pc{ P P P \P@ 
t 
	 
 
 
 \
 	M 	M$ 	M 	M 	M 	M'J ' ' ' '	
*t+ 	
 	
 	
 	
J J J J      6

 

 

 

 

 

 

r*   r   )peeweeresources.EnvironmentEnvironmentrm   src.model.BaseModelr   src.model.BountyLoanr   src.model.GroupChatr   src.model.Userr    src.model.enums.BountyLoanSourcer   src.model.enums.GameStatusr	   src.service.date_servicer
   r   r   r   create_tabler)   r*   r+   <module>r      s       # # # # # # ) ) ) ) ) ) + + + + + + ) ) ) ) ) )       = = = = = = 1 1 1 1 1 1         u
 u
 u
 u
 u
i u
 u
 u
p        r*   