diff --git a/ansi_macro/src/lib.rs b/ansi_macro/src/lib.rs index 13dddce..f08ccd4 100644 --- a/ansi_macro/src/lib.rs +++ b/ansi_macro/src/lib.rs @@ -36,6 +36,7 @@ pub fn ansi(input: TokenStream) -> TokenStream { Special(s) if s == "bold" => "\x1b[1m", Special(s) if s == "under" => "\x1b[4m", 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 == "red" => "\x1b[31m", 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 == "bgcyan" => "\x1b[46m", Special(s) if s == "bgwhite" => "\x1b[47m", + Special(s) if s == "lt" => "<", Special(r) => panic!("Unknown ansi type {}", r) } ), eof)(i).map(|(_, (r, _))| r) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index ca7d411..43b8823 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -65,9 +65,10 @@ impl DBPool { 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( - "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] ).await?; Ok(()) diff --git a/blastmud_game/src/message_handler.rs b/blastmud_game/src/message_handler.rs index eb32559..fcf41c9 100644 --- a/blastmud_game/src/message_handler.rs +++ b/blastmud_game/src/message_handler.rs @@ -3,7 +3,9 @@ use crate::db; use MessageFromListener::*; use uuid::Uuid; use crate::DResult; -use ansi_macro::ansi; + +mod new_session; +mod user_commands; #[derive(Clone,Debug)] pub struct ListenerSession { @@ -15,15 +17,14 @@ pub async fn handle(listener: Uuid, msg: MessageFromListener, pool: db::DBPool) -> DResult<()> { match msg { ListenerPing { .. } => { pool.record_listener_ping(listener).await?; } - SessionConnected { session, source: _ } => { - pool.start_session(ListenerSession { listener, session }).await?; + SessionConnected { session, source } => { + new_session::handle(&ListenerSession { listener, session }, &source, pool).await?; } SessionDisconnected { session } => { pool.end_session(ListenerSession { listener, session }).await?; } SessionSentLine { session, msg } => { - pool.queue_for_session(&ListenerSession { listener, session }, - &format!(ansi!("You hear an echo saying: {}\r\n"), msg)).await?; + user_commands::handle(&ListenerSession { listener, session }, &msg, pool).await?; } AcknowledgeMessage => {} } diff --git a/blastmud_game/src/message_handler/new_session.rs b/blastmud_game/src/message_handler/new_session.rs new file mode 100644 index 0000000..3892a8b --- /dev/null +++ b/blastmud_game/src/message_handler/new_session.rs @@ -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 BlastMud - a text-based post-apocalyptic \ + game restricted to adults (18+)\r\n\ + Some commands to get you started:\r\n\ + \tregister username> password> email> to register as a new user.\r\n\ + \tconnect username> password> to log in as an existing user.\r\n\ + \thelp to learn more.\r\n")).await?; + Ok(()) +} diff --git a/blastmud_game/src/message_handler/user_commands.rs b/blastmud_game/src/message_handler/user_commands.rs new file mode 100644 index 0000000..90d4b05 --- /dev/null +++ b/blastmud_game/src/message_handler/user_commands.rs @@ -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: {}\r\n" + ), msg)).await?; + Ok(()) +}