diff --git a/src/discord/commands.rs b/src/discord/commands.rs index 00c4dde..1d834af 100644 --- a/src/discord/commands.rs +++ b/src/discord/commands.rs @@ -23,9 +23,7 @@ use crate::game::Phase; use crate::messages::DiscordUser; #[group] -#[commands( - start, say, end, broadcast, next_phase, terminate, add_time, test_theme -)] +#[commands(start, say, end, broadcast, next_phase, kill, add_time, test_theme)] struct Host; #[command] @@ -280,37 +278,24 @@ async fn next_phase(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu #[command] #[only_in(guilds)] #[allowed_roles("wolfx host")] -async fn terminate(ctx: &Context, msg: &Message, args: Args) -> CommandResult { +async fn kill(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); - let guild = msg.guild(&ctx.cache).await.unwrap(); let mut global_data = global_data.lock().await; let target = args.rest().to_lowercase(); - let index = global_data + let player = global_data .game_state_mut()? - .player_data - .iter() - .position(|p| p.codename.to_lowercase() == target); + .get_player_by_codename_mut(&target); - if let Some(index) = index { - let player = global_data.game_state_mut()?.player_data.remove(index); - - let player_channel = guild - .channels - .get(&ChannelId::from(player.channel)) - .unwrap(); - - player_channel.delete(&ctx.http).await.unwrap(); - - msg.reply(&ctx.http, format!("{} ", player.codename)) - .await - .unwrap(); + if let Some(player) = player { + player.alive = false; + msg.reply(&ctx.http, format!("{} has been killed.", player.codename)) + .await?; } else { msg.reply(&ctx.http, "No player found with that codename.") - .await - .unwrap(); + .await?; } global_data.save_game_state().unwrap(); @@ -368,6 +353,7 @@ async fn add_time(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult #[command] #[only_in(guilds)] +#[allowed_roles("wolfx host")] #[description = "Display all the messages from a theme"] async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; @@ -388,6 +374,7 @@ async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { vote_target: None, profile_pic_url: "".to_string(), channel_webhook_id: 0, + alive: true, }; let player_1_discord = DiscordUser { @@ -402,6 +389,7 @@ async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { vote_target: None, profile_pic_url: "".to_string(), channel_webhook_id: 0, + alive: false, }; let mut players = vec![test_player_1.clone(), test_player_2.clone()]; @@ -630,7 +618,12 @@ async fn players(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { msg_builder.push_line(&global_data.game_cfg()?.player_group_name); for player in &global_data.game_state()?.player_data { - msg_builder.push("* ").push(&player.codename); + let alive_status = if !player.alive { " (Dead) " } else { "" }; + + msg_builder + .push("* ") + .push(&player.codename) + .push(alive_status); if msg.channel_id.0 == global_data.cfg.discord_config.host_channel { let guild = msg.guild(&ctx.cache).await.unwrap(); diff --git a/src/discord/event_handler.rs b/src/discord/event_handler.rs index 8da9ce2..3dcb986 100644 --- a/src/discord/event_handler.rs +++ b/src/discord/event_handler.rs @@ -38,6 +38,11 @@ impl EventHandler for Handler { .unwrap() .get_player_from_channel(msg.channel_id.0) { + // Don't process message if the player is dead + if !player_data.alive { + return; + } + let guild = msg.guild(&ctx.cache).await.unwrap(); let user_msg = msg.content.clone(); diff --git a/src/discord/helper.rs b/src/discord/helper.rs index 0b25d28..ddd6c5b 100644 --- a/src/discord/helper.rs +++ b/src/discord/helper.rs @@ -261,6 +261,7 @@ pub async fn add_user_to_game( codename, channel_webhook_id: webhook.id.0, profile_pic_url: profile_pic.unwrap().link, + alive: true, }; global_data diff --git a/src/game/game_state.rs b/src/game/game_state.rs index 37b0f4b..71eab8f 100644 --- a/src/game/game_state.rs +++ b/src/game/game_state.rs @@ -53,6 +53,12 @@ impl GameState { .cloned() } + pub fn get_player_by_codename_mut(&mut self, codename: &str) -> Option<&mut PlayerData> { + self.player_data + .iter_mut() + .find(|p| p.codename.to_lowercase() == codename.to_lowercase()) + } + pub fn next_phase(&mut self, duration: Duration) { if self.current_phase == Phase::Night { self.current_phase = Phase::Day diff --git a/src/game/player_data.rs b/src/game/player_data.rs index fec5c88..3299d52 100644 --- a/src/game/player_data.rs +++ b/src/game/player_data.rs @@ -8,6 +8,7 @@ pub struct PlayerData { pub vote_target: Option, pub profile_pic_url: String, pub channel_webhook_id: u64, + pub alive: bool, } impl PlayerData {