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.
This commit is contained in:
trotFunky 2024-07-26 19:25:41 +01:00
parent ba98c3be84
commit 07d8cf42d7
2 changed files with 26 additions and 10 deletions

View file

@ -91,6 +91,7 @@ pub async fn vote(week: u8, form: Form<VoteForm>,
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
pub struct VoteData { pub struct VoteData {
truth_count: u8,
votes: HashMap<String, Vec<u8>>, votes: HashMap<String, Vec<u8>>,
} }
@ -110,10 +111,23 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>) -> Opti
Vec::<VotingData>::new() Vec::<VotingData>::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}); 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. // Only show the graph if we have all the votes and this is not the last week.
// FIXME: Make the 42 not hardcoded if max_vote_count == 0
if vote_count < 42 || week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;") || vote_count < max_vote_count
|| week == sqlx::query_scalar("SELECT number from Weeks WHERE is_last_week == 1;")
.fetch_optional(&mut **db) .fetch_optional(&mut **db)
.await.unwrap_or(Some(0)).unwrap_or(0) { .await.unwrap_or(Some(0)).unwrap_or(0) {
return None; return None;
@ -147,7 +161,7 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection<database::Db>) -> Opti
next_truth_number = raw_vote.truth_number + 1; 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: // FIXME:

View file

@ -6,18 +6,20 @@ async function main() {
return; 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 = [] let datasets = []
try { try {
const vote_data = (await vote_response.json()).votes; const vote_data = (await vote_response.json());
for (let player in vote_data) { for (let player in vote_data.votes) {
datasets.push({ datasets.push({
parsing: true, parsing: true,
label: player, 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) { } catch (error) {
console.error("Failed to parse vote data : \n\t" + error.message); console.error("Failed to parse vote data : \n\t" + error.message);
return; return;