1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
use crate::card::{Rank, 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;
extern crate wee_alloc;
// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub const SUIT_DISPLAY_ORDER: [Suit; 4] = [Suit::Diamond, Suit::Club, Suit::Heart, Suit::Spade];
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)]
pub fn app() -> Html {
let mut rng = rand::thread_rng();
let mut deal = card::deal(&mut rng);
deal.sort(&SUIT_DISPLAY_ORDER, card::RankOrder::Descending);
let north = use_state(|| HandProps::from_iter(deal.north.into_iter()));
let west = use_state(|| HandProps::from_iter(deal.west.into_iter()));
let south = use_state(|| HandProps::from_iter(deal.south.into_iter()));
let east = use_state(|| HandProps::from_iter(deal.east.into_iter()));
let bidding_state = use_state(|| BiddingResult::new(Player::East));
let shuffle = {
let north = north.clone();
let west = west.clone();
let south = south.clone();
let east = east.clone();
Callback::from(move |_| {
let mut rng = rand::thread_rng();
let mut deal = card::deal(&mut rng);
deal.sort(&SUIT_DISPLAY_ORDER, card::RankOrder::Descending);
north.set(deal.north.into_iter().collect());
west.set(deal.west.into_iter().collect());
south.set(deal.south.into_iter().collect());
east.set(deal.east.into_iter().collect());
})
};
html! {
<>
<p>{ "Bidding table" }</p>
<BiddingTable bidding={ (*bidding_state).clone().bidding().clone() } />
<p>{ "Bidding box" }</p>
<BiddingBox current_bid={ (*bidding_state).clone().bidding().highest_bid() } />
<p>{ "North" }</p>
<Hand ..(*north).clone() />
<p>{ "West" }</p>
<Hand ..(*west).clone() />
<p>{ "South" }</p>
<Hand ..(*south).clone() />
<p>{ "East" }</p>
<Hand ..(*east).clone() />
<p>{ "Controls" }</p>
<button onclick={shuffle}>{ "Shuffle" }</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>
}
}
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<CardProps>,
}
impl<C: Into<CardProps>> FromIterator<C> for HandProps {
fn from_iter<Cards>(cards: Cards) -> Self
where
Cards: std::iter::IntoIterator<Item = C>,
{
HandProps {
cards: cards.into_iter().map(Into::into).collect(),
}
}
}
#[function_component(Card)]
pub fn wcard(props: &CardProps) -> Html {
html! {
<div class="card">
<div class={ suit_css_class(props.suit) }>
{ props.rank }
</div>
</div>
}
}
#[derive(PartialEq, Properties, Clone)]
pub struct CardProps {
suit: Suit,
rank: Rank,
}
impl From<card::Card> for CardProps {
fn from(card::Card(suit, rank): card::Card) -> Self {
CardProps { suit, rank }
}
}
pub fn bid_css_class(suit: Option<Suit>) -> &'static str {
match suit {
None => "suit-notrump",
Some(x) => suit_css_class(x),
}
}
fn padding(dealer: Player) -> Html {
let mut padding : Vec<Html> = vec![];
let mut player = Player::West;
while player != dealer {
padding.push(html! { <div/> });
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!{ <div class="bid">{ bid }</div> },
Some(raise) => html!{
<div class="bid">
{ raise.level }
<div class={ bid_css_class(raise.suit) }/>
</div>
},
};
let bids: Html = props
.bidding
.bids
.iter()
.map(|b| { bid(b) })
.collect();
let padding : Html = padding(props.bidding.dealer);
html! {
<div class="bidding-table">
<div class="header">{ "West" }</div>
<div class="header">{ "North" }</div>
<div class="header">{ "East" }</div>
<div class="header">{ "South" }</div>
{ padding }
{ bids }
</div>
}
}
#[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! {
<div class={class}>
{ bid.level }
</div>
}
})
.collect();
html! {
<div class="bidding-box">
{ bids }
<div>{ "Pass" }</div>
<div>{ "X" }</div>
<div>{ "XX" }</div>
</div>
}
}
#[derive(PartialEq, Properties, Clone)]
pub struct BiddingBoxProps {
current_bid: Option<Raise>,
}
#[cfg(test)]
mod tests {
pub fn test_setup() {
dotenv::dotenv().ok();
let _ = env_logger::builder().is_test(true).try_init();
}
}
|