From cab440c8a209ae92eba07d50f7b7127dadbd65c0 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Tue, 6 Sep 2022 22:07:28 -0400 Subject: Move components to a separate module --- webapp/src/main.rs | 164 ++--------------------------------------------------- 1 file changed, 4 insertions(+), 160 deletions(-) (limited to 'webapp/src/main.rs') diff --git a/webapp/src/main.rs b/webapp/src/main.rs index c99eeee..8f94899 100644 --- a/webapp/src/main.rs +++ b/webapp/src/main.rs @@ -1,14 +1,12 @@ -use crate::card::{Rank, Suit}; +use crate::card::Suit; #[allow(unused_imports)] use log::{debug, error, info, warn}; use yew::prelude::*; pub mod bridge_engine; pub mod card; -use bridge_engine::Bid; -use bridge_engine::Bidding; -use bridge_engine::BiddingResult; -use bridge_engine::Player; -use bridge_engine::Raise; +pub mod components; +use bridge_engine::{Player, BiddingResult}; +use components::{BiddingTable, BiddingBox, Hand, HandProps}; extern crate wee_alloc; // Use `wee_alloc` as the global allocator. @@ -73,160 +71,6 @@ pub fn app() -> Html { } } -#[function_component(Hand)] -pub fn hand(props: &HandProps) -> Html { - let cards: Html = props - .cards - .iter() - .map(|c| { - html! { - - } - }) - .collect(); - - html! { -
- { cards } -
- } -} - -pub fn suit_css_class(suit: Suit) -> &'static str { - match suit { - Suit::Club => "suit-club", - Suit::Diamond => "suit-diamond", - Suit::Heart => "suit-heart", - Suit::Spade => "suit-spade", - } -} - -#[derive(Clone, Default, PartialEq, Properties)] -pub struct HandProps { - #[prop_or_default] - cards: Vec, -} - -impl> FromIterator for HandProps { - fn from_iter(cards: Cards) -> Self - where - Cards: std::iter::IntoIterator, - { - HandProps { - cards: cards.into_iter().map(Into::into).collect(), - } - } -} - -#[function_component(Card)] -pub fn wcard(props: &CardProps) -> Html { - html! { -
-
- { props.rank } -
-
- } -} - -#[derive(PartialEq, Properties, Clone)] -pub struct CardProps { - suit: Suit, - rank: Rank, -} - -impl From for CardProps { - fn from(card::Card(suit, rank): card::Card) -> Self { - CardProps { suit, rank } - } -} - -pub fn bid_css_class(suit: Option) -> &'static str { - match suit { - None => "suit-notrump", - Some(x) => suit_css_class(x), - } -} - -fn padding(dealer: Player) -> Html { - let mut padding : Vec = vec![]; - let mut player = Player::West; - while player != dealer { - padding.push(html! {
}); - player = player.next(); - } - padding.into_iter().collect() -} - -#[function_component(BiddingTable)] -pub fn bidding_table(props: &BiddingTableProps) -> Html { - let bid = |bid: &Bid| match bid.as_raise() { - None => html!{
{ bid }
}, - Some(raise) => html!{ -
- { raise.level } -
-
- }, - }; - let bids: Html = props - .bidding - .bids - .iter() - .map(|b| { bid(b) }) - .collect(); - let padding : Html = padding(props.bidding.dealer); - html! { -
-
{ "West" }
-
{ "North" }
-
{ "East" }
-
{ "South" }
- { padding } - { bids } -
- } -} - -#[derive(PartialEq, Properties, Clone)] -pub struct BiddingTableProps { - bidding: Bidding, -} - -#[function_component(BiddingBox)] -pub fn bidding_box(props: &BiddingBoxProps) -> Html { - let bids: Html = Raise::all_raises() - .iter() - .map(|bid| { - let mut class = if Some(*bid) <= props.current_bid { - classes!("disabled") - } else { - classes!("enabled") - }; - class.extend(classes!(bid_css_class(bid.suit))); - html! { -
- { bid.level } -
- } - }) - .collect(); - - html! { -
- { bids } -
{ "Pass" }
-
{ "X" }
-
{ "XX" }
-
- } -} - -#[derive(PartialEq, Properties, Clone)] -pub struct BiddingBoxProps { - current_bid: Option, -} - #[cfg(test)] mod tests { pub fn test_setup() { -- cgit v1.2.3