From 18acb7d0f8dc4c048b3cf39391845a1b527b8350 Mon Sep 17 00:00:00 2001 From: skye Date: Fri, 28 Apr 2023 14:58:21 +0300 Subject: [PATCH] feat!(client): replace gamestate consts with enums --- Cargo.lock | 12 ++++++++++++ client/Cargo.toml | 2 ++ client/src/api/game/mod.rs | 17 +++++++++++------ client/src/plugins/menu/ui/browse.rs | 6 +++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61705dc..3770278 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2032,6 +2032,7 @@ dependencies = [ "futures-lite", "reqwest", "serde", + "serde_repr", ] [[package]] @@ -2869,6 +2870,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_repr" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcec881020c684085e55a25f7fd888954d56609ef363479dc5a1305eb0d40cab" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" diff --git a/client/Cargo.toml b/client/Cargo.toml index 7723a16..f73aa13 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -15,6 +15,8 @@ futures-lite = "1.13.0" reqwest = { version = "0.11.16", features = [ "blocking", "json", "gzip" ] } +serde_repr = "0.1.12" + [dependencies.serde] version = "1.0.160" default-features = false diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs index cea561b..a91d36b 100644 --- a/client/src/api/game/mod.rs +++ b/client/src/api/game/mod.rs @@ -7,6 +7,7 @@ */ use serde::{Deserialize, Serialize}; +use serde_repr::Deserialize_repr; use super::user::User; @@ -49,6 +50,15 @@ pub struct Action { pub command: Command, } +#[derive(Deserialize_repr, Clone, PartialEq)] +#[repr(u8)] +pub enum GameState { + Forming = 0, + InProgress= 1, + Finished = 2, + Cancelled = 3, +} + #[derive(Deserialize, Clone)] pub struct Game { pub id: String, @@ -59,12 +69,7 @@ pub struct Game { pub host: Option, pub guest_id: String, pub guest: Option, - pub state: u8, + pub state: GameState, pub turn: u8, pub actions: Option>, } - -pub const GAMESTATE_FORMING: u8 = 0; -pub const GAMESTATE_INPROGRESS: u8 = 1; -pub const GAMESTATE_FINISHED: u8 = 2; -pub const GAMESTATE_CANCELLED: u8 = 3; diff --git a/client/src/plugins/menu/ui/browse.rs b/client/src/plugins/menu/ui/browse.rs index 1cfe4af..683c715 100644 --- a/client/src/plugins/menu/ui/browse.rs +++ b/client/src/plugins/menu/ui/browse.rs @@ -13,7 +13,7 @@ use crate::{plugins::{ GameCreateCallEvent, BrowseState, GameFormingCallEvent, GameMyGamesCallEvent, GameJoinCallEvent, menu::MenuState -}, api::game::{GAMESTATE_INPROGRESS, GAMESTATE_FINISHED}, Global, AppState}; +}, api::game::GameState, Global, AppState}; use super::{MenuData, MenuUIState}; @@ -106,7 +106,7 @@ pub fn view( } BrowseState::InProgress => { let mut games = data.my_games.clone(); - games.retain(|g| g.state == GAMESTATE_INPROGRESS); + games.retain(|g| g.state == GameState::InProgress); for game in &games { egui::Frame::none() .fill(egui::Color32::BLACK) @@ -131,7 +131,7 @@ pub fn view( } BrowseState::Finished => { let mut games = data.my_games.clone(); - games.retain(|g| g.state == GAMESTATE_FINISHED); + games.retain(|g| g.state == GameState::Finished); for game in &games { egui::Frame::none() .fill(egui::Color32::BLACK) -- 2.44.1