mod.rs (4151B)
1 use std::{ 2 collections::HashMap, 3 time::{Duration, SystemTime}, 4 }; 5 6 use macroquad::math::{Rect, Vec2}; 7 use serde::{Deserialize, Serialize}; 8 use structdiff::{Difference, StructDiff}; 9 use uuid::Uuid; 10 11 use crate::game::{ 12 book::{BookId, SlotAddress}, 13 state::GameState, 14 }; 15 16 pub mod book; 17 pub mod state; 18 19 pub const TICK_DURATION: Duration = Duration::from_micros(16_667); 20 21 pub type Players = HashMap<Uuid, Player>; 22 23 /// Equivalent to [macroquad::math::Rect::overlaps], except using strict comparison (i.e., `<` and 24 /// `>` instead of `<=` and `>=`) 25 fn rects_strictly_overlap(a: &Rect, b: &Rect) -> bool { 26 a.left() < b.right() && a.right() > b.left() && a.top() < b.bottom() && a.bottom() > b.top() 27 } 28 #[derive(Clone, Debug, Deserialize, Difference, PartialEq, Serialize)] 29 pub struct Player { 30 name: String, 31 position: Vec2, 32 collider: RectSerde, 33 timestamp: SystemTime, 34 books: Vec<BookId>, 35 } 36 37 impl Player { 38 const MAX_BOOKS_COUNT: usize = 5; 39 40 fn new(name: String) -> Self { 41 Player { 42 name, 43 position: Vec2::ZERO, 44 collider: RectSerde::new(0., 0., 43.4, 40.2), 45 timestamp: SystemTime::now(), 46 books: vec![], 47 } 48 } 49 pub fn name(&self) -> String { 50 self.name.clone() 51 } 52 pub fn position(&self) -> Vec2 { 53 self.position 54 } 55 pub fn collider(&self) -> RectSerde { 56 self.collider 57 } 58 pub fn books(&self) -> Vec<BookId> { 59 self.books.clone() 60 } 61 } 62 63 #[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)] 64 pub struct RectSerde { 65 x: f32, 66 y: f32, 67 w: f32, 68 h: f32, 69 } 70 71 impl RectSerde { 72 pub fn new(x: f32, y: f32, w: f32, h: f32) -> Self { 73 RectSerde { x, y, w, h } 74 } 75 pub fn rect(&self) -> Rect { 76 (*self).into() 77 } 78 } 79 80 impl From<Rect> for RectSerde { 81 fn from(value: Rect) -> Self { 82 RectSerde { 83 x: value.x, 84 y: value.y, 85 w: value.w, 86 h: value.h, 87 } 88 } 89 } 90 91 impl From<RectSerde> for Rect { 92 fn from(value: RectSerde) -> Self { 93 Rect::new(value.x, value.y, value.w, value.h) 94 } 95 } 96 97 #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] 98 pub struct Action { 99 sequence_number: u32, 100 timestamp: SystemTime, 101 player_id: Uuid, 102 kind: ActionKind, 103 } 104 105 impl Action { 106 pub fn new(sequence_number: u32, player_id: Uuid, kind: ActionKind) -> Self { 107 Action { 108 sequence_number, 109 timestamp: SystemTime::now(), 110 player_id, 111 kind, 112 } 113 } 114 pub fn sequence_number(&self) -> u32 { 115 self.sequence_number 116 } 117 pub fn timestamp(&self) -> SystemTime { 118 self.timestamp 119 } 120 pub fn player_id(&self) -> Uuid { 121 self.player_id 122 } 123 pub fn kind(&self) -> ActionKind { 124 self.kind.clone() 125 } 126 } 127 128 #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] 129 pub enum ActionKind { 130 Join { 131 name: String, 132 }, 133 Move { 134 new_position: Vec2, 135 }, 136 PickUpBook { 137 book_id: BookId, 138 }, 139 DropBook { 140 book_id: BookId, 141 }, 142 PutBookOnShelf { 143 book_id: BookId, 144 slot_address: SlotAddress, 145 }, 146 TakeBookOffShelf { 147 slot_address: SlotAddress, 148 }, 149 } 150 151 #[derive(Clone, Debug, Deserialize, Serialize)] 152 pub struct Response { 153 timestamp: SystemTime, 154 sequence_number: u32, 155 kind: ResponseKind, 156 } 157 158 impl Response { 159 pub fn new(timestamp: SystemTime, sequence_number: u32, kind: ResponseKind) -> Self { 160 Response { 161 timestamp, 162 sequence_number, 163 kind, 164 } 165 } 166 pub fn timestamp(&self) -> SystemTime { 167 self.timestamp 168 } 169 pub fn sequence_number(&self) -> u32 { 170 self.sequence_number 171 } 172 pub fn kind(&self) -> ResponseKind { 173 self.kind.clone() 174 } 175 } 176 177 #[derive(Clone, Debug, Deserialize, Serialize)] 178 pub enum ResponseKind { 179 SetId { 180 id: Uuid, 181 }, 182 UpdateGameState { 183 diffs: Vec<<GameState as StructDiff>::Diff>, 184 }, 185 ReplaceGameState { 186 new_game_state: GameState, 187 }, 188 }