Added test_theme command
ci/woodpecker/push/woodpecker Pipeline was successful Details

+ Allows you to see all the messages in a theme easily
+ Clippy + fmt
msg_refactor
Joey Hines 2022-05-29 13:14:30 -06:00
parent 45b368fd11
commit 3daa20db9a
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
3 changed files with 149 additions and 6 deletions

View File

@ -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::<GlobalData>().unwrap();
let global_data = global_data.lock().await;
let theme = args.parse::<String>()?;
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 <code_name>"]
#[description = "vote another subject for termination. $vote <code_name>"]
async fn vote(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let mut data = ctx.data.write().await;
let global_data = data.get_mut::<GlobalData>().unwrap();

View File

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

View File

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