Files
cm/game_balance/validator.py
2026-02-11 17:44:26 +03:00

124 lines
5.2 KiB
Python

"""
ВАЛИДАТОР БАЛАНСА
Проверяет корректность настроек
"""
class BalanceValidator:
@staticmethod
def validate_assets(assets):
"""Проверка конфигурации активов"""
errors = []
if not isinstance(assets, dict):
errors.append("Assets config must be a dictionary")
return errors
for asset_id, asset_data in assets.items():
if not isinstance(asset_data, dict):
errors.append(f"Asset {asset_id}: data must be a dictionary")
continue
# Проверка обязательных полей
required_fields = ['name', 'category', 'base_price', 'volatility']
for field in required_fields:
if field not in asset_data:
errors.append(f"Asset {asset_id}: missing required field '{field}'")
# Проверка цены
price = asset_data.get('base_price', 0)
if not isinstance(price, (int, float)) or price <= 0:
errors.append(f"Asset {asset_id}: base_price must be positive number")
# Проверка волатильности
volatility = asset_data.get('volatility', 0)
if not isinstance(volatility, (int, float)) or volatility < 0 or volatility > 1:
errors.append(f"Asset {asset_id}: volatility must be between 0 and 1")
# Проверка доходности
income = asset_data.get('income_per_month', 0)
if not isinstance(income, (int, float)) or income < 0 or income > 0.5:
errors.append(f"Asset {asset_id}: income_per_month must be between 0 and 0.5")
# Проверка количества
total = asset_data.get('total_quantity')
if total is not None:
if not isinstance(total, (int, float)) or total <= 0:
errors.append(f"Asset {asset_id}: total_quantity must be positive or None")
return errors
@staticmethod
def validate_economy(economy_config):
"""Проверка экономической конфигурации"""
errors = []
if not isinstance(economy_config, dict):
errors.append("Economy config must be a dictionary")
return errors
# Проверка налогов
tax_system = economy_config.get('TAX_SYSTEM', {})
income_tax = tax_system.get('income_tax', [])
if not isinstance(income_tax, list):
errors.append("income_tax must be a list")
else:
last_threshold = -1
for i, bracket in enumerate(income_tax):
if not isinstance(bracket, dict):
errors.append(f"Tax bracket {i}: must be a dictionary")
continue
threshold = bracket.get('threshold', 0)
if threshold <= last_threshold:
errors.append(f"Tax brackets must be in ascending order at index {i}")
last_threshold = threshold
# Проверка кредитов
loan_system = economy_config.get('LOAN_SYSTEM', {})
max_multiplier = loan_system.get('max_loan_multiplier', 0)
if not isinstance(max_multiplier, (int, float)) or max_multiplier <= 0:
errors.append("max_loan_multiplier must be positive")
# Проверка корреляций
correlations = economy_config.get('ASSET_CORRELATIONS', {})
if not isinstance(correlations, dict):
errors.append("ASSET_CORRELATIONS must be a dictionary")
else:
for key, value in correlations.items():
if not isinstance(key, str):
errors.append(f"Correlation key must be string, got {type(key)}")
if not isinstance(value, (int, float)) or value < -1 or value > 1:
errors.append(f"Correlation value must be between -1 and 1, got {value}")
return errors
@staticmethod
def validate_balance_mode(balance):
"""Полная проверка режима баланса"""
all_errors = []
# Проверяем наличие необходимых методов
required_methods = ['get_assets_config', 'get_players_config', 'get_economy_config']
for method in required_methods:
if not hasattr(balance, method):
all_errors.append(f"Balance mode missing required method: {method}")
return all_errors
try:
# Проверка активов
assets = balance.get_assets_config()
all_errors.extend(BalanceValidator.validate_assets(assets))
# Проверка экономики
economy = balance.get_economy_config()
all_errors.extend(BalanceValidator.validate_economy(economy))
# Проверка конфигурации игроков
players = balance.get_players_config()
if not isinstance(players, dict):
all_errors.append("Players config must be a dictionary")
except Exception as e:
all_errors.append(f"Error validating balance: {str(e)}")
return all_errors