Store the gender in CharacterInfo

This commit is contained in:
juliancoffee 2024-02-03 18:32:49 +02:00
parent aecdadca14
commit 5aa88ac223
8 changed files with 33 additions and 29 deletions

View File

@ -2250,6 +2250,7 @@ impl Client {
player_info.character = match &player_info.character { player_info.character = match &player_info.character {
Some(character) => Some(msg::CharacterInfo { Some(character) => Some(msg::CharacterInfo {
name: character.name.to_string(), name: character.name.to_string(),
gender: character.gender,
}), }),
None => { None => {
warn!( warn!(
@ -2887,9 +2888,8 @@ impl Client {
pub fn lookup_msg_context(&self, msg: &comp::ChatMsg) -> ChatTypeContext { pub fn lookup_msg_context(&self, msg: &comp::ChatMsg) -> ChatTypeContext {
let mut result = ChatTypeContext { let mut result = ChatTypeContext {
you: self.uid().expect("Client doesn't have a Uid!!!"), you: self.uid().expect("Client doesn't have a Uid!!!"),
player_alias: HashMap::new(), player_info: HashMap::new(),
entity_name: HashMap::new(), entity_name: HashMap::new(),
gender: HashMap::new(),
}; };
let name_of_uid = |uid| { let name_of_uid = |uid| {
@ -2903,24 +2903,10 @@ impl Client {
.map(|(c, _)| c.name.clone()) .map(|(c, _)| c.name.clone())
}; };
let gender_of_uid = |uid| {
let ecs = self.state.ecs();
(
&ecs.read_storage::<comp::Stats>(),
&ecs.read_storage::<Uid>(),
)
.join()
.find(|(_, u)| u == &uid)
.and_then(|(c, _)| c.original_body.humanoid_gender())
};
let mut add_data_of = |uid| { let mut add_data_of = |uid| {
match self.player_list.get(uid) { match self.player_list.get(uid) {
Some(player_info) => { Some(player_info) => {
result.player_alias.insert(*uid, player_info.clone()); result.player_info.insert(*uid, player_info.clone());
result
.gender
.insert(*uid, gender_of_uid(uid).unwrap_or(comp::Gender::Masculine));
}, },
None => { None => {
result result

View File

@ -257,14 +257,14 @@ pub struct PlayerInfo {
/// used for localisation, filled by client and used by i18n code /// used for localisation, filled by client and used by i18n code
pub struct ChatTypeContext { pub struct ChatTypeContext {
pub you: Uid, pub you: Uid,
pub player_alias: HashMap<Uid, PlayerInfo>, pub player_info: HashMap<Uid, PlayerInfo>,
pub entity_name: HashMap<Uid, String>, pub entity_name: HashMap<Uid, String>,
pub gender: HashMap<Uid, Gender>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CharacterInfo { pub struct CharacterInfo {
pub name: String, pub name: String,
pub gender: Option<Gender>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -203,9 +203,10 @@ impl<
/// Should be used for localization with extreme care. /// Should be used for localization with extreme care.
/// For basically everything except *maybe* humanoids, it's simply wrong to /// For basically everything except *maybe* humanoids, it's simply wrong to
/// assume that this may be used as grammatical gender. /// assume that this may be used as grammatical gender.
// ///
// TODO: remove this and instead add GUI for players to choose preferred gender. /// TODO: remove this and instead add GUI for players to choose preferred
// Read a comment for `gender_str` in voxygen/i18n-helpers/src/lib.rs. /// gender. Read a comment for `gender_str` in voxygen/i18n-helpers/src/lib.rs.
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
pub enum Gender { pub enum Gender {
Masculine, Masculine,
Feminine, Feminine,

View File

@ -522,6 +522,8 @@ pub fn handle_possess(server: &mut Server, possessor_uid: Uid, possessee_uid: Ui
character: ecs.read_storage::<comp::Stats>().get(possessee).map(|s| { character: ecs.read_storage::<comp::Stats>().get(possessee).map(|s| {
msg::CharacterInfo { msg::CharacterInfo {
name: s.name.clone(), name: s.name.clone(),
// NOTE: hack, read docs for humanoid_gender() for more
gender: s.original_body.humanoid_gender(),
} }
}), }),
uuid: player.uuid(), uuid: player.uuid(),

View File

@ -724,6 +724,8 @@ impl StateExt for State {
self.notify_players(ServerGeneral::PlayerListUpdate( self.notify_players(ServerGeneral::PlayerListUpdate(
PlayerListUpdate::SelectedCharacter(player_uid, CharacterInfo { PlayerListUpdate::SelectedCharacter(player_uid, CharacterInfo {
name: String::from(&stats.name), name: String::from(&stats.name),
// NOTE: hack, read docs for humanoid_gender() for more
gender: stats.original_body.humanoid_gender(),
}), }),
)); ));

View File

@ -99,6 +99,8 @@ impl<'a> System<'a> for Sys {
player_alias: player.alias.clone(), player_alias: player.alias.clone(),
character: stats.map(|stats| CharacterInfo { character: stats.map(|stats| CharacterInfo {
name: stats.name.clone(), name: stats.name.clone(),
// NOTE: hack, read docs for humanoid_gender()
gender: stats.original_body.humanoid_gender(),
}), }),
uuid: player.uuid(), uuid: player.uuid(),
}), }),

View File

@ -20,7 +20,7 @@ pub fn localize_chat_message(
) -> (ChatType<String>, String) { ) -> (ChatType<String>, String) {
let info = lookup_fn(&msg); let info = lookup_fn(&msg);
let name_format_or_complex = |complex, uid: &Uid| match info.player_alias.get(uid).cloned() { let name_format_or_complex = |complex, uid: &Uid| match info.player_info.get(uid).cloned() {
Some(pi) => { Some(pi) => {
if complex { if complex {
insert_alias(info.you == *uid, pi, localization) insert_alias(info.you == *uid, pi, localization)
@ -75,10 +75,21 @@ pub fn localize_chat_message(
// If the language can represent Female, Male and Neuter, we can pass these. // If the language can represent Female, Male and Neuter, we can pass these.
// //
// Exact design of such a complex system is honestly up to discussion. // Exact design of such a complex system is honestly up to discussion.
let gender_str = |uid: &Uid| match info.gender.get(uid) { let gender_str = |uid: &Uid| match info.player_info.get(uid) {
Some(pi) => match pi.character.as_ref().and_then(|c| c.gender) {
Some(Gender::Feminine) => "she".to_owned(), Some(Gender::Feminine) => "she".to_owned(),
Some(Gender::Masculine) => "he".to_owned(), Some(Gender::Masculine) => "he".to_owned(),
None => "??".to_owned(), None => {
tracing::error!("We tried to get the gender, but failed");
"??".to_owned()
}
},
None => {
tracing::error!("We tried to get the gender of the player we can't find");
"??".to_owned()
},
}; };
// This is where the most fun begings. // This is where the most fun begings.
@ -128,7 +139,7 @@ pub fn localize_chat_message(
let message_format = |from: &Uid, content: &Content, group: Option<&String>| { let message_format = |from: &Uid, content: &Content, group: Option<&String>| {
let alias = name_format_or_complex(true, from); let alias = name_format_or_complex(true, from);
let name = if let Some(pi) = info.player_alias.get(from).cloned() && show_char_name { let name = if let Some(pi) = info.player_info.get(from).cloned() && show_char_name {
pi.character.map(|c| c.name) pi.character.map(|c| c.name)
} else { } else {
None None

View File

@ -481,7 +481,7 @@ impl<'a> Widget for Chat<'a> {
.and_then(|uid| { .and_then(|uid| {
self.client self.client
.lookup_msg_context(m) .lookup_msg_context(m)
.player_alias .player_info
.get(&uid) .get(&uid)
.map(|i| i.is_moderator) .map(|i| i.is_moderator)
}) })