Spaces:
Sleeping
Sleeping
Commit ·
852feb6
1
Parent(s): 1519d8b
new way
Browse files
app.py
CHANGED
|
@@ -81,47 +81,16 @@ def parse_pgn_content(pgn_content):
|
|
| 81 |
return games
|
| 82 |
|
| 83 |
def detect_opening(game):
|
| 84 |
-
"""Detect opening from ECO code or
|
| 85 |
-
eco = game.headers.get("ECO", "")
|
| 86 |
opening = game.headers.get("Opening", "")
|
|
|
|
| 87 |
|
| 88 |
if opening:
|
| 89 |
return opening
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
for move in list(game.mainline_moves())[:6]:
|
| 96 |
-
# Convert to SAN BEFORE pushing the move
|
| 97 |
-
san_moves.append(board.san(move))
|
| 98 |
-
moves.append(move)
|
| 99 |
-
board.push(move)
|
| 100 |
-
|
| 101 |
-
# Basic opening detection
|
| 102 |
-
if len(san_moves) >= 2:
|
| 103 |
-
first_moves = " ".join(san_moves[:4])
|
| 104 |
-
|
| 105 |
-
if "e4 e5" in first_moves:
|
| 106 |
-
return "Open Game (e4 e5)"
|
| 107 |
-
elif "e4 c5" in first_moves:
|
| 108 |
-
return "Sicilian Defense"
|
| 109 |
-
elif "e4 e6" in first_moves:
|
| 110 |
-
return "French Defense"
|
| 111 |
-
elif "e4 c6" in first_moves:
|
| 112 |
-
return "Caro-Kann Defense"
|
| 113 |
-
elif "d4 d5" in first_moves:
|
| 114 |
-
return "Queen's Pawn Game"
|
| 115 |
-
elif "d4 Nf6" in first_moves:
|
| 116 |
-
if "c4" in first_moves:
|
| 117 |
-
return "Indian Defense"
|
| 118 |
-
return "Indian Game"
|
| 119 |
-
elif "Nf3" in first_moves and "d4" not in first_moves:
|
| 120 |
-
return "Reti Opening"
|
| 121 |
-
elif "c4" in first_moves and "d4" not in first_moves:
|
| 122 |
-
return "English Opening"
|
| 123 |
-
|
| 124 |
-
return "Other Opening"
|
| 125 |
|
| 126 |
def analyze_game_detailed(game, username):
|
| 127 |
board = game.board()
|
|
@@ -141,6 +110,19 @@ def analyze_game_detailed(game, username):
|
|
| 141 |
result = game.headers.get("Result", "*")
|
| 142 |
opening = detect_opening(game)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
material_values = {chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9}
|
| 145 |
|
| 146 |
def count_material(board):
|
|
@@ -190,7 +172,8 @@ def analyze_game_detailed(game, username):
|
|
| 190 |
'mistakes': mistakes,
|
| 191 |
'opening': opening,
|
| 192 |
'result': result,
|
| 193 |
-
'user_color': user_color
|
|
|
|
| 194 |
}
|
| 195 |
|
| 196 |
def categorize_mistakes(all_mistakes):
|
|
@@ -247,35 +230,8 @@ def fetch_lichess_puzzles(count=5):
|
|
| 247 |
except:
|
| 248 |
pass
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
|
| 252 |
-
{
|
| 253 |
-
'id': 'default_1',
|
| 254 |
-
'fen': 'r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 0 1',
|
| 255 |
-
'moves': ['h5f7'],
|
| 256 |
-
'rating': 1200,
|
| 257 |
-
'themes': ['fork', 'tactics']
|
| 258 |
-
},
|
| 259 |
-
{
|
| 260 |
-
'id': 'default_2',
|
| 261 |
-
'fen': 'r1bqk2r/ppp2ppp/2n5/3np1N1/1b1P4/2N5/PPP1QPPP/R1B1KB1R w KQkq - 0 1',
|
| 262 |
-
'moves': ['g5f7'],
|
| 263 |
-
'rating': 1300,
|
| 264 |
-
'themes': ['fork', 'knight']
|
| 265 |
-
},
|
| 266 |
-
{
|
| 267 |
-
'id': 'default_3',
|
| 268 |
-
'fen': '6k1/5ppp/8/8/8/8/2R2PPP/6K1 w - - 0 1',
|
| 269 |
-
'moves': ['c2c8'],
|
| 270 |
-
'rating': 1000,
|
| 271 |
-
'themes': ['endgame', 'rook']
|
| 272 |
-
}
|
| 273 |
-
]
|
| 274 |
-
|
| 275 |
-
while len(puzzles) < count:
|
| 276 |
-
puzzles.append(default_puzzles[len(puzzles) % len(default_puzzles)])
|
| 277 |
-
|
| 278 |
-
return puzzles[:count]
|
| 279 |
|
| 280 |
def create_board_svg(fen, size=400):
|
| 281 |
"""Create SVG representation of chess position"""
|
|
@@ -363,31 +319,23 @@ def analyze_games(username, pgn_file):
|
|
| 363 |
all_mistakes.extend(analysis['mistakes'])
|
| 364 |
|
| 365 |
opening = analysis['opening']
|
| 366 |
-
|
| 367 |
user_color = analysis['user_color']
|
| 368 |
|
| 369 |
opening_stats[opening]['total'] += 1
|
| 370 |
|
| 371 |
-
if
|
|
|
|
| 372 |
color_key = 'white' if user_color == chess.WHITE else 'black'
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
if user_color == chess.BLACK:
|
| 383 |
-
opening_stats[opening]['wins'] += 1
|
| 384 |
-
color_stats[color_key]['wins'] += 1
|
| 385 |
-
else:
|
| 386 |
-
opening_stats[opening]['losses'] += 1
|
| 387 |
-
color_stats[color_key]['losses'] += 1
|
| 388 |
-
elif result == "1/2-1/2":
|
| 389 |
-
opening_stats[opening]['draws'] += 1
|
| 390 |
-
color_stats[color_key]['draws'] += 1
|
| 391 |
|
| 392 |
weaknesses = categorize_mistakes(all_mistakes)
|
| 393 |
|
|
@@ -442,6 +390,20 @@ def analyze_games(username, pgn_file):
|
|
| 442 |
# Fetch puzzles
|
| 443 |
puzzles = fetch_lichess_puzzles(5)
|
| 444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
puzzle_svgs = []
|
| 446 |
puzzle_info = []
|
| 447 |
|
|
|
|
| 81 |
return games
|
| 82 |
|
| 83 |
def detect_opening(game):
|
| 84 |
+
"""Detect opening from ECO code or Opening header"""
|
|
|
|
| 85 |
opening = game.headers.get("Opening", "")
|
| 86 |
+
eco = game.headers.get("ECO", "")
|
| 87 |
|
| 88 |
if opening:
|
| 89 |
return opening
|
| 90 |
+
elif eco:
|
| 91 |
+
return f"ECO {eco}"
|
| 92 |
+
else:
|
| 93 |
+
return "Unknown Opening"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def analyze_game_detailed(game, username):
|
| 96 |
board = game.board()
|
|
|
|
| 110 |
result = game.headers.get("Result", "*")
|
| 111 |
opening = detect_opening(game)
|
| 112 |
|
| 113 |
+
# Determine user result
|
| 114 |
+
user_won = None
|
| 115 |
+
user_result = None
|
| 116 |
+
if user_color is not None and result != "*":
|
| 117 |
+
if result == "1-0":
|
| 118 |
+
user_won = (user_color == chess.WHITE)
|
| 119 |
+
user_result = "win" if user_won else "loss"
|
| 120 |
+
elif result == "0-1":
|
| 121 |
+
user_won = (user_color == chess.BLACK)
|
| 122 |
+
user_result = "win" if user_won else "loss"
|
| 123 |
+
elif result == "1/2-1/2":
|
| 124 |
+
user_result = "draw"
|
| 125 |
+
|
| 126 |
material_values = {chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9}
|
| 127 |
|
| 128 |
def count_material(board):
|
|
|
|
| 172 |
'mistakes': mistakes,
|
| 173 |
'opening': opening,
|
| 174 |
'result': result,
|
| 175 |
+
'user_color': user_color,
|
| 176 |
+
'user_result': user_result
|
| 177 |
}
|
| 178 |
|
| 179 |
def categorize_mistakes(all_mistakes):
|
|
|
|
| 230 |
except:
|
| 231 |
pass
|
| 232 |
|
| 233 |
+
# If we couldn't fetch puzzles, return empty list
|
| 234 |
+
return puzzles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
def create_board_svg(fen, size=400):
|
| 237 |
"""Create SVG representation of chess position"""
|
|
|
|
| 319 |
all_mistakes.extend(analysis['mistakes'])
|
| 320 |
|
| 321 |
opening = analysis['opening']
|
| 322 |
+
user_result = analysis.get('user_result')
|
| 323 |
user_color = analysis['user_color']
|
| 324 |
|
| 325 |
opening_stats[opening]['total'] += 1
|
| 326 |
|
| 327 |
+
if user_result == 'win':
|
| 328 |
+
opening_stats[opening]['wins'] += 1
|
| 329 |
color_key = 'white' if user_color == chess.WHITE else 'black'
|
| 330 |
+
color_stats[color_key]['wins'] += 1
|
| 331 |
+
elif user_result == 'loss':
|
| 332 |
+
opening_stats[opening]['losses'] += 1
|
| 333 |
+
color_key = 'white' if user_color == chess.WHITE else 'black'
|
| 334 |
+
color_stats[color_key]['losses'] += 1
|
| 335 |
+
elif user_result == 'draw':
|
| 336 |
+
opening_stats[opening]['draws'] += 1
|
| 337 |
+
color_key = 'white' if user_color == chess.WHITE else 'black'
|
| 338 |
+
color_stats[color_key]['draws'] += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
weaknesses = categorize_mistakes(all_mistakes)
|
| 341 |
|
|
|
|
| 390 |
# Fetch puzzles
|
| 391 |
puzzles = fetch_lichess_puzzles(5)
|
| 392 |
|
| 393 |
+
if not puzzles:
|
| 394 |
+
# If no puzzles fetched, show Lichess link
|
| 395 |
+
return (
|
| 396 |
+
full_report,
|
| 397 |
+
ai_report,
|
| 398 |
+
"## 🧩 Masalalar\n\nMasalalarni [Lichess.org](https://lichess.org/training) saytidan ishlang",
|
| 399 |
+
"",
|
| 400 |
+
None,
|
| 401 |
+
"",
|
| 402 |
+
None,
|
| 403 |
+
"",
|
| 404 |
+
None
|
| 405 |
+
)
|
| 406 |
+
|
| 407 |
puzzle_svgs = []
|
| 408 |
puzzle_info = []
|
| 409 |
|