diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-08 17:16:06 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-08 17:16:06 +0200 |
commit | 70bb8744cd742dc6c8a71ab174e4a1b058c7bec8 (patch) | |
tree | 78c5a190856f1c6281e8773a05408ede02a13947 /server/protocol/src/lib.rs | |
parent | a1921104e13c9cf39439ee424d54f7cece67fc5b (diff) | |
download | hurrycurry-70bb8744cd742dc6c8a71ab174e4a1b058c7bec8.tar hurrycurry-70bb8744cd742dc6c8a71ab174e4a1b058c7bec8.tar.bz2 hurrycurry-70bb8744cd742dc6c8a71ab174e4a1b058c7bec8.tar.zst |
split off protocol into its own crate
Diffstat (limited to 'server/protocol/src/lib.rs')
-rw-r--r-- | server/protocol/src/lib.rs | 164 |
1 files changed, 164 insertions, 0 deletions
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 <https://www.gnu.org/licenses/>. + +*/ +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<String>, + pub tile_names: Vec<String>, + pub tile_collide: Vec<bool>, + pub tile_interact: Vec<bool>, + pub map_names: HashSet<String>, +} + +#[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<Message>, + persist: bool, + }, + + #[serde(skip)] + /// For internal use only + ReplaceHand { + item: Option<ItemIndex>, + }, +} + +#[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<ItemIndex>, + }, + SetProgress { + item: ItemLocation, + progress: Option<f32>, + warn: bool, + }, + UpdateMap { + tile: IVec2, + kind: Option<TileIndex>, + neighbors: [Option<TileIndex>; 4], + }, + Collide { + player: PlayerID, + force: Vec2, + }, + Communicate { + player: PlayerID, + message: Option<Message>, + persist: bool, + }, + ServerMessage { + text: String, + }, + Score { + points: i64, + demands_failed: usize, + demands_completed: usize, + time_remaining: Option<f32>, + }, + 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), +} |