From 25673a08e9dc6d669dc0278a0a7ca71f089e29ee Mon Sep 17 00:00:00 2001 From: skye Date: Thu, 4 May 2023 10:06:14 +0300 Subject: [PATCH] feat(client): add needed functionality for log --- Cargo.lock | 1 + client/Cargo.toml | 1 + client/src/api/game/mod.rs | 2 +- client/src/game_status/mod.rs | 3 +++ client/src/game_status/parser.rs | 6 ++---- client/src/plugins/game/ui/mod.rs | 14 +++++++++++++- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e355b0..1282c54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2033,6 +2033,7 @@ dependencies = [ "futures-lite", "reqwest", "serde", + "serde_json", "serde_repr", ] diff --git a/client/Cargo.toml b/client/Cargo.toml index 768b899..712ce25 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -16,6 +16,7 @@ futures-lite = "1.13.0" reqwest = { version = "0.11.16", features = [ "blocking", "json", "gzip" ] } serde_repr = "0.1.12" +serde_json = "1.0.96" fastrand = "1.9.0" diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs index e7a2f9f..e14f9c5 100644 --- a/client/src/api/game/mod.rs +++ b/client/src/api/game/mod.rs @@ -42,7 +42,7 @@ pub enum Command { Discard { index: usize }, } -#[derive(Deserialize, Clone)] +#[derive(Deserialize, Serialize, Clone)] pub struct Action { pub id: String, pub created_at: String, diff --git a/client/src/game_status/mod.rs b/client/src/game_status/mod.rs index 38d53fd..f0afba8 100644 --- a/client/src/game_status/mod.rs +++ b/client/src/game_status/mod.rs @@ -46,6 +46,9 @@ pub struct PlayerStatus { /// constructed from a vector of [`Action`]s pub struct GameStatus { + /// a modifiable Actions Vector, will be modified when parsing actions, + /// used for showing the log + pub actions: Vec, pub supply_piles: Vec, /// player ids mapped to statuses pub players: HashMap, diff --git a/client/src/game_status/parser.rs b/client/src/game_status/parser.rs index affe64c..4096990 100644 --- a/client/src/game_status/parser.rs +++ b/client/src/game_status/parser.rs @@ -14,15 +14,13 @@ use crate::{api::game::{Action, Command}, game_status::SupplyPile}; use super::{GameStatus, PlayerStatus, Card}; pub fn parse(actions_init: &Vec) -> Result { - // as some actions can add more actions, we need a mutable - // actions vector. - let mut actions = actions_init; let mut game_status = GameStatus { + actions: actions_init.to_vec(), supply_piles: vec![], players: HashMap::new(), }; - for action in actions { + for action in &game_status.actions { // the one who invoked the action let invoker = game_status.players.get_mut(&action.invoker) .unwrap_or_else(|| unreachable!()); diff --git a/client/src/plugins/game/ui/mod.rs b/client/src/plugins/game/ui/mod.rs index 35710ee..56bbdad 100644 --- a/client/src/plugins/game/ui/mod.rs +++ b/client/src/plugins/game/ui/mod.rs @@ -54,7 +54,7 @@ pub fn ui( }); egui::CollapsingHeader::new("Supply Piles") - .default_open(true) + .default_open(false) .show(ui, |ui| { for pile in &status.supply_piles { egui::CollapsingHeader::new(&pile.card.name) @@ -64,5 +64,17 @@ pub fn ui( }); } }); + + egui::CollapsingHeader::new("Log") + .default_open(false) + .show(ui, |ui| { + for action in &status.actions { + ui.add( + egui::TextEdit::multiline(&mut serde_json::to_string_pretty(action).unwrap()) + .code_editor() + .interactive(false) + ); + } + }); }); } -- 2.44.1