diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-24 12:43:42 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-24 12:43:42 +0200 |
commit | 9453f5f4b3e68d79fb09494b55bc885432ac0351 (patch) | |
tree | 66eea4b897cda02db1b72bbe305afcc6e4e8a4d8 | |
parent | f68d3a579f05e5bbe490cbaa9cc4d1e1bff208ac (diff) | |
download | hurrycurry-9453f5f4b3e68d79fb09494b55bc885432ac0351.tar hurrycurry-9453f5f4b3e68d79fb09494b55bc885432ac0351.tar.bz2 hurrycurry-9453f5f4b3e68d79fb09494b55bc885432ac0351.tar.zst |
format server in english locale
-rw-r--r-- | server/src/entity/book.rs | 2 | ||||
-rw-r--r-- | server/src/entity/campaign.rs | 2 | ||||
-rw-r--r-- | server/src/entity/mod.rs | 2 | ||||
-rw-r--r-- | server/src/entity/tutorial.rs | 2 | ||||
-rw-r--r-- | server/src/lib.rs | 69 | ||||
-rw-r--r-- | server/src/message.rs | 122 | ||||
-rw-r--r-- | server/src/server.rs | 3 | ||||
-rw-r--r-- | server/src/state.rs | 2 |
8 files changed, 131 insertions, 73 deletions
diff --git a/server/src/entity/book.rs b/server/src/entity/book.rs index 11ec847b..5833a871 100644 --- a/server/src/entity/book.rs +++ b/server/src/entity/book.rs @@ -16,7 +16,7 @@ */ use super::{Entity, EntityContext}; -use crate::TrError; +use crate::message::TrError; use anyhow::Result; use hurrycurry_protocol::{glam::IVec2, Menu, PacketC, PlayerID}; diff --git a/server/src/entity/campaign.rs b/server/src/entity/campaign.rs index 8edebd41..5838d3ce 100644 --- a/server/src/entity/campaign.rs +++ b/server/src/entity/campaign.rs @@ -16,7 +16,7 @@ */ use super::{Entity, EntityContext}; -use crate::{scoreboard::ScoreboardStore, server::GameServerExt, trm, TrError}; +use crate::{message::TrError, scoreboard::ScoreboardStore, server::GameServerExt, trm}; use anyhow::Result; use hurrycurry_protocol::{ glam::{IVec2, Vec2}, diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index aa565c83..3dad1311 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -25,7 +25,7 @@ pub mod item_portal; pub mod player_portal; pub mod tutorial; -use crate::{data::ItemTileRegistry, scoreboard::ScoreboardStore, TrError}; +use crate::{data::ItemTileRegistry, scoreboard::ScoreboardStore, message::TrError}; use anyhow::{anyhow, Result}; use book::Book; use campaign::{Gate, GateCondition, Map}; diff --git a/server/src/entity/tutorial.rs b/server/src/entity/tutorial.rs index b1503004..3189687b 100644 --- a/server/src/entity/tutorial.rs +++ b/server/src/entity/tutorial.rs @@ -16,7 +16,7 @@ */ use super::{Entity, EntityContext}; -use crate::{trm, TrError}; +use crate::{message::TrError, trm}; use anyhow::Result; use hurrycurry_protocol::{ glam::IVec2, ItemIndex, Message, PacketC, PlayerID, Recipe, RecipeIndex, TileIndex, diff --git a/server/src/lib.rs b/server/src/lib.rs index 69a9849e..5c7665c0 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -28,12 +28,13 @@ pub mod commands; pub mod data; pub mod entity; pub mod interaction; +pub mod message; pub mod network; pub mod scoreboard; pub mod server; pub mod state; -use hurrycurry_protocol::{glam::Vec2, Message}; +use hurrycurry_protocol::glam::Vec2; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct ConnectionID(pub i64); @@ -52,69 +53,3 @@ impl InterpolateExt for f32 { *self = target + (*self - target) * (-dt).exp(); } } - -#[macro_export] -macro_rules! trm { - ($id:literal $(, $typ:ident = $param:expr)*) => { - hurrycurry_protocol::Message::Translation { - id: $id.to_owned(), - params: vec![$(crate::trm_param!($typ, $param)),*] - } - }; -} - -#[macro_export] -macro_rules! trm_param { - (s, $x:expr) => { - hurrycurry_protocol::Message::Text($x) - }; - (i, $x:expr) => { - hurrycurry_protocol::Message::Item($x) - }; - (t, $x:expr) => { - hurrycurry_protocol::Message::Tile($x) - }; - (m, $x:expr) => { - $x - }; -} - -#[derive(Debug)] -pub struct TrError { - pub id: &'static str, - pub params: Vec<Message>, -} -impl From<TrError> for Message { - fn from(value: TrError) -> Self { - Self::Translation { - id: value.id.to_owned(), - params: value.params, - } - } -} - -#[macro_export] -macro_rules! tre { - ($id:literal $(, $typ:ident = $param:expr)*) => { - crate::TrError { - id: $id, - params: vec![$(crate::tre_param!($typ, $param)),*] - } - }; -} - -#[macro_export] -macro_rules! tre_param { - (s, $x:expr) => { - hurrycurry_protocol::Message::Text($x) - }; - (i, $x:expr) => { - hurrycurry_protocol::Message::Item($x) - }; - (t, $x:expr) => { - hurrycurry_protocol::Message::Tile($x) - }; - (m, $x:expr) => { - $x - }; -} diff --git a/server/src/message.rs b/server/src/message.rs new file mode 100644 index 00000000..c248fff8 --- /dev/null +++ b/server/src/message.rs @@ -0,0 +1,122 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ + +use anyhow::{anyhow, Result}; +use hurrycurry_protocol::Message; +use std::{collections::HashMap, fmt::Debug, ops::Index, sync::LazyLock}; + +#[macro_export] +macro_rules! trm { + ($id:literal $(, $typ:ident = $param:expr)*) => { + hurrycurry_protocol::Message::Translation { + id: $id.to_owned(), + params: vec![$(crate::trm_param!($typ, $param)),*] + } + }; +} + +#[macro_export] +macro_rules! trm_param { + (s, $x:expr) => { + hurrycurry_protocol::Message::Text($x) + }; + (i, $x:expr) => { + hurrycurry_protocol::Message::Item($x) + }; + (t, $x:expr) => { + hurrycurry_protocol::Message::Tile($x) + }; + (m, $x:expr) => { + $x + }; +} + +pub struct TrError { + pub id: &'static str, + pub params: Vec<Message>, +} +impl From<TrError> for Message { + fn from(value: TrError) -> Self { + Self::Translation { + id: value.id.to_owned(), + params: value.params, + } + } +} +impl Debug for TrError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {:?}", tr(self.id), self.params) + } +} + +#[macro_export] +macro_rules! tre { + ($id:literal $(, $typ:ident = $param:expr)*) => { + crate::message::TrError { + id: $id, + params: vec![$(crate::tre_param!($typ, $param)),*] + } + }; +} + +#[macro_export] +macro_rules! tre_param { + (s, $x:expr) => { + hurrycurry_protocol::Message::Text($x) + }; + (i, $x:expr) => { + hurrycurry_protocol::Message::Item($x) + }; + (t, $x:expr) => { + hurrycurry_protocol::Message::Tile($x) + }; + (m, $x:expr) => { + $x + }; +} + +pub struct Strings(HashMap<String, String>); +impl Index<&'static str> for Strings { + type Output = str; + fn index(&self, index: &'static str) -> &Self::Output { + self.0.get(index).map(|s| s.as_str()).unwrap_or(index) + } +} + +impl Strings { + pub fn load() -> Result<Self> { + Ok(Self( + include_str!("../../locale/en.ini") + .lines() + .skip(1) + .map(|l| { + let (k, v) = l.split_once("=").ok_or(anyhow!("'=' missing"))?; + Ok::<_, anyhow::Error>(( + k.trim_end().to_owned(), + v.trim_start().replace("%n", "\n"), + )) + }) + .try_collect()?, + )) + } +} + +static TR: LazyLock<Strings> = LazyLock::new(|| Strings::load().unwrap()); +pub fn tr<'a>(s: &'static str) -> &'a str { + &TR[s] +} diff --git a/server/src/server.rs b/server/src/server.rs index 0c3df634..9ee98b97 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -19,8 +19,9 @@ use crate::{ data::{index::GamedataIndex, DataIndex, Serverdata}, entity::{Entities, EntityContext}, interaction::{interact, tick_slot}, + message::TrError, scoreboard::ScoreboardStore, - tre, ConnectionID, TrError, + tre, ConnectionID, }; use anyhow::{Context, Result}; use hurrycurry_client_lib::{Game, Involvement, Item, Player, Tile}; diff --git a/server/src/state.rs b/server/src/state.rs index 65e6dfd2..068b45a8 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use crate::{server::Server, tre, trm, ConnectionID, TrError}; +use crate::{message::TrError, server::Server, tre, trm, ConnectionID}; use anyhow::Result; use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID}; use log::{debug, trace}; |