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;
#[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::<GlobalData>().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();

View File

@ -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();

View File

@ -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

View File

@ -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

View File

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