blastmud/blastmud_game/src/message_handler/user_commands.rs

231 lines
6.7 KiB
Rust
Raw Normal View History

2022-12-24 21:16:23 +11:00
use super::ListenerSession;
2022-12-24 13:43:28 +11:00
use crate::DResult;
use crate::db::{DBTrans, DBPool, ItemSearchParams};
use ansi::ansi;
2022-12-24 21:16:23 +11:00
use phf::phf_map;
use async_trait::async_trait;
use crate::models::{session::Session, user::User, item::Item};
use log::warn;
use once_cell::sync::OnceCell;
use std::sync::Arc;
2022-12-24 13:43:28 +11:00
mod agree;
2023-01-15 17:30:23 +11:00
pub mod attack;
2023-01-30 22:28:43 +11:00
mod buy;
mod describe;
2022-12-24 21:16:23 +11:00
mod help;
mod ignore;
2023-02-02 22:58:20 +11:00
mod inventory;
2022-12-26 01:30:59 +11:00
mod less_explicit_mode;
2023-01-29 00:26:06 +11:00
mod list;
2022-12-27 16:08:27 +11:00
mod login;
mod look;
mod map;
2023-01-02 13:25:05 +11:00
pub mod movement;
pub mod parsing;
mod quit;
mod register;
pub mod say;
mod whisper;
2023-01-28 23:00:53 +11:00
mod who;
2022-12-24 21:16:23 +11:00
pub struct VerbContext<'l> {
pub session: &'l ListenerSession,
pub session_dat: &'l mut Session,
pub user_dat: &'l mut Option<User>,
pub trans: &'l DBTrans
2022-12-24 21:16:23 +11:00
}
pub enum CommandHandlingError {
UserError(String),
SystemError(Box<dyn std::error::Error + Send + Sync>)
}
use CommandHandlingError::*;
#[async_trait]
pub trait UserVerb {
2022-12-26 01:30:59 +11:00
async fn handle(self: &Self, ctx: &mut VerbContext, verb: &str, remaining: &str) -> UResult<()>;
2022-12-24 21:16:23 +11:00
}
pub type UResult<A> = Result<A, CommandHandlingError>;
2022-12-27 16:08:27 +11:00
impl<T> From<T> for CommandHandlingError where T: Into<Box<dyn std::error::Error + Send + Sync>> {
fn from(input: T) -> CommandHandlingError {
SystemError(input.into())
2022-12-24 21:16:23 +11:00
}
}
pub fn user_error<A>(msg: String) -> UResult<A> {
Err(UserError(msg))
}
2022-12-26 01:30:59 +11:00
/* Verb registries list types of commands available in different circumstances. */
pub type UserVerbRef = &'static (dyn UserVerb + Sync + Send);
2022-12-24 21:16:23 +11:00
type UserVerbRegistry = phf::Map<&'static str, UserVerbRef>;
static ALWAYS_AVAILABLE_COMMANDS: UserVerbRegistry = phf_map! {
"" => ignore::VERB,
"help" => help::VERB,
"quit" => quit::VERB,
2022-12-24 21:16:23 +11:00
};
2022-12-26 01:30:59 +11:00
static UNREGISTERED_COMMANDS: UserVerbRegistry = phf_map! {
"agree" => agree::VERB,
"connect" => login::VERB,
2022-12-26 01:30:59 +11:00
"less_explicit_mode" => less_explicit_mode::VERB,
2022-12-27 16:08:27 +11:00
"login" => login::VERB,
"register" => register::VERB,
};
static REGISTERED_COMMANDS: UserVerbRegistry = phf_map! {
// Movement comments first:
"north" => movement::VERB,
"n" => movement::VERB,
"northeast" => movement::VERB,
"ne" => movement::VERB,
"east" => movement::VERB,
"e" => movement::VERB,
"southeast" => movement::VERB,
"se" => movement::VERB,
"south" => movement::VERB,
"s" => movement::VERB,
"southwest" => movement::VERB,
"sw" => movement::VERB,
"west" => movement::VERB,
"w" => movement::VERB,
"northwest" => movement::VERB,
"nw" => movement::VERB,
"up" => movement::VERB,
"down" => movement::VERB,
// Other commands (alphabetical except aliases grouped):
2023-01-15 17:30:23 +11:00
"attack" => attack::VERB,
2023-01-30 22:28:43 +11:00
"buy" => buy::VERB,
2023-02-02 22:58:20 +11:00
"inventory" => inventory::VERB,
"inv" => inventory::VERB,
"i" => inventory::VERB,
2023-01-15 17:30:23 +11:00
"kill" => attack::VERB,
"k" => attack::VERB,
"describe" => describe::VERB,
"l" => look::VERB,
"look" => look::VERB,
"read" => look::VERB,
2023-01-29 00:26:06 +11:00
"list" => list::VERB,
2023-01-15 17:30:23 +11:00
"lmap" => map::VERB,
2023-01-15 17:30:23 +11:00
"\'" => say::VERB,
"say" => say::VERB,
2023-01-15 17:30:23 +11:00
"-" => whisper::VERB,
"whisper" => whisper::VERB,
"tell" => whisper::VERB,
2023-01-28 23:00:53 +11:00
"who" => who::VERB,
2022-12-26 01:30:59 +11:00
};
fn resolve_handler(ctx: &VerbContext, cmd: &str) -> Option<&'static UserVerbRef> {
let mut result = ALWAYS_AVAILABLE_COMMANDS.get(cmd);
match &ctx.user_dat {
None => {
result = result.or_else(|| UNREGISTERED_COMMANDS.get(cmd));
}
Some(user_dat) => {
if user_dat.terms.terms_complete {
result = result.or_else(|| REGISTERED_COMMANDS.get(cmd));
} else if cmd == "agree" {
result = Some(&agree::VERB);
}
}
2022-12-26 01:30:59 +11:00
}
result
}
2022-12-24 21:16:23 +11:00
pub async fn handle(session: &ListenerSession, msg: &str, pool: &DBPool) -> DResult<()> {
let (cmd, params) = parsing::parse_command_name(msg);
2022-12-25 00:25:52 +11:00
let trans = pool.start_transaction().await?;
2022-12-26 01:30:59 +11:00
let (mut session_dat, mut user_dat) = match trans.get_session_user_model(session).await? {
None => {
// If the session has been cleaned up from the database, there is
// nowhere to go from here, so just ignore it.
warn!("Got command from session not in database: {}", session.session);
return Ok(());
}
Some(v) => v
};
2022-12-26 01:30:59 +11:00
let mut ctx = VerbContext { session, trans: &trans, session_dat: &mut session_dat,
user_dat: &mut user_dat };
let handler_opt = resolve_handler(&ctx, cmd);
2022-12-24 21:16:23 +11:00
match handler_opt {
None => {
2022-12-25 00:25:52 +11:00
trans.queue_for_session(session,
Some(ansi!(
2022-12-25 00:25:52 +11:00
"That's not a command I know. Try <bold>help<reset>\r\n"
))
2022-12-24 21:16:23 +11:00
).await?;
trans.commit().await?;
2022-12-24 21:16:23 +11:00
}
Some(handler) => {
2022-12-26 01:30:59 +11:00
match handler.handle(&mut ctx, cmd, params).await {
Ok(()) => {
trans.commit().await?;
}
2022-12-24 21:16:23 +11:00
Err(UserError(err_msg)) => {
pool.queue_for_session(session, Some(&(err_msg + "\r\n"))).await?;
2022-12-24 21:16:23 +11:00
}
Err(SystemError(e)) => Err(e)?
}
}
}
2023-01-28 23:00:53 +11:00
pool.bump_session_time(&session).await?;
2022-12-24 13:43:28 +11:00
Ok(())
}
pub fn is_likely_explicit(msg: &str) -> bool {
static EXPLICIT_MARKER_WORDS: OnceCell<Vec<&'static str>> =
OnceCell::new();
let markers = EXPLICIT_MARKER_WORDS.get_or_init(||
vec!("fuck", "sex", "cock", "cunt", "dick", "pussy", "whore",
"orgasm", "erection", "nipple", "boob", "tit"));
for word in markers {
if msg.contains(word) {
return true
}
}
false
}
pub fn get_user_or_fail<'l>(ctx: &'l VerbContext) -> UResult<&'l User> {
ctx.user_dat.as_ref()
.ok_or_else(|| UserError("Not logged in".to_owned()))
}
pub fn get_user_or_fail_mut<'l>(ctx: &'l mut VerbContext) -> UResult<&'l mut User> {
ctx.user_dat.as_mut()
.ok_or_else(|| UserError("Not logged in".to_owned()))
}
pub async fn get_player_item_or_fail(ctx: &VerbContext<'_>) -> UResult<Arc<Item>> {
Ok(ctx.trans.find_item_by_type_code(
"player", &get_user_or_fail(ctx)?.username.to_lowercase()).await?
.ok_or_else(|| UserError("Your character is gone, you'll need to re-register or ask an admin".to_owned()))?)
}
pub async fn search_item_for_user<'l>(ctx: &'l VerbContext<'l>, search: &'l ItemSearchParams<'l>) ->
UResult<Arc<Item>> {
Ok(match &ctx.trans.resolve_items_by_display_name_for_player(search).await?[..] {
[] => user_error("Sorry, I couldn't find anything matching.".to_owned())?,
[match_it] => match_it.clone(),
[item1, ..] =>
item1.clone(),
})
}