|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +def get_game_description(game_path): |
| 8 | + readme_path = os.path.join(game_path, 'README.md') |
| 9 | + if os.path.exists(readme_path): |
| 10 | + try: |
| 11 | + with open(readme_path, 'r', encoding='utf-8') as f: |
| 12 | + content = f.read() |
| 13 | + lines = content.split('\n') |
| 14 | + for line in lines: |
| 15 | + line = line.strip() |
| 16 | + if line and not line.startswith('#') and len(line) > 10: |
| 17 | + clean_line = re.sub(r'[*_`]', '', line) |
| 18 | + return clean_line |
| 19 | + except: |
| 20 | + pass |
| 21 | + |
| 22 | + for file in os.listdir(game_path): |
| 23 | + if file.lower().endswith(('.py', '.js', '.java', '.cpp')): |
| 24 | + try: |
| 25 | + with open(os.path.join(game_path, file), 'r', encoding='utf-8') as f: |
| 26 | + content = f.read(1000) |
| 27 | + lines = content.split('\n')[:15] |
| 28 | + for line in lines: |
| 29 | + line = line.strip() |
| 30 | + if line.startswith('#'): |
| 31 | + desc = line[1:].strip() |
| 32 | + if len(desc) > 10 and not any(word in desc.lower() for word in |
| 33 | + ['!/usr/bin/env', 'coding:', 'author:', 'date:', 'copyright']): |
| 34 | + return desc |
| 35 | + elif line.startswith('//'): |
| 36 | + desc = line[2:].strip() |
| 37 | + if len(desc) > 10 and 'license' not in desc.lower(): |
| 38 | + return desc |
| 39 | + elif '/*' in line or line.startswith('*'): |
| 40 | + desc = re.sub(r'/\*|\*/|\*', '', line).strip() |
| 41 | + if len(desc) > 10: |
| 42 | + return desc |
| 43 | + except: |
| 44 | + continue |
| 45 | + |
| 46 | + return "A fun game built with core programming concepts" |
| 47 | + |
| 48 | +def scan_games(): |
| 49 | + languages = ['Python', 'Java', 'Javascript'] |
| 50 | + games = {} |
| 51 | + |
| 52 | + for lang in languages: |
| 53 | + if os.path.exists(lang): |
| 54 | + games[lang] = [] |
| 55 | + for item in os.listdir(lang): |
| 56 | + item_path = os.path.join(lang, item) |
| 57 | + if os.path.isdir(item_path): |
| 58 | + description = get_game_description(item_path) |
| 59 | + games[lang].append({ |
| 60 | + 'name': item, |
| 61 | + 'path': f"./{item_path}", |
| 62 | + 'description': description |
| 63 | + }) |
| 64 | + |
| 65 | + return games |
| 66 | + |
| 67 | +def generate_index(games): |
| 68 | + content = [] |
| 69 | + content.append("# 🎮 Game Scripts Index") |
| 70 | + content.append("") |
| 71 | + content.append("Welcome to Game_Scripts — an open-source collection of mini games!") |
| 72 | + content.append("This index automatically tracks all games across different programming languages.") |
| 73 | + content.append(f"Last updated: {os.popen('date').read().strip() if os.name != 'nt' else 'N/A'}") |
| 74 | + content.append("") |
| 75 | + content.append(f"Tracked {sum(len(v) for v in games.values())} games across {len(games)} languages.") |
| 76 | + content.append("") |
| 77 | + |
| 78 | + content.append("## 📚 Table of Contents") |
| 79 | + for lang in sorted(games.keys()): |
| 80 | + if games[lang]: |
| 81 | + content.append(f"- [{lang.title()}](#{lang.lower()}-games)") |
| 82 | + |
| 83 | + content.append("") |
| 84 | + |
| 85 | + for lang in sorted(games.keys()): |
| 86 | + if games[lang]: |
| 87 | + content.append(f"## {lang.title()} Games") |
| 88 | + content.append("") |
| 89 | + |
| 90 | + for game in sorted(games[lang], key=lambda x: x['name'].lower()): |
| 91 | + content.append(f"### 🎯 [{game['name']}]({game['path']}/)") |
| 92 | + content.append(f"{game['description']}") |
| 93 | + content.append("") |
| 94 | + |
| 95 | + return "\n".join(content) |
| 96 | + |
| 97 | +def main(): |
| 98 | + print("🔍 Scanning for games...") |
| 99 | + games = scan_games() |
| 100 | + |
| 101 | + print("📝 Generating INDEX.md...") |
| 102 | + index_content = generate_index(games) |
| 103 | + |
| 104 | + with open('INDEX.md', 'w', encoding='utf-8') as f: |
| 105 | + f.write(index_content) |
| 106 | + |
| 107 | + total_games = sum(len(v) for v in games.values()) |
| 108 | + print(f"✅ INDEX.md updated successfully!") |
| 109 | + print(f"📊 Tracked {total_games} games across {len(games)} languages.") |
| 110 | + |
| 111 | + if total_games > 0: |
| 112 | + print("\n📋 Games found:") |
| 113 | + for lang, game_list in games.items(): |
| 114 | + if game_list: |
| 115 | + print(f" {lang.title()}: {len(game_list)} games") |
| 116 | + for game in game_list[:3]: |
| 117 | + print(f" - {game['name']}") |
| 118 | + if len(game_list) > 3: |
| 119 | + print(f" ... and {len(game_list) - 3} more") |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments