aboutsummaryrefslogtreecommitdiff
path: root/server/protocol
diff options
context:
space:
mode:
Diffstat (limited to 'server/protocol')
-rw-r--r--server/protocol/src/helpers.rs110
-rw-r--r--server/protocol/src/lib.rs129
2 files changed, 140 insertions, 99 deletions
diff --git a/server/protocol/src/helpers.rs b/server/protocol/src/helpers.rs
new file mode 100644
index 00000000..924d0886
--- /dev/null
+++ b/server/protocol/src/helpers.rs
@@ -0,0 +1,110 @@
+use std::fmt::Display;
+
+use crate::{
+ DocumentElement, Gamedata, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex,
+};
+
+impl Gamedata {
+ pub fn tile_name(&self, index: TileIndex) -> &String {
+ &self.tile_names[index.0]
+ }
+ pub fn is_tile_colliding(&self, index: TileIndex) -> bool {
+ self.tile_collide[index.0]
+ }
+ pub fn is_tile_interactable(&self, index: TileIndex) -> bool {
+ self.tile_interact[index.0]
+ }
+ pub fn item_name(&self, index: ItemIndex) -> &String {
+ &self.item_names[index.0]
+ }
+ pub fn recipe(&self, index: RecipeIndex) -> &Recipe {
+ &self.recipes[index.0]
+ }
+ pub fn get_tile_by_name(&self, name: &str) -> Option<TileIndex> {
+ self.tile_names
+ .iter()
+ .position(|t| t == name)
+ .map(TileIndex)
+ }
+ pub fn get_item_by_name(&self, name: &str) -> Option<ItemIndex> {
+ self.item_names
+ .iter()
+ .position(|t| t == name)
+ .map(ItemIndex)
+ }
+ pub fn recipes(&self) -> impl Iterator<Item = (RecipeIndex, &Recipe)> {
+ self.recipes
+ .iter()
+ .enumerate()
+ .map(|(i, e)| (RecipeIndex(i), e))
+ }
+}
+
+impl Recipe {
+ pub fn tile(&self) -> Option<TileIndex> {
+ match self {
+ Recipe::Passive { tile, .. } => *tile,
+ Recipe::Active { tile, .. } => *tile,
+ Recipe::Instant { tile, .. } => *tile,
+ }
+ }
+ pub fn speed(&self) -> Option<f32> {
+ match self {
+ Recipe::Passive { speed, .. } => Some(*speed),
+ Recipe::Active { speed, .. } => Some(*speed),
+ _ => None,
+ }
+ }
+ pub fn revert_speed(&self) -> Option<f32> {
+ match self {
+ Recipe::Passive { revert_speed, .. } => *revert_speed,
+ _ => None,
+ }
+ }
+ pub fn warn(&self) -> bool {
+ match self {
+ Recipe::Passive { warn, .. } => *warn,
+ _ => false,
+ }
+ }
+ pub fn inputs(&self) -> Vec<ItemIndex> {
+ match self {
+ Recipe::Passive { input, .. } => vec![*input],
+ Recipe::Active { input, .. } => vec![*input],
+ Recipe::Instant { inputs, .. } => inputs.iter().flat_map(|e| e.to_owned()).collect(),
+ }
+ }
+ pub fn outputs(&self) -> Vec<ItemIndex> {
+ match self {
+ Recipe::Passive { output, .. } => output.iter().copied().collect(),
+ Recipe::Active { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(),
+ Recipe::Instant { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(),
+ }
+ }
+ pub fn supports_tile(&self, tile: Option<TileIndex>) -> bool {
+ if let Some(tile_constraint) = self.tile() {
+ if let Some(tile) = tile {
+ tile == tile_constraint
+ } else {
+ false
+ }
+ } else {
+ true
+ }
+ }
+}
+
+impl Display for ItemLocation {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ItemLocation::Tile(pos) => write!(f, "tile({pos})"),
+ ItemLocation::Player(PlayerID(id)) => write!(f, "player({id})"),
+ }
+ }
+}
+
+impl Default for DocumentElement {
+ fn default() -> Self {
+ Self::Document { es: vec![] }
+ }
+}
diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs
index 1acf7997..3337db0a 100644
--- a/server/protocol/src/lib.rs
+++ b/server/protocol/src/lib.rs
@@ -21,10 +21,11 @@ use bincode::{
};
use glam::{IVec2, Vec2};
use serde::{Deserialize, Serialize};
-use std::{collections::HashSet, fmt::Display, sync::LazyLock};
+use std::{collections::HashSet, sync::LazyLock};
pub use glam;
+pub mod helpers;
pub mod movement;
pub mod registry;
@@ -290,7 +291,7 @@ pub enum PacketC {
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
#[serde(rename_all = "snake_case", tag = "menu", content = "data")]
pub enum Menu {
- Book,
+ Document(DocumentElement),
Score(Score),
}
@@ -339,96 +340,6 @@ pub enum Recipe {
},
}
-impl Gamedata {
- pub fn tile_name(&self, index: TileIndex) -> &String {
- &self.tile_names[index.0]
- }
- pub fn is_tile_colliding(&self, index: TileIndex) -> bool {
- self.tile_collide[index.0]
- }
- pub fn is_tile_interactable(&self, index: TileIndex) -> bool {
- self.tile_interact[index.0]
- }
- pub fn item_name(&self, index: ItemIndex) -> &String {
- &self.item_names[index.0]
- }
- pub fn recipe(&self, index: RecipeIndex) -> &Recipe {
- &self.recipes[index.0]
- }
- pub fn get_tile_by_name(&self, name: &str) -> Option<TileIndex> {
- self.tile_names
- .iter()
- .position(|t| t == name)
- .map(TileIndex)
- }
- pub fn get_item_by_name(&self, name: &str) -> Option<ItemIndex> {
- self.item_names
- .iter()
- .position(|t| t == name)
- .map(ItemIndex)
- }
- pub fn recipes(&self) -> impl Iterator<Item = (RecipeIndex, &Recipe)> {
- self.recipes
- .iter()
- .enumerate()
- .map(|(i, e)| (RecipeIndex(i), e))
- }
-}
-
-impl Recipe {
- pub fn tile(&self) -> Option<TileIndex> {
- match self {
- Recipe::Passive { tile, .. } => *tile,
- Recipe::Active { tile, .. } => *tile,
- Recipe::Instant { tile, .. } => *tile,
- }
- }
- pub fn speed(&self) -> Option<f32> {
- match self {
- Recipe::Passive { speed, .. } => Some(*speed),
- Recipe::Active { speed, .. } => Some(*speed),
- _ => None,
- }
- }
- pub fn revert_speed(&self) -> Option<f32> {
- match self {
- Recipe::Passive { revert_speed, .. } => *revert_speed,
- _ => None,
- }
- }
- pub fn warn(&self) -> bool {
- match self {
- Recipe::Passive { warn, .. } => *warn,
- _ => false,
- }
- }
- pub fn inputs(&self) -> Vec<ItemIndex> {
- match self {
- Recipe::Passive { input, .. } => vec![*input],
- Recipe::Active { input, .. } => vec![*input],
- Recipe::Instant { inputs, .. } => inputs.iter().flat_map(|e| e.to_owned()).collect(),
- }
- }
- pub fn outputs(&self) -> Vec<ItemIndex> {
- match self {
- Recipe::Passive { output, .. } => output.iter().copied().collect(),
- Recipe::Active { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(),
- Recipe::Instant { outputs, .. } => outputs.iter().flat_map(|e| e.to_owned()).collect(),
- }
- }
- pub fn supports_tile(&self, tile: Option<TileIndex>) -> bool {
- if let Some(tile_constraint) = self.tile() {
- if let Some(tile) = tile {
- tile == tile_constraint
- } else {
- false
- }
- } else {
- true
- }
- }
-}
-
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, Copy, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum ItemLocation {
@@ -436,11 +347,31 @@ pub enum ItemLocation {
Player(PlayerID),
}
-impl Display for ItemLocation {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- ItemLocation::Tile(pos) => write!(f, "tile({pos})"),
- ItemLocation::Player(PlayerID(id)) => write!(f, "player({id})"),
- }
- }
+#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
+#[serde(rename_all = "snake_case", tag = "t")]
+pub enum DocumentElement {
+ Document {
+ es: Vec<DocumentElement>,
+ },
+ Page {
+ background: Option<String>,
+ es: Vec<DocumentElement>,
+ },
+ List {
+ es: Vec<DocumentElement>,
+ },
+ Table {
+ es: Vec<Vec<DocumentElement>>,
+ },
+ Par {
+ es: Vec<DocumentElement>,
+ },
+ Text {
+ s: Message,
+ color: String,
+ size: f32,
+ font: String,
+ #[serde(default)]
+ bold: bool,
+ },
}