Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions agent_docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions db/src/buddy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ pub fn mutual_ids(cid: i32) -> crate::Result<Vec<i32>> {
.collect())
}

/// Characters whose list holds `cid` at all, an unanswered request too.
pub fn listed_in(cid: i32) -> crate::Result<Vec<i32>> {
let mut conn = crate::connection()?;
Ok(buddies::table
.filter(buddies::buddy_id.eq(cid))
.select(buddies::character_id)
.load::<i32>(&mut conn)?)
}

/// Characters whose (accepted) list holds `cid`.
fn listed_by(conn: &mut PgConnection, cid: i32) -> QueryResult<HashSet<i32>> {
Ok(buddies::table
Expand Down
38 changes: 38 additions & 0 deletions integration-harness/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Integer, _>(owner)
.bind::<Integer, _>(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::<Integer, _>(character_id)
.bind::<Text, _>(old)
.bind::<Text, _>(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> {
Expand Down
62 changes: 61 additions & 1 deletion integration-harness/tests/cash_shop_name_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
47 changes: 36 additions & 11 deletions runtime/src/actor/client/buddies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,37 @@ impl ClientActor {
}

async fn send_buddies(&mut self, rows: Vec<Buddy>, 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::<db::Result<Vec<_>>>()
})
.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<ClientId>,
Expand All @@ -166,3 +179,15 @@ impl ClientActor {
.await
}
}

fn listed(rows: Vec<Buddy>) -> Vec<ListedBuddy> {
rows.into_iter()
.map(|buddy| ListedBuddy {
id: buddy.id,
name: buddy.name,
group: buddy.group,
mutual: buddy.mutual,
pending: buddy.pending,
})
.collect()
}
20 changes: 20 additions & 0 deletions runtime/src/actor/client/guilds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion runtime/src/actor/client/name_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down
Loading