Skip to content

Commit

Permalink
Add query filtering for heya on basho page
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldickison committed Jul 29, 2024
1 parent f37fe54 commit 5694c7c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 34 deletions.
71 changes: 48 additions & 23 deletions src/data/leaders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use rusqlite::{Connection, Result as SqlResult};
use std::{collections::HashMap, ops::Range};

use super::{BashoId, BashoRikishi, Player, PlayerId, Rank, RankName, RankSide, Result, RikishiId};
use super::{
heya::HeyaId, BashoId, BashoRikishi, Player, PlayerId, Rank, RankName, RankSide, Result,
RikishiId,
};
use crate::util::GroupRuns;
use std::sync::Arc;

Expand Down Expand Up @@ -59,37 +62,59 @@ impl BashoPlayerResults {
rikishi: HashMap<RikishiId, BashoRikishi>,
include_best_worst: bool,
limit: usize,
heya_id: Option<HeyaId>,
) -> Result<Vec<Self>> {
debug!("fetching {} leaders for basho {}", limit, basho_id);
debug!(
"fetching {} leaders for basho {} heya {:?}",
limit, basho_id, heya_id
);

let rikishi = Arc::new(rikishi);
let (heya_join, params) = if heya_id.is_some() {
(
"JOIN heya_player AS hp ON hp.player_id = player.id AND hp.heya_id = :heya_id",
named_params! {
":basho_id": basho_id,
":player_id": player_id,
":limit": limit as u32,
":heya_id": heya_id,
},
)
} else {
(
"",
named_params! {
":basho_id": basho_id,
":player_id": player_id,
":limit": limit as u32
},
)
};
let mut leaders: Vec<BashoPlayerResults> = db
.prepare(
&format!(
"
SELECT
player.*,
pr.rank,
COALESCE(br.wins, 0) AS basho_wins,
COALESCE(br.rank, 0) AS basho_rank,
player.id = :player_id AS is_self,
GROUP_CONCAT(pick.rikishi_id) AS pick_ids
FROM pick
JOIN player_info AS player ON player.id = pick.player_id
LEFT JOIN player_rank AS pr ON pr.player_id = player.id AND pr.before_basho_id = pick.basho_id
LEFT JOIN basho_result AS br USING (player_id, basho_id)
WHERE pick.basho_id = :basho_id
GROUP BY player.id
ORDER BY is_self DESC, basho_wins DESC, player.id ASC
LIMIT :limit
",
SELECT
player.*,
pr.rank,
COALESCE(br.wins, 0) AS basho_wins,
COALESCE(br.rank, 0) AS basho_rank,
player.id = :player_id AS is_self,
GROUP_CONCAT(pick.rikishi_id) AS pick_ids
FROM pick
JOIN player_info AS player ON player.id = pick.player_id
{heya_join}
LEFT JOIN player_rank AS pr ON pr.player_id = player.id AND pr.before_basho_id = pick.basho_id
LEFT JOIN basho_result AS br USING (player_id, basho_id)
WHERE pick.basho_id = :basho_id
GROUP BY player.id
ORDER BY is_self DESC, basho_wins DESC, player.id ASC
LIMIT :limit
")
)
.unwrap()
.query_map(
named_params! {
":basho_id": basho_id,
":player_id": player_id,
":limit": limit as u32,
},
params,
|row| -> SqlResult<(Player, u8, u32, String)> {
Ok((
Player::from_row(row)?,
Expand Down
11 changes: 10 additions & 1 deletion src/handlers/basho.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashSet;
use super::{BaseTemplate, HandlerError, IdentityExt, Result};
use crate::data::leaders::{BashoPlayerResults, ResultPlayer};
use crate::data::{
self, BashoId, BashoInfo, BashoRikishiByRank, DataError, FetchBashoRikishi, PlayerId,
self, BashoId, BashoInfo, BashoRikishiByRank, DataError, FetchBashoRikishi, Heya, PlayerId,
RankGroup, RankSide, RikishiId,
};
use crate::AppState;
Expand All @@ -19,6 +19,7 @@ use askama::Template;
pub struct BashoTemplate {
base: BaseTemplate,
basho: BashoInfo,
heya: Option<Heya>,
leaders: Vec<BashoPlayerResults>,
self_leader_index: Option<usize>,
rikishi_by_rank: Vec<BashoRikishiByRank>,
Expand All @@ -42,6 +43,7 @@ impl BashoTemplate {
#[derive(Deserialize)]
pub struct BashoQuery {
all: Option<bool>,
heya: Option<String>,
}

#[get("")]
Expand Down Expand Up @@ -77,18 +79,25 @@ pub async fn basho(
} else {
100
};
let heya = query
.0
.heya
.and_then(|slug| Heya::with_slug(&db, &slug).transpose())
.transpose()?;
let leaders = BashoPlayerResults::fetch(
&db,
basho_id,
player_id,
rikishi_by_id,
basho.has_started(),
limit,
heya.as_ref().map(|h| h.id),
)?;
let self_leader_index = leaders.iter().position(|l| l.is_self);
Ok(Either::Left(BashoTemplate {
leaders,
self_leader_index,
heya,
next_day: rikishi_by_rank
.iter()
.map(|rr| rr.next_day())
Expand Down
24 changes: 14 additions & 10 deletions templates/basho.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h3>Admin Controls</h3>
</menu>
</section>
{% endif %}

{# TODO: put sponsorship data in the db #}
{% if basho.id.id()[..] >= "202311" %}
<section id="sponsors">
Expand All @@ -62,7 +62,7 @@ <h2>Rewards</h2>
<p>
<a href="https://tegatastore.com" target="_blank"><strong>The Tegata Store</strong></a> will ship any one <a href="https://tegatastore.com/collections/authentic-torikumi-sheets" target="_blank">authentic torikumi sheet</a> to the winner of this basho!
</p>

<p class="fine-print">
In the case of a tie, the player with the highest rank at the start of the basho will win the reward.
</p>
Expand Down Expand Up @@ -95,7 +95,7 @@ <h2><img alt="Banzuke Picks" src="/static/img2/banzuke/Banzuke-Picks.webp" width
<span class="js-basho-count-down" data-start-date="{{basho.start_date.timestamp_millis()}}"></span>
</p>
{%- endif %}

<form id="banzuke-select-rikishi-form" action="{{basho.id.url_path()}}/picks">

<div class="buttons">
Expand All @@ -115,7 +115,7 @@ <h2><img alt="Banzuke Picks" src="/static/img2/banzuke/Banzuke-Picks.webp" width
<div class="header rank">Rank</div>
<div class="header west rikishi">West</div>
<div class="header west pick-count">Picks</div>

{% for rr in rikishi_by_rank -%}
{% let row_style = format!("grid-row: {}", _loop_item.index + 2) %}
{% if rr.rank == "boundary" %}
Expand All @@ -139,11 +139,15 @@ <h2><img alt="Banzuke Picks" src="/static/img2/banzuke/Banzuke-Picks.webp" width
</div>
</form>
</section>

{% if basho.has_started() %}
<section id="leaderboard">
<h2>Basho Leaderboard</h2>


{% if let Some(heya) = heya %}
<h3><a href="{{heya.url_path()}}">{{heya.name}}</a></h3>
{% endif %}

{% match self.self_rank() %}
{% when Some with (rank) %}
<p class="current-rank">
Expand All @@ -152,7 +156,7 @@ <h2>Basho Leaderboard</h2>
</p>
{% when None %}
{% endmatch %}

<div class="leaderboard-table">
<div class="header day">Day</div>
<div class="header rank">#</div>
Expand All @@ -162,7 +166,7 @@ <h2>Basho Leaderboard</h2>
<div class="header score">{{day}}</div>
{% endfor -%}
<div class="header score total">Total</div>

{% for leader in leaders -%}
{# inside of `let` we access the loop context as `_loop_item` and its index is 0-based #}
{% let row_style = format!("grid-row: {}", _loop_item.index + 3) %}
Expand All @@ -177,7 +181,7 @@ <h2>Basho Leaderboard</h2>
{%- when _ -%}
{% let row_classes = "" %}
{%- endmatch %}

{% match leader.player -%}
{%- when ResultPlayer::Max %}
<div style="{{row_style}}" class="{{row_classes}} name">Best Possible</div>
Expand Down Expand Up @@ -212,7 +216,7 @@ <h2>Basho Leaderboard</h2>

<div style="{{row_style}}" class="{{row_classes}} score total">{{leader.total}}</div>
{%- endfor %}

{% match self_leader_index %}
{% when Some with (idx) %}
<div id="self-leader"
Expand Down

0 comments on commit 5694c7c

Please sign in to comment.