From fb51bbfffd2e3cbc024fc5d5dbd3a832c0fb029f Mon Sep 17 00:00:00 2001 From: Oupson Date: Mon, 24 Aug 2020 19:23:20 +0200 Subject: [PATCH] Better roulette --- src/commands/music.rs | 1 - src/commands/roulette.rs | 48 +++++++++++++++++++++++++++++++--------- src/main.rs | 4 ++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/commands/music.rs b/src/commands/music.rs index 156b395..802e9dc 100644 --- a/src/commands/music.rs +++ b/src/commands/music.rs @@ -99,7 +99,6 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { .expect("Expected VoiceManager in TypeMap."); let mut manager = manager_lock.lock().await; - if let Some(handler) = manager.get_mut(guild_id) { handler.stop(); manager.remove(guild_id); diff --git a/src/commands/roulette.rs b/src/commands/roulette.rs index 56162e3..4ef5d94 100644 --- a/src/commands/roulette.rs +++ b/src/commands/roulette.rs @@ -13,13 +13,13 @@ use std::collections::HashMap; pub struct BulletsContainer; impl TypeMapKey for BulletsContainer { - type Value = HashMap; + type Value = HashMap; } #[group] #[default_command(shot)] #[prefix("roulette")] -#[commands(reload, shot)] +#[commands(reload, shot, check)] struct Roulette; #[command] @@ -48,20 +48,22 @@ async fn _shot(ctx: &Context, msg: &Message) -> commands::Result<()> { let bullets_map = data .get_mut::() .expect("Expected CommandCounter in TypeMap."); - let bullets = bullets_map.entry(msg.author.id.0).or_insert(6); - if rand::thread_rng().gen_range(0, *bullets) == 0 { - api::send_reply(ctx, &msg, "BOOM !").await?; - *bullets = 6; + let bullets = bullets_map + .entry(msg.author.id.0) + .or_insert((5, rand::thread_rng().gen_range(0, 6))); + if bullets.0 == bullets.1 { + api::send_reply(ctx, &msg, "💥").await?; + *bullets = (5, rand::thread_rng().gen_range(0, 6)); } else { - *bullets -= 1; + *bullets = (bullets.0 - 1, bullets.1); api::send_reply( &ctx, &msg, - format!("Click ! bullets remaining : {}", bullets), + format!("Click ! bullets remaining : {}", bullets.0 + 1), ) .await?; } - debugln!("{:?}", bullets_map); + debugln!("Bullets Map : {:?}", bullets_map); Ok(()) } @@ -80,8 +82,34 @@ async fn _reload(ctx: &Context, msg: &Message) -> commands::Result<()> { let bullets_map = data .get_mut::() .expect("Expected CommandCounter in TypeMap."); - bullets_map.insert(msg.author.id.0, 6); + bullets_map.insert(msg.author.id.0, (5, rand::thread_rng().gen_range(0, 6))); msg.react(ctx, ReactionType::Unicode(String::from("✅"))) .await?; Ok(()) } + +#[command] +#[description = "If you use that, you are a coward"] +async fn check(ctx: &Context, msg: &Message) -> CommandResult { + let mut data = ctx.data.write().await; + let bullets_map = data + .get_mut::() + .expect("Expected CommandCounter in TypeMap."); + let bullets = bullets_map + .entry(msg.author.id.0) + .or_insert((5, rand::thread_rng().gen_range(0, 6))); + msg.channel_id.say(ctx, format!("Because you are a little shit, you open your barrel and you see that the bullet was at the {} position", bullet_to_str(bullets.1 + 1))).await?; + Ok(()) +} + +fn bullet_to_str<'m>(nbr: u8) -> &'m str { + match nbr { + 1 => "first", + 2 => "second", + 3 => "third", + 4 => "fourth", + 5 => "fifth", + 6 => "sixth", + _ => unimplemented!(), + } +} diff --git a/src/main.rs b/src/main.rs index f2f7f49..9d9a527 100644 --- a/src/main.rs +++ b/src/main.rs @@ -136,7 +136,7 @@ async fn main() -> IoResult<()> { struct Messages {} impl Messages { - async fn _reaction_add( + /*async fn _reaction_add( &self, ctx: Context, reaction: Reaction, @@ -171,7 +171,7 @@ impl Messages { } } Ok(()) - } + }*/ } #[async_trait]