blastmud/blastmud_game/src/message_handler/user_commands.rs

112 lines
3.3 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;
2022-12-25 00:25:52 +11:00
use crate::db::{DBTrans, DBPool};
2022-12-24 13:43:28 +11:00
use ansi_macro::ansi;
2022-12-24 21:16:23 +11:00
use phf::phf_map;
use async_trait::async_trait;
2022-12-26 01:30:59 +11:00
use crate::models::{session::Session, user::User};
use log::warn;
2022-12-24 13:43:28 +11:00
2022-12-24 21:16:23 +11:00
mod parsing;
mod ignore;
mod help;
mod quit;
2022-12-26 01:30:59 +11:00
mod less_explicit_mode;
mod register;
2022-12-24 21:16:23 +11:00
pub struct VerbContext<'l> {
session: &'l ListenerSession,
session_dat: &'l mut Session,
2022-12-26 01:30:59 +11:00
user_dat: &'l mut Option<User>,
2022-12-25 00:25:52 +11:00
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>;
impl From<Box<dyn std::error::Error + Send + Sync>> for CommandHandlingError {
fn from(input: Box<dyn std::error::Error + Send + Sync>) -> CommandHandlingError {
SystemError(input)
}
}
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! {
"less_explicit_mode" => less_explicit_mode::VERB,
"register" => register::VERB,
};
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(_) => {}
}
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?;
}
Some(handler) => {
2022-12-26 01:30:59 +11:00
match handler.handle(&mut ctx, cmd, params).await {
2022-12-24 21:16:23 +11:00
Ok(()) => {}
Err(UserError(err_msg)) => {
trans.queue_for_session(session, Some(&(err_msg + "\r\n"))).await?;
2022-12-24 21:16:23 +11:00
}
Err(SystemError(e)) => Err(e)?
}
}
}
2022-12-25 00:25:52 +11:00
trans.commit().await?;
2022-12-24 13:43:28 +11:00
Ok(())
}