Skip to content

Commit

Permalink
Show a meta share history graph on /seasons/all/archetypes/{archetype}
Browse files Browse the repository at this point in the history
It's a bit rudimentary for now but it's a stake in the ground.
  • Loading branch information
bakert committed May 19, 2024
1 parent 750948c commit 6b38dbb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion decksite/controllers/metagame.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def archetype(archetype_id: str, deck_type: str | None = None) -> str:
all_archetypes = archs.load_archetypes(season_id=season_id, tournament_only=tournament_only)
archetype_matchups = archs.load_matchups(archetype_id=a.id, season_id=season_id, tournament_only=tournament_only)
seasons_active = archs.seasons_active(a.id)
view = Archetype(a, all_archetypes, archetype_matchups, seasons_active, tournament_only=tournament_only)
meta_share = archs.meta_share(a.id, tournament_only=tournament_only)
view = Archetype(a, all_archetypes, archetype_matchups, seasons_active, meta_share, tournament_only=tournament_only)
return view.page()


Expand Down
31 changes: 31 additions & 0 deletions decksite/data/archetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ 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])

def meta_share(archetype_id: int, tournament_only: bool = False) -> list[float]:
where = 'TRUE'
if tournament_only:
where = "deck_type = 'tournament'"
sql = f"""
WITH season_totals AS (
SELECT
season_id,
SUM(wins + losses + draws) AS total_matches
FROM
_arch_stats
WHERE
archetype_id <= 9 AND {where}
GROUP BY
season_id
)
SELECT
SUM(a.wins + a.losses + a.draws) / st.total_matches AS meta_share
FROM
season_totals AS st
LEFT JOIN
_arch_stats AS a ON a.season_id = st.season_id AND a.archetype_id = %s
WHERE
{where}
GROUP BY
st.season_id
ORDER BY
st.season_id;
"""
return [float(v) if v else 0.0 for v in db().values(sql, [archetype_id])]

def add(name: str, parent: int, description: str) -> None:
archetype_id = db().insert('INSERT INTO archetype (name, description) VALUES (%s, %s)', [name, description])
ancestors = db().select('SELECT ancestor, depth FROM archetype_closure WHERE descendant = %s', [parent])
Expand Down
8 changes: 8 additions & 0 deletions decksite/templates/archetype.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
<h2>Cards</h2>
{{> livecardtable}}
</section>
{{#history_chart}}
<section>
<h2>History</h2>
<div class="chart-container" style="position: relative; width:80vw; max-width: 60rem">
<canvas class="chart" data-type="{{type}}" data-labels="{{labels}}" data-series="{{series}}" data-options="{{options}}"></canvas>
</div>
</section>
{{/history_chart}}
{{#show_record}}
<section>
<h2>Matchups</h2>
Expand Down
34 changes: 33 additions & 1 deletion decksite/views/archetype.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
from typing import Any, TypedDict

from flask import url_for
Expand All @@ -16,7 +17,7 @@ class Matchups(TypedDict):
archetypes: list[archs.Archetype]

class Archetype(View):
def __init__(self, archetype: archs.Archetype, archetypes: list[archs.Archetype], matchups: list[Container], seasons_active: list[int], tournament_only: bool = False) -> None:
def __init__(self, archetype: archs.Archetype, archetypes: list[archs.Archetype], matchups: list[Container], seasons_active: list[int], meta_share: list[float], tournament_only: bool = False) -> None:
super().__init__()
if not archetype:
raise DoesNotExistException('No archetype supplied to view.')
Expand All @@ -43,6 +44,37 @@ def __init__(self, archetype: archs.Archetype, archetypes: list[archs.Archetype]
self.toggle_results_url = url_for('.archetype', archetype_id=self.archetype.id, deck_type=None if tournament_only else DeckType.TOURNAMENT.value)
self.show_archetype_tree = len(self.archetypes) > 0
self.has_cards = True
if self.season_id() == 0:
self.history_chart = {
'type': 'bar',
'labels': json.dumps(list(range(1, len(meta_share) + 1))),
'series': json.dumps(meta_share),
'options': json.dumps({
'indexAxis': 'x',
'plugins': {
'datalabels': {
'display': False,
},
'tooltip': {
'enabled': True,
},
},
'scales': {
'x': {
'grid': {
'display': False,
},
},
'y': {
'ticks': {
'format': {
'style': 'percent',
},
},
},
},
}),
}

def og_title(self) -> str:
return self.archetype.name
Expand Down

0 comments on commit 6b38dbb

Please sign in to comment.