Keep dead player channels
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/tag/woodpecker Pipeline was successful Details

+ Instead of deleting channels, players are marked dead so they can't interact with the game
+ Players are listed as dead in the $players command
+ All player channels are cleaned up at the end of the game
+ clippy + fmt
msg_refactor
Joey Hines 2022-06-24 21:15:06 -06:00
parent 0f70ac5f51
commit 2aec084712
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
5 changed files with 31 additions and 25 deletions

View File

@ -23,9 +23,7 @@ use crate::game::Phase;
use crate::messages::DiscordUser; use crate::messages::DiscordUser;
#[group] #[group]
#[commands( #[commands(start, say, end, broadcast, next_phase, kill, add_time, test_theme)]
start, say, end, broadcast, next_phase, terminate, add_time, test_theme
)]
struct Host; struct Host;
#[command] #[command]
@ -280,37 +278,24 @@ async fn next_phase(ctx: &Context, msg: &Message, mut args: Args) -> CommandResu
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[allowed_roles("wolfx host")] #[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 mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap(); let global_data = data.get_mut::<GlobalData>().unwrap();
let guild = msg.guild(&ctx.cache).await.unwrap();
let mut global_data = global_data.lock().await; let mut global_data = global_data.lock().await;
let target = args.rest().to_lowercase(); let target = args.rest().to_lowercase();
let index = global_data let player = global_data
.game_state_mut()? .game_state_mut()?
.player_data .get_player_by_codename_mut(&target);
.iter()
.position(|p| p.codename.to_lowercase() == target);
if let Some(index) = index { if let Some(player) = player {
let player = global_data.game_state_mut()?.player_data.remove(index); player.alive = false;
msg.reply(&ctx.http, format!("{} has been killed.", player.codename))
let player_channel = guild .await?;
.channels
.get(&ChannelId::from(player.channel))
.unwrap();
player_channel.delete(&ctx.http).await.unwrap();
msg.reply(&ctx.http, format!("{} ", player.codename))
.await
.unwrap();
} else { } else {
msg.reply(&ctx.http, "No player found with that codename.") msg.reply(&ctx.http, "No player found with that codename.")
.await .await?;
.unwrap();
} }
global_data.save_game_state().unwrap(); global_data.save_game_state().unwrap();
@ -368,6 +353,7 @@ async fn add_time(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
#[command] #[command]
#[only_in(guilds)] #[only_in(guilds)]
#[allowed_roles("wolfx host")]
#[description = "Display all the messages from a theme"] #[description = "Display all the messages from a theme"]
async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult { async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut data = ctx.data.write().await; 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, vote_target: None,
profile_pic_url: "".to_string(), profile_pic_url: "".to_string(),
channel_webhook_id: 0, channel_webhook_id: 0,
alive: true,
}; };
let player_1_discord = DiscordUser { let player_1_discord = DiscordUser {
@ -402,6 +389,7 @@ async fn test_theme(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
vote_target: None, vote_target: None,
profile_pic_url: "".to_string(), profile_pic_url: "".to_string(),
channel_webhook_id: 0, channel_webhook_id: 0,
alive: false,
}; };
let mut players = vec![test_player_1.clone(), test_player_2.clone()]; 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); msg_builder.push_line(&global_data.game_cfg()?.player_group_name);
for player in &global_data.game_state()?.player_data { 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 { if msg.channel_id.0 == global_data.cfg.discord_config.host_channel {
let guild = msg.guild(&ctx.cache).await.unwrap(); let guild = msg.guild(&ctx.cache).await.unwrap();

View File

@ -38,6 +38,11 @@ impl EventHandler for Handler {
.unwrap() .unwrap()
.get_player_from_channel(msg.channel_id.0) .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 guild = msg.guild(&ctx.cache).await.unwrap();
let user_msg = msg.content.clone(); let user_msg = msg.content.clone();

View File

@ -261,6 +261,7 @@ pub async fn add_user_to_game(
codename, codename,
channel_webhook_id: webhook.id.0, channel_webhook_id: webhook.id.0,
profile_pic_url: profile_pic.unwrap().link, profile_pic_url: profile_pic.unwrap().link,
alive: true,
}; };
global_data global_data

View File

@ -53,6 +53,12 @@ impl GameState {
.cloned() .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) { pub fn next_phase(&mut self, duration: Duration) {
if self.current_phase == Phase::Night { if self.current_phase == Phase::Night {
self.current_phase = Phase::Day self.current_phase = Phase::Day

View File

@ -8,6 +8,7 @@ pub struct PlayerData {
pub vote_target: Option<u64>, pub vote_target: Option<u64>,
pub profile_pic_url: String, pub profile_pic_url: String,
pub channel_webhook_id: u64, pub channel_webhook_id: u64,
pub alive: bool,
} }
impl PlayerData { impl PlayerData {