Better roulette

This commit is contained in:
Oupson 2020-08-24 19:23:20 +02:00
parent f53b4efee0
commit fb51bbfffd
3 changed files with 40 additions and 13 deletions

View File

@ -99,7 +99,6 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult {
.expect("Expected VoiceManager in TypeMap."); .expect("Expected VoiceManager in TypeMap.");
let mut manager = manager_lock.lock().await; let mut manager = manager_lock.lock().await;
if let Some(handler) = manager.get_mut(guild_id) { if let Some(handler) = manager.get_mut(guild_id) {
handler.stop(); handler.stop();
manager.remove(guild_id); manager.remove(guild_id);

View File

@ -13,13 +13,13 @@ use std::collections::HashMap;
pub struct BulletsContainer; pub struct BulletsContainer;
impl TypeMapKey for BulletsContainer { impl TypeMapKey for BulletsContainer {
type Value = HashMap<u64, u8>; type Value = HashMap<u64, (u8, u8)>;
} }
#[group] #[group]
#[default_command(shot)] #[default_command(shot)]
#[prefix("roulette")] #[prefix("roulette")]
#[commands(reload, shot)] #[commands(reload, shot, check)]
struct Roulette; struct Roulette;
#[command] #[command]
@ -48,20 +48,22 @@ async fn _shot(ctx: &Context, msg: &Message) -> commands::Result<()> {
let bullets_map = data let bullets_map = data
.get_mut::<BulletsContainer>() .get_mut::<BulletsContainer>()
.expect("Expected CommandCounter in TypeMap."); .expect("Expected CommandCounter in TypeMap.");
let bullets = bullets_map.entry(msg.author.id.0).or_insert(6); let bullets = bullets_map
if rand::thread_rng().gen_range(0, *bullets) == 0 { .entry(msg.author.id.0)
api::send_reply(ctx, &msg, "BOOM !").await?; .or_insert((5, rand::thread_rng().gen_range(0, 6)));
*bullets = 6; if bullets.0 == bullets.1 {
api::send_reply(ctx, &msg, "💥").await?;
*bullets = (5, rand::thread_rng().gen_range(0, 6));
} else { } else {
*bullets -= 1; *bullets = (bullets.0 - 1, bullets.1);
api::send_reply( api::send_reply(
&ctx, &ctx,
&msg, &msg,
format!("Click ! bullets remaining : {}", bullets), format!("Click ! bullets remaining : {}", bullets.0 + 1),
) )
.await?; .await?;
} }
debugln!("{:?}", bullets_map); debugln!("Bullets Map : {:?}", bullets_map);
Ok(()) Ok(())
} }
@ -80,8 +82,34 @@ async fn _reload(ctx: &Context, msg: &Message) -> commands::Result<()> {
let bullets_map = data let bullets_map = data
.get_mut::<BulletsContainer>() .get_mut::<BulletsContainer>()
.expect("Expected CommandCounter in TypeMap."); .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(""))) msg.react(ctx, ReactionType::Unicode(String::from("")))
.await?; .await?;
Ok(()) 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::<BulletsContainer>()
.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!(),
}
}

View File

@ -136,7 +136,7 @@ async fn main() -> IoResult<()> {
struct Messages {} struct Messages {}
impl Messages { impl Messages {
async fn _reaction_add( /*async fn _reaction_add(
&self, &self,
ctx: Context, ctx: Context,
reaction: Reaction, reaction: Reaction,
@ -171,7 +171,7 @@ impl Messages {
} }
} }
Ok(()) Ok(())
} }*/
} }
#[async_trait] #[async_trait]