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;