summaryrefslogtreecommitdiff
path: root/webapp/src/main.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2022-09-04 13:16:01 -0400
committerKjetil Orbekk <kj@orbekk.com>2022-09-04 13:30:55 -0400
commitca37b1757a92e7b85fc434474642d1506dcd86a3 (patch)
tree47075badf3f2cd84aa4ca986e92e3beaef02b12a /webapp/src/main.rs
parent1c2d88434021f25c43c2503cfce72ef5336c288e (diff)
Add playing card representation
Diffstat (limited to 'webapp/src/main.rs')
-rw-r--r--webapp/src/main.rs158
1 files changed, 151 insertions, 7 deletions
diff --git a/webapp/src/main.rs b/webapp/src/main.rs
index baeeff9..2d01713 100644
--- a/webapp/src/main.rs
+++ b/webapp/src/main.rs
@@ -1,22 +1,166 @@
+#[allow(unused_imports)]
+use log::{debug, error, info, warn};
+use std::fmt;
use yew::prelude::*;
+fn main() {
+ std::panic::set_hook(Box::new(console_error_panic_hook::hook));
+ wasm_logger::init(wasm_logger::Config::default());
+ yew::start_app::<App>();
+}
+
#[function_component(App)]
-fn app() -> Html {
+pub fn app() -> Html {
+ let hand = use_state(|| HandProps {
+ cards: vec![
+ CardProps {
+ suit: Suit::Club,
+ rank: Rank::Two,
+ },
+ CardProps {
+ suit: Suit::Club,
+ rank: Rank::Ace,
+ },
+ ],
+ });
+
+ let onclick = {
+ let hand = hand.clone();
+ Callback::from(move |_| {
+ let mut cards = hand.cards.clone();
+ cards.push(CardProps {
+ suit: Suit::Heart,
+ rank: Rank::Three,
+ });
+ hand.set(HandProps { cards })
+ })
+ };
+
html! {
<>
- <h1>{ "Hello, World" }</h1>
- <Card/>
+ <Hand ..(*hand).clone() />
+ <button {onclick}>{ "Add card" }</button>
</>
}
}
+#[function_component(Hand)]
+pub fn hand(props: &HandProps) -> Html {
+ let cards: Html = props
+ .cards
+ .iter()
+ .map(|c| {
+ html! {
+ <Card ..c.clone() />
+ }
+ })
+ .collect();
+
+ html! {
+ <div class="hand">
+ { cards }
+ </div>
+ }
+}
+
+#[derive(Clone, Default, PartialEq, Properties)]
+pub struct HandProps {
+ #[prop_or_default]
+ cards: Vec<CardProps>,
+}
+
#[function_component(Card)]
-fn card() -> Html {
+pub fn card(props: &CardProps) -> Html {
html! {
- <div class="card" draggable="true" />
+ <div class="card">
+ <div class={ props.suit.css_class() }>
+ { props.rank }
+ </div>
+ </div>
}
}
-fn main() {
- yew::start_app::<App>();
+#[derive(PartialEq, Properties, Clone)]
+pub struct CardProps {
+ suit: Suit,
+ rank: Rank,
+}
+
+#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
+pub enum Suit {
+ Club,
+ Diamond,
+ Heart,
+ Spade,
+}
+
+#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy)]
+pub enum Rank {
+ Two = 2,
+ Three,
+ Four,
+ Five,
+ Six,
+ Seven,
+ Eight,
+ Nine,
+ Ten,
+ Jack,
+ Queen,
+ King,
+ Ace,
+}
+
+impl Suit {
+ pub fn css_class(&self) -> &'static str {
+ match self {
+ Suit::Club => "suit-club",
+ Suit::Diamond => "suit-diamond",
+ Suit::Heart => "suit-heart",
+ Suit::Spade => "suit-spade",
+ }
+ }
+}
+
+impl fmt::Display for Suit {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ f.write_str(match self {
+ Suit::Club => "♣",
+ Suit::Diamond => "♢",
+ Suit::Heart => "♡",
+ Suit::Spade => "♠",
+ })
+ }
+}
+
+impl fmt::Debug for Suit {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ write!(f, "{}", self)
+ }
+}
+
+impl fmt::Display for Rank {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ f.write_str(match self {
+ Rank::Ace => "A",
+ Rank::King => "K",
+ Rank::Queen => "Q",
+ Rank::Jack => "J",
+ Rank::Ten => "10",
+ Rank::Nine => "9",
+ Rank::Eight => "8",
+ Rank::Seven => "7",
+ Rank::Six => "6",
+ Rank::Five => "5",
+ Rank::Four => "4",
+ Rank::Three => "3",
+ Rank::Two => "2",
+ })
+ }
+}
+
+impl fmt::Debug for Rank {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
+ write!(f, "{}", self)
+ }
}