From 50a4038aa603c7b1a7410995ad0ebda0c26f2f53 Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Sun, 31 Mar 2024 15:23:18 +0200 Subject: [PATCH] rendu textuel closes #4 --- client_cli/src/app.rs | 43 +++++++++++++++++++++++++++++++++---------- store/src/game.rs | 5 +++++ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs index 6ca617b..97ba41b 100644 --- a/client_cli/src/app.rs +++ b/client_cli/src/app.rs @@ -1,6 +1,9 @@ use bot::Bot; use pretty_assertions::assert_eq; -use store::{CheckerMove, Color, Dice, DiceRoller, GameEvent, GameState, PlayerId, TurnStage}; +use std::fmt; +use store::{ + CheckerMove, Color, Dice, DiceRoller, GameEvent, GameState, PlayerId, Stage, TurnStage, +}; #[derive(Debug, Default)] pub struct AppArgs { @@ -167,18 +170,34 @@ impl App { pub fn display(&mut self) -> String { let mut output = "-------------------------------".to_owned(); - output = output - + "\nWaiting for player " - + &self - .game + output += format!( + "\n{:?} > {} > {:?}", + self.game.state.stage, + self.game .state .who_plays() .map(|pl| &pl.name) - .unwrap_or(&"?".to_owned()); + .unwrap_or(&"?".to_owned()), + self.game.state.turn_stage + ) + .as_str(); output = output + "\nRolled dice : " + &self.game.state.dice.to_display_string(); - output = output + "\n-------------------------------"; - output = output + "\n" + &self.game.state.board.to_display_grid(9); + + if self.game.state.stage != Stage::PreGame { + // display players points + output += format!("\n\n{:<11} :: {:<5} :: {}", "Player", "holes", "points").as_str(); + for (player_id, player) in self.game.state.players.iter() { + output += format!( + "\n{}. {:<8} :: {:<5} :: {}", + &player_id, &player.name, &player.holes, &player.points + ) + .as_str(); + } + } + + output += "\n-------------------------------\n"; + output += &self.game.state.board.to_display_grid(9); output } } @@ -190,7 +209,7 @@ mod tests { #[test] fn test_display() { let expected = "------------------------------- -Waiting for player ? +PreGame > ? > RollDice Rolled dice : 0 & 0 ------------------------------- @@ -225,8 +244,12 @@ Rolled dice : 0 & 0 #[test] fn test_move() { let expected = "------------------------------- -Waiting for player myself +InGame > myself > RollDice Rolled dice : 4 & 6 + +Player :: holes :: points +1. myself :: 0 :: 0 +2. bot :: 0 :: 0 ------------------------------- 13 14 15 16 17 18 19 20 21 22 23 24 diff --git a/store/src/game.rs b/store/src/game.rs index b1364dc..341f066 100644 --- a/store/src/game.rs +++ b/store/src/game.rs @@ -5,6 +5,7 @@ use crate::player::{Color, Player, PlayerId}; use crate::Error; use log::{error, info}; use std::cmp; +use std::fmt::Display; // use itertools::Itertools; use serde::{Deserialize, Serialize}; @@ -49,6 +50,10 @@ pub struct GameState { impl fmt::Display for GameState { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut s = String::new(); + s.push_str(&format!( + "Stage: {:?} / {:?}\n", + self.stage, self.turn_stage + )); s.push_str(&format!("Dice: {:?}\n", self.dice)); // s.push_str(&format!("Who plays: {}\n", self.who_plays().map(|player| &player.name ).unwrap_or(""))); s.push_str(&format!("Board: {:?}\n", self.board));