From 1904a6d315db0911d22dbe459cfd39ac77c5b2ca Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 24 May 2023 22:34:54 +0300 Subject: [PATCH] feat(client): sorta kinda check for end conditions --- client/src/game_status/parser.rs | 3 --- .../plugins/async_tasks/parse_game_status.rs | 20 ++++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/client/src/game_status/parser.rs b/client/src/game_status/parser.rs index a9d31ec..5c886e5 100644 --- a/client/src/game_status/parser.rs +++ b/client/src/game_status/parser.rs @@ -95,9 +95,6 @@ pub fn parse(game: &Game) -> Result { parse_action(&action, game, &mut game_status); } - // TODO: check for end conditions, declare one player as winner. - // update game state in API to ended, set ended date (in API). - Ok(game_status) } diff --git a/client/src/plugins/async_tasks/parse_game_status.rs b/client/src/plugins/async_tasks/parse_game_status.rs index 45d60bf..f3738a9 100644 --- a/client/src/plugins/async_tasks/parse_game_status.rs +++ b/client/src/plugins/async_tasks/parse_game_status.rs @@ -18,6 +18,8 @@ use bevy::{ }; use futures_lite::future; +use super::GameEndCallEvent; + #[derive(Component)] pub struct ParseGameStatus(Task); @@ -39,6 +41,7 @@ pub fn handle_call( mut commands: Commands, mut tasks: Query<(Entity, &mut ParseGameStatus)>, mut game_data: ResMut, + mut eg_ev_w: EventWriter, mut frg_ev_w: EventWriter, ) { if let Ok((entity, mut task)) = tasks.get_single_mut() { @@ -46,7 +49,22 @@ pub fn handle_call( game_data.game_status = Some(response); game_data.locked = false; - frg_ev_w.send(FinishedRefreshGameEvent); + // NOTE: checking for end conditions + let mut empty_piles = 0; + for pile in &game_data.game_status.as_ref().unwrap().supply_piles { + if pile.amount == 0 { + empty_piles += 1; + } + } + if empty_piles >= 2 { + // end me daddy + eg_ev_w.send(GameEndCallEvent { + game_id: game_data.game.as_ref().unwrap().id.clone(), + }); + } else { + // dont end me daddy + frg_ev_w.send(FinishedRefreshGameEvent); + } // remove the task commands.entity(entity).remove::(); -- 2.44.1