From e5e6c45e817d5ac6f72030a67df3f980aaa4c4a8 Mon Sep 17 00:00:00 2001 From: Shagnor Date: Sun, 25 Dec 2022 01:42:51 +1100 Subject: [PATCH] Support help that is hidden in less explicit mode. --- blastmud_game/src/db.rs | 12 ++++++++++++ .../src/message_handler/user_commands.rs | 11 +++++++++-- .../src/message_handler/user_commands/help.rs | 15 +++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index 2b877bb7..ca0d86de 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -164,6 +164,18 @@ impl DBTrans { Ok(()) } + pub async fn get_session_model(self: &Self, session: &ListenerSession) -> DResult> { + match self.pg_trans()? + .query_opt("SELECT details FROM sessions WHERE session = $1", &[&session.session]) + .await? { + None => Ok(None), + Some(row) => + Ok(Some(serde_json::from_value( + row.get("details") + )?)) + } + } + pub async fn commit(mut self: Self) -> DResult<()> { let trans_opt = self.with_trans_mut(|t| std::mem::replace(t, None)); for trans in trans_opt { diff --git a/blastmud_game/src/message_handler/user_commands.rs b/blastmud_game/src/message_handler/user_commands.rs index dbbbc0dc..302d3b63 100644 --- a/blastmud_game/src/message_handler/user_commands.rs +++ b/blastmud_game/src/message_handler/user_commands.rs @@ -4,6 +4,7 @@ use crate::db::{DBTrans, DBPool}; use ansi_macro::ansi; use phf::phf_map; use async_trait::async_trait; +use crate::models::session::Session; mod parsing; mod ignore; @@ -11,6 +12,7 @@ mod help; pub struct VerbContext<'l> { session: &'l ListenerSession, + session_dat: &'l mut Session, trans: &'l DBTrans } @@ -48,8 +50,13 @@ static ALWAYS_AVAILABLE_COMMANDS: UserVerbRegistry = phf_map! { pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> { let (cmd, params) = parsing::parse_command_name(msg); let trans = pool.start_transaction().await?; + let mut session_dat = match trans.get_session_model(session).await? { + None => { return Ok(()) } + Some(v) => v + }; let handler_opt = ALWAYS_AVAILABLE_COMMANDS.get(cmd); - + let ctx = VerbContext { session, trans: &trans, session_dat: &mut session_dat }; + match handler_opt { None => { trans.queue_for_session(session, @@ -59,7 +66,7 @@ pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DRes ).await?; } Some(handler) => { - match handler.handle(&VerbContext { session, trans: &trans }, cmd, params).await { + match handler.handle(&ctx, cmd, params).await { Ok(()) => {} Err(UserError(err_msg)) => { trans.queue_for_session(session, &(err_msg + "\r\n")).await?; diff --git a/blastmud_game/src/message_handler/user_commands/help.rs b/blastmud_game/src/message_handler/user_commands/help.rs index ae77de07..2de8fd98 100644 --- a/blastmud_game/src/message_handler/user_commands/help.rs +++ b/blastmud_game/src/message_handler/user_commands/help.rs @@ -19,13 +19,24 @@ static HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! { \thelp register will tell you about the register command.") }; +static EXPLICIT_HELP_PAGES: phf::Map<&'static str, &'static str> = phf_map! { + "fuck" => + ansi!("Type fuck name> to fuck someone. It only works if \ + they have consented.") +}; + pub struct Verb; #[async_trait] impl UserVerb for Verb { async fn handle(self: &Self, ctx: &VerbContext, _verb: &str, remaining: &str) -> UResult<()> { - let help = HELP_PAGES.get(remaining).ok_or( + let mut help = None; + if !ctx.session_dat.less_explicit_mode { + help = help.or_else(|| EXPLICIT_HELP_PAGES.get(remaining)) + } + help = help.or_else(|| HELP_PAGES.get(remaining)); + let help_final = help.ok_or( UserError("No help available on that".to_string()))?; - ctx.trans.queue_for_session(ctx.session, &(help.to_string() + "\r\n")).await?; + ctx.trans.queue_for_session(ctx.session, &(help_final.to_string() + "\r\n")).await?; Ok(()) } }