a
    %ßi"  ã                   @   s¸   d Z ddlmZmZmZ ddlZddlmZ ee	ee
dœdd„Zee	eee	ef dœdd	„Zeee d
œdd„Zdeeedœdd„ZddddddddddddddœZe	edœdd„ZdS )zx
Diamond Economy System

Handles diamond earning, spending, and tracking.
Diamonds are the premium currency in BaoLife.
é    )ÚDictÚAnyÚOptionalN)Úget_database_connection)Ú	player_idÚreasonÚamountÚreturnc                 C   s†  |dkr$t  d| › d|› d¡ dS d}d}z4z¦tƒ }|jdd}| d	|| f¡ | d
| ||f¡ | ¡  | d| f¡ | ¡ }|r|d n|}t  d| › d|› d|› d|› d	¡ W W |rÈ| ¡  |rÔ| ¡  dS  t	yB } zR|rô| 
¡  t jd| › d|› dd W Y d}~W |r(| ¡  |r6| ¡  dS d}~0 0 W |rT| ¡  |r‚| ¡  n|rr| ¡  |r€| ¡  0 dS )a  
    Award diamonds to player with tracking.

    Args:
        player_id: The player's ID
        reason: Reason for awarding diamonds (for logging)
        amount: Number of diamonds to award (must be positive)

    Returns:
        True if successful, False otherwise
    r   ú"Invalid diamond amount for player ú: ú (must be positive)FNT©Ú
dictionaryz9UPDATE players SET diamonds = diamonds + %s WHERE id = %szˆINSERT INTO diamond_transactions
               (player_id, transaction_type, amount, reason)
               VALUES (%s, 'earn', %s, %s)ú*SELECT diamonds FROM players WHERE id = %sÚdiamondsúPlayer z earned ú diamonds: z	 (total: ú)z"Error awarding diamonds to player ©Úexc_info)ÚloggingÚerrorr   ÚcursorÚexecuteÚcommitÚfetchoneÚinfoÚcloseÚ	ExceptionÚrollback)r   r   r   Úconnr   ÚresultZ	new_totalÚe© r#   ú=/var/www/lichun.app/lichun/ws/monetization/diamond_economy.pyÚaward_diamonds   sZ    þü$	ôú
ýr%   c           	      C   s  |dkr*t  d| › d|› d¡ dddœS d}d}z°ztƒ }|jd	d
}| d| f¡ | ¡ }|sŒdddœW W |r~| ¡  |rŠ| ¡  S |d }||k rÎdd|› d|› dœW W |rÀ| ¡  |rÌ| ¡  S | d|| f¡ | d| ||f¡ | ¡  || }t  d| › d|› d|› d|› d	¡ d	d|dœW W |r@| ¡  |rN| ¡  S  t	yÄ } z\|rn| 
¡  t jd| › d|› d	d dddœW  Y d}~W |r¬| ¡  |rº| ¡  S d}~0 0 W |rÖ| ¡  |r| ¡  n|rô| ¡  |r| ¡  0 dS )a  
    Deduct diamonds from player with validation.

    Args:
        player_id: The player's ID
        reason: Reason for deducting diamonds (for logging)
        amount: Number of diamonds to deduct (must be positive)

    Returns:
        dict with 'success' and 'message' keys
    r   r
   r   r   FzInvalid amount)ÚsuccessÚmessageNTr   r   zPlayer not foundr   zInsufficient diamonds. Need z, have z9UPDATE players SET diamonds = diamonds - %s WHERE id = %sz‰INSERT INTO diamond_transactions
               (player_id, transaction_type, amount, reason)
               VALUES (%s, 'spend', %s, %s)r   z spent r   z (remaining: r   zDiamonds deducted successfully)r&   r'   Únew_balancez%Error deducting diamonds from player r   zServer error)r   r   r   r   r   r   r   r   r   r   r   )	r   r   r   r    r   r!   Zcurrent_balancer(   r"   r#   r#   r$   Údeduct_diamondsK   s‚    
*Óþ%Øþü$ýðú
ýr)   )r   r	   c              
   C   sð   d}d}zÈzTt ƒ }|jdd}| d| f¡ | ¡ }|r@|d ndW W |rR| ¡  |r^| ¡  S  ty´ } z>t d| › d|› ¡ W Y d}~W |rœ| ¡  |r¨| ¡  dS d}~0 0 W |rÄ| ¡  |rì| ¡  n|rÞ| ¡  |rê| ¡  0 dS )zŸ
    Get player's current diamond balance.

    Args:
        player_id: The player's ID

    Returns:
        Diamond balance or None if player not found
    NTr   r   r   z)Error getting diamond balance for player r   )r   r   r   r   r   r   r   r   )r   r    r   r!   r"   r#   r#   r$   Úget_diamond_balance˜   s:    
öú
ýr*   é2   )r   Úlimitr	   c              
   C   sä   d}d}z¼zFt ƒ }|jdd}| d| |f¡ | ¡ W W |rD| ¡  |rP| ¡  S  ty¨ } z@t d| › d|› ¡ g W  Y d}~W |r’| ¡  |rž| ¡  S d}~0 0 W |r¸| ¡  |rà| ¡  n|rÒ| ¡  |rÞ| ¡  0 dS )zÐ
    Get player's diamond transaction history.

    Args:
        player_id: The player's ID
        limit: Maximum number of transactions to return

    Returns:
        List of transaction dictionaries
    NTr   zÀSELECT transaction_type, amount, reason, created_at
               FROM diamond_transactions
               WHERE player_id = %s
               ORDER BY created_at DESC
               LIMIT %sz-Error getting transaction history for player r   )r   r   r   Úfetchallr   r   r   r   )r   r,   r    r   r"   r#   r#   r$   Úget_diamond_transaction_history¹   s>    ú	
öú
ýr.   é
   é   é   é   éd   é   é   )Zlevel_upZcomplete_school_yearZgraduate_high_schoolÚgraduate_collegeZget_first_jobÚ	promotionÚ
become_ceoÚget_marriedZhave_first_childZreach_age_milestoneZdaily_loginZweekly_streakZmonthly_streak)r   r	   c                 C   s   t  | d¡S )zª
    Get the diamond reward amount for a specific reason.

    Args:
        reason: The reason/achievement key

    Returns:
        Diamond amount (0 if not found)
    r   )ÚDIAMOND_REWARDSÚget)r   r#   r#   r$   Úget_reward_amountó   s    
r<   )r+   )Ú__doc__Útypingr   r   r   r   Údatabaser   ÚintÚstrÚboolr%   r)   r*   Úlistr.   r:   r<   r#   r#   r#   r$   Ú<module>   s,   >M!*ó