Modularise and cleanup handlers

This commit is contained in:
Condorra 2022-12-24 13:43:28 +11:00
parent 7ff85d7fdb
commit 729a218b36
5 changed files with 39 additions and 7 deletions

View File

@ -36,6 +36,7 @@ pub fn ansi(input: TokenStream) -> TokenStream {
Special(s) if s == "bold" => "\x1b[1m", Special(s) if s == "bold" => "\x1b[1m",
Special(s) if s == "under" => "\x1b[4m", Special(s) if s == "under" => "\x1b[4m",
Special(s) if s == "strike" => "\x1b[9m", Special(s) if s == "strike" => "\x1b[9m",
Special(s) if s == "nounder" => "\x1b[24m",
Special(s) if s == "black" => "\x1b[30m", Special(s) if s == "black" => "\x1b[30m",
Special(s) if s == "red" => "\x1b[31m", Special(s) if s == "red" => "\x1b[31m",
Special(s) if s == "green" => "\x1b[32m", Special(s) if s == "green" => "\x1b[32m",
@ -52,6 +53,7 @@ pub fn ansi(input: TokenStream) -> TokenStream {
Special(s) if s == "bgmagenta" => "\x1b[45m", Special(s) if s == "bgmagenta" => "\x1b[45m",
Special(s) if s == "bgcyan" => "\x1b[46m", Special(s) if s == "bgcyan" => "\x1b[46m",
Special(s) if s == "bgwhite" => "\x1b[47m", Special(s) if s == "bgwhite" => "\x1b[47m",
Special(s) if s == "lt" => "<",
Special(r) => panic!("Unknown ansi type {}", r) Special(r) => panic!("Unknown ansi type {}", r)
} }
), eof)(i).map(|(_, (r, _))| r) ), eof)(i).map(|(_, (r, _))| r)

View File

@ -65,9 +65,10 @@ impl DBPool {
Ok(()) Ok(())
} }
pub async fn start_session(self: Self, session: ListenerSession) -> DResult<()> { pub async fn start_session(self: Self, session: &ListenerSession) -> DResult<()> {
self.get_conn().await?.execute( self.get_conn().await?.execute(
"INSERT INTO sessions (session, listener, details) VALUES ($1, $2, '{}')", "INSERT INTO sessions (session, listener, details) \
VALUES ($1, $2, '{}') ON CONFLICT (session) DO NOTHING",
&[&session.session, &session.listener] &[&session.session, &session.listener]
).await?; ).await?;
Ok(()) Ok(())

View File

@ -3,7 +3,9 @@ use crate::db;
use MessageFromListener::*; use MessageFromListener::*;
use uuid::Uuid; use uuid::Uuid;
use crate::DResult; use crate::DResult;
use ansi_macro::ansi;
mod new_session;
mod user_commands;
#[derive(Clone,Debug)] #[derive(Clone,Debug)]
pub struct ListenerSession { pub struct ListenerSession {
@ -15,15 +17,14 @@ pub async fn handle(listener: Uuid, msg: MessageFromListener, pool: db::DBPool)
-> DResult<()> { -> DResult<()> {
match msg { match msg {
ListenerPing { .. } => { pool.record_listener_ping(listener).await?; } ListenerPing { .. } => { pool.record_listener_ping(listener).await?; }
SessionConnected { session, source: _ } => { SessionConnected { session, source } => {
pool.start_session(ListenerSession { listener, session }).await?; new_session::handle(&ListenerSession { listener, session }, &source, pool).await?;
} }
SessionDisconnected { session } => { SessionDisconnected { session } => {
pool.end_session(ListenerSession { listener, session }).await?; pool.end_session(ListenerSession { listener, session }).await?;
} }
SessionSentLine { session, msg } => { SessionSentLine { session, msg } => {
pool.queue_for_session(&ListenerSession { listener, session }, user_commands::handle(&ListenerSession { listener, session }, &msg, pool).await?;
&format!(ansi!("You hear an echo saying: <bggreen><red>{}<reset>\r\n"), msg)).await?;
} }
AcknowledgeMessage => {} AcknowledgeMessage => {}
} }

View File

@ -0,0 +1,16 @@
use crate::message_handler::ListenerSession;
use crate::DResult;
use crate::db::DBPool;
use ansi_macro::ansi;
pub async fn handle(session: &ListenerSession, _source: &str, pool: DBPool) -> DResult<()> {
pool.clone().start_session(session).await?;
pool.queue_for_session(&session, &ansi!("\
Welcome to <red>BlastMud<reset> - a text-based post-apocalyptic \
game <bold>restricted to adults (18+)<reset>\r\n\
Some commands to get you started:\r\n\
\t<bold>register <lt>username> <lt>password> <lt>email><reset> to register as a new user.\r\n\
\t<bold>connect <lt>username> <lt>password><reset> to log in as an existing user.\r\n\
\t<bold>help<reset> to learn more.\r\n")).await?;
Ok(())
}

View File

@ -0,0 +1,12 @@
use crate::message_handler::ListenerSession;
use crate::DResult;
use crate::db::DBPool;
use ansi_macro::ansi;
pub async fn handle(session: &ListenerSession, msg: &str, pool: DBPool) -> DResult<()> {
pool.queue_for_session(session,
&format!(ansi!(
"You hear an echo saying: <bggreen><red>{}<reset>\r\n"
), msg)).await?;
Ok(())
}