From 3daa20db9a2cdcf906a92c79e885dbad7d2ad3f0 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 29 May 2022 13:14:30 -0600 Subject: [PATCH] Added test_theme command + Allows you to see all the messages in a theme easily + Clippy + fmt --- src/discord/commands.rs | 149 +++++++++++++++++++++++++++++++++++++++- src/game/global_data.rs | 2 +- src/messages/mod.rs | 4 +- 3 files changed, 149 insertions(+), 6 deletions(-) diff --git a/src/discord/commands.rs b/src/discord/commands.rs index b681b47..2c0b8bd 100644 --- a/src/discord/commands.rs +++ b/src/discord/commands.rs @@ -1,3 +1,4 @@ +use chrono::Duration; use std::collections::HashSet; use rand::prelude::SliceRandom; @@ -16,12 +17,15 @@ use serenity::utils::MessageBuilder; use crate::discord::helper::{add_user_to_game, parse_duration_arg, send_msg_to_player_channels}; use crate::error::{Result, WoxlfError}; use crate::game::global_data::GlobalData; +use crate::game::player_data::PlayerData; use crate::game::MessageSource; use crate::game::Phase; use crate::messages::DiscordUser; #[group] -#[commands(start, say, end, broadcast, next_phase, terminate, add_time)] +#[commands( + start, say, end, broadcast, next_phase, terminate, add_time, test_theme +)] struct Host; #[command] @@ -107,7 +111,9 @@ async fn start(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { global_data.save_game_state().unwrap(); - ctx.http.edit_nickname(guild.id.0, Some(&global_data.game_cfg()?.bot_name)).await?; + ctx.http + .edit_nickname(guild.id.0, Some(&global_data.game_cfg()?.bot_name)) + .await?; Ok(()) } @@ -346,13 +352,150 @@ async fn add_time(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult Ok(()) } +#[command] +#[only_in(guilds)] +#[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; + + let global_data = data.get_mut::().unwrap(); + let global_data = global_data.lock().await; + + let theme = args.parse::()?; + + let mut test_global_data = GlobalData::new(global_data.cfg.clone()); + + test_global_data.start_game(&theme, Phase::Day, Duration::hours(1))?; + + let test_player_1 = PlayerData { + channel: 0, + discord_id: 0, + codename: "Test Player 1".to_string(), + vote_target: None, + profile_pic_url: "".to_string(), + channel_webhook_id: 0, + }; + + let player_1_discord = DiscordUser { + display_name: "Test Player Discord".to_string(), + mention: "@Test Player Discorcd".to_string(), + }; + + let test_player_2 = PlayerData { + channel: 0, + discord_id: 0, + codename: "Test Player 2".to_string(), + vote_target: None, + profile_pic_url: "".to_string(), + channel_webhook_id: 0, + }; + + let mut players = vec![test_player_1.clone(), test_player_2.clone()]; + + test_global_data + .game_state_mut()? + .player_data + .append(&mut players); + + let codename_format = test_global_data.templates()?.build_name( + &test_global_data, + Some("FirstName".to_lowercase()), + Some("LastName".to_lowercase()), + )?; + + let phase_extend_message = test_global_data + .templates()? + .build_phase_extend_message(&test_global_data)?; + + let vote_message = test_global_data.templates()?.build_vote_message( + &test_global_data, + &test_player_1, + &test_player_2, + )?; + + let status_message = test_global_data + .templates()? + .build_satus_message(&test_global_data)?; + + let announcement_message = test_global_data + .templates()? + .build_announcement(&test_global_data, "Test announcement")?; + + let welcome_message = test_global_data.templates()?.build_welcome_message( + &test_global_data, + &player_1_discord, + &test_player_1, + )?; + + let tally_message = test_global_data + .templates()? + .build_vote_tally(&test_global_data)?; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Codename Format==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(codename_format)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Phase Extend Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(phase_extend_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Vote Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(vote_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Status Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(status_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Announcement Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(announcement_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Welcome Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(welcome_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + + msg.channel_id + .send_message(&ctx.http, |m| m.content("==Tally Message==")) + .await?; + msg.channel_id + .send_message(&ctx.http, |m| m.content(tally_message)) + .await?; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + Ok(()) +} + #[group] #[commands(vote, status, players)] struct Player; #[command] #[only_in(guilds)] -#[description = "Vote another subject for termination. $vote "] +#[description = "vote another subject for termination. $vote "] async fn vote(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let mut data = ctx.data.write().await; let global_data = data.get_mut::().unwrap(); diff --git a/src/game/global_data.rs b/src/game/global_data.rs index 3e41d17..25d4534 100644 --- a/src/game/global_data.rs +++ b/src/game/global_data.rs @@ -82,7 +82,7 @@ impl GlobalData { .get_game_config(&self.game_state.as_ref().unwrap().game_name) .unwrap(); - self.templates = Some(MessageTemplates::try_from(game_config.messages.clone()).unwrap()); + self.templates = Some(MessageTemplates::try_from(game_config.messages.clone())?); self.game_cfg = Some(game_config); Ok(()) diff --git a/src/messages/mod.rs b/src/messages/mod.rs index fe7c8e7..dd2327d 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -35,8 +35,8 @@ fn time_to_discord_time( #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DiscordUser { - display_name: String, - mention: String, + pub(crate) display_name: String, + pub(crate) mention: String, } #[derive(Debug, Clone, Serialize, Deserialize)]