4 Commits

Author SHA1 Message Date
gvcgael
6d92ccb908 new algorithm 2022-11-30 22:54:29 +01:00
gvcgael
487b6312f6 wip : shuffle at the init phase 2022-11-30 18:38:22 +01:00
gvcgael
1e7dbc61e9 add logs 2022-11-30 16:45:37 +01:00
f34980e28f Merge pull request 'feat(verif): add a confirm dialog for tocards' (#3) from feat/french_with_check into french
Reviewed-on: #3
2022-11-30 15:20:39 +00:00
3 changed files with 120 additions and 34 deletions

91
Cargo.lock generated
View File

@@ -3,16 +3,36 @@
version = 3 version = 3
[[package]] [[package]]
name = "async-trait" name = "aho-corasick"
version = "0.1.58" version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e805d94e6b5001b651426cf4cd446b1ab5f319d27bab5c644f61de0a804360c" checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "async-trait"
version = "0.1.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e6e93155431f3931513b243d371981bb2770112b370c82745a1d19d2f99364"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@@ -21,9 +41,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "axum" name = "axum"
version = "0.6.0" version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "744864363a200a5e724a7e61bc8c11b6628cf2e3ec519c8a1a48e609a8156b40" checksum = "08b108ad2665fa3f6e6a517c3d80ec3e77d224c47d605167aefaa5d7ef97fa48"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum-core", "axum-core",
@@ -87,6 +107,19 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "env_logger"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@@ -195,6 +228,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "0.14.23" version = "0.14.23"
@@ -256,9 +295,9 @@ dependencies = [
[[package]] [[package]]
name = "matchit" name = "matchit"
version = "0.6.0" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfc802da7b1cf80aefffa0c7b2f77247c8b32206cc83c270b61264f5b360a80" checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40"
[[package]] [[package]]
name = "memchr" name = "memchr"
@@ -414,6 +453,8 @@ version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
] ]
@@ -446,18 +487,18 @@ checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.147" version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.147" version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -501,6 +542,8 @@ name = "sesan"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"env_logger",
"log",
"once_cell", "once_cell",
"rand", "rand",
"serde", "serde",
@@ -535,9 +578,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.103" version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" checksum = "4ae548ec36cf198c0ef7710d3c230987c2d6d7bd98ad6edc0274462724c585ce"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -550,6 +593,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8" checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8"
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.4" version = "1.1.4"
@@ -577,9 +629,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "1.8.0" version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -734,6 +786,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@@ -12,3 +12,5 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
once_cell = "1.8.0" once_cell = "1.8.0"
rand = "0.8.4" rand = "0.8.4"
log = "0.4.0"
env_logger = "0.9.0"

View File

@@ -5,13 +5,22 @@ use axum::{
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rand::seq::SliceRandom;
use serde::Deserialize; use serde::Deserialize;
use std::net::SocketAddr; use std::net::SocketAddr;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use log::debug;
use log::error;
use log::info;
use log::warn;
#[macro_use]
extern crate log;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
tracing_subscriber::fmt::init(); env_logger::init();
// tracing_subscriber::fmt::init();
// build our application with some routes // build our application with some routes
let app = Router::new() let app = Router::new()
@@ -38,8 +47,8 @@ static INDEX_HTML: Lazy<String> = Lazy::new(|| {
static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default); static STATE: Lazy<Mutex<State>> = Lazy::new(Mutex::default);
struct State { struct State {
rem_a: Vec<String>, participants: Vec<String>,
rem_b: Vec<String>, remaining: Vec<String>,
} }
fn everyone() -> Vec<String> { fn everyone() -> Vec<String> {
@@ -48,9 +57,12 @@ fn everyone() -> Vec<String> {
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { let mut p = everyone();
rem_a: everyone(), p.shuffle(&mut thread_rng());
rem_b: everyone(), info!("distribution : {:?}", p.join(" => "));
return Self {
participants: p,
remaining: everyone()
} }
} }
} }
@@ -63,21 +75,32 @@ struct Input {
async fn input(Json(input): Json<Input>) -> String { async fn input(Json(input): Json<Input>) -> String {
let mut state = STATE.lock().await; let mut state = STATE.lock().await;
if state.rem_a.is_empty() { if state.remaining.is_empty() {
return "ERROR (everybody drew already)".into(); return "ERROR (everybody drew already)".into();
} }
info!("joueurs qui restent : {:?}", state.remaining.join(","));
match state.rem_a.iter().position(|p| input.person == *p) { match state.remaining.iter().position(|p| input.person == *p).map(|e| state.remaining.remove(e)) {
Some(pos) => { Some(rem) => {
state.rem_a.remove(pos); match state.participants.iter().position(|p| input.person == *p) {
} Some(pos) => {
None => return "ERROR (you drew already)".into(), info!("joueur qui pioche : {:?}", state.participants[pos]);
} return match state.participants.iter().nth(pos+1) {
Some(x) => {
loop { info!("joueur qui suit : {:?}", x);
let num = thread_rng().gen_range(0..state.rem_b.len()); return x.to_string();
if state.rem_b[num] != input.person { }
return state.rem_b.remove(num); None => return match state.participants.first() {
Some(x) => {
info!("joueur qui suit : {:?}", x);
return x.to_string();
}
None => "ERROR".to_string()
}
}
}
None => "ERROR".to_string()
}
} }
None => "Vous avez déja pioché !".to_string()
} }
} }