Fix _set_match_status_and_result to properly handle UNDER/OVER results

- When result is UNDER/OVER, store it in under_over_result field
- Extract actual fight winner from winning_outcomes or match outcomes
- Prevents match.result from being incorrectly set to UNDER/OVER
- This fixes the recurring bug where result field showed UNDER/OVER instead of actual fight winner
parent 427df498
......@@ -3407,7 +3407,13 @@ class GamesThread(ThreadedComponent):
logger.error(f"Failed to set match status: {e}")
def _set_match_status_and_result(self, match_id: int, status: str, result: str):
"""Set match status and result in database"""
"""Set match status and result in database
NOTE: This function is called early with UNDER/OVER results from video playback.
The result field should contain the actual fight winner (WIN1, DRAW, KO1, etc.),
not UNDER/OVER which is stored in under_over_result field.
"""
import json
try:
logger.info(f"DEBUG _set_match_status_and_result: Called with match_id={match_id}, status='{status}', result='{result}'")
......@@ -3417,17 +3423,59 @@ class GamesThread(ThreadedComponent):
if match:
logger.info(f"DEBUG _set_match_status_and_result: Found match {match_id}, current status='{match.status}', current result='{match.result}'")
match.status = status
# Handle UNDER/OVER results properly
# The match.result should contain the actual fight winner, not UNDER/OVER
if result in ['UNDER', 'OVER']:
logger.info(f"DEBUG _set_match_status_and_result: Result is UNDER/OVER, storing in under_over_result field")
match.under_over_result = result
# Try to find the actual fight winner from winning_outcomes
actual_result = None
if match.winning_outcomes:
try:
winning_outcomes = json.loads(match.winning_outcomes)
if isinstance(winning_outcomes, list):
for outcome in winning_outcomes:
if outcome not in ['UNDER', 'OVER']:
actual_result = outcome
logger.info(f"DEBUG _set_match_status_and_result: Found fight winner '{actual_result}' from winning_outcomes")
break
except (json.JSONDecodeError, TypeError):
pass
# If no winning_outcomes, try to find from match outcomes
if not actual_result:
match_outcomes = session.query(MatchOutcomeModel).filter(
MatchOutcomeModel.match_id == match_id
).all()
for outcome in match_outcomes:
if outcome.column_name not in ['UNDER', 'OVER']:
actual_result = outcome.column_name
logger.info(f"DEBUG _set_match_status_and_result: Found fight winner '{actual_result}' from match outcomes")
break
if actual_result:
match.result = actual_result
logger.info(f"DEBUG _set_match_status_and_result: Set match.result to fight winner '{actual_result}'")
else:
# Keep existing result if we can't find a fight winner
logger.warning(f"DEBUG _set_match_status_and_result: Could not find fight winner, keeping existing result '{match.result}'")
else:
# Result is not UNDER/OVER, set it directly
match.result = result
logger.info(f"DEBUG _set_match_status_and_result: Set match.result to '{result}'")
# Set end_time when match is completed
if status == 'done':
match.end_time = datetime.utcnow()
logger.info(f"DEBUG _set_match_status_and_result: Set end_time for match {match_id}")
session.commit()
logger.info(f"Updated match {match_id} status to {status} and result to {result}")
logger.info(f"Updated match {match_id} status to {status} and result to {match.result}")
# DEBUG: Verify the update
session.refresh(match)
logger.info(f"DEBUG _set_match_status_and_result: After commit - match.status='{match.status}', match.result='{match.result}'")
logger.info(f"DEBUG _set_match_status_and_result: After commit - match.status='{match.status}', match.result='{match.result}', match.under_over_result='{match.under_over_result}'")
else:
logger.error(f"Match {match_id} not found")
finally:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment