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

Show meta on competition detail page #13163

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion decksite/controllers/competitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decksite import APP, SEASONS, auth, get_season_id
from decksite.cache import cached
from decksite.data import achievements as achs
from decksite.data import archetype
from decksite.data import competition as comp
from decksite.data import deck as ds
from decksite.data import person as ps
Expand All @@ -17,7 +18,8 @@ def competitions() -> str:
@APP.route('/competitions/<competition_id>/')
@cached()
def competition(competition_id: int) -> str:
view = Competition(comp.load_competition(competition_id, should_load_decks=False))
ars = archetype.load_competition_archetypes(competition_id)
view = Competition(comp.load_competition(competition_id, should_load_decks=False), ars)
return view.page()

@APP.route('/tournaments/')
Expand Down
29 changes: 29 additions & 0 deletions decksite/data/archetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,35 @@ def load_archetype(archetype: int | str) -> Archetype:
arch.name = db().value('SELECT name FROM archetype WHERE id = %s', [archetype_id])
return arch

def load_competition_archetypes(competition_id: int) -> list[Archetype]:
sql = f"""
SELECT
a.id,
a.name,
SUM(CASE WHEN d.id IS NOT NULL THEN 1 ELSE 0 END) AS num_decks,
IFNULL(SUM(dsum.wins), 0) AS wins,
IFNULL(SUM(dsum.losses), 0) AS losses,
IFNULL(SUM(dsum.draws), 0) AS draws,
SUM(CASE WHEN dsum.wins >= 5 AND dsum.losses = 0 AND d.source_id IN (SELECT id FROM source WHERE name = 'League') THEN 1 ELSE 0 END) AS perfect_runs,
SUM(CASE WHEN dsum.finish = 1 THEN 1 ELSE 0 END) AS tournament_wins,
SUM(CASE WHEN dsum.finish <= 8 THEN 1 ELSE 0 END) AS tournament_top8s,
(CASE WHEN ct.name = 'League' THEN 'league' WHEN ct.name = 'Gatherling' THEN 'tournament' ELSE 'other' END) AS deck_type,
IFNULL(ROUND((SUM(dsum.wins) / NULLIF(SUM(dsum.wins + dsum.losses), 0)) * 100, 1), '') AS win_percent
FROM
archetype AS a
LEFT JOIN
deck AS d ON a.id = d.archetype_id
{query.competition_join()}
{query.season_join()}
{deck.nwdl_join()}
WHERE
competition_id = %s
GROUP BY
a.id
"""
archetypes = db().select(sql, [competition_id])
return [Archetype(a) for a in archetypes]

def seasons_active(archetype_id: int) -> list[int]:
sql = 'SELECT season_id FROM _arch_stats WHERE archetype_id = %s'
return db().values(sql, [archetype_id])
Expand Down
6 changes: 6 additions & 0 deletions decksite/templates/competition.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
{{> liveleaderboardtable}}
</section>
{{/has_leaderboard}}
{{#archetypes}}
<section>
<h2>Archetypes</h2>
{{> archetypetree}}
</section>
{{/archetypes}}
5 changes: 3 additions & 2 deletions decksite/views/competition.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Any

from decksite.data.archetype import Archetype
from decksite.view import View
from magic.models import Competition as Comp
from shared import dtutil


class Competition(View):
def __init__(self, competition: Comp) -> None:
def __init__(self, competition: Comp, archetypes: list[Archetype]) -> None:
super().__init__()
self.competition = competition
self.competitions = [self.competition]
Expand All @@ -18,7 +19,7 @@ def __init__(self, competition: Comp) -> None:
self.hide_top8 = True
self.has_leaderboard = True
self.date = dtutil.display_date(competition.start_date)
self.sponsor_name = competition.sponsor_name
self.archetypes = archetypes

def __getattr__(self, attr: str) -> Any:
return getattr(self.competition, attr)
Expand Down