diff options
Diffstat (limited to 'server/protocol')
-rw-r--r-- | server/protocol/src/book.rs | 70 | ||||
-rw-r--r-- | server/protocol/src/helpers.rs | 12 | ||||
-rw-r--r-- | server/protocol/src/lib.rs | 84 |
3 files changed, 87 insertions, 79 deletions
diff --git a/server/protocol/src/book.rs b/server/protocol/src/book.rs new file mode 100644 index 00000000..5bcd0a22 --- /dev/null +++ b/server/protocol/src/book.rs @@ -0,0 +1,70 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + 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 crate::Message; +use bincode::{Decode, Encode}; +use glam::Vec2; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] +pub struct Book { + pub pages: Vec<BookPage>, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] +#[serde(rename_all = "snake_case", tag = "page_type")] +pub enum BookPage { + Cover, + Contents { + table: Vec<(Message, usize)>, + }, + Text { + paragraphs: Vec<Message>, + }, + Recipe { + description: Message, + instruction: Message, + diagram: Diagram, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode, Default)] +pub struct Diagram { + pub nodes: Vec<DiagramNode>, + pub edges: Vec<DiagramEdge>, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] +pub struct DiagramNode { + #[bincode(with_serde)] + pub position: Vec2, + pub label: Message, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] +pub struct DiagramEdge { + pub src: usize, + pub dst: usize, + pub label: Option<Message>, +} + +impl Default for Book { + fn default() -> Self { + Book { pages: vec![] } + } +} diff --git a/server/protocol/src/helpers.rs b/server/protocol/src/helpers.rs index b85c2f84..542f7754 100644 --- a/server/protocol/src/helpers.rs +++ b/server/protocol/src/helpers.rs @@ -1,10 +1,6 @@ +use crate::{Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, TileIndex}; use std::fmt::Display; -use crate::{ - DocumentElement, Gamedata, Hand, ItemIndex, ItemLocation, PlayerID, Recipe, RecipeIndex, - TileIndex, -}; - impl Gamedata { pub fn tile_name(&self, index: TileIndex) -> &String { &self.tile_names[index.0] @@ -109,9 +105,3 @@ impl Display for Hand { write!(f, "h{}", self.0) } } - -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 3b87c7b8..146a5a92 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -25,6 +25,9 @@ use std::{collections::HashSet, sync::LazyLock}; pub use glam; +use crate::book::Book; + +pub mod book; pub mod helpers; pub mod movement; pub mod registry; @@ -320,8 +323,9 @@ pub enum PacketC { #[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] #[serde(rename_all = "snake_case", tag = "menu", content = "data")] pub enum Menu { - Document(DocumentElement), Score(Score), + Scoreboard(Scoreboard), + Book(Book), AnnounceStart, } @@ -345,6 +349,17 @@ pub struct Score { pub instant_recipes: usize, } +#[derive(Debug, Serialize, Deserialize, Encode, Decode, Clone, Default)] +pub struct Scoreboard { + pub plays: usize, + pub best: Vec<ScoreboardEntry>, +} +#[derive(Debug, Serialize, Deserialize, Encode, Decode, Clone)] +pub struct ScoreboardEntry { + pub players: Vec<String>, + pub score: Score, +} + #[derive(Debug, Clone, Serialize, Encode, Decode, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Recipe { @@ -377,73 +392,6 @@ pub enum ItemLocation { Player(PlayerID, Hand), } -#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] -#[serde(rename_all = "snake_case", tag = "t")] -pub enum DocumentElement { - Document { - es: Vec<DocumentElement>, - }, - /// One page of the document, √2:1 aspect ratio - Page { - /// Name of background image - background: Option<String>, - es: Vec<DocumentElement>, - }, - /// Implicit element layouting - Container { - es: Vec<DocumentElement>, - }, - List { - /// Should only contain par or text elements - es: Vec<DocumentElement>, - }, - /// Table with elements arranged as row arrays - Table { - es: Vec<Vec<DocumentElement>>, - }, - /// A paragraph. - Par { - /// Should only contain text elements - es: Vec<DocumentElement>, - }, - /// A text span - Text { - s: Message, - size: f32, - color: Option<String>, - font: Option<String>, - #[serde(default)] - bold: bool, - }, - /// Document part that is only shown conditionally. Used for image attribution - Conditional { - cond: String, - value: bool, - e: Box<DocumentElement>, - }, - /// Makes the child element clickable that jumps to the label with the same id - Ref { - id: String, - e: Box<DocumentElement>, - }, - /// Declares a label - Label { - id: String, - e: Box<DocumentElement>, - }, - Align { - dir: DocumentAlign, - e: Box<DocumentElement>, - }, -} - -#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)] -#[serde(rename_all = "snake_case")] -pub enum DocumentAlign { - FlowEnd, - Bottom, -} - fn deser_i64<'de, D: Deserializer<'de>>(deserializer: D) -> Result<i64, D::Error> { let x = f64::deserialize(deserializer)?; Ok(x.trunc() as i64) |