Skip to content

Restore profile tournament visibility for CPU-involved brackets - #21

Merged
Horus0305 merged 2 commits into
mainfrom
copilot/fix-tournament-visibility-issue
Jun 3, 2026
Merged

Horus0305 merged 2 commits into
mainfrom
copilot/fix-tournament-visibility-issue

Conversation

Copilot AI commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

User profiles were missing tournaments whenever CPU participants were present due to overly restrictive tournament-query filtering. This change keeps CPU exclusion scoped to leaderboard aggregation only, while returning all tournaments a user actually participated in.

  • Tournament history query path

    • Updated get_user_tournaments() to avoid SQL pre-filtering on TournamentHistory.players.contains(username), which could exclude valid rows.
    • Retains exact participation check against parsed TournamentHistory.players JSON (if username in players) so profile history includes CPU-involved tournaments without widening results to non-participants.
  • Leaderboard CPU filtering scope

    • No change to leaderboard logic; CPU exclusion remains where intended (FormatStats.format != 'cpu' in /api/leaderboard).
  • Tournament persistence coverage

    • Added focused regression coverage to confirm save_tournament_history() persists full participant lists (human + CPU) in TournamentHistory.players.
    • Added endpoint-level regression coverage for user tournament retrieval including CPU-involved tournaments.
# stats.py
rows = (
    db.query(TournamentHistory)
    .order_by(TournamentHistory.timestamp.desc())
    .all()
)

for t in rows:
    players = _json_list(t.players)
    if username in players:
        out.append(t.to_dict())
Original prompt

Tournament Visibility Bug: CPU-Involved Tournaments Not Showing in User Profile

Issue: Tournaments where the CPU reached the final (or participated at all) are not visible in the user profile, even though the human player participated.

Root Cause: In CricketGame/backend/api/stats.py, the get_user_tournaments() endpoint (lines 96-114) should return all tournaments a player participated in, regardless of CPU involvement. However, the implementation may be incorrectly filtered.

Additionally, a CPU involvement filter was added to global stats calculations, which may be interfering with tournament visibility. The get_user_tournaments() endpoint should NOT apply CPU filtering—only the leaderboard should exclude CPU-involved matches.

Expected Behavior:

  • Players should see ALL tournaments they participated in on their profile
  • This includes tournaments where CPU players also participated
  • The CPU exclusion filter should ONLY apply to /api/leaderboard (line 263: format != 'cpu'), not to individual player tournament history

Fix Required:

  1. Ensure get_user_tournaments() returns ALL tournaments where a player participated, with NO CPU filtering
  2. Verify tournament records are being saved correctly in save_tournament_history()
  3. Ensure the TournamentHistory.players JSON list includes both human and CPU players
  4. Keep the CPU filter ONLY for leaderboard calculations, NOT for individual player records

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Currently the tournaments where the CPU reached final, no matter who won, are not visible in the user profile, find the issue

Assistant: I'll help you find the issue about tournaments where the CPU reached the final not being visible in the user profile. Let me search for this in your repository.

Assistant: Let me search more broadly for issues related to tournaments and user profiles:

User: Currently the tournaments where the CPU reached final, no matter who won, are not visible in the user profile, find th issue and please fix that,I did implement something so that the start where the CPU is involved too much are not counted in the global stats so maybe that is interfering with the whole tournament issue right now

Assistant: I'll help you find and fix this issue. Let me start by exploring the repository to understand the codebase structure and locate the relevant code for tournaments and CPU stats.

Assistant: Let me try with lexical search instead:

Assistant: Let me get more details on the tournament history and stats retrieval:

Assistant: Now I can see the issue! In the get_user_tournaments endpoint (lines 96-114), the function filters tournaments by checking if the username is in the players list. However, when you look at line 100:

.filter(TournamentHistory.players.contains(username))

This uses a string containment check on the JSON field. This works, but then on line 110, there's a secondary check:

if username in players:

The problem is likely that tournaments where the CPU reached the final are not being saved to the TournamentHistory table properly, or they're being filtered out somewhere. Let me check the save logic and the match stats logic:

Assistant: Looking at the code, I found the issue! In the get_user_tournaments function in CricketGame/backend/api/stats.py, tournaments where the CPU reached the final are being filtered out due to a check that was implemented to exclude matches where the CPU is "too involved" in global stats.

The Problem

The issue is in CricketGame/backend/api/stats.py at lines 96-114. The get_user_tournaments function queries tournaments by checking if a username is in the players list:

@router.get("/tournaments/{username}")
def get_user_tournaments(username: str, limit: int = 100, db: Session = Depends(get_db)):
    rows = (
        db.query(TournamentHistory)
        .filter(TournamentHistory.players.contains(username))
        .order_by(TournamentHistory.timestamp.desc())
        .all()
    )

However, this doesn't account for tournaments where CPU players participated. When you implemented the CPU involvement filtering in global stats (around line 263 in the same file where format != 'cpu' is checked), the tournament query likely needs to also filter out CPU-involved tournaments from the profile view.

The root cause is likely that the TournamentHistory.players list contains CPU names, and when a user tries to view tournaments where they participated alongside CPUs that reached the final, those tournaments aren't being properly displayed because the filtering logic is too strict.

The Fix

Here's the corrected version of the tournament endpoint that should be applied:

@router.get("/tournaments/{username}")
def get_user_tournaments(username: str, limit: int = 100, db: Session = Depends(get_db)):
    # Query all tournaments where the ...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

@vercel

vercel Bot commented Jun 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
very-important-study-material Ready Ready Preview, Comment Jun 3, 2026 9:42am

@Horus0305
Horus0305 marked this pull request as ready for review June 3, 2026 09:44
Copilot AI review requested due to automatic review settings June 3, 2026 09:44
@Horus0305
Horus0305 merged commit 8abe27a into main Jun 3, 2026
2 checks passed
Copilot AI changed the title [WIP] Fix tournament visibility for CPU-involved matches Restore profile tournament visibility for CPU-involved brackets Jun 3, 2026
Copilot AI requested a review from Horus0305 June 3, 2026 09:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to fix a reported issue where tournaments involving CPU participants were not visible in a user’s tournament history, ensuring per-user tournament visibility remains independent from CPU-exclusion logic used in global stats/leaderboards.

Changes:

  • Adds a backend test suite covering user tournament visibility when CPU players participated.
  • Adjusts get_user_tournaments() query behavior in stats.py (removes the SQL pre-filter on TournamentHistory.players).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
CricketGame/backend/api/stats.py Removes the SQL-level players filter in get_user_tournaments().
CricketGame/backend/test_tournament_visibility.py Adds tests asserting user tournament history includes CPU-involved tournaments and that persistence stores both human+CPU players.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 98 to 102
rows = (
db.query(TournamentHistory)
.filter(TournamentHistory.players.contains(username))
.order_by(TournamentHistory.timestamp.desc())
.all()
)

This branch was successfully deployed

1 active deployment
Preview — 164594cb Deployed Jun 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants