review fixes

This commit is contained in:
Maxicarlos08 2024-01-15 17:52:35 +01:00
parent 56cc33ec55
commit d814296676
No known key found for this signature in database
5 changed files with 32 additions and 20 deletions

View File

@ -790,7 +790,7 @@ fn handle_motd(
server server
.editable_settings() .editable_settings()
.server_description .server_description
.get(locale.as_ref()) .get(locale.as_deref())
.map_or("", |d| &d.motd) .map_or("", |d| &d.motd)
.to_string(), .to_string(),
), ),

View File

@ -32,6 +32,8 @@ use std::{
use tracing::{error, warn}; use tracing::{error, warn};
use world::sim::FileOpts; use world::sim::FileOpts;
use self::server_description::ServerDescription;
const DEFAULT_WORLD_SEED: u32 = 230; const DEFAULT_WORLD_SEED: u32 = 230;
const CONFIG_DIR: &str = "server_config"; const CONFIG_DIR: &str = "server_config";
const SETTINGS_FILENAME: &str = "settings.ron"; const SETTINGS_FILENAME: &str = "settings.ron";
@ -382,9 +384,9 @@ impl EditableSettings {
let mut server_description = ServerDescriptions::default(); let mut server_description = ServerDescriptions::default();
server_description server_description
.descriptions .descriptions
.values_mut() .insert("en".to_string(), ServerDescription {
.for_each(|entry| { motd: "Who needs friends anyway?".to_string(),
entry.motd = "Who needs friends anyway?".into(); rules: None,
}); });
let mut admins = Admins::default(); let mut admins = Admins::default();

View File

@ -219,16 +219,32 @@ mod v2 {
} }
impl ServerDescriptions { impl ServerDescriptions {
pub fn get(&self, locale: Option<&String>) -> Option<&ServerDescription> { fn unwrap_locale_or_default<'a, 'b: 'a>(&'b self, locale: Option<&'a str>) -> &'a str {
let locale = locale.map_or(&self.default_locale, |locale| { locale.map_or(&self.default_locale, |locale| {
if self.descriptions.contains_key(locale) { if self.descriptions.contains_key(locale) {
locale locale
} else { } else {
&self.default_locale &self.default_locale
} }
}); })
}
self.descriptions.get(locale) pub fn get(&self, locale: Option<&str>) -> Option<&ServerDescription> {
self.descriptions.get(self.unwrap_locale_or_default(locale))
}
/// Attempts to get the rules in the specified locale, falls back to
/// `default_locale` if no rules were specified in this locale
pub fn get_rules(&self, locale: Option<&str>) -> Option<&str> {
self.descriptions
.get(self.unwrap_locale_or_default(locale))
.and_then(|d| d.rules.as_deref())
.or_else(|| {
self.descriptions
.get(&self.default_locale)?
.rules
.as_deref()
})
} }
} }

View File

@ -47,7 +47,7 @@ impl Sys {
// Give the player a welcome message // Give the player a welcome message
let localized_description = editable_settings let localized_description = editable_settings
.server_description .server_description
.get(client.locale.as_ref()); .get(client.locale.as_deref());
if !localized_description.map_or(true, |d| d.motd.is_empty()) { if !localized_description.map_or(true, |d| d.motd.is_empty()) {
client.send(ServerGeneral::server_msg( client.send(ServerGeneral::server_msg(
ChatType::CommandInfo, ChatType::CommandInfo,

View File

@ -329,17 +329,11 @@ impl<'a> System<'a> for Sys {
client.send(Ok(()))?; client.send(Ok(()))?;
let description = read_data let server_descriptions = &read_data.editable_settings.server_description;
.editable_settings let description = ServerDescription {
.server_description motd: server_descriptions.get(client.locale.as_deref()).map(|d| d.motd.clone()).unwrap_or_default(),
.get(client.locale.as_ref()) rules: server_descriptions.get_rules(client.locale.as_deref()).map(str::to_string),
.map(|description| };
ServerDescription {
motd: description.motd.clone(),
rules: description.rules.clone()
}
)
.unwrap_or_default();
// Send client all the tracked components currently attached to its entity // Send client all the tracked components currently attached to its entity
// as well as synced resources (currently only `TimeOfDay`) // as well as synced resources (currently only `TimeOfDay`)