a
    ng                     @   sR   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ G dd dZ	dd Z
dS )    N)DictAnySet)Pathc                   @   s4   e Zd Zdd Zdd Zeeef edddZ	dS )	GameSettingsValidatorc              
   C   st   t tj}|d d d d | _t | _i | _z|   W n4 tyn } zt	d|   W Y d }~n
d }~0 0 d S )Nsrc
componentsgameszgameSettings.jszError initializing validator: )
r   __file__parentsettings_pathsetr	   Zglobal_settingsload_settings	Exceptionprint)selfcurrent_dire r   -/var/www/html/idle/game_settings_validator.py__init__   s    
zGameSettingsValidator.__init__c           	   
   C   s   zt | jddd}| }W d   n1 s00    Y  |dd}td|}|s^tdd	}t||d
}|D ]&}|d
}|dkrx| j	
|  qxtd| j	  W n4 ty } ztd|   W Y d}~n
d}~0 0 dS )z(Load the games list from gameSettings.jsrzutf-8)encodingN
 zGAME_SETTINGS\s*=\s*{(.*)};\s*$z#Could not find GAME_SETTINGS objectz([A-Z]+):\s*{\s*ICON:   GLOBALzFound games: zError loading settings: )openr   readreplaceresearch
ValueErrorfinditergroupr	   addlowerr   r   )	r   filecontentZsettings_matchZgame_patternZgames_matchesmatchZ	game_namer   r   r   r   r      s"    &
z#GameSettingsValidator.load_settings)	save_datareturnc              
   C   sN  z | di }h d}|t|  }|r:td| | di }| jD ]2}| d}t| |dttfsLt| dqL| di }| jD ]R}||vrtd	| || }	h d
}
|
t|	  }|rtd| d| q| di }| jD ]T}||vrtd| || }h d}|t|  }|rtd| d| q| di }| jD ]N}||vr|td| || }t| ddttfs`td| dq`t| ddtstd| dd }|r|| jvrtd| dW dS  t	yH } z,t
d|  tdt| W Y d}~n
d}~0 0 dS ) zValidate save data structurestate>   r	   exchangeRatespoints
automationselectedGamecoinszMissing required fields: r.   ZPointsr   z must be a numberr	   zMissing game configuration for >   sizeunlockedcostr,   nameZiconzGame z missing required fields: r/   z%Missing automation configuration for >   ZrequiredCoinsr3   levelZpointsPerSecondr4   zAutomation config for z missing fields: r-   z(Missing exchange rate configuration for ZbaseRatezExchange rate baseRate for r1   zCoins must be an integerr0    zSelected game 'z' not validTzValidation error: zSave data validation failed: N)getr   keysr"   r	   
isinstanceintfloatr&   r   r   str)r   r*   r,   required_fieldsZmissing_fieldsr.   ZgameZ
points_keyr	   Zgame_configZrequired_game_fieldsZmissing_game_fieldsr/   Zauto_configZrequired_auto_fieldsZmissing_auto_fieldsZexchange_ratesZrate_configZselected_gamer   r   r   r   validate_save_data.   sZ    






z(GameSettingsValidator.validate_save_dataN)
__name__
__module____qualname__r   r   r   r=   r   boolr?   r   r   r   r   r      s   r   c                   C   s   t  S )N)r   r   r   r   r   get_game_validatorn   s    rD   )osr    jsontypingr   r   r   pathlibr   r   rD   r   r   r   r   <module>   s   g