
    Ii                        S r SSKJrJr  SSKJr  SSKrSSKJrJrJ	r	  SSK
Jr  SS	KJrJrJr  SS
KJr  SSKJr  SSKJrJr  SSKJrJr  SSKJr  SSKJr  SSKJrJ r   SSK!J"r"J#r#  \ " S S\5      5       r$\ " S S\	5      5       r%\\ " S S\5      5       5       r&S\'4S jr(S\'4S jr)S\'4S jr*\+" S5       H?  r,\-" \&S\, S3\(" \,5      5        \-" \&S\, S 3\)" \,5      5        \-" \&S!\, S"3\*" \,5      5        MA     S#\'4S$ jr.S#\'4S% jr/S#\'4S& jr0\+" SS'5       H?  r1\-" \&S(\1 S3\." \15      5        \-" \&S(\1 S 3\/" \15      5        \-" \&S)\1 S"3\0" \15      5        MA     g)*a  
Tradeoff Game Implementation for PlayPalace v11.

A dice trading game where players roll dice, trade unwanted ones to a shared pool,
and take dice back in turn order (lowest scorer first). After 3 iterations,
players score based on set combinations formed from their 15 dice.
    )	dataclassfield)datetimeN   )GamePlayerGameOptions)register_game   )Action	ActionSet
Visibility)	BotHelper)	roll_dice)
GameResultPlayerResult)	IntOptionoption_field)Localization)DiceKeepingStyle   )SET_DEFINITIONSfind_best_scoring)bot_think_tradingbot_think_takingc                       \ rS rSr% Sr\" \S9r\\   \	S'   \" \S9r
\\   \	S'   \" \S9r\\   \	S'   Sr\\	S'   \" \S9r\\   \	S	'   S
r\\	S'   S
r\\	S'   S
r\\	S'   Srg)TradeoffPlayer   zPlayer state for Tradeoff game.default_factoryhandrolled_dicetrading_indicesFtrades_confirmedtraded_dicer   dice_traded_countdice_taken_countround_score N)__name__
__module____qualname____firstlineno____doc__r   listr!   int__annotations__r"   r#   r$   boolr%   r&   r'   r(   __static_attributes__r)       9c:\Users\dbart\PlayPalace11\server\games\tradeoff\game.pyr   r      s    ) D1D$s)1 #48Kc8 "'t!<OT#Y< #d" #48Kc8 s c Kr4   r   c                   H    \ rS rSr% Sr\" \" SSSSSSS	S
95      r\\	S'   Sr
g)TradeoffOptions8   zOptions for Tradeoff game.<      i  scoreztradeoff-set-targetztradeoff-enter-targetztradeoff-option-changed-target)defaultmin_valmax_val	value_keylabelprompt
change_msgtarget_scorer)   N)r*   r+   r,   r-   r.   r   r   rC   r0   r1   r3   r)   r4   r5   r7   r7   8   s3    $$'*7	

L# 
r4   r7   c            	       :  ^  \ rS rSr% SrS/r\" \S9r\\	   \
S'   \" \S9r\\
S'   Sr\\
S'   S	r\\
S
'   \" \S9r\\   \
S'   \" \S9r\\   \
S'   S	r\\
S'   \S\4S j5       r\S\4S j5       r\S\4S j5       rS\S\4S jr\S\4S j5       r\S\4S j5       r\S\\   4S j5       rSXS\S\S\S\	4S jjrS\S\S\S-  4S jr S\S\S\!4S jr"S\S\S\4S  jr#S\S\S-  4S! jr$S\S\!4S" jr%S\S#\S\4S$ jr&S\S%\S\S-  4S& jr'S\S%\S\!4S' jr(S\S%\S\4S( jr)S\S\S-  4S) jr*S\S\!4S* jr+S\S\S-  4S+ jr,S\S\!4S, jr-S\S\S-  4S- jr.S\S\!4S. jr/S\S\S-  4S/ jr0S\S\!4S0 jr1S\	S\24S1 jr3SYU 4S2 jjr4S\S#\SS4S3 jr5S\S#\SS4S4 jr6S\S#\SS4S5 jr7S\S6\SS4S7 jr8S\S%\SS4S8 jr9S\S%\SS4S9 jr:S\S%\SS4S: jr;S\S\SS4S; jr<S\S#\SS4S< jr=SYS= jr>S\S#\SS4S> jr?S\S%\SS4S? jr@SYS@ jrASYSA jrBSYSB jrCSYSC jrDS\S#\SS4SD jrES\S#\SS4SE jrFS\S#\SS4SF jrGSYSG jrHSYSH jrISYSI jrJSJ\SK\SL\\   S\4SM jrKSYSN jrLSYSO jrMS\N4SP jrOSQ\NSJ\S\\   4SR jrPSYU 4SS jjrQSYST jrRSYSU jrSS\	S\S-  4SV jrTSWrUU =rV$ )ZTradeoffGameI   a3  
Tradeoff dice trading game.

Players roll 5 dice each, select dice to trade to a shared pool, then
take dice back from the pool in turn order (lowest scorer first).
After 3 iterations of this, players score based on the sets they form
from their 15 accumulated dice.

First to reach the target score wins.
dice_keeping_styler   playersoptionswaitingphaser   	iterationpooltaking_ordertaking_indexreturnc                     g)NTradeoffr)   clss    r5   get_nameTradeoffGame.get_nameg       r4   c                     g)Ntradeoffr)   rS   s    r5   get_typeTradeoffGame.get_typek   rW   r4   c                     g)Nzcategory-dice-gamesr)   rS   s    r5   get_categoryTradeoffGame.get_categoryo   s    $r4   player_namec                 b    U R                   R                  U5      nU(       a  UR                  $ S$ )z1Get a player's total score from the team manager.r   )_team_managerget_teamtotal_score)selfr_   teams      r5   _get_player_scoreTradeoffGame._get_player_scores   s+    !!**;7#'t.Q.r4   c                     g)Nr   r)   rS   s    r5   get_min_playersTradeoffGame.get_min_playersx       r4   c                     g)N   r)   rS   s    r5   get_max_playersTradeoffGame.get_max_players|   rk   r4   c                     SSSSSSS./$ )Nscore_per_roundzfinal_scores.{player_name}rounds_playedsumavgr   )id	numeratordenominator	aggregateformatdecimalsr)   rS   s    r5   get_leaderboard_types"TradeoffGame.get_leaderboard_types   s%     (9."	
 		
r4   	player_idnameis_botc                     [        XUS9$ )z1Create a new player with Tradeoff-specific state.)ru   r~   r   )r   )rd   r}   r~   r   s       r5   create_playerTradeoffGame.create_player   s    fEEr4   playerindexNc                     U R                   S:w  a  gUR                  (       a  gU R                  S:w  a  gUnUR                  (       a  gU[	        UR
                  5      :  a  gg)	z4Check if toggling trade for die at index is enabled.playingaction-not-playingaction-spectatortradingtradeoff-not-trading-phasetradeoff-already-confirmedztradeoff-no-dieN)statusis_spectatorrK   r$   lenr"   rd   r   r   tps       r5   _is_toggle_trade_enabled%TradeoffGame._is_toggle_trade_enabled   sT    ;;)#'%::"/#/C''$r4   c                 l   U R                   S:w  a  [        R                  $ UR                  (       a  [        R                  $ U R                  S:w  a  [        R                  $ UnUR
                  (       a  [        R                  $ UR                  (       d  [        R                  $ [        R                  $ )z'Check if toggle trade action is hidden.r   r   r   r   HIDDENr   rK   r$   r"   VISIBLEr   s       r5   _is_toggle_trade_hidden$TradeoffGame._is_toggle_trade_hidden       ;;)#$$$$$$::"$$$#$$$~~$$$!!!r4   c                 :   UnU R                  U5      nU(       a  UR                  OSnU[        UR                  5      :  a  SUS-    3$ UR                  U   nX#R                  ;   n[
        R                  " UU(       a  SOS5      n[
        R                  " USXhS9$ )z"Get label for toggle trade action.enDie r   ztradeoff-trade-status-tradingztradeoff-trade-status-keepingztradeoff-toggle-trade)valuer   )get_userlocaler   r"   r#   r   get)	rd   r   r   r   userr   die_val
is_tradingr   s	            r5   _get_toggle_trade_label$TradeoffGame._get_toggle_trade_label   s    #}}V$ $$C''%!)%%..'000
!!/9+?^
 (?w^^r4   c                     U R                   S:w  a  gUR                  (       a  gU R                  S:w  a  gUnUR                  (       a  gg)z*Check if confirm trades action is enabled.r   r   r   r   r   r   N)r   r   rK   r$   rd   r   r   s      r5   _is_confirm_trades_enabled'TradeoffGame._is_confirm_trades_enabled   s@    ;;)#'%::"/#/r4   c                 l   U R                   S:w  a  [        R                  $ UR                  (       a  [        R                  $ U R                  S:w  a  [        R                  $ UnUR
                  (       a  [        R                  $ UR                  (       d  [        R                  $ [        R                  $ )z)Check if confirm trades action is hidden.r   r   r   r   s      r5   _is_confirm_trades_hidden&TradeoffGame._is_confirm_trades_hidden   r   r4   	action_idc                     UnU R                  U5      nU(       a  UR                  OSn[        UR                  5      n[        R
                  " USUS9$ )z$Get label for confirm trades action.r   tradeoff-confirm-tradescount)r   r   r   r#   r   r   )rd   r   r   r   r   r   trade_counts          r5   _get_confirm_trades_label&TradeoffGame._get_confirm_trades_label   sH    #}}V$ $$",,-(AUUr4   r   c                 ^   U R                   S:w  a  gUR                  (       a  gU R                  S:w  a  gU R                  [	        U R
                  5      :  a  gU R
                  U R                     UR                  :w  a  gUnUR                  UR                  :  a  gX R                  ;  a  gg	)
z*Check if taking die with value is enabled.r   r   r   takingtradeoff-not-taking-phasezaction-not-your-turnztradeoff-no-more-takesztradeoff-not-in-poolN)
r   r   rK   rO   r   rN   ru   r'   r&   rM   rd   r   r   r   s       r5   _is_take_enabledTradeoffGame._is_take_enabled   s    ;;)#'%::!. D$5$5 66)T../699<)#""6"66+		!)r4   c                 N   U R                   S:w  a  [        R                  $ UR                  (       a  [        R                  $ U R                  S:w  a  [        R                  $ U R
                  [        U R                  5      :  a  [        R                  $ U R                  U R
                     UR                  :w  a  [        R                  $ UnUR                  UR                  :  a  [        R                  $ X R                  ;  a  [        R                  $ [        R                  $ )zCheck if take action is hidden.r   r   )r   r   r   r   rK   rO   r   rN   ru   r'   r&   rM   r   r   s       r5   _is_take_hiddenTradeoffGame._is_take_hidden  s    ;;)#$$$$$$::!$$$ D$5$5 66$$$T../699<$$$#""6"66$$$		!$$$!!!r4   c                     U R                  U5      nU(       a  UR                  OSnU R                  R                  U5      n[        R
                  " USX%S9$ )zGet label for take action.r   ztradeoff-take-die)r   	remaining)r   r   rM   r   r   r   )rd   r   r   r   r   r   s         r5   _get_take_labelTradeoffGame._get_take_label  sE    }}V$ $$		&(;5ZZr4   c                 &    U R                   S:w  a  gg)z7Dice keybind actions are enabled during trading/taking.r   r   Nr   rd   r   s     r5   _is_dice_key_enabled!TradeoffGame._is_dice_key_enabled&      ;;)#'r4   c                 "    [         R                  $ )z'Dice keybind actions are always hidden.r   r   r   s     r5   _is_dice_key_hidden TradeoffGame._is_dice_key_hidden,         r4   c                 H    U R                   S:w  a  gU R                  S:w  a  gg)z%Check if view hand action is enabled.r   r   r   r   Nr   rK   r   s     r5   _is_view_hand_enabled"TradeoffGame._is_view_hand_enabled1  #    ;;)#'::!.r4   c                 "    [         R                  $ )z*View hand is always hidden (keybind only).r   r   s     r5   _is_view_hand_hidden!TradeoffGame._is_view_hand_hidden9  r   r4   c                 &    U R                   S:w  a  gg)z%Check if view pool action is enabled.r   r   Nr   r   s     r5   _is_view_pool_enabled"TradeoffGame._is_view_pool_enabled=  r   r4   c                 "    [         R                  $ )z*View pool is always hidden (keybind only).r   r   s     r5   _is_view_pool_hidden!TradeoffGame._is_view_pool_hiddenC  r   r4   c                 H    U R                   S:w  a  gU R                  S:w  a  gg)z(Check if view players action is enabled.r   r   r   r   Nr   r   s     r5   _is_view_players_enabled%TradeoffGame._is_view_players_enabledG  r   r4   c                 "    [         R                  $ )z-View players is always hidden (keybind only).r   r   s     r5   _is_view_players_hidden$TradeoffGame._is_view_players_hiddenO  r   r4   c                    U R                  U5      nU(       a  UR                  OSn[        SS9n[        S5       H5  nUR	                  [        SU 3SUS-    3SS	U S
3S	U S3SU S3S95        M7     [        SS5       HG  nUR	                  [        SU 3SU 3SSSS95        UR	                  [        SU 3SU 3SSSS95        MI     UR	                  [        S[        R                  " USSS9SSSS S95        [        SS5       H2  nUR	                  [        S!U 3S"U 3S#S$U S
3S$U S3S%U S3S95        M4     UR	                  [        S&[        R                  " US'5      S(S)S*S95        UR	                  [        S+[        R                  " US,5      S-S.S/S95        UR	                  [        S0[        R                  " US15      S2S3S4S95        U$ )5z(Create the turn action set for a player.r   turn)r~      toggle_trade_r   r   _action_toggle_trade_is_toggle_trade__enabled_hidden_get_toggle_trade__label)ru   r@   handler
is_enabled	is_hidden	get_label   	dice_key_	Dice key _action_dice_keyr   r   )ru   r@   r   r   r   dice_trade_zTrade _action_dice_tradeconfirm_tradesr   r   r   _action_confirm_tradesr   r   r   take_zTake a _action_take	_is_take_
_get_take_	view_handztradeoff-view-hand_action_view_handr   r   	view_poolztradeoff-view-pool_action_view_poolr   r   view_playersztradeoff-view-players_action_view_playersr   r   )r   r   r   rangeaddr   r   r   )rd   r   r   r   
action_setivs          r5   create_turn_action_set#TradeoffGame.create_turn_action_setW  s5   }}V$ $$F+
 qANN&qc* Q.2!21#X> 1!G< 21#V<	  q!ANN"1#%aS/.53 NN$QC("1#,053 , 	#"&&v/HPQR0755		
 q!ANNqc{#A3-*!*1#X6 )!G4 *1#V4	  	"&&v/CD+20	
 	"&&v/CD+20	
 	!"&&v/FG.53	
 r4   c                   > [         TU ]  5         SSKJn  [	        SS5       HU  nU R                  [        U5      SU 3SU 3/UR                  S9  U R                  SU 3S	U 3S
U 3/UR                  S9  MW     U R                  SSS/UR                  S9  U R                  SSS/UR                  SS9  U R                  SSS/UR                  SS9  U R                  SSS/UR                  SS9  g)z!Define all keybinds for the game.r   )KeybindStater   r   r   r   )statezshift+zTrade dice r   bzConfirm tradesr   hz	View handr   T)r
  include_spectatorspz	View poolr   r  zView playersr   N)supersetup_keybindsserver.core.ui.keybindsr	  r  define_keybindstrACTIVE)rd   r	  r  	__class__s      r5   r  TradeoffGame.setup_keybinds  sH     	9q!AAA3QC!"))	    aS!qc"#"))	      	%%	 	 	
 	M%%# 	 	
 	M%%# 	 	
 	%%# 	 	
r4   c                 `    [        UR                  S5      S   5      nU R                  X5        g)zDHandle toggle_trade_X action by extracting die index from action_id._N)r0   split_toggle_traderd   r   r   r   s       r5   r   !TradeoffGame._action_toggle_trade  s(    IOOC(,-6)r4   c                 `    [        UR                  S5      S   5      nU R                  X5        g)zAHandle dice_key_X action by extracting key number from action_id.r  r  N)r0   r  _handle_dice_key)rd   r   r   key_nums       r5   r   TradeoffGame._action_dice_key  s(    iooc*2./f.r4   c                 `    [        UR                  S5      S   5      nU R                  X5        g)z>Handle dice_trade_X action by extracting value from action_id.r  r  N)r0   r  _handle_dice_traderd   r   r   r   s       r5   r   TradeoffGame._action_dice_trade  s(    IOOC(,-.r4   r   c                    U R                  U5      nU(       d  gUR                  R                  SU R                  5       S9nU[        R
                  :X  a  US::  a  U R                  XS-
  5        ggU R                  X5        g)z
Handle a dice key press (1-6).

Behavior depends on user's dice keeping style preference:
- PlayPalace style: Toggle die at index (key_num - 1) for keys 1-5
- Quentin C style: Keep first trading die with face value key_num
NrG   	game_typer   r   )r   preferencesget_effectiverZ   r   
PLAYPALACEr  _keep_by_value)rd   r   r   r   styles        r5   r  TradeoffGame._handle_dice_key   sw     }}V$  ../Ct}}._$///!|""6Q;7  0r4   c                     U R                  U5      nU(       d  gUR                  R                  SU R                  5       S9nU[        R
                  :X  a  U R                  X5        gg)zj
Handle shift+key press for trading by value.

Only works in Quentin C style. Silent in PlayPalace style.
NrG   r'  )r   r)  r*  rZ   r   	QUENTIN_C_trade_by_value)rd   r   r   r   r-  s        r5   r#  TradeoffGame._handle_dice_trade  s\     }}V$  ../Ct}}._$...  / /r4   c                 `   UnU R                   S:w  d  UR                  (       a  g[        UR                  5       Hq  u  pEXR:X  d  M  XCR                  ;   d  M  UR                  R                  U5        U R                  U5      nU(       a  UR                  SUS9  U R                  U5          g   g)z{
Keep the first trading die with the given face value (Quentin C style).

Silent if no trading die with that value exists.
r   Ntradeoff-keepingr   )	rK   r$   	enumerater"   r#   remover   speak_lrebuild_player_menurd   r   r   tradeoff_playerr  r   r   s          r5   r,  TradeoffGame._keep_by_value&  s     +1::"o&F&F $O$?$?@JAA)H)H$H//66q9}}V,LL!35LA((0 Ar4   c                 `   UnU R                   S:w  d  UR                  (       a  g[        UR                  5       Hq  u  pEXR:X  d  M  XCR                  ;  d  M  UR                  R                  U5        U R                  U5      nU(       a  UR                  SUS9  U R                  U5          g   g)z|
Trade the first keeping die with the given face value (Quentin C style).

Silent if no keeping die with that value exists.
r   Ntradeoff-tradingr5  )	rK   r$   r6  r"   r#   appendr   r8  r9  r:  s          r5   r1  TradeoffGame._trade_by_value<  s     +1::"o&F&F $O$?$?@JAA-L-L$L//66q9}}V,LL!35LA((0 Ar4   c                    UnU R                   S:w  d  UR                  (       a  gU[        UR                  5      :  a  gUR                  U   nU R	                  U5      nX#R
                  ;   a3  UR
                  R                  U5        U(       a  UR                  SUS9  O2UR
                  R                  U5        U(       a  UR                  SUS9  U R                  U5        g)z%Toggle whether a die is being traded.r   Nr4  r5  r>  )
rK   r$   r   r"   r   r#   r7  r8  r?  r9  )rd   r   r   r;  	die_valuer   s         r5   r  TradeoffGame._toggle_tradeR  s    *0::"o&F&FC3344#//6	}}V$333++2259/yA++2259/yA  (r4   c                    UnU R                   S:w  d  UR                  (       a  gSUl        [        UR                  5      Ul        UR                   Vs/ s H  oCR
                  U   PM     snUl        [        UR
                  5       HL  u  pEXCR                  ;   a  U R                  R                  U5        M1  UR                  R                  U5        MN     / Ul        / Ul        U R                  5         U R                  5         gs  snf )z&Confirm the player's trade selections.r   NT)rK   r$   r   r#   r&   r"   r%   r6  rM   r?  r!   _check_all_tradedrebuild_all_menus)rd   r   r   r;  r  r   s         r5   r   #TradeoffGame._action_confirm_tradesl  s    *0::"o&F&F+/(,/0O0O,P) 5D4S4S'
4Sq''*4S'
#
 $O$?$?@JA333		  )$$++G4	 A ')#*,' 	  %'
s   Dc                    U R                  5       n[        S U 5       5      nU(       a  U Hx  nUnUR                  (       aH  SR                  S [	        UR                  5       5       5      nU R                  SUR                  US9  M^  U R                  SUR                  S9  Mz     U R                  (       a  U R                  5         gU R                  5         gg)	z1Check if all players have confirmed their trades.c              3   8   #    U  H  oR                   v   M     g 7fN)r$   ).0r  s     r5   	<genexpr>1TradeoffGame._check_all_traded.<locals>.<genexpr>  s     G1..   , c              3   8   #    U  H  n[        U5      v   M     g 7frJ  r  rK  ds     r5   rL  rM    s     (P9OAQ9OrN  ztradeoff-player-traded)r   diceztradeoff-player-traded-noner   N)
get_active_playersallr%   joinsortedbroadcast_lr~   rM   _start_taking_phase_end_iteration)rd   active_playersall_confirmedr  r   dice_strs         r5   rE  TradeoffGame._check_all_traded  s    002GGG#%&>>#yy(P9O(PPH$$%=affS[$\$$%B166$R $ yy((* ##% r4   c                 `    [        UR                  S5      S   5      nU R                  X5        g)z<Handle take_X action by extracting die value from action_id.r  r  N)r0   r  	_take_dier$  s       r5   r   TradeoffGame._action_take  s&    IOOC(,-v%r4   c                    UnU R                   S:w  a  gU R                  [        U R                  5      :  a  gU R                  U R                     UR                  :w  a  gUR
                  UR                  :  a  gX R                  ;  a  gU R                  R                  U5        UR                  R                  U5        U=R
                  S-  sl        U R                  USSUS9  U R                  5         U R                  5         g)z2Take a die with the specified value from the pool.r   Nr   ztradeoff-you-takeztradeoff-player-takesr5  )rK   rO   r   rN   ru   r'   r&   rM   r7  r!   r?  broadcast_personal_l_advance_takerrF  )rd   r   r   r;  s       r5   rb  TradeoffGame._take_die  s    *0::! D$5$5 66T../699< ++/P/PP 		! 			##E*((A-(!!#	 	" 	
 	 r4   c                 d  ^  ST l         T R                  5       nS[        4U 4S jjn[        XS9nU Vs/ s H   oDR                  S:  d  M  UR
                  PM"     snT l        ST l        U H
  nSUl        M     T R                  (       a  T R                  5         gT R                  5         gs  snf )zStart the taking phase.r   r  c                   > TR                  U R                  5      nU R                  (       a  [        U R                  5      O'U R                  (       a  [        U R                  5      OSnUU[
        R
                  " 5       4$ )Nr   )rf   r~   r!   rs   r"   random)r  r;   dice_sumrd   s      r5   sort_key2TradeoffGame._start_taking_phase.<locals>.sort_key  sW    **1662E&'ffs166{#amm2D[\H r4   )keyr   N)rK   rV  r   rY  r&   ru   rN   rO   r'   _announce_current_takerr\  )rd   r]  rl  sorted_playersr  s   `    r5   r[   TradeoffGame._start_taking_phase  s    
 002	 	  =+9U>a=P=PST=TTQTT>U  A!"A   ((* ! Vs   B-B-c                 v   U R                   [        U R                  5      :  a  gU R                  U R                  U R                      5      nU(       af  U R	                  U5      nU(       a  UR                  S5        UR                  (       a+  [        R                  " U[        R                  " SS5      S9  ggg)z"Announce whose turn it is to take.Nztradeoff-your-turn-take
      ticks)rO   r   rN   get_player_by_idr   r8  r   r   jolt_botrj  randint)rd   r   r   s      r5   ro  $TradeoffGame._announce_current_taker  s    D$5$5 66&&t'8'89J9J'KL==(D67 }}""6B1GH  r4   c                    U R                   (       d  U R                  5         g[        U R                  5      nUS:X  a  U R                  5         g[	        U5       Hy  nU R
                  S-   U-  U l        U R                  U R                  U R
                     5      nU(       d  MK  UnUR                  UR                  :  d  Mi  U R                  5           g   U R                  5         g)z9Advance to the next player in taking order (round-robin).Nr   r   )
rM   r\  r   rN   r  rO   rw  r'   r&   ro  )rd   num_playersr  r   r   s        r5   rf  TradeoffGame._advance_taker  s     yy!$++,!! {#A!%!2!2Q!6+ ED**4+<+<T=N=N+OPFv%+&&)=)==002 $ 	r4   c                 f    U R                   S:  a  U R                  5         gU R                  5         g)zEnd the current iteration.r   N)rL   _start_iteration_do_scoring)rd   s    r5   r\  TradeoffGame._end_iteration  s(    >>A!!# r4   c                    UnU R                  U5      nU(       d  gUR                  (       aP  SR                  S [        UR                  5       5       5      nUR	                  S[        UR                  5      US9  gUR	                  SSSS9  g)zView the player's current hand.NrO  c              3   8   #    U  H  n[        U5      v   M     g 7frJ  rQ  rR  s     r5   rL  1TradeoffGame._action_view_hand.<locals>.<genexpr>'  s      N1MAQ1MrN  ztradeoff-hand-displayr   rT  r   none)r   r!   rX  rY  r8  r   )rd   r   r   r;  r   hand_strs         r5   r   TradeoffGame._action_view_hand  su    *0}}V$yy N8L8L1M NNHLL0O<P<P8QX`LaLL0LGr4   c                    U R                  U5      nU(       d  gU R                  (       aP  SR                  S [        U R                  5       5       5      nUR	                  S[        U R                  5      US9  gUR	                  SSSS9  g)zView the current pool.NrO  c              3   8   #    U  H  n[        U5      v   M     g 7frJ  rQ  rR  s     r5   rL  1TradeoffGame._action_view_pool.<locals>.<genexpr>3  s      C1BAQ1BrN  ztradeoff-pool-displayr  r   r  )r   rM   rX  rY  r8  r   )rd   r   r   r   pool_strs        r5   r   TradeoffGame._action_view_pool,  sh    }}V$99yy C		1B CCHLL0DIIXLVLL0LGr4   c                    U R                  U5      nU(       d  gU R                  5        H  nUnUR                  (       a,  SR                  S [	        UR                  5       5       5      nOSnUR
                  (       aH  SR                  S [	        UR
                  5       5       5      nUR                  SUR                  XgS9  M  UR                  SUR                  US	9  M     g)
z-View all players' hands and what they traded.NrO  c              3   8   #    U  H  n[        U5      v   M     g 7frJ  rQ  rR  s     r5   rL  4TradeoffGame._action_view_players.<locals>.<genexpr>C  s     $E_SVV_rN  emptyc              3   8   #    U  H  n[        U5      v   M     g 7frJ  rQ  rR  s     r5   rL  r  I  s     &N7M!s1vv7MrN  ztradeoff-player-info)r   r!   tradedztradeoff-player-info-no-trade)r   r!   )r   rV  r!   rX  rY  r%   r8  r~   )rd   r   r   r   r  r   r  
traded_strs           r5   r   !TradeoffGame._action_view_players8  s    }}V$((*A!"B ww99$EVBGG_$EE" ~~!YY&Nvbnn7M&NN
*166   <QVVRZ[! +r4   c                 H   SU l         SU l        SU l        U R                  5       nU R	                  U5        SU R
                  l        U R
                  R                  U Vs/ s H  o"R                  PM     sn5        U R                  S5        U R                  5         gs  snf )zCalled when the game starts.r   Tr   
individualzgame_pig/mus.oggN)r   game_activeroundrV  set_turn_playersra   	team_modesetup_teamsr~   
play_music_start_round)rd   r]  r  s      r5   on_startTradeoffGame.on_startQ  s    
 002n- (4$&&'G1'GH 	*+ 	 (Hs   !Bc                 @   U =R                   S-  sl         SU l        / U l        U R                  5        H=  nUn/ Ul        / Ul        / Ul        SUl        / Ul        SUl	        SUl
        SUl        M?     U R                  SU R                   S9  U R                  5         g)zStart a new round.r   r   Fztradeoff-round-startr  N)r  rL   rM   rV  r!   r"   r#   r$   r%   r&   r'   r(   rZ  r  )rd   r  r   s      r5   r  TradeoffGame._start_rounde  s    

a
	 ((*A!"BBGBN!#B"'BBN#$B "#BBN + 	/tzzBr4   c                    U =R                   S-  sl         SU l        / U l        U R                  SU R                   S9  U R	                  5       nU H  nUn[        SS5      Ul        [        [        S5      5      Ul	        SUl
        / Ul        SUl        SUl        U R                  U5      nU(       d  Me  S	R                  S
 UR                   5       5      nUR!                  SUS9  M     U H@  nUR"                  (       d  M  [$        R&                  " U[(        R*                  " SS5      S9  MB     U R-                  5         g)zStart a new trading iteration.r   r   ztradeoff-iteration)rL   r      Fr   rO  c              3   8   #    U  H  n[        U5      v   M     g 7frJ  rQ  rR  s     r5   rL  0TradeoffGame._start_iteration.<locals>.<genexpr>  s     $D^SVV^rN  ztradeoff-you-rolled)rT     r:   ru  N)rL   rK   rM   rZ  rV  r   r"   r/   r  r#   r$   r%   r&   r'   r   rX  r8  r   r   rx  rj  ry  rF  )rd   r]  r  r   r   r_  s         r5   r  TradeoffGame._start_iterationz  s   !
	-H 002A!"B&q!_BN!%eAhB"'BBN#$B "#B ==#Dt99$DR^^$DD2B     Axxx""1FNN2r,BC   	 r4   r   set_namerT  c                    [        U5      nUS:X  a  [        R                  " USUS   S9$ US:X  a  [        R                  " USUS   S9$ US:X  a  [        R                  " USUS   US	   S
9$ US:X  a  [        R                  " USUS   US	   S
9$ US:X  aD  SSKJn  U" U5      n[        UR                  5       5      n[        R                  " USUS   US   S9$ US:X  aD  SSKJn  U" U5      n[        UR                  5       5      n[        R                  " USUS   US   S9$ US:X  a  [        R                  " US5      $ US:X  a  [        R                  " US5      $ U$ )z&Format a concise description of a set.tripleztradeoff-set-tripler   r5  groupztradeoff-set-groupmini_straightztradeoff-set-mini-straightr  )lowhighstraightztradeoff-set-straightdouble_triple)Counterztradeoff-set-double-tripler   )v1v2double_groupztradeoff-set-double-group
all_groupsztradeoff-set-all-groupsall_tripletsztradeoff-set-all-triplets)rY  r   r   collectionsr  keys)rd   r   r  rT  sorted_dicer  countsvaluess           r5   _format_set_description$TradeoffGame._format_set_description  s   Tlx##F,AUVXX ##F,@TUWW(##4+a.{[]  ###/[^+VX/  (+[)FFKKM*F##4vay  '+[)FFKKM*F##F,GFSTIZ`abZcdd%##F,EFF'##F,GHHOr4   c           
      ^   SU l         U R                  S5        U R                  5       nU GH.  nUn[        UR                  5      n[        S U 5       5      nXSl        U(       a  U R                   H  nU R                  U5      nU(       d  M  UR                  n/ n	U H)  u  pnU R                  XU5      nU	R                  U5        M+     [        R                  " X5      n[        R                  " USUR                  UUS9nUR!                  U5        M     OU R#                  SUR                  S9  U R$                  R'                  UR                  U5        GM1     U R$                  R)                  5         U R#                  SU R*                  S	9  U HF  nUnU R-                  UR                  5      nU R#                  S
UR                  UR                  US9  MH     SnSnU H*  nU R-                  UR                  5      nUU:  d  M&  UnUnM,     U(       a  U R#                  SUR                  US9  U HJ  nU R-                  UR                  5      nUU R.                  R0                  :  d  M:  U R3                  5           g   U R$                  R5                  5         U R7                  5         g)z/Score all players' hands at the end of a round.scoringzgame_pig/bank.oggc              3   *   #    U  H	  oS    v   M     g7f)r   Nr)   )rK  ss     r5   rL  +TradeoffGame._do_scoring.<locals>.<genexpr>  s     2TtTs   ztradeoff-player-scored)r   pointssetsztradeoff-no-setsrU  ztradeoff-round-scoresr  ztradeoff-score-line)r   round_pointstotalr   Nztradeoff-leaderr   r;   )rK   
play_soundrV  r   r!   rs   r(   rH   r   r   r  r?  r   format_list_andr   r~   speakrZ  ra   add_to_team_round_scorecommit_round_scoresr  rf   rI   rC   	_end_gamereset_round_scoresr  )rd   r]  r  r   r  total_points	recipientrecipient_userrecipient_localeset_descriptionsr  	dice_usedr  descsets_strmsgr  
best_scoreleaderr;   s                       r5   r  TradeoffGame._do_scoring  sp   
+,002A!"B %RWW-D2T22L)N!%I%)]]9%=N) '5'<'<$ (*$7;3V#;;<LXab(//5 8<  ,;;<L_H&**(0 vv+%C #((-) ".,   !3AFF C 66qvv|LG  L 	..0 	0

CA!"B**1662E%aff2>>Y^     
A**1662Ez!"
	   .v{{*U  A**1662E111 	   	--/r4   c                 \  ^  T R                  5       n[        U 4S jU 5       5      nU Vs/ s H&  nT R                  UR                  5      U:X  d  M$  UPM(     nnT R	                  S5        [        U5      S:X  a  T R                  SUS   R                  US9  OzU Vs/ s H  oUR                  PM     nnT R                   HO  nT R                  U5      nU(       d  M  [        R                  " UR                  U5      nUR                  SXSS	9  MQ     T R                  5         g
s  snf s  snf )z$End the game and announce winner(s).c              3   Z   >#    U  H   nTR                  UR                  5      v   M"     g 7frJ  rf   r~   )rK  r  rd   s     r5   rL  )TradeoffGame._end_game.<locals>.<genexpr>  s#     PA//77s   (+zgame_pig/win.oggr   ztradeoff-winnerr   r  ztradeoff-winners-tietable)rH   r;   bufferN)rV  maxrf   r~   r  r   rZ  rH   r   r   r  r   r8  finish_game)	rd   r]  
high_scorer  winnerswwinner_namesr   	names_strs	   `        r5   r  TradeoffGame._end_game  s   002 PPP
,]n0F0Fqvv0NR\0\1n]*+w<1.wqzjY,34GqFFGL4\\}}Q'4 , < <T[[, WILL.	\c ! 	 " 	! ^ 5s   #D$D$D)c                   ^  T R                  5       n[        UU 4S jSS9n0 nU H*  nT R                  UR                  5      X4R                  '   M,     U(       a  US   OSn[	        T R                  5       [        R                  " 5       R                  5       T R                  U Vs/ s H8  n[        UR                  UR                  UR                  [        USS5      S9PM:     snU(       a  UR                  OSU(       a  T R                  UR                  5      OSUT R                  T R                  R                   S	.S
9$ s  snf )z2Build the game result with Tradeoff-specific data.c                 :   > TR                  U R                  5      $ rJ  r  )r  rd   s    r5   <lambda>0TradeoffGame.build_game_result.<locals>.<lambda>,  s    $008r4   T)rn  reverser   Nis_virtual_botF)r}   r_   r   r  )winner_namewinner_scorefinal_scoresrr   rC   )r(  	timestampduration_ticksplayer_resultscustom_data)rV  rY  rf   r~   r   rZ   r   now	isoformatsound_scheduler_tickr   ru   r   getattrr  rI   rC   )rd   r]  rp  r  r  winners   `     r5   build_game_resultTradeoffGame.build_game_result&  s   0028
 A#'#9#9!&&#AL    '5"$mmolln..044 ( (A dd !88#*1.>#F	 ( /5v{{$GM 6 6v{{ CST ,!% $ 9 9
 	
s   &?Eresultc                    [         R                  " US5      /nUR                  R                  S0 5      n[        UR	                  5       S5       H7  u  nu  pg[         R                  " USUS9nUR                  U SU SU 35        M9     U$ )z(Format the end screen for Tradeoff game.zgame-final-scoresr  r   zgame-pointsr   z. z: )r   r   r  r6  itemsr?  )	rd   r  r   linesr  r  r~   r;   
points_strs	            r5   format_end_screenTradeoffGame.format_end_screenM  s    !!&*=>?))--nbA ),*<*<*> BA}%))&-uMJLLA3bb56 !C r4   c                    > [         TU ]  5         U R                  (       d  gU R                  S:X  a  U R	                  5         gU R                  S:X  a  U R                  5         gg)zCalled every tick.Nr   r   )r  on_tickr  rK   _process_trading_bots_process_taking_bot)rd   r  s    r5   r  TradeoffGame.on_tickX  sP     ::"&&(ZZ8#$$& $r4   c                    U R                    H  nUR                  (       a  UR                  (       a  M'  UnUR                  (       a  M<  UR                  S:  a  U=R                  S-  sl        Mc  UR
                  (       a&  UR
                  nSUl        U R                  X5        M  U R                  U5      nU(       d  M  X1l        M     g)z-Process all bot actions during trading phase.r   r   N)rH   r   r   r$   bot_think_ticksbot_pending_actionexecute_action	bot_think)rd   r   r   r   s       r5   r  "TradeoffGame._process_trading_botsf  s    llF==F$7$7!'B"" %%)&&!+& (("55	,0)##F6 r*Iy,5)/ #r4   c                    U R                   [        U R                  5      :  a  gU R                  U R                  U R                      5      nU(       a  UR                  (       d  gUnUR
                  S:  a  U=R
                  S-  sl        gUR                  (       a%  UR                  nSUl        U R                  X5        gU R                  U5      nU(       a  X1l        gg)z<Process bot action during taking phase (only current taker).Nr   r   )	rO   r   rN   rw  r   r  r  r  r  )rd   current_takerr   r   s       r5   r	   TradeoffGame._process_taking_bot  s    D$5$5 66--d.?.?@Q@Q.RSM$8$8* ((1,))Q.) ++%88I/3M,9 NN2&	/8, r4   c                 p    U R                   S:X  a  [        X5      $ U R                   S:X  a  [        X5      $ g)zBot AI decision making.r   r   N)rK   r   r   r   s     r5   r  TradeoffGame.bot_think  s3    ::"$T22ZZ8##D11r4   )r  rL   rK   rM   r  r   rO   rN   )F)rP   N)Wr*   r+   r,   r-   r.   relevant_preferencesr   r/   rH   r   r1   r7   rI   rK   r  rL   r0   rM   rN   rO   classmethodrU   rZ   r]   rf   ri   rn   dictr{   r2   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,  r1  r  r   rE  r   rb  r[  ro  rf  r\  r   r   r   r  r  r  r  r  r  r   r  r  r  r  r	  r  r3   __classcell__)r  s   @r5   rE   rE   I   sD   	 11$)$$?GT.!?$_EG_E E3Is D1D$s)1 $D9L$s)9L#      %S % %/S /S /
       

d4j 

 

Fs F# Ft FP^ Fv c cDj "f "S "Z "_f _S _S _* C$J " ": "V V3 V3 Vv c cDj ."f "S "Z ".[f [S [S [6 cDj !& !Z !
F sTz !6 !j !F sTz !6 !j !v #* !f ! !`^ `	 `D3
l*6 *c *d */v /# /$ // /C /D /
1v 1 1 1,0 0 0 0 V C D ,f S T ,)F )3 )4 )4!V ! ! !>&.&6 &c &d &
$! $!s $!t $!L"@I2H H3 H4 H
H 
H3 
H4 
H\6 \c \d \2( *!@$c $S $S	 $VY $LL\0%
: %
N	
 	C 	DI 	'6698 3:  r4   rE   r   c                    ^  SS.U 4S jjnU$ )z6Create an is_enabled method for a toggle_trade action.Nr   c                (   > U R                  UT5      $ rJ  )r   r  s      r5   method*_make_toggle_trade_enabled.<locals>.method  s    ,,VU;;r4   r)   r   r  s   ` r5   _make_toggle_trade_enabledr     s     +/ < < Mr4   c                    ^  SS.U 4S jjnU$ )z5Create an is_hidden method for a toggle_trade action.Nr  c                (   > U R                  UT5      $ rJ  )r   r  s      r5   r  )_make_toggle_trade_hidden.<locals>.method      ++FE::r4   r)   r  s   ` r5   _make_toggle_trade_hiddenr%    s     +/ ; ; Mr4   c                    ^  U 4S jnU$ )z4Create a get_label method for a toggle_trade action.c                 (   > U R                  UT5      $ rJ  )r   r  s      r5   r  (_make_toggle_trade_label.<locals>.method  r$  r4   r)   r  s   ` r5   _make_toggle_trade_labelr)    s    ; Mr4   r   r   r   r   r   r   r   c                    ^  SS.U 4S jjnU$ )z.Create an is_enabled method for a take action.Nr  c                (   > U R                  UT5      $ rJ  )r   r$  s      r5   r  "_make_take_enabled.<locals>.method  s    $$VU33r4   r)   r   r  s   ` r5   _make_take_enabledr.    s     +/ 4 4 Mr4   c                    ^  SS.U 4S jjnU$ )z-Create an is_hidden method for a take action.Nr  c                (   > U R                  UT5      $ rJ  )r   r$  s      r5   r  !_make_take_hidden.<locals>.method      ##FE22r4   r)   r-  s   ` r5   _make_take_hiddenr3    s     +/ 3 3 Mr4   c                    ^  U 4S jnU$ )z,Create a get_label method for a take action.c                 (   > U R                  UT5      $ rJ  )r   r$  s      r5   r   _make_take_label.<locals>.method  r2  r4   r)   r-  s   ` r5   _make_take_labelr7    s    3 Mr4   r   r   r   )2r.   dataclassesr   r   r   rj  baser   r   r	   registryr
   game_utils.actionsr   r   r   game_utils.bot_helperr   game_utils.dicer   game_utils.game_resultr   r   game_utils.optionsr   r   messages.localizationr   server.core.users.preferencesr   r  r   r   botr   r   r   r7   rE   r0   r   r%  r)  r  _isetattrr.  r3  r7  _vr)   r4   r5   <module>rF     s   )   , , $ ? ? . ( > 9 1 : 7 4 V  8 k    X4 X  XD#c S C  (BL-bT:<VWY<Z[L-bT9;TUW;XYL.rd&9;STV;WX c S C  1+BLIbT24Fr4JKLIbT13DR3HILJrd&13CB3GH r4   