
    soj>                         d dl Z d dlmZ d dlT 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  G d
 de          Ze                                 dS )    N)Any)*)	BaseModeldb_obj)
GameStatus)CrewChestSpendingReason)CrewLevelUpgradeType)CrewRole)DevilFruitAbilityTypec                      e Zd ZU dZ eej                                                  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j        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                                                  Zeez  e
d<    ed          Zee
d<    ed          Zee
d<    eej                                                  Zeez  e
d<    eej                                        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d          Z%eez  e
d<    ed          Z&eez  e
d<    ed          Z'eez  e
d<   dZ(dZ)dZ*dZ+dZ,dZ-dZ.dZ/ G d d           Z0d!efd"Z1d!efd#Z2dLd$ed%ed!e3fd&Z4d!e3fd'Z5e6de	d!d fd(            Z7e6d)ed!d fd*            Z8d!e9fd+Z:d!e9fd,Z;d!efd-Z<d!efd.Z=d!e3fd/Z>d0e?d!efd1Z@d0e?d!e	fd2ZAd!e	fd3ZBd!e3fd4ZCd!e3eD         fd5ZEd6eDd!e3fd7ZFd!eGfd8ZHd9eGd!efd:ZId; ZJe6dMd<            ZKd!e	fd=ZLd!e	fd>ZMd!e	fd?ZNdMd@ZOe6dMdA            ZPd!e9fdBZQd!e9fdCZRd!e9fdDZSd!e9fdEZTdNdFefdGZUd!efdHZVd!efdIZWdNdJefdKZXdS )OCrewz
    Crew class
    )
max_lengthnameT)defaultcan_accept_new_members	is_active)nulldisband_dater   chest_amounttotal_gained_chest_amountfrom_reset_chest_amount   levelmax_abilitiescan_promote_first_matecan_promote_captainmax_members)r   r   descriptionrequired_bountyallow_view_in_searchallow_join_from_searchFis_fullallow_davy_back_fight_requestauto_accept_davy_back_fightauto_accept_joinNc                       e Zd ZdZdS )	Crew.MetacrewN)__name__
__module____qualname__db_table     /app/src/model/Crew.pyMetar'   5   s        r.   r0   returnc                     ddl m} | j                            t                              |j                                                            S )z\
        Returns the average bounty of the crew
        :return: The average bounty
        r   User)src.model.Userr4   crew_membersselectfnAvgbountyscalarselfr4   s     r/   get_average_bountyzCrew.get_average_bounty8   sG     	(''''' ''t{(;(;<<CCEEEr.   c                     ddl m} | j                            t                              |j                                                            S )zl
        Returns the average location level of the crew
        :return: The average location level
        r   r3   )r5   r4   r6   r7   r8   r9   location_levelr;   r<   s     r/   get_average_location_levelzCrew.get_average_location_levelB   sH     	(''''' ''t/B(C(CDDKKMMMr.   limitpagec                 F   ddl m} |W| j                                                            |j                                                                      ||          S | j                                                            |j                                                  S )z
        Returns the crew members ordered by join date

        :param limit: The number of members to return. If None, returns all members
        :param page: The page to return. If None, returns the first page
        :return: The crew member
        r   r3   )r5   r4   r6   r7   order_bycrew_join_dateascpaginate)r=   rB   rC   r4   s       r/   get_memberszCrew.get_membersL   s     	('''''!((**$-113344$&&  ''))2243F3J3J3L3LMMMr.   c                 J    t          |                                 d           S )zp
        Returns the crew members ordered by Davy Back Fight priority
        :return: The crew members
        c                 .    | j         d u | j         | j        fS )N)crew_davy_back_fight_priorityrF   )xs    r/   <lambda>zDCrew.get_members_order_by_davy_back_fight_priority.<locals>.<lambda>h   s     /47/  r.   )key)sortedrI   r=   s    r/   -get_members_order_by_davy_back_fight_priorityz2Crew.get_members_order_by_davy_back_fight_priority`   s4      
 
 
 	
r.   c                 r    t                               t           j        | z  t           j        dk    z            S )zt
        Returns the crew by name, case-insensitive
        :param name: The name
        :return: The crew
        T)r   get_or_noner   r   )r   s    r/   get_by_name_if_activezCrew.get_by_name_if_activeo   s*     DT^t5K LMMMr.   crew_idc                 t    t                               t           j        | k    t           j        dk    z            S )z{
        Returns the Crew if it exists and is active
        :param crew_id: The crew id
        :return: The crew
        T)r   rT   idr   )rV   s    r/   logical_getzCrew.logical_gety   s+     G!3$8N OPPPr.   c                     ddl m} | j                                                            |j        t          j        k                                              S )zL
        Returns the crew captain
        :return: The crew captain
        r   r3   )	r5   r4   r6   r7   where	crew_roler
   CAPTAINgetr<   s     r/   get_captainzCrew.get_captain   sO     	(''''' ''))//(BR0RSSWWYYYr.   c                     ddl m} | j                                                            |j        t          j        k                                              S )zR
        Returns the crew first mate
        :return: The crew first mate
        r   r3   )	r5   r4   r6   r7   r[   r\   r
   
FIRST_MATErT   r<   s     r/   get_first_matezCrew.get_first_mate   sS     	(''''' $$&&,,T^x?R-RSS__aa	
r.   c                 .    |                                  duS )zn
        Returns True if the crew has a first mate
        :return: True if the crew has a first mate
        N)rb   rQ   s    r/   has_first_matezCrew.has_first_mate   s     ""$$D00r.   c                 X    | j                                                                         S )z`
        Returns the number of crew members
        :return: The number of crew members
        )r6   r7   countrQ   s    r/   get_member_countzCrew.get_member_count   s%      ''))//111r.   c                     ddl m} | j                                                            |j                                                  S )zd
        Returns the crew members ordered by contribution
        :return: The crew members
        r   )CrewMemberChestContribution)%src.model.CrewMemberChestContributionri   crew_member_chest_contributionsr7   rE   amountdesc)r=   ri   s     r/   get_chest_contributionszCrew.get_chest_contributions   sP     	VUUUUU3::<<EE'.3355
 
 	
r.   reasonc                     |t           j        ur&t          j                                        | j        z  S t          j                                        d| j        dz
  z  z  S )zz
        Returns the next powerup price
        :param reason: The reason
        :return: The next powerup price
           r   )r   LEVEL_UPEnvCREW_POWERUP_BASE_PRICEget_intr   )r=   ro   s     r/   get_powerup_pricezCrew.get_powerup_price   sV     0999.66884:EE *2244dj1n8MNNr.   c                 J    ddl m}  ||                     |                    S )z
        Returns the next powerup price formatted
        :param reason: The reason
        :return: The next powerup price formatted
        r   get_belly_formatted)src.utils.string_utilsry   rv   )r=   ro   ry   s      r/   get_powerup_price_formattedz Crew.get_powerup_price_formatted   s5     	?>>>>>""4#9#9&#A#ABBBr.   c                 .    ddl m}  || j                  S )z\
        Returns the crew chest formatted
        :return: The crew chest formatted
        r   rx   )rz   ry   r   r=   ry   s     r/   get_crew_chest_formattedzCrew.get_crew_chest_formatted   s*     	?>>>>>""4#4555r.   c                     ddl m} | j                                                            |j        t          j                                        k              S )z^
        Returns the crew active abilities
        :return: The crew active abilities
        r   CrewAbility)src.model.CrewAbilityr   crew_abilitiesr7   r[   expiration_datedatetimenow)r=   r   s     r/   get_active_abilitieszCrew.get_active_abilities   sW     	655555"))++11'(*;*?*?*A*AA
 
 	
r.   c                 r    t          t          d |                                 D                                 S )zj
        Returns the crew active abilities types
        :return: The crew active abilities types
        c                 6    g | ]}|                                 S r-   )get_ability_type).0crew_abilitys     r/   
<listcomp>z3Crew.get_active_abilities_types.<locals>.<listcomp>   s$    aaa\..00aaar.   )listsetr   rQ   s    r/   get_active_abilities_typeszCrew.get_active_abilities_types   s<     aaTE^E^E`E`aaabb
 
 	
r.   ability_typec                     ddl m} | j                                                            |j        t          j                                        k    |j        |j	        k    z            S )z
        Returns the crew active ability of the given type
        :param ability_type: The ability type
        :return: The crew active ability
        r   r   )
r   r   r   r7   r[   r   r   r   r   value)r=   r   r   s      r/   get_active_abilityzCrew.get_active_ability   sj     	655555"))++11(8+<+@+@+B+BB'<+==?
 
 	
r.   c                 :    t          j        | j        dz             S )zb
        Returns the next level upgrade type
        :return: The next level upgrade type
        r   )r	   get_by_levelr   rQ   s    r/   get_next_level_upgrade_typez Crew.get_next_level_upgrade_type   s     $0a@@@r.   upgrade_typec                 :    |t           j        u r| j        S | j        S )z
        Returns the number of upgrades of the given type
        :param upgrade_type: The upgrade type
        :return: The number of upgrades
        )r	   MEMBERr   r   r=   r   s     r/   get_upgrade_type_countzCrew.get_upgrade_type_count  s%     /666##!!r.   c                 &   |                                  }|t          j        u r,| xj        dz  c_        d| _        |                                  n|t          j        u r| xj        dz  c_        | xj        dz  c_        | 	                                 dS )z9
        Level up the crew
        :return: None
        r   TN)
r   r	   r   r   r   set_is_fullABILITYr   r   saver   s     r/   level_upzCrew.level_up  s     7799/666!*.D'1999!#

a

		r.   c                  
   t           j        dz  t          j                                        k    t          j                                        ft           j        dz  dk     dfg} t          d| t           j        dz            }t                               |                                           t                               t          j                                        t          
                    t           j        dz
  dz            z   t          j                                        t                              t           j        dz
  dz            z                                              t                                            dS )z<
        Reset the crew level
        :return: None
        rq   r   N)r   )r   r   )r   r   rs   CREW_MAX_LEVEL_AFTER_RESETru   CaseupdateexecuteCREW_MAX_MEMBERSr8   ROUNDCREW_MAX_ABILITIESFLOORset_is_full_for_all_crews)
conditions	case_stmts     r/   reset_levelzCrew.reset_level#  s,    a3#A#I#I#K#KK.6688
 j1n!1%.

 z4:>::	)$$,,... 	-5577"((DJQRNVWCW:X:XX199;;bhh
UVZ[G[>\>\\ 	 	
 	
 '))) 	&&(((((r.   c                 .    ddl m}  || j                  S )zV
        Returns the crew name escaped
        :return: The crew name escaped
        r   escape_valid_markdown_chars)src.service.message_servicer   r   r=   r   s     r/   get_name_escapedzCrew.get_name_escapedB  s*     	LKKKKK**49555r.   c                 .    ddl m}  || j                  S )zd
        Returns the crew description escaped
        :return: The crew description escaped
        r   r   )r   r   r   r   s     r/   get_description_escapedzCrew.get_description_escapedL  s+     	LKKKKK**4+;<<<r.   c                 .    ddl m}  || j                  S )zp
        Returns the crew required bounty formatted
        :return: The crew required bounty formatted
        r   rx   )rz   ry   r   r}   s     r/   get_required_bounty_formattedz"Crew.get_required_bounty_formattedV  s*     	?>>>>>""4#7888r.   c                     |                                  | j        k    rd| _        nd| _        |                                  |                                  dS )z<
        Set the crew as full
        :return: None
        TFN)rg   r   r"   r   set_davy_back_fight_priorityrQ   s    r/   r   zCrew.set_is_full`  sU    
   ""d&666DLL DL		 	))+++++r.   c                  V    d} t          j                                        |            dS )zJ
        Set the crew as full for all crews
        :return: None
        zxupdate crew set is_full = IF(((select count(*) from user where crew_id = crew.id) < crew.max_members), 0, 1) where true;N)r   get_dbexecute_sql)	raw_querys    r/   r   zCrew.set_is_full_for_all_crewso  s.     	 	##I.....r.   c                    ddl m} |                                                    |j        | k    |j        | k    z  |j        t          j        k    |j        t          j	        k    z  z            
                                S )zb
        Returns the current Davy Back Fight
        :return: The current Davy Back Fight
        r   DavyBackFight)src.model.DavyBackFightr   r7   r[   challenger_crewopponent_crewstatusr   COUNTDOWN_TO_STARTIN_PROGRESSrT   r=   r   s     r/   get_active_davy_back_fightzCrew.get_active_davy_back_fight~  s    
 	:99999   ""U/47M<W[_<_`")Z-JJ$+z/EEG  []]
	
r.   c                     ddl m} |                                                    |j        | k    |j        | k    z  |j        t          j        k    z            	                                S )zj
        Returns the in progress Davy Back Fight
        :return: The in progress Davy Back Fight
        r   r   )
r   r   r7   r[   r   r   r   r   r   rT   r   s     r/   get_in_progress_davy_back_fightz$Crew.get_in_progress_davy_back_fight  ss     	:99999   ""U/47M<W[_<_` ':+AAC  []]	
r.   c                     ddl m} | j                                                            |j        t          j        k                                              S )zp
        Returns the pending Davy Back Fight as challenger
        :return: The pending Davy Back Fight
        r   r   )	r   r   davy_back_fights_challengersr7   r[   r   r   AWAITING_OPPONENT_CONFIRMATIONrT   r   s     r/   get_pending_davy_back_fightz Crew.get_pending_davy_back_fight  sP     	:99999 -4466U=':+TTUU[]]	
r.   c                 R   ddl m} |                                                    |j        | k    |j        t          j        k    z  |j        | k    |j        t          j	        k    z  z  |j
        t          j                                        k    z                                            S )zb
        Returns the penalty Davy Back Fight
        :return: The penalty Davy Back Fight
        r   r   )r   r   r7   r[   r   r   r   LOSTr   WONpenalty_end_dater   r   rT   r   s     r/   get_penalty_davy_back_fightz Crew.get_penalty_davy_back_fight  s     	:99999   ""U '6$>(/:?BD '4<(/:>AC !1H4E4I4I4K4KK
M  []]	
r.   	add_levelc                 (    ddl m}  || |          S )z
        Returns the crew name with deeplink
        :param add_level: Whether to add the level to the name
        :return: The crew name with deeplink
        r   )get_crew_name_with_deeplink)r   )src.service.crew_servicer   )r=   r   r   s      r/   get_name_with_deeplinkzCrew.get_name_with_deeplink  s-     	IHHHHH**49EEEEr.   c                 .    |                                  duS )z
        Returns True if the crew has an active Davy Back Fight
        :return: True if the crew has an active Davy Back Fight
        N)r   rQ   s    r/   has_active_davy_back_fightzCrew.has_active_davy_back_fight  s     ..00<<r.   c                 .    |                                  duS )z
        Returns True if the crew has a penalty Davy Back Fight
        :return: True if the crew has a penalty Davy Back Fight
        N)r   rQ   s    r/   has_penalty_davy_back_fightz Crew.has_penalty_davy_back_fight  s     //11==r.   should_resetc                     ddl m} |r|                                 n|                                 }t	          |          D ]#\  }}|dz   |_        |                                 $dS )z
        Set the crew members Davy Back Fight priority
        :param should_reset: Whether to reset the priority
        :return: None
        r   r3   r   N)r5   r4   rI   rR   	enumeraterL   r   )r=   r   r4   membersimembers         r/   r   z!Crew.set_davy_back_fight_priority  s     	('''''
 FDCCEE 	 #7++ 	 	IAv34q5F0KKMMMM	 	r.   )Nr   )r1   N)F)Yr)   r*   r+   __doc__	CharFieldrs   CREW_NAME_MAX_LENGTHru   r   str__annotations__BooleanFieldr   boolr   DateTimeFieldr   r   BigIntegerFieldr   intr   r   IntegerFieldr   r   r   r   r   r   r   CREW_DESCRIPTION_MAX_LENGTHr   r   r    r!   r"   r#   r$   r%   r   crew_chest_spending_recordscrew_join_requestsrk   davy_back_fight_participantsr   davy_back_fights_opponentsr6   r0   r>   rA   r   rI   rR   staticmethodrU   rY   r   r_   rb   rd   rg   rn   r   rv   r{   r~   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r.   r/   r   r      s          &I1I1Q1Q1S1STTTD#	/TTT2>,t2L2L2LD</LLL%1\$%?%?%?Itl"???6Cm6N6N6NL(#m3NNN*9/!*D*D*DL#'DDD7Fq7Q7Q7Qs_4QQQ5D_Q5O5O5OS?2OOO ,Q 7 7 7E3777(4S=S=[=[=]=](^(^(^M3%^^^#/<#=#=#=D=== ,T : : ::::&2l3;O;W;W;Y;Y&Z&Z&ZK|#ZZZ#,92::<<4$ $ $Ky    .=_Q-G-G-GOS?*GGG0<T0J0J0J$-JJJ2>,t2L2L2LD</LLL#/<#>#>#>GTL >>>9Ed9S9S9S!4,#6SSS7C|E7R7R7R!4RRR,8L,F,F,Fd\)FFF N"&&*##' #' !%L       FC F F F FNC N N N NN N N3 Nt N N N N(
t 
 
 
 
 NC NF N N N \N QS QV Q Q Q \QZS Z Z Z Z

 

 

 

 

1 1 1 1 12# 2 2 2 2

 

 

 

 

O(? OC O O O O	C2I 	Cc 	C 	C 	C 	C6# 6 6 6 6

d 

 

 

 


D1F,G 
 
 
 

/D 
 
 
 
 
A-A A A A A
"3G 
"C 
" 
" 
" 
"  & ) ) ) \)<6# 6 6 6 6= = = = =9s 9 9 9 9, , , , / / / \/
C 
 
 
 
&
 
 
 
 
"
S 
 
 
 

S 
 
 
 
4	F 	F 	F 	F 	F 	F=D = = = =>T > > > >       r.   r   )r   typingr   peeweeresources.EnvironmentEnvironmentrs   src.model.BaseModelr   r   src.model.enums.GameStatusr   ,src.model.enums.crew.CrewChestSpendingReasonr   )src.model.enums.crew.CrewLevelUpgradeTyper	   src.model.enums.crew.CrewRoler
   1src.model.enums.devil_fruit.DevilFruitAbilityTyper   r   create_tabler-   r.   r/   <module>r      s               # # # # # # 1 1 1 1 1 1 1 1 1 1 1 1 1 1 P P P P P P J J J J J J 2 2 2 2 2 2 S S S S S Sh h h h h9 h h hV        r.   