Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruleset_id column to seasons table #11844

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

venix12
Copy link
Member

@venix12 venix12 commented Feb 7, 2025

Part of #8736.

Prerequisite for displaying season division/rank on user profile.

@nanaya
Copy link
Collaborator

nanaya commented Feb 12, 2025

how's it going to be queried exactly?

@venix12
Copy link
Member Author

venix12 commented Feb 21, 2025

for now, probably something like Season::active()->where('ruleset_id', $rulesetId)->first()
...or Season::orderByDesc('id')->where('ruleset_id', $rulesetId)->first(), guess it could be indexed then 🤔

@nanaya
Copy link
Collaborator

nanaya commented Feb 22, 2025

The question was actually for all the way for whatever that needs to be displayed on the user page, not just this specific model. But I guess it's not too bad if just one.

@venix12
Copy link
Member Author

venix12 commented Feb 22, 2025

namespace App\Libraries\User;

class SeasonStats
{
    public static function get(User $user, Season $season): ?array 
    {
        $score = $season->userScores()
            ->whereBelongsTo($user)
            ->first();
        
        if ($score === null) {
            return null;
        }
        
        $rank = $season->userScores()
            ->where('user_id', '<>', $score->user_id)
            ->where('total_score', '>=', $score->total_score)
            ->count() + 1;
        
        foreach ($season->divisionsWithMaxRanks() as $division) {
            if ($rank <= $division['max_rank']) {
                $userDivision = $division['division'];
                break;
            }
        }
         
        if (!isset($userDivision)) {
            return null;
        }
        
        return [
            'division' => json_item($userDivision, new SeasonDivisionTransformer()),
            'rank' => $rank,
            'season' => json_item($season, new SeasonTransformer()),
            'total_score' => $score->total_score,
        ];
    }
}

Got something barebones like this rolling at the very moment, would also be cached somehow probably.

Some seasonal rank history display would be nice in the future, though probably out of scope at the very moment.

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.

2 participants