From 89bbb3e7d988f5064e775a6a398c5b53096fa186 Mon Sep 17 00:00:00 2001 From: skye Date: Thu, 4 May 2023 12:28:16 +0300 Subject: [PATCH] feat(client): change parser to just take &Game --- client/src/game_status/mod.rs | 6 +++--- client/src/game_status/parser.rs | 20 +++++++++++++++---- .../plugins/async_tasks/parse_game_status.rs | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/client/src/game_status/mod.rs b/client/src/game_status/mod.rs index f0afba8..8fe39f9 100644 --- a/client/src/game_status/mod.rs +++ b/client/src/game_status/mod.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; -use crate::api::game::{Action, Command}; +use crate::api::game::{Action, Command, Game}; mod parser; @@ -55,8 +55,8 @@ pub struct GameStatus { } impl GameStatus { - pub fn new(actions: &Vec) -> Self { - match parser::parse(actions) { + pub fn new(game: &Game) -> Self { + match parser::parse(game) { Ok(res) => res, Err(_) => panic!("parsing actions failed"), } diff --git a/client/src/game_status/parser.rs b/client/src/game_status/parser.rs index 4096990..4c6db74 100644 --- a/client/src/game_status/parser.rs +++ b/client/src/game_status/parser.rs @@ -9,17 +9,29 @@ use std::collections::HashMap; use fastrand::Rng; -use crate::{api::game::{Action, Command}, game_status::SupplyPile}; +use crate::{api::game::{Action, Command, Game}, game_status::SupplyPile}; use super::{GameStatus, PlayerStatus, Card}; -pub fn parse(actions_init: &Vec) -> Result { +pub fn parse(game: &Game) -> Result { let mut game_status = GameStatus { - actions: actions_init.to_vec(), + actions: game.actions.as_ref().unwrap().to_vec(), supply_piles: vec![], - players: HashMap::new(), + players: HashMap::new() }; + game_status.players.insert(game.host_id.clone(), PlayerStatus { + hand: vec![], + deck: vec![], + discard: vec![] + }); + + game_status.players.insert(game.guest_id.clone(), PlayerStatus { + hand: vec![], + deck: vec![], + discard: vec![] + }); + for action in &game_status.actions { // the one who invoked the action let invoker = game_status.players.get_mut(&action.invoker) diff --git a/client/src/plugins/async_tasks/parse_game_status.rs b/client/src/plugins/async_tasks/parse_game_status.rs index ca59406..23d1fdf 100644 --- a/client/src/plugins/async_tasks/parse_game_status.rs +++ b/client/src/plugins/async_tasks/parse_game_status.rs @@ -29,7 +29,7 @@ pub fn start_call( let thread_pool = AsyncComputeTaskPool::get(); let ev = ev.clone(); let task = thread_pool.spawn(async move { - let res = GameStatus::new(&ev.game.actions.unwrap()); + let res = GameStatus::new(&ev.game); res }); -- 2.44.1