From 07d8cf42d73ab9e740ffab8720a1e7d5c4fb7b9f Mon Sep 17 00:00:00 2001 From: trotFunky Date: Fri, 26 Jul 2024 19:25:41 +0100 Subject: [PATCH] vote: Properly take player and truth numbers into account Fetch the amount of truths for the week and the player count to find the total possible votes. Use this to remove the hardocded value in the check. Update the check to fail if there was an error as well. Now that the truth count is calculated, send it to the javascript to build the graph labels automatically as well. --- src/vote.rs | 24 +++++++++++++++++++----- static_files/vote_chart.js | 12 +++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/vote.rs b/src/vote.rs index 6795167..5d7d40c 100644 --- a/src/vote.rs +++ b/src/vote.rs @@ -91,6 +91,7 @@ pub async fn vote(week: u8, form: Form, #[derive(Serialize, Deserialize)] #[serde(crate = "rocket::serde")] pub struct VoteData { + truth_count: u8, votes: HashMap>, } @@ -110,12 +111,25 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection) -> Opti Vec::::new() }); + let truth_count: u8 = sqlx::query_scalar("SELECT count(id) from Truths WHERE week == $1;") + .bind(week) + .fetch_one(&mut **db) + .await.unwrap_or(0); + + let player_count: u8 = sqlx::query_scalar("SELECT count(id) from Players WHERE is_admin == 0;") + .fetch_one(&mut **db) + .await.unwrap_or(0); + + // Each player should have a truth assigned to them which they cannot vote on. + let max_vote_count = truth_count * (player_count - 1); + let vote_count = raw_votes.iter().fold(0, |count, votes| {count + votes.votes}); // Only show the graph if we have all the votes and this is not the last week. - // FIXME: Make the 42 not hardcoded - if vote_count < 42 || week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;") - .fetch_optional(&mut **db) - .await.unwrap_or(Some(0)).unwrap_or(0) { + if max_vote_count == 0 + || vote_count < max_vote_count + || week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;") + .fetch_optional(&mut **db) + .await.unwrap_or(Some(0)).unwrap_or(0) { return None; } @@ -147,7 +161,7 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection) -> Opti next_truth_number = raw_vote.truth_number + 1; } - Some(Json(VoteData{votes: vote_data})) + Some(Json(VoteData{truth_count: truth_count, votes: vote_data})) } // FIXME: diff --git a/static_files/vote_chart.js b/static_files/vote_chart.js index 09850d4..f82b977 100644 --- a/static_files/vote_chart.js +++ b/static_files/vote_chart.js @@ -6,18 +6,20 @@ async function main() { return; } - const keys = ["Vérité 1", "Vérité 2", "Vérité 3", "Vérité 4", "Vérité 5", "Vérité 6", "Vérité 7"] + let keys = [] let datasets = [] - try { - const vote_data = (await vote_response.json()).votes; - for (let player in vote_data) { + const vote_data = (await vote_response.json()); + for (let player in vote_data.votes) { datasets.push({ parsing: true, label: player, - data: vote_data[player], + data: vote_data.votes[player], }) } + for (let i = 1; i <= vote_data.truth_count; i++) { + keys.push("Vérité " + i) + } } catch (error) { console.error("Failed to parse vote data : \n\t" + error.message); return;