Amogh1221 commited on
Commit
7b994ac
·
verified ·
1 Parent(s): 9d40400

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -38
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
- is_player_turn = board.turn == chess.WHITE if player_is_white else board.turn == chess.BLACK
 
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
- # Only analyze the player's moves
246
- if is_player_turn:
247
- # Calculate Centipawn Loss (diff between score before and score after)
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
- # Classification mapping
262
- if cpl <= 15:
263
- cls = "Best"
264
- elif cpl <= 35:
265
- cls = "Excellent"
266
- elif cpl <= 75:
267
- cls = "Good"
268
- elif cpl <= 150:
269
- cls = "Inaccuracy"
270
- elif cpl <= 300:
271
- cls = "Mistake"
272
- else:
273
- cls = "Blunder"
274
-
 
275
  counts[cls] += 1
276
-
277
- analysis_results.append(MoveAnalysis(
278
- move_num=i+1,
279
- san=san_move,
280
- fen=board.fen(),
281
- classification=cls,
282
- cpl=cpl,
283
- score_before=score_before / 100.0,
284
- score_after=score_after / 100.0
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