General update: site works.
This commit is contained in:
@@ -7,8 +7,10 @@ CREATE TABLE IF NOT EXISTS votes
|
||||
(
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
submit_date TEXT NOT NULL,
|
||||
player_id INTEGER NOT NULL,
|
||||
nickname TEXT NOT NULL,
|
||||
reason TEXT NOT NULL,
|
||||
is_plus BOOLEAN NOT NULL DEFAULT 0
|
||||
plus_player_id INTEGER NOT NULL,
|
||||
plus_nickname TEXT NOT NULL,
|
||||
plus_reason TEXT NOT NULL,
|
||||
minus_player_id INTEGER NOT NULL,
|
||||
minus_nickname TEXT NOT NULL,
|
||||
minus_reason TEXT NOT NULL
|
||||
)
|
||||
@@ -1,5 +1,6 @@
|
||||
{
|
||||
"/": "static/html/index.html",
|
||||
"/results": "static/html/results.html",
|
||||
"/archives": "static/html/archives.html"
|
||||
"/archives": "static/html/archives.html",
|
||||
"/favicon.ico": "static/img/favicon.ico"
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
{
|
||||
"database_url": "sqlite:vote.db",
|
||||
"bind_address": "127.0.0.1:8080"
|
||||
"bind_address": "127.0.0.1:8000"
|
||||
}
|
||||
58
src/main.rs
58
src/main.rs
@@ -31,10 +31,12 @@ struct Player {
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Vote {
|
||||
player_id: i64,
|
||||
nickname: String,
|
||||
reason: String,
|
||||
is_plus: bool,
|
||||
plus_player_id: i64,
|
||||
plus_nickname: String,
|
||||
plus_reason: String,
|
||||
minus_player_id: i64,
|
||||
minus_nickname: String,
|
||||
minus_reason: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -109,25 +111,23 @@ async fn get_data(path: &str, req: &Request<Incoming>, db: Arc<Mutex<SqlitePool>
|
||||
}
|
||||
"/data/results" => {
|
||||
let votes = get_votes(req, db).await;
|
||||
let ids: Vec<(i64, bool)> = votes.iter().map(|x| (x.player_id, x.is_plus)).collect();
|
||||
let ids: Vec<(i64, i64)> = votes.iter().map(|x| (x.plus_player_id, x.minus_player_id)).collect();
|
||||
|
||||
let mut plus_results: HashMap<i64, i64> = HashMap::new();
|
||||
let mut minus_results: HashMap<i64, i64> = HashMap::new();
|
||||
|
||||
let _ = ids.iter().filter(|x| x.1).for_each(|x| {
|
||||
let id = x.0;
|
||||
if plus_results.contains_key(&id) {
|
||||
plus_results.insert(id, 0);
|
||||
let _ = ids.iter().for_each(|x| {
|
||||
let plus_id = x.0;
|
||||
if !plus_results.contains_key(&plus_id) {
|
||||
plus_results.insert(plus_id, 0);
|
||||
}
|
||||
*plus_results.get_mut(&id).unwrap() += 1;
|
||||
});
|
||||
*plus_results.get_mut(&plus_id).unwrap() += 1;
|
||||
|
||||
let _ = ids.iter().filter(|x| !x.1).for_each(|x| {
|
||||
let id = x.0;
|
||||
if minus_results.contains_key(&id) {
|
||||
minus_results.insert(id, 0);
|
||||
let minus_id = x.1;
|
||||
if !minus_results.contains_key(&minus_id) {
|
||||
minus_results.insert(minus_id, 0);
|
||||
}
|
||||
*minus_results.get_mut(&id).unwrap() += 1;
|
||||
*minus_results.get_mut(&minus_id).unwrap() += 1;
|
||||
});
|
||||
|
||||
let mut plus_results: Vec<(i64, i64)> = plus_results.into_iter().collect();
|
||||
@@ -164,13 +164,15 @@ async fn get_votes(req: &Request<Incoming>, db: Arc<Mutex<SqlitePool>>) -> Vec<V
|
||||
}
|
||||
let items = futures::executor::block_on(async move {
|
||||
let formatted_date = format!("{}", date.unwrap().format("%d/%m/%Y"));
|
||||
sqlx::query!(r#"SELECT * FROM votes WHERE SUBMIT_DATE = ?1 ORDER BY id"#, formatted_date).fetch_all(&pool).await.unwrap()
|
||||
sqlx::query!(r#"SELECT * FROM votes WHERE submit_date = ?1 ORDER BY id"#, formatted_date).fetch_all(&pool).await.unwrap()
|
||||
});
|
||||
items.iter().map(|x| Vote {
|
||||
player_id: x.player_id,
|
||||
nickname: x.nickname.clone(),
|
||||
reason: x.reason.clone(),
|
||||
is_plus: x.is_plus,
|
||||
plus_player_id: x.plus_player_id,
|
||||
plus_nickname: x.plus_nickname.clone(),
|
||||
plus_reason: x.plus_reason.clone(),
|
||||
minus_player_id: x.minus_player_id,
|
||||
minus_nickname: x.minus_nickname.clone(),
|
||||
minus_reason: x.minus_reason.clone(),
|
||||
}).collect()
|
||||
}
|
||||
|
||||
@@ -189,12 +191,14 @@ async fn post(req: Request<Incoming>, db: Arc<Mutex<SqlitePool>>) -> Result<Resp
|
||||
let formatted = timestamp.format("%d/%m/%Y").to_string();
|
||||
let pool = db.clone().lock().unwrap().clone();
|
||||
let mut conn = pool.acquire().await.unwrap();
|
||||
let result = sqlx::query!(r#"INSERT INTO votes ( PLAYER_ID, NICKNAME, REASON, IS_PLUS, SUBMIT_DATE )
|
||||
VALUES ( ?1, ?2, ?3, ?4, ?5 )"#,
|
||||
vote.player_id,
|
||||
vote.nickname,
|
||||
vote.reason,
|
||||
vote.is_plus,
|
||||
let result = sqlx::query!(r#"INSERT INTO votes ( plus_player_id, plus_nickname, plus_reason, minus_player_id, minus_nickname, minus_reason, submit_date )
|
||||
values ( ?1, ?2, ?3, ?4, ?5, ?6, ?7 )"#,
|
||||
vote.plus_player_id,
|
||||
vote.plus_nickname,
|
||||
vote.plus_reason,
|
||||
vote.minus_player_id,
|
||||
vote.minus_nickname,
|
||||
vote.minus_reason,
|
||||
formatted).execute(&mut *conn).await;
|
||||
if result.is_err() {
|
||||
return Ok(Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(Full::new(Bytes::from("Internet Error"))).unwrap());
|
||||
|
||||
BIN
static/img/favicon.ico
Normal file
BIN
static/img/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 205 KiB |
@@ -1,10 +1,10 @@
|
||||
Vote = {
|
||||
vote_plus_id: null,
|
||||
vote_plus_nickname: "",
|
||||
vote_plus_reason: "",
|
||||
vote_moins_id: null,
|
||||
vote_moins_nickname: "",
|
||||
vote_moins_reason: ""
|
||||
plus_player_id: null,
|
||||
plus_nickname: "",
|
||||
plus_reason: "",
|
||||
minus_player_id: null,
|
||||
minus_nickname: "",
|
||||
minus_reason: ""
|
||||
}
|
||||
|
||||
let _ = load_page(0)
|
||||
@@ -20,43 +20,43 @@ async function load_page(id) {
|
||||
const buttons = document.createElement("div");
|
||||
buttons.className = "buttons";
|
||||
|
||||
const vote_id = document.createElement("h2");
|
||||
vote_id.textContent = "Pour qui votes-tu?"
|
||||
const vote_id_select = document.createElement("select");
|
||||
const vote_nickname = document.createElement("h2");
|
||||
vote_nickname.textContent = "As-tu un surnom à lui donner?";
|
||||
const vote_nickname_input = document.createElement("input");
|
||||
vote_nickname_input.inputMode = "text";
|
||||
vote_nickname_input.maxLength = 100;
|
||||
const vote_reason = document.createElement("h2");
|
||||
vote_reason.textContent = "Pourquoi votes-tu pour lui?";
|
||||
const vote_reason_input = document.createElement("textarea");
|
||||
vote_reason_input.maxLength = 1000;
|
||||
const idlbl = document.createElement("h2");
|
||||
idlbl.textContent = "Pour qui votes-tu?"
|
||||
const id_select = document.createElement("select");
|
||||
const nickname = document.createElement("h2");
|
||||
nickname.textContent = "As-tu un surnom à lui donner?";
|
||||
const nickname_input = document.createElement("input");
|
||||
nickname_input.inputMode = "text";
|
||||
nickname_input.maxLength = 100;
|
||||
const reason = document.createElement("h2");
|
||||
reason.textContent = "Pourquoi votes-tu pour lui?";
|
||||
const reason_input = document.createElement("textarea");
|
||||
reason_input.maxLength = 1000;
|
||||
|
||||
players.forEach((player) => {
|
||||
const player_option = document.createElement("option");
|
||||
player_option.value = player["id"];
|
||||
player_option.innerText = player["name"];
|
||||
vote_id_select.appendChild(player_option);
|
||||
id_select.appendChild(player_option);
|
||||
})
|
||||
|
||||
if (id) {
|
||||
vote_id_select.value = Vote.vote_moins_id;
|
||||
vote_nickname_input.value = Vote.vote_moins_nickname;
|
||||
vote_reason_input.value = Vote.vote_moins_reason;
|
||||
id_select.value = Vote.minus_player_id;
|
||||
nickname_input.value = Vote.minus_nickname;
|
||||
reason_input.value = Vote.minus_reason;
|
||||
header.textContent = "Vote -";
|
||||
let previous = document.createElement("button");
|
||||
previous.textContent = "Précédent";
|
||||
previous.addEventListener("click", () => {
|
||||
Vote.vote_moins_id = vote_id_select.value;
|
||||
Vote.vote_moins_nickname = vote_nickname_input.value;
|
||||
Vote.vote_moins_reason = vote_reason_input.value;
|
||||
Vote.minus_player_id = id_select.value;
|
||||
Vote.minus_nickname = nickname_input.value;
|
||||
Vote.minus_reason = reason_input.value;
|
||||
load_page(0)
|
||||
});
|
||||
let submit = document.createElement("button");
|
||||
submit.textContent = "A Voté";
|
||||
submit.addEventListener("click", async () => {
|
||||
if (vote.vote_plus_id === null || vote.vote_moins_id === null) {
|
||||
if (Vote.plus_id === null || id_select.value === "") {
|
||||
const popUp = document.createElement("div");
|
||||
popUp.className = "popup";
|
||||
const message = document.createElement("h2");
|
||||
@@ -65,9 +65,9 @@ async function load_page(id) {
|
||||
popUp.append(message);
|
||||
page.append(popUp);
|
||||
} else {
|
||||
Vote.vote_moins_id = vote_id_select.value;
|
||||
Vote.vote_moins_nickname = vote_nickname_input.value;
|
||||
Vote.vote_moins_reason = vote_reason_input.value;
|
||||
Vote.minus_player_id = id_select.value;
|
||||
Vote.minus_nickname = nickname_input.value;
|
||||
Vote.minus_reason = reason_input.value;
|
||||
if (await send_vote(Vote)) {
|
||||
confirm_popup();
|
||||
save_state();
|
||||
@@ -78,16 +78,16 @@ async function load_page(id) {
|
||||
previous.className = "left";
|
||||
buttons.append(previous, submit);
|
||||
} else {
|
||||
vote_id_select.value = Vote.vote_plus_id;
|
||||
vote_nickname_input.value = Vote.vote_plus_nickname;
|
||||
vote_reason_input.value = Vote.vote_plus_reason;
|
||||
id_select.value = Vote.plus_player_id;
|
||||
nickname_input.value = Vote.plus_nickname;
|
||||
reason_input.value = Vote.plus_reason;
|
||||
header.textContent = "Vote +";
|
||||
let next = document.createElement("button");
|
||||
next.innerText = "Suivant";
|
||||
next.addEventListener("click", () => {
|
||||
Vote.vote_plus_id = vote_id_select.value;
|
||||
Vote.vote_plus_nickname = vote_nickname_input.value;
|
||||
Vote.vote_plus_reason = vote_reason_input.value;
|
||||
Vote.plus_player_id = id_select.value;
|
||||
Vote.plus_nickname = nickname_input.value;
|
||||
Vote.plus_reason = reason_input.value;
|
||||
load_page(1)
|
||||
});
|
||||
next.className = "right";
|
||||
@@ -97,7 +97,7 @@ async function load_page(id) {
|
||||
|
||||
const div = document.getElementById("app");
|
||||
div.innerHTML = "";
|
||||
div.append(header, vote_id, vote_id_select, vote_nickname, vote_nickname_input, vote_reason, vote_reason_input, buttons);
|
||||
div.append(header, idlbl, id_select, nickname, nickname_input, reason, reason_input, buttons);
|
||||
}
|
||||
|
||||
function confirm_popup() {
|
||||
@@ -124,8 +124,8 @@ function get_state() {
|
||||
return cookie.includes("hasvoted=true");
|
||||
}
|
||||
async function send_vote(vote) {
|
||||
vote.vote_plus_id = parseInt(vote.vote_plus_id, 10);
|
||||
vote.vote_moins_id = parseInt(vote.vote_moins_id, 10);
|
||||
vote.plus_player_id = parseInt(vote.plus_player_id, 10);
|
||||
vote.minus_player_id = parseInt(vote.minus_player_id, 10);
|
||||
let body = JSON.stringify(vote);
|
||||
let result = await fetch(window.location.href + "post", {method: "POST", body: body}).then(r => r.status);
|
||||
return result === 200;
|
||||
|
||||
@@ -23,42 +23,42 @@ function show_plus(id, votes, players) {
|
||||
const app = document.getElementById("app");
|
||||
app.innerHTML = "";
|
||||
let vote = votes[id];
|
||||
let player = players[vote["vote_plus_id"] - 1]["name"];
|
||||
let nickname = vote["vote_plus_nickname"];
|
||||
let reason = vote["vote_plus_reason"];
|
||||
let moins = document.createElement("button");
|
||||
moins.textContent = "Et en moins..."
|
||||
moins.addEventListener("click", () => {show_moins(id, votes, players)})
|
||||
moins.className = "right";
|
||||
let player = players[vote["plus_player_id"] - 1]["name"];
|
||||
let nickname = vote["plus_nickname"];
|
||||
let reason = vote["plus_reason"];
|
||||
let minus = document.createElement("button");
|
||||
minus.textContent = "Et en moins..."
|
||||
minus.addEventListener("click", () => {show_minus(id, votes, players)})
|
||||
minus.className = "right";
|
||||
|
||||
const vote_p = document.createElement("h2");
|
||||
const p = document.createElement("h2");
|
||||
if (nickname === "") {
|
||||
vote_p.innerHTML = `${player}`;
|
||||
p.innerHTML = `${player}`;
|
||||
}
|
||||
else {
|
||||
vote_p.innerHTML = `${nickname} (${player})`
|
||||
p.innerHTML = `${nickname} (${player})`
|
||||
}
|
||||
|
||||
const vote_r = document.createElement("p");
|
||||
vote_r.textContent = reason;
|
||||
const r = document.createElement("p");
|
||||
r.textContent = reason;
|
||||
|
||||
const head = document.createElement("h1");
|
||||
head.innerText = "EN PLUS";
|
||||
|
||||
const buttons = document.createElement("div");
|
||||
buttons.className = "buttons";
|
||||
buttons.append(moins)
|
||||
buttons.append(minus)
|
||||
|
||||
app.append(head, vote_p, vote_r, buttons);
|
||||
app.append(head, p, r, buttons);
|
||||
}
|
||||
|
||||
function show_moins(id, votes, players) {
|
||||
function show_minus(id, votes, players) {
|
||||
const app = document.getElementById("app");
|
||||
app.innerHTML = "";
|
||||
let vote = votes[id];
|
||||
let nickname = vote["vote_moins_nickname"];
|
||||
let reason = vote["vote_moins_reason"];
|
||||
let player = players[vote["vote_moins_id"] - 1]["name"];
|
||||
let nickname = vote["minus_nickname"];
|
||||
let reason = vote["minus_reason"];
|
||||
let player = players[vote["minus_player_id"] - 1]["name"];
|
||||
let next = document.createElement("button");
|
||||
if (id === votes.length - 1) {
|
||||
next.textContent = "Résultats";
|
||||
@@ -74,16 +74,16 @@ function show_moins(id, votes, players) {
|
||||
})
|
||||
next.className = "right";
|
||||
|
||||
const vote_p = document.createElement("h2");
|
||||
const p = document.createElement("h2");
|
||||
if (nickname === "") {
|
||||
vote_p.innerHTML = `${player}`;
|
||||
p.innerHTML = `${player}`;
|
||||
}
|
||||
else {
|
||||
vote_p.innerHTML = `${nickname} (${player})`
|
||||
p.innerHTML = `${nickname} (${player})`
|
||||
}
|
||||
|
||||
const vote_r = document.createElement("p");
|
||||
vote_r.textContent = reason;
|
||||
const r = document.createElement("p");
|
||||
r.textContent = reason;
|
||||
|
||||
const head = document.createElement("h1");
|
||||
head.innerText = "EN MOINS";
|
||||
@@ -92,7 +92,7 @@ function show_moins(id, votes, players) {
|
||||
buttons.className = "buttons";
|
||||
buttons.append(next)
|
||||
|
||||
app.append(head, vote_p, vote_r, buttons);
|
||||
app.append(head, p, r, buttons);
|
||||
}
|
||||
|
||||
async function show_results(players) {
|
||||
@@ -107,7 +107,7 @@ async function show_results(players) {
|
||||
app.append(vp);
|
||||
let results = await fetch("data/results").then(r => r.json());
|
||||
let plus = results[0];
|
||||
let moins = results[1];
|
||||
let minus = results[1];
|
||||
let prev_score = null;
|
||||
let counter = 0;
|
||||
for (let i = 0; i < plus.length; i++) {
|
||||
@@ -132,8 +132,8 @@ async function show_results(players) {
|
||||
app.append(vm);
|
||||
prev_score = null;
|
||||
counter = 0;
|
||||
for (let i = 0; i < moins.length; i++) {
|
||||
let p = moins[i];
|
||||
for (let i = 0; i < minus.length; i++) {
|
||||
let p = minus[i];
|
||||
let player = players[p[0] - 1]["name"];
|
||||
let score = p[1];
|
||||
if (prev_score == null || score < prev_score) {
|
||||
|
||||
Reference in New Issue
Block a user