#!/usr/bin/env python # -*- coding: utf-8 -*- """ 检查所有相关文件中的权重默认值是否已从5改为100 """ import os import re import logging from colorama import init, Fore, Style # 初始化colorama init() # 配置日志 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger('check_weights') # 需要检查的文件和目录 CHECK_PATHS = [ 'templates/admin/characters.html', 'templates/weight_management.html', 'static/js/admin/characters.js', 'static/js/game.js', 'app_sqlite.py', ] # 权重相关的正则表达式模式 WEIGHT_PATTERNS = [ # HTML中的默认值 (r'id="character-weight"[^>]*value="(\d+)"', 'HTML表单默认值'), (r'min="1" max="(\d+)" value="(\d+)"', 'HTML数字输入范围'), # JavaScript中的默认值引用 (r'character\.weight \|\| (\d+)', 'JS默认权重引用'), (r'weight: parseInt\([^)]+\) \|\| (\d+)', 'JS解析默认权重'), # Python中的默认值 (r"character\['weight'\] = (\d+)", 'Python默认权重赋值'), # 描述文本中的范围 (r'权重 \(1-(\d+)\)', '权重范围描述'), (r'默认值为(\d+)', '默认值描述'), # 范围验证 (r'weight < 1 \|\| weight > (\d+)', 'JS权重验证'), ] def check_file(file_path): """检查单个文件中的权重默认值""" if not os.path.exists(file_path): logger.warning(f"文件不存在: {file_path}") return [], [] with open(file_path, 'r', encoding='utf-8') as f: content = f.read() correct_matches = [] incorrect_matches = [] # 检查所有权重模式 for pattern, description in WEIGHT_PATTERNS: matches = re.finditer(pattern, content) for match in matches: line_num = content[:match.start()].count('\n') + 1 line_text = content.splitlines()[line_num-1] # 根据不同的模式检查不同的组 if 'max' in pattern and 'value' in pattern: # 处理同时有max和value的情况 max_val = match.group(1) value = match.group(2) if max_val == '1000' and value == '100': correct_matches.append((file_path, line_num, description, line_text)) else: incorrect_matches.append((file_path, line_num, description, line_text)) else: # 普通情况,检查第一个捕获组 weight_value = match.group(1) if description == '权重范围描述' and weight_value == '1000': correct_matches.append((file_path, line_num, description, line_text)) elif weight_value == '100': correct_matches.append((file_path, line_num, description, line_text)) else: incorrect_matches.append((file_path, line_num, description, line_text)) return correct_matches, incorrect_matches def main(): """主函数""" logger.info("开始检查权重默认值") all_correct_matches = [] all_incorrect_matches = [] # 查找所有匹配的文件 for path in CHECK_PATHS: if os.path.isdir(path): # 处理目录 for root, _, files in os.walk(path): for file in files: if file.endswith(('.html', '.js', '.py', '.css')): file_path = os.path.join(root, file) correct, incorrect = check_file(file_path) all_correct_matches.extend(correct) all_incorrect_matches.extend(incorrect) else: # 处理单个文件 correct, incorrect = check_file(path) all_correct_matches.extend(correct) all_incorrect_matches.extend(incorrect) # 输出结果 print("\n" + "="*80) print(f"{Fore.GREEN}正确的权重默认值 (100 或 1000): {len(all_correct_matches)}{Style.RESET_ALL}") for file_path, line_num, desc, line_text in all_correct_matches: print(f"{Fore.GREEN}✓ {file_path}:{line_num} - {desc}{Style.RESET_ALL}") print(f" {line_text.strip()}") print("\n" + "="*80) print(f"{Fore.RED}不正确的权重默认值 (非 100 或 1000): {len(all_incorrect_matches)}{Style.RESET_ALL}") for file_path, line_num, desc, line_text in all_incorrect_matches: print(f"{Fore.RED}✗ {file_path}:{line_num} - {desc}{Style.RESET_ALL}") print(f" {line_text.strip()}") # 总结 print("\n" + "="*80) if len(all_incorrect_matches) == 0: print(f"{Fore.GREEN}所有权重相关默认值检查通过!{Style.RESET_ALL}") else: print(f"{Fore.YELLOW}发现 {len(all_incorrect_matches)} 个需要修改的地方。{Style.RESET_ALL}") return len(all_incorrect_matches) == 0 if __name__ == "__main__": main()