From e39aaeb94bf0e0b1559e0d9c0afa92f7dbc9992a Mon Sep 17 00:00:00 2001 From: David Goldstein Date: Mon, 9 Nov 2026 14:27:06 -0500 Subject: [PATCH] Row 3027: a rename at logout sends online buddies their list and guildmates the guild again A buddy's login sends only the channel (0x3F 0x14), so a buddy online through a Name Change kept the old name in the Friend tab and in the login card. Party names already refresh on login (Parties::update). --- agent_docs/fields.md | 9 ++- db/src/buddy.rs | 9 +++ integration-harness/src/database.rs | 38 ++++++++++++ .../tests/cash_shop_name_change.rs | 62 ++++++++++++++++++- runtime/src/actor/client/buddies.rs | 47 ++++++++++---- runtime/src/actor/client/guilds.rs | 20 ++++++ runtime/src/actor/client/name_change.rs | 12 +++- 7 files changed, 182 insertions(+), 15 deletions(-) diff --git a/agent_docs/fields.md b/agent_docs/fields.md index b836b566f..d0a6b3727 100644 --- a/agent_docs/fields.md +++ b/agent_docs/fields.md @@ -314,8 +314,13 @@ Prepaid, with no month to wait; either way the receipt goes from the Cash Inventory and the bags. The world applies every waiting request when it starts; one that fails waits for the next logout. Deleting the character refunds its request. Gifts not yet thanked for (`cash_items.gift_from`) -follow the new name; notes, owner tags and the like keep the old one. Name -Change Coupons (5401000) stay sold out. +follow the new name; notes, owner tags and the like keep the old one. A +rename at logout sends everyone online whose buddy list holds the character +the whole list again (`0x3F` 7), and the other guild members online the +guild: their clients keep the names they were last sent, and a buddy's +login sends only the channel (`0x3F` 0x14), so the next login card would +show the old name. Parties refresh the name on login (`Parties::update`). +Name Change Coupons (5401000) stay sold out. ## Packets diff --git a/db/src/buddy.rs b/db/src/buddy.rs index 1230057d7..249afcf90 100644 --- a/db/src/buddy.rs +++ b/db/src/buddy.rs @@ -80,6 +80,15 @@ pub fn mutual_ids(cid: i32) -> crate::Result> { .collect()) } +/// Characters whose list holds `cid` at all, an unanswered request too. +pub fn listed_in(cid: i32) -> crate::Result> { + let mut conn = crate::connection()?; + Ok(buddies::table + .filter(buddies::buddy_id.eq(cid)) + .select(buddies::character_id) + .load::(&mut conn)?) +} + /// Characters whose (accepted) list holds `cid`. fn listed_by(conn: &mut PgConnection, cid: i32) -> QueryResult> { Ok(buddies::table diff --git a/integration-harness/src/database.rs b/integration-harness/src/database.rs index 872694b1a..fe919fae1 100644 --- a/integration-harness/src/database.rs +++ b/integration-harness/src/database.rs @@ -202,6 +202,44 @@ pub async fn rename_character(character_id: i32, name: &str) -> Result<(), Harne .await } +/// Make the characters `a` and `b` buddies of each other. +pub async fn befriend(a: i32, b: i32) -> Result<(), HarnessError> { + for (owner, buddy) in [(a, b), (b, a)] { + change_one_row("befriend", move |conn| { + diesel::sql_query( + "INSERT INTO buddies (character_id, buddy_id, group_name, pending) \ + VALUES ($1, $2, 'Default Group', false)", + ) + .bind::(owner) + .bind::(buddy) + .execute(conn) + }) + .await?; + } + Ok(()) +} + +/// Queue a Name Change from `old` to `new` for `character_id`, as a +/// bought one would be, to be made at its next logout. +pub async fn queue_name_change( + character_id: i32, + old: &str, + new: &str, +) -> Result<(), HarnessError> { + let (old, new) = (old.to_string(), new.to_string()); + change_one_row("queue name change", move |conn| { + diesel::sql_query( + "INSERT INTO name_changes (character_id, cash_id, price, old_name, new_name) \ + VALUES ($1, $1, 0, $2, $3)", + ) + .bind::(character_id) + .bind::(old) + .bind::(new) + .execute(conn) + }) + .await +} + /// Make the logged-out character `character_id` a level `level` `job` /// with `mp` MP of `mp`. pub async fn set_job(character_id: i32, job: i16, level: i16, mp: i16) -> Result<(), HarnessError> { diff --git a/integration-harness/tests/cash_shop_name_change.rs b/integration-harness/tests/cash_shop_name_change.rs index 2aec238dc..4c2ae403a 100644 --- a/integration-harness/tests/cash_shop_name_change.rs +++ b/integration-harness/tests/cash_shop_name_change.rs @@ -3,7 +3,9 @@ //! taking effect at logout, with a month before the next (report c060). use integration_harness::connection::{MapleTestConnection, PacketEnvelope}; -use integration_harness::database::{rename_character, set_job, set_nx_prepaid}; +use integration_harness::database::{ + befriend, found_guild, join_guild, queue_name_change, rename_character, set_job, set_nx_prepaid, +}; use integration_harness::packets::{ build_cash_shop_take_out, build_check_char_name, build_enter_cash_shop, build_leave_cash_shop, build_player_logged_in, decode_channel_change, opcode_name, @@ -296,3 +298,61 @@ async fn a_name_taken_meanwhile_refunds_the_request() { assert_eq!(enter_cash_shop(connection).await, (0, PRICE), "refunded"); assert_eq!(rules(connection, id).await, 0, "no month to wait"); } + +/// Buddies and guild members online keep the names of the last list they +/// were sent, and a buddy's login sends only their channel: so the rename +/// sends them the list and the guild again. +#[tokio::test] +async fn buddies_and_guild_online_see_the_new_name() { + let config = load_harness_config_or_fail().await; + let other = load_harness_config_or_fail().await; + let session = login_to_world_session(&config) + .await + .expect("login-to-world session flow failed"); + let (id, old_name) = (session.character_id, session.character_name.clone()); + drop(session); + let other_id = login_to_world_session(&other) + .await + .expect("the other character") + .character_id; + sleep(Duration::from_millis(500)).await; + befriend(id, other_id).await.expect("buddies"); + let guild = format!("rn{id}"); + found_guild(id, &guild).await.expect("a guild"); + join_guild(other_id, &guild).await.expect("join it"); + let new_name = format!("renamed{id}"); + queue_name_change(id, &old_name, &new_name) + .await + .expect("a Name Change"); + + let mut watcher = relogin(&other).await; + let renamed = relogin(&config).await; + sleep(Duration::from_millis(500)).await; + drop(renamed); + + let named = |bytes: &[u8]| { + bytes + .windows(new_name.len()) + .any(|w| w == new_name.as_bytes()) + }; + let (mut buddies, mut guild) = (false, false); + while !(buddies && guild) { + let envelope = timeout( + Duration::from_secs(5), + watcher.connection.read_packet("the new name"), + ) + .await + .unwrap_or_else(|_| panic!("timed out: buddies {buddies}, guild {guild}")) + .expect("read"); + let bytes = &envelope.packet.bytes; + if !named(bytes) { + continue; + } + if envelope.opcode() == SendOpcode::BuddyList as i16 { + assert_eq!(bytes[2], 0x07, "the whole list"); + buddies = true; + } else if envelope.opcode() == SendOpcode::GuildOperation as i16 { + guild = true; + } + } +} diff --git a/runtime/src/actor/client/buddies.rs b/runtime/src/actor/client/buddies.rs index ceb2d6788..e561209fa 100644 --- a/runtime/src/actor/client/buddies.rs +++ b/runtime/src/actor/client/buddies.rs @@ -135,24 +135,37 @@ impl ClientActor { } async fn send_buddies(&mut self, rows: Vec, entering: bool) -> Result<(), RuntimeError> { - let buddies = rows - .into_iter() - .map(|buddy| ListedBuddy { - id: buddy.id, - name: buddy.name, - group: buddy.group, - mutual: buddy.mutual, - pending: buddy.pending, - }) - .collect(); self.send_to_directory(DirectoryMessage::BuddyList { owner: self.client_id, - buddies, + buddies: listed(rows), entering, }) .await } + /// After a Name Change, at logout: everyone online whose list holds the + /// player gets the whole list again. The client keeps the names of the + /// last list it had, and a buddy's login only gives their channel. + pub(super) async fn show_new_name_to_buddies(&mut self) -> Result<(), RuntimeError> { + let cid = self.client_id; + let lists = spawn_db(move || { + db::buddy::listed_in(cid)? + .into_iter() + .map(|owner| Ok((owner, db::buddy::list(owner)?))) + .collect::>>() + }) + .await?; + for (owner, rows) in lists { + self.send_to_directory(DirectoryMessage::BuddyList { + owner, + buddies: listed(rows), + entering: false, + }) + .await?; + } + Ok(()) + } + async fn announce_channel( &mut self, to: Vec, @@ -166,3 +179,15 @@ impl ClientActor { .await } } + +fn listed(rows: Vec) -> Vec { + rows.into_iter() + .map(|buddy| ListedBuddy { + id: buddy.id, + name: buddy.name, + group: buddy.group, + mutual: buddy.mutual, + pending: buddy.pending, + }) + .collect() +} diff --git a/runtime/src/actor/client/guilds.rs b/runtime/src/actor/client/guilds.rs index 2b5ba1fcf..90d09be25 100644 --- a/runtime/src/actor/client/guilds.rs +++ b/runtime/src/actor/client/guilds.rs @@ -353,6 +353,26 @@ impl ClientActor { .await } + /// After a Name Change, at logout: the other members online get the + /// guild again, with the new name in it. + pub(super) async fn show_new_name_to_guild(&mut self) -> Result<(), RuntimeError> { + let Some(guild_id) = self.in_game()?.player.guild.as_ref().map(|tag| tag.id) else { + return Ok(()); + }; + let Some(guild) = spawn_db(move || db::guild::load(guild_id)).await? else { + return Ok(()); + }; + let cid = self.client_id; + for member in guild.members.iter().filter(|member| member.id != cid) { + self.send_to_directory(DirectoryMessage::GuildInfo { + owner: member.id, + guild: guild.clone(), + }) + .await?; + } + Ok(()) + } + /// The directory, which knows who is online, sends the guild. async fn show_guild(&mut self, guild_id: i32) -> Result<(), RuntimeError> { let Some(guild) = spawn_db(move || db::guild::load(guild_id)).await? else { diff --git a/runtime/src/actor/client/name_change.rs b/runtime/src/actor/client/name_change.rs index 9fd2e639d..a2416a8fb 100644 --- a/runtime/src/actor/client/name_change.rs +++ b/runtime/src/actor/client/name_change.rs @@ -138,7 +138,17 @@ impl ClientActor { }; let character_id = game.player.stats.id; match spawn_db(move || db::name_change::apply(character_id, SystemTime::now())).await { - Ok(Some(applied)) => log_applied(character_id, &applied), + Ok(Some(applied)) => { + log_applied(character_id, &applied); + if matches!(applied, Applied::Renamed { .. }) { + if let Err(error) = self.show_new_name_to_buddies().await { + warn!(self.client_id, %error, "Failed to show buddies a new name"); + } + if let Err(error) = self.show_new_name_to_guild().await { + warn!(self.client_id, %error, "Failed to show the guild a new name"); + } + } + } Ok(None) => {} Err(error) => warn!(self.client_id, %error, "Failed to apply a Name Change"), }