From 8084b020c32b13e1d6889ec41f696e1b5bca18e2 Mon Sep 17 00:00:00 2001 From: Condorra Date: Sun, 19 Mar 2023 15:41:48 +1100 Subject: [PATCH] Show someones primary corp in who. --- blastmud_game/src/db.rs | 13 ++++++++++++- .../src/message_handler/user_commands/who.rs | 6 ++++-- blastmud_game/src/models/corp.rs | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/blastmud_game/src/db.rs b/blastmud_game/src/db.rs index 12b89f45..7adf0fe5 100644 --- a/blastmud_game/src/db.rs +++ b/blastmud_game/src/db.rs @@ -65,6 +65,7 @@ impl From for SendqueueItem { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct OnlineInfo { pub username: String, + pub corp: Option, pub time: Option> } @@ -679,11 +680,21 @@ impl DBTrans { pub async fn get_online_info(&self) ->DResult> { Ok(self.pg_trans()?.query( - "SELECT jsonb_build_object(\ + "WITH show_corps AS (\ + SELECT DISTINCT ON (member_username) \ + m.member_username AS username, \ + c.details ->> 'name' AS corpname \ + FROM corps c JOIN corp_membership m ON m.corp_id = c.corp_id \ + ORDER BY m.member_username, (m.details->>'priority')::int DESC NULLS LAST, \ + (m.details->>'joined_at') :: TIMESTAMPTZ ASC \ + )\ + SELECT jsonb_build_object(\ 'username', u.details->>'username',\ + 'corp', c.corpname, \ 'time', s.details->>'last_active'\ ) FROM sessions s \ JOIN users u ON u.current_session = s.session \ + LEFT JOIN show_corps c ON c.username = LOWER(u.username) \ ORDER BY s.details->>'last_active' DESC", &[] ).await? .into_iter() diff --git a/blastmud_game/src/message_handler/user_commands/who.rs b/blastmud_game/src/message_handler/user_commands/who.rs index 8c2c2e26..5abc79fa 100644 --- a/blastmud_game/src/message_handler/user_commands/who.rs +++ b/blastmud_game/src/message_handler/user_commands/who.rs @@ -8,8 +8,9 @@ pub struct Verb; impl UserVerb for Verb { async fn handle(self: &Self, ctx: &mut VerbContext, _verb: &str, _remaining: &str) -> UResult<()> { let mut msg = String::new(); - msg.push_str(&format!(ansi!("| {:20} | {:15} |\n"), + msg.push_str(&format!(ansi!("| {:20} | {:20} | {:15} |\n"), ansi!("Username"), + ansi!("Corp"), ansi!("Idle"))); for online in ctx.trans.get_online_info().await? { if let Some(online_time) = online.time { @@ -18,7 +19,8 @@ impl UserVerb for Verb { std::time::Duration::from_secs( (Utc::now() - online_time).num_seconds() as u64)); msg.push_str(&format!( - "| {:20} | {:15} |\n", &ignore_special_characters(&online.username), + "| {:20} | {:20} | {:15} |\n", &ignore_special_characters(&online.username), + &ignore_special_characters(&online.corp.unwrap_or("".to_string())), &format!("{}", &diff))); } } diff --git a/blastmud_game/src/models/corp.rs b/blastmud_game/src/models/corp.rs index 425cc890..a490f370 100644 --- a/blastmud_game/src/models/corp.rs +++ b/blastmud_game/src/models/corp.rs @@ -38,6 +38,8 @@ pub struct CorpMembership { pub permissions: Vec, pub allow_combat: bool, pub job_title: String, + pub priority: i64, + pub chat_on: bool } impl Default for CorpMembership { @@ -48,6 +50,8 @@ impl Default for CorpMembership { permissions: vec!(), allow_combat: false, job_title: "Employee".to_owned(), + priority: 100, + chat_on: true, } } }