From 70bb8744cd742dc6c8a71ab174e4a1b058c7bec8 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Mon, 8 Jul 2024 17:16:06 +0200 Subject: split off protocol into its own crate --- server/Cargo.toml | 2 + server/examples/client.rs | 2 +- server/protocol/Cargo.toml | 9 ++ server/protocol/src/lib.rs | 164 +++++++++++++++++++++++++++++++++++++ server/replaytool/Cargo.toml | 7 ++ server/replaytool/src/main.rs | 3 + server/src/bin/graph.rs | 2 +- server/src/customer/mod.rs | 7 +- server/src/customer/movement.rs | 2 +- server/src/customer/pathfinding.rs | 2 +- server/src/data.rs | 2 +- server/src/entity/conveyor.rs | 2 +- server/src/entity/mod.rs | 3 +- server/src/game.rs | 16 ++-- server/src/interaction.rs | 2 +- server/src/lib.rs | 1 - server/src/main.rs | 7 +- server/src/protocol.rs | 154 ---------------------------------- server/src/state.rs | 7 +- 19 files changed, 211 insertions(+), 183 deletions(-) create mode 100644 server/protocol/Cargo.toml create mode 100644 server/protocol/src/lib.rs create mode 100644 server/replaytool/Cargo.toml create mode 100644 server/replaytool/src/main.rs delete mode 100644 server/src/protocol.rs (limited to 'server') diff --git a/server/Cargo.toml b/server/Cargo.toml index 6b9037ac..e8cb0ed9 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -20,3 +20,5 @@ shlex = "1.3.0" clap = { version = "4.5.8", features = ["derive"] } fake = "2.9.2" pollster = "0.3.0" + +hurrycurry-protocol = { path = "protocol" } diff --git a/server/examples/client.rs b/server/examples/client.rs index 487c9738..80ba8ffa 100644 --- a/server/examples/client.rs +++ b/server/examples/client.rs @@ -22,7 +22,7 @@ use std::{ }; use glam::Vec2; -use hurrycurry_server::protocol::{PacketC, PacketS}; +use hurrycurry_protocol::{PacketC, PacketS}; fn main() { let mut sock = TcpStream::connect("127.0.0.1:27031").unwrap(); diff --git a/server/protocol/Cargo.toml b/server/protocol/Cargo.toml new file mode 100644 index 00000000..8fc63121 --- /dev/null +++ b/server/protocol/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hurrycurry-protocol" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.204", features = ["derive"] } +glam = { version = "0.28.0", features = ["serde"] } + diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs new file mode 100644 index 00000000..0342049c --- /dev/null +++ b/server/protocol/src/lib.rs @@ -0,0 +1,164 @@ +/* + 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 . + +*/ +use glam::{IVec2, Vec2}; +use serde::{Deserialize, Serialize}; +use std::collections::HashSet; + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] +pub struct PlayerID(pub i64); + +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] +pub struct ItemIndex(pub usize); +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] +pub struct TileIndex(pub usize); +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] +pub struct RecipeIndex(pub usize); +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[serde(transparent)] +pub struct DemandIndex(pub usize); + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +#[rustfmt::skip] +pub struct ClientGamedata { + pub item_names: Vec, + pub tile_names: Vec, + pub tile_collide: Vec, + pub tile_interact: Vec, + pub map_names: HashSet, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", tag = "type")] +pub enum PacketS { + Join { + name: String, + character: i32, + }, + Leave, + Position { + pos: Vec2, + rot: f32, + boosting: bool, + }, + Interact { + pos: IVec2, + edge: bool, + }, + Collide { + player: PlayerID, + force: Vec2, + }, + Communicate { + message: Option, + persist: bool, + }, + + #[serde(skip)] + /// For internal use only + ReplaceHand { + item: Option, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Message { + Text(String), + Item(ItemIndex), + Effect(String), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", tag = "type")] +pub enum PacketC { + Init { + id: PlayerID, + }, + Data { + data: ClientGamedata, + }, + AddPlayer { + id: PlayerID, + position: Vec2, + character: i32, + name: String, + }, + RemovePlayer { + id: PlayerID, + }, + Position { + player: PlayerID, + pos: Vec2, + rot: f32, + boosting: bool, + }, + MoveItem { + from: ItemLocation, + to: ItemLocation, + }, + SetItem { + location: ItemLocation, + item: Option, + }, + SetProgress { + item: ItemLocation, + progress: Option, + warn: bool, + }, + UpdateMap { + tile: IVec2, + kind: Option, + neighbors: [Option; 4], + }, + Collide { + player: PlayerID, + force: Vec2, + }, + Communicate { + player: PlayerID, + message: Option, + persist: bool, + }, + ServerMessage { + text: String, + }, + Score { + points: i64, + demands_failed: usize, + demands_completed: usize, + time_remaining: Option, + }, + SetIngame { + state: bool, + lobby: bool, + }, + Error { + message: String, + }, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Copy)] +#[serde(rename_all = "snake_case")] +pub enum ItemLocation { + Tile(IVec2), + Player(PlayerID), +} diff --git a/server/replaytool/Cargo.toml b/server/replaytool/Cargo.toml new file mode 100644 index 00000000..6983781d --- /dev/null +++ b/server/replaytool/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hurrycurry-replaytool" +version = "0.1.0" +edition = "2021" + +[dependencies] +hurrycurry-protocol = { path = "../protocol" } diff --git a/server/replaytool/src/main.rs b/server/replaytool/src/main.rs new file mode 100644 index 00000000..58dfaaa8 --- /dev/null +++ b/server/replaytool/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + +} diff --git a/server/src/bin/graph.rs b/server/src/bin/graph.rs index ace1b676..888119aa 100644 --- a/server/src/bin/graph.rs +++ b/server/src/bin/graph.rs @@ -16,10 +16,10 @@ */ use anyhow::{anyhow, Result}; +use hurrycurry_protocol::{ItemIndex, RecipeIndex}; use hurrycurry_server::{ data::{DataIndex, Demand}, interaction::Recipe, - protocol::{ItemIndex, RecipeIndex}, }; #[tokio::main] diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index 191f9e3b..c11a1aec 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -18,14 +18,11 @@ pub mod movement; mod pathfinding; -use crate::{ - data::Gamedata, - game::Tile, - protocol::{DemandIndex, Message, PacketS, PlayerID}, -}; +use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use fake::{faker, Fake}; use glam::IVec2; +use hurrycurry_protocol::{DemandIndex, Message, PacketS, PlayerID}; use log::debug; use movement::MovementBase; use pathfinding::{find_path, Path}; diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs index ca7afdbe..4da76de7 100644 --- a/server/src/customer/movement.rs +++ b/server/src/customer/movement.rs @@ -16,8 +16,8 @@ along with this program. If not, see . */ -use crate::protocol::PacketS; use glam::{IVec2, Vec2}; +use hurrycurry_protocol::PacketS; use std::collections::HashSet; const PLAYER_SIZE: f32 = 0.4; diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 743515b9..a2623e5c 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -16,8 +16,8 @@ */ use super::movement::MovementBase; -use crate::protocol::PacketS; use glam::{IVec2, Vec2}; +use hurrycurry_protocol::PacketS; use log::debug; use std::{ cmp::Ordering, diff --git a/server/src/data.rs b/server/src/data.rs index bca7f543..fd028c94 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -19,10 +19,10 @@ use crate::{ entity::{construct_entity, Entity, EntityDecl}, interaction::Recipe, - protocol::{DemandIndex, ItemIndex, RecipeIndex, TileIndex}, }; use anyhow::{anyhow, bail, Result}; use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{DemandIndex, ItemIndex, RecipeIndex, TileIndex}; use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index 0a73b55c..f4692f6d 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -19,10 +19,10 @@ use super::EntityT; use crate::{ data::Gamedata, game::{interact_effect, Tile}, - protocol::{ItemLocation, PacketC}, }; use anyhow::{anyhow, Result}; use glam::IVec2; +use hurrycurry_protocol::{ItemLocation, PacketC}; use std::collections::{HashMap, VecDeque}; #[derive(Debug, Default, Clone)] diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 5cb3ed0f..1bc558b1 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -16,10 +16,11 @@ */ pub mod conveyor; -use crate::{data::Gamedata, game::Tile, protocol::PacketC}; +use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use conveyor::Conveyor; use glam::IVec2; +use hurrycurry_protocol::PacketC; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; diff --git a/server/src/game.rs b/server/src/game.rs index 851d485c..daf08ad3 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -20,16 +20,16 @@ use crate::{ data::Gamedata, entity::{Entity, EntityT}, interaction::{interact, tick_slot, InteractEffect, TickEffect}, - protocol::{ - ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex, - }, }; use anyhow::{anyhow, bail, Result}; use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{ + ClientGamedata, ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, RecipeIndex, + TileIndex, +}; use log::{info, warn}; use std::{ collections::{HashMap, VecDeque}, - ops::Deref, sync::Arc, time::{Duration, Instant}, }; @@ -164,7 +164,13 @@ impl Game { pub fn prime_client(&self) -> Vec { let mut out = Vec::new(); out.push(PacketC::Data { - data: self.data.deref().to_owned(), + data: ClientGamedata { + item_names: self.data.item_names.clone(), + tile_names: self.data.tile_names.clone(), + tile_collide: self.data.tile_collide.clone(), + tile_interact: self.data.tile_interact.clone(), + map_names: self.data.map_names.clone(), + }, }); for (&id, player) in &self.players { out.push(PacketC::AddPlayer { diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 25b96813..b3f6af8c 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -18,8 +18,8 @@ use crate::{ data::Gamedata, game::{Involvement, Item}, - protocol::{ItemIndex, TileIndex}, }; +use hurrycurry_protocol::{ItemIndex, TileIndex}; use log::info; use serde::{Deserialize, Serialize}; diff --git a/server/src/lib.rs b/server/src/lib.rs index a6d29c03..6d0d4e26 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -21,5 +21,4 @@ pub mod data; pub mod entity; pub mod game; pub mod interaction; -pub mod protocol; pub mod state; diff --git a/server/src/main.rs b/server/src/main.rs index 99834fbd..4c6bcc34 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -18,6 +18,8 @@ use anyhow::{anyhow, Result}; use clap::Parser; use futures_util::{SinkExt, StreamExt}; +use hurrycurry_protocol::{PacketC, PacketS, PlayerID}; +use hurrycurry_server::{data::DATA_DIR, state::State}; use log::{debug, info, warn, LevelFilter}; use std::{path::PathBuf, process::exit, str::FromStr, sync::Arc, time::Duration}; use tokio::{ @@ -27,11 +29,6 @@ use tokio::{ time::interval, }; use tokio_tungstenite::tungstenite::Message; -use hurrycurry_server::{ - data::DATA_DIR, - protocol::{PacketC, PacketS, PlayerID}, - state::State, -}; #[derive(Parser)] struct Args { diff --git a/server/src/protocol.rs b/server/src/protocol.rs deleted file mode 100644 index fc8abda4..00000000 --- a/server/src/protocol.rs +++ /dev/null @@ -1,154 +0,0 @@ -/* - 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 . - -*/ -use crate::data::Gamedata; -use glam::{IVec2, Vec2}; -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] -pub struct PlayerID(pub i64); - -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] -pub struct ItemIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] -pub struct TileIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] -pub struct RecipeIndex(pub usize); -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[serde(transparent)] -pub struct DemandIndex(pub usize); - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "snake_case", tag = "type")] -pub enum PacketS { - Join { - name: String, - character: i32, - }, - Leave, - Position { - pos: Vec2, - rot: f32, - boosting: bool, - }, - Interact { - pos: IVec2, - edge: bool, - }, - Collide { - player: PlayerID, - force: Vec2, - }, - Communicate { - message: Option, - persist: bool, - }, - - #[serde(skip)] - /// For internal use only - ReplaceHand { - item: Option, - }, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum Message { - Text(String), - Item(ItemIndex), - Effect(String), -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "snake_case", tag = "type")] -pub enum PacketC { - Init { - id: PlayerID, - }, - Data { - data: Gamedata, - }, - AddPlayer { - id: PlayerID, - position: Vec2, - character: i32, - name: String, - }, - RemovePlayer { - id: PlayerID, - }, - Position { - player: PlayerID, - pos: Vec2, - rot: f32, - boosting: bool, - }, - MoveItem { - from: ItemLocation, - to: ItemLocation, - }, - SetItem { - location: ItemLocation, - item: Option, - }, - SetProgress { - item: ItemLocation, - progress: Option, - warn: bool, - }, - UpdateMap { - tile: IVec2, - kind: Option, - neighbors: [Option; 4], - }, - Collide { - player: PlayerID, - force: Vec2, - }, - Communicate { - player: PlayerID, - message: Option, - persist: bool, - }, - ServerMessage { - text: String, - }, - Score { - points: i64, - demands_failed: usize, - demands_completed: usize, - time_remaining: Option, - }, - SetIngame { - state: bool, - lobby: bool, - }, - Error { - message: String, - }, -} - -#[derive(Debug, Clone, Serialize, Deserialize, Copy)] -#[serde(rename_all = "snake_case")] -pub enum ItemLocation { - Tile(IVec2), - Player(PlayerID), -} diff --git a/server/src/state.rs b/server/src/state.rs index 72d3e911..5af6f181 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -17,13 +17,10 @@ */ use std::time::Duration; -use crate::{ - data::DataIndex, - game::Game, - protocol::{Message, PacketC, PacketS, PlayerID}, -}; +use crate::{data::DataIndex, game::Game}; use anyhow::{anyhow, bail, Result}; use clap::{Parser, ValueEnum}; +use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID}; use log::debug; use tokio::sync::broadcast::Sender; -- cgit v1.2.3-70-g09d2