Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -223,7 +223,8 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 223 |
player_is_white = (request.player_color.lower() == "white")
|
| 224 |
|
| 225 |
for i, san_move in enumerate(request.moves):
|
| 226 |
-
|
|
|
|
| 227 |
|
| 228 |
# The current_score is the score BEFORE this move
|
| 229 |
score_before = current_score
|
|
@@ -242,47 +243,41 @@ async def analyze_game(request: AnalyzeRequest):
|
|
| 242 |
# Update current score for next iteration
|
| 243 |
current_score = score_after
|
| 244 |
|
| 245 |
-
#
|
| 246 |
-
if
|
| 247 |
-
|
| 248 |
-
# If player is White, positive score is good. If White drops from +100 to +50 -> CPL = 50.
|
| 249 |
-
# If player is Black, negative score is good. If Black rises from -100 to -50 -> CPL = 50.
|
| 250 |
-
if player_is_white:
|
| 251 |
-
cpl = max(0, score_before - score_after)
|
| 252 |
-
else:
|
| 253 |
-
cpl = max(0, score_after - score_before)
|
| 254 |
-
|
| 255 |
-
# Cap CPL to 1000 so one massive blunder doesn't utterly ruin the stats
|
| 256 |
-
cpl = min(cpl, 1000.0)
|
| 257 |
|
|
|
|
|
|
|
| 258 |
total_cpl += cpl
|
| 259 |
player_moves_count += 1
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
| 275 |
counts[cls] += 1
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
|
| 287 |
# Win probability matching accuracy formula
|
| 288 |
# Accuracy = 100 * exp(-0.02 * avg_cpl) smoothed
|
|
|
|
| 223 |
player_is_white = (request.player_color.lower() == "white")
|
| 224 |
|
| 225 |
for i, san_move in enumerate(request.moves):
|
| 226 |
+
is_white_turn = board.turn == chess.WHITE
|
| 227 |
+
is_player_turn = is_white_turn if player_is_white else not is_white_turn
|
| 228 |
|
| 229 |
# The current_score is the score BEFORE this move
|
| 230 |
score_before = current_score
|
|
|
|
| 243 |
# Update current score for next iteration
|
| 244 |
current_score = score_after
|
| 245 |
|
| 246 |
+
# Calculate Centipawn Loss (diff between score before and score after)
|
| 247 |
+
cpl = max(0, score_before - score_after) if is_white_turn else max(0, score_after - score_before)
|
| 248 |
+
cpl = min(cpl, 1000.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
+
# Only track these stats for the requested player
|
| 251 |
+
if is_player_turn:
|
| 252 |
total_cpl += cpl
|
| 253 |
player_moves_count += 1
|
| 254 |
+
|
| 255 |
+
# Classification mapping
|
| 256 |
+
if cpl <= 15:
|
| 257 |
+
cls = "Best"
|
| 258 |
+
elif cpl <= 35:
|
| 259 |
+
cls = "Excellent"
|
| 260 |
+
elif cpl <= 75:
|
| 261 |
+
cls = "Good"
|
| 262 |
+
elif cpl <= 150:
|
| 263 |
+
cls = "Inaccuracy"
|
| 264 |
+
elif cpl <= 300:
|
| 265 |
+
cls = "Mistake"
|
| 266 |
+
else:
|
| 267 |
+
cls = "Blunder"
|
| 268 |
+
|
| 269 |
+
if is_player_turn:
|
| 270 |
counts[cls] += 1
|
| 271 |
+
|
| 272 |
+
analysis_results.append(MoveAnalysis(
|
| 273 |
+
move_num=i+1,
|
| 274 |
+
san=san_move,
|
| 275 |
+
fen=board.fen(),
|
| 276 |
+
classification=cls,
|
| 277 |
+
cpl=cpl,
|
| 278 |
+
score_before=score_before / 100.0,
|
| 279 |
+
score_after=score_after / 100.0
|
| 280 |
+
))
|
| 281 |
|
| 282 |
# Win probability matching accuracy formula
|
| 283 |
# Accuracy = 100 * exp(-0.02 * avg_cpl) smoothed
|