-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin.rs
202 lines (195 loc) · 14.8 KB
/
coin.rs
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
use mogwai::prelude::*;
use web_sys::{Request, RequestInit, RequestMode, Response};
#[derive(Clone)]
pub struct Coin {
pub arm: usize,
}
#[derive(Clone)]
pub enum CoinIn {
/// The coin is clicked. Make a request to get the flip result, then animate and notify parent.
Clicked,
/// Response from the server.
Response(bool),
/// No action.
None,
}
#[derive(Clone)]
pub enum CoinOut {
/// Tell the parent the result of a coin flip.
Flipped(bool),
}
impl Component for Coin {
type ModelMsg = CoinIn;
type ViewMsg = CoinOut;
type DomNode = HtmlElement;
fn update(&mut self, msg: &CoinIn, tx: &Transmitter<CoinOut>, subscriber: &Subscriber<CoinIn>) {
match msg {
CoinIn::Clicked => {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::SameOrigin);
let url = format!("/flip/{}", self.arm);
// create an async request. send, take response, and send it on the transmitter
// subscribe to the receiver via subscriber, then act on the response
let (tx_req, rx_req): (Transmitter<CoinIn>, Receiver<CoinIn>) = txrx();
tx_req.send_async(async move {
let window = web_sys::window().unwrap();
let request =
Request::new_with_str_and_init(&url, &opts).expect("Should be valid URL");
let response = JsFuture::from(window.fetch_with_request(&request))
.await
.expect("Failed to send request")
.dyn_into::<Response>()
.expect("Malformed response");
if response.status() == 200 {
let result = JsFuture::from(response.text().expect("Malformed response"))
.await
.expect("Malformed response")
== "true";
CoinIn::Response(result)
} else {
CoinIn::None
}
});
subscriber.subscribe(&rx_req);
}
CoinIn::Response(result) => {
tx.send(&CoinOut::Flipped(*result));
}
CoinIn::None => {}
}
}
#[allow(unused_braces)]
fn view(&self, tx: &Transmitter<CoinIn>, rx: &Receiver<CoinOut>) -> ViewBuilder<HtmlElement> {
let tx_click = tx.contra_map(|_: &Event| CoinIn::Clicked);
let (tx_color, rx_color) = txrx();
rx.branch_map(|msg: &CoinOut| {
match msg {
CoinOut::Flipped(true) => "green",
CoinOut::Flipped(false) => "firebrick",
}
.to_string()
})
.forward_map(&tx_color, |e| e.clone());
// once animation ends, must update the style
let tx_animate = tx_color.contra_map(|_: &Event| "grey".to_string());
let ns = "http://www.w3.org/2000/svg";
builder!(
<svg x="0px" y="0px" viewBox="0 0 58 58" xmlns=ns on:click=tx_click on:transitionend=tx_animate>
<g fill={("grey", rx_color)} xmlns=ns>
<path xmlns=ns d="M32,0c-1.021,0-2.026,0.076-3.015,0.21C27.953,0.072,26.972,0,26,0c-4.883,0-9.449,1.572-13.33,4.29
c-0.001,0.001-0.003,0.002-0.004,0.003c-1.968,1.379-3.758,3.055-5.326,4.97C7.293,9.306,7.258,9.356,7.22,9.407
c-1.484,1.838-2.76,3.896-3.791,6.122c-0.028,0.05-0.051,0.1-0.07,0.155c-0.71,1.556-1.303,3.191-1.759,4.895
c-0.045,0.096-0.065,0.201-0.078,0.311C0.861,23.465,0.5,26.185,0.5,29c0,4.403,0.871,8.577,2.422,12.319
C2.936,41.361,2.95,41.4,2.97,41.439c0.928,2.214,2.097,4.273,3.466,6.136c0.037,0.051,0.067,0.105,0.113,0.148
c1.389,1.864,2.982,3.524,4.738,4.94c0.051,0.057,0.113,0.099,0.176,0.143C15.59,56.076,20.6,58,26,58
c0.972,0,1.953-0.072,2.985-0.21C29.974,57.924,30.979,58,32,58c14.061,0,25.5-13.009,25.5-29S46.061,0,32,0z M21.419,2.625
c-0.318,0.166-0.632,0.338-0.943,0.518c-0.189,0.109-0.38,0.216-0.566,0.33C19.636,3.642,19.368,3.82,19.1,4h-1.926
c1.462-0.684,2.988-1.224,4.579-1.559C21.64,2.498,21.532,2.566,21.419,2.625z M11.36,46H7.773c-0.882-1.246-1.665-2.585-2.345-4
h3.788c0.157,0.355,0.319,0.706,0.488,1.052c0.072,0.148,0.148,0.293,0.222,0.439c0.206,0.404,0.42,0.801,0.642,1.192
c0.081,0.144,0.16,0.289,0.243,0.431C10.988,45.415,11.173,45.708,11.36,46z M4.968,17h3.827c-0.176,0.438-0.342,0.881-0.498,1.329
c-0.043,0.123-0.086,0.245-0.128,0.368C8.024,19.127,7.89,19.561,7.763,20H3.857C4.175,18.969,4.547,17.969,4.968,17z M2.5,29
c0-0.674,0.036-1.338,0.078-2h3.987c-0.001,0.016-0.001,0.032-0.002,0.048C6.526,27.694,6.5,28.343,6.5,29
c0,0.335,0.012,0.667,0.022,1H2.539C2.528,29.666,2.5,29.337,2.5,29z M6.636,32c0.038,0.421,0.087,0.839,0.141,1.255
c0.004,0.03,0.006,0.061,0.01,0.092C6.861,33.903,6.951,34.454,7.052,35H3.108c-0.195-0.982-0.349-1.981-0.448-3H6.636z M7.256,22
c-0.026,0.119-0.048,0.239-0.073,0.358c-0.027,0.131-0.054,0.261-0.08,0.392c-0.122,0.628-0.231,1.261-0.316,1.903
c-0.004,0.03-0.006,0.061-0.01,0.092C6.766,24.83,6.759,24.915,6.748,25H2.779c0.132-1.018,0.299-2.022,0.528-3H7.256z M7.495,37
c0.031,0.123,0.06,0.247,0.093,0.37c0.011,0.043,0.021,0.087,0.033,0.13c0.165,0.61,0.35,1.21,0.549,1.802
c0.041,0.123,0.085,0.245,0.128,0.368c0.038,0.11,0.075,0.22,0.114,0.329H4.558c-0.379-0.971-0.705-1.973-0.983-3H7.495z
M10.811,12.885c-0.083,0.142-0.162,0.287-0.243,0.431c-0.222,0.391-0.436,0.789-0.642,1.192c-0.074,0.146-0.15,0.291-0.222,0.439
C9.695,14.965,9.686,14.982,9.678,15H5.929c0.757-1.427,1.629-2.762,2.596-4h3.503c-0.122,0.175-0.245,0.348-0.363,0.526
C11.37,11.969,11.084,12.422,10.811,12.885z M13.731,6h2.763C16.247,6.216,16,6.434,15.759,6.66
c-0.203,0.192-0.4,0.391-0.598,0.589c-0.196,0.197-0.39,0.396-0.58,0.6c-0.186,0.199-0.373,0.396-0.554,0.6
C13.868,8.629,13.716,8.815,13.561,9h-3.302C11.333,7.883,12.492,6.876,13.731,6z M9.324,48h3.432
c0.113,0.148,0.225,0.297,0.341,0.442c0.302,0.379,0.612,0.749,0.931,1.109c0.181,0.205,0.367,0.402,0.554,0.6
c0.191,0.203,0.384,0.403,0.58,0.6c0.081,0.082,0.159,0.169,0.241,0.249h-3.008C11.297,50.101,10.27,49.097,9.324,48z
M21.753,55.559c-2.293-0.483-4.46-1.369-6.47-2.559h2.419c0.125,0.096,0.253,0.187,0.379,0.281
c0.213,0.158,0.424,0.317,0.641,0.468c0.389,0.271,0.785,0.531,1.187,0.778c0.187,0.115,0.377,0.221,0.566,0.33
c0.311,0.18,0.625,0.353,0.943,0.518C21.532,55.434,21.64,55.502,21.753,55.559z M32,56c-0.794,0-1.579-0.047-2.354-0.136
l-0.397-0.055c-0.049-0.007-0.098-0.015-0.147-0.021c-0.348-0.049-0.693-0.107-1.036-0.173c-0.025-0.005-0.049-0.01-0.074-0.015
c-0.335-0.066-0.667-0.139-0.996-0.221c-0.044-0.011-0.087-0.023-0.13-0.035c-0.309-0.079-0.616-0.163-0.92-0.256
c-0.082-0.025-0.162-0.054-0.243-0.08c-0.265-0.084-0.529-0.17-0.789-0.265c-0.185-0.067-0.367-0.142-0.549-0.214
c-0.156-0.061-0.313-0.119-0.467-0.184c-5.376-2.276-9.834-6.748-12.546-12.464c-0.024-0.052-0.047-0.104-0.071-0.156
c-0.146-0.312-0.288-0.628-0.423-0.948c-0.053-0.125-0.102-0.252-0.153-0.378c-0.102-0.25-0.202-0.501-0.298-0.755
c-0.06-0.161-0.117-0.324-0.175-0.487c-0.079-0.223-0.157-0.447-0.232-0.674c-0.06-0.183-0.117-0.367-0.173-0.552
c-0.065-0.212-0.128-0.425-0.189-0.64c-0.055-0.195-0.107-0.391-0.158-0.587c-0.055-0.21-0.107-0.422-0.157-0.635
c-0.048-0.201-0.094-0.403-0.137-0.607c-0.046-0.213-0.088-0.428-0.129-0.643c-0.039-0.204-0.078-0.409-0.113-0.614
c-0.038-0.222-0.071-0.446-0.104-0.671c-0.03-0.201-0.06-0.401-0.086-0.604c-0.031-0.24-0.055-0.483-0.08-0.725
c-0.02-0.189-0.042-0.377-0.058-0.567c-0.024-0.278-0.04-0.558-0.056-0.839c-0.009-0.158-0.022-0.314-0.029-0.472
C8.511,29.889,8.5,29.446,8.5,29s0.011-0.889,0.029-1.33c0.007-0.158,0.02-0.315,0.029-0.472c0.016-0.28,0.032-0.561,0.056-0.839
c0.016-0.19,0.038-0.378,0.058-0.567c0.025-0.242,0.05-0.485,0.08-0.725c0.026-0.202,0.057-0.403,0.086-0.604
c0.033-0.224,0.067-0.448,0.104-0.671c0.035-0.206,0.074-0.41,0.113-0.614c0.041-0.215,0.084-0.43,0.129-0.643
c0.044-0.203,0.09-0.405,0.137-0.607c0.05-0.213,0.103-0.424,0.157-0.635c0.051-0.197,0.103-0.392,0.158-0.587
c0.06-0.215,0.124-0.428,0.189-0.64C9.884,19.884,9.94,19.699,10,19.517c0.074-0.226,0.152-0.45,0.232-0.674
c0.058-0.163,0.114-0.326,0.175-0.487c0.095-0.254,0.196-0.505,0.298-0.755c0.051-0.126,0.1-0.253,0.153-0.378
c0.135-0.32,0.277-0.635,0.423-0.948c0.024-0.052,0.046-0.105,0.071-0.156c2.712-5.717,7.171-10.189,12.546-12.464
c0.154-0.065,0.311-0.122,0.467-0.184c0.183-0.072,0.365-0.147,0.549-0.214c0.261-0.094,0.525-0.181,0.789-0.265
c0.081-0.026,0.162-0.055,0.243-0.08c0.304-0.093,0.611-0.177,0.92-0.256c0.044-0.011,0.087-0.024,0.13-0.035
c0.329-0.082,0.661-0.155,0.996-0.221c0.025-0.005,0.049-0.01,0.074-0.015c0.343-0.066,0.688-0.124,1.036-0.173
c0.049-0.007,0.098-0.015,0.147-0.021l0.397-0.055C30.421,2.047,31.206,2,32,2c12.958,0,23.5,12.112,23.5,27S44.958,56,32,56z"></path>
<path xmlns=ns d="M32,8.476c0.606,0,1.219,0.036,1.818,0.105c0.04,0.005,0.079,0.007,0.117,0.007c0.501,0,0.933-0.375,0.992-0.884
c0.064-0.548-0.329-1.045-0.877-1.109c-0.673-0.079-1.358-0.119-2.068-0.119c-0.004,0-0.008,0-0.012,0l-0.013,1v-1
c-0.553,0-0.979,0.447-0.979,1C30.979,8.029,31.447,8.476,32,8.476z"></path>
<path xmlns=ns d="M24.329,10.337c0.152,0,0.308-0.035,0.453-0.109c0.558-0.284,1.133-0.538,1.709-0.753c0.517-0.193,0.779-0.77,0.586-1.287
c-0.193-0.518-0.767-0.781-1.287-0.586c-0.646,0.242-1.291,0.526-1.916,0.845c-0.492,0.25-0.688,0.853-0.437,1.345
C23.614,10.138,23.965,10.337,24.329,10.337z"></path>
<path xmlns=ns d="M49.135,23.292c0.113,0.454,0.521,0.757,0.969,0.757c0.081,0,0.162-0.01,0.244-0.03c0.536-0.135,0.861-0.678,0.727-1.213
c-0.167-0.665-0.364-1.332-0.587-1.982c-0.18-0.522-0.748-0.801-1.271-0.621c-0.522,0.179-0.801,0.748-0.622,1.271
C48.8,22.07,48.981,22.682,49.135,23.292z"></path>
<path xmlns=ns d="M47.731,15.277c-0.387-0.571-0.805-1.13-1.242-1.662c-0.352-0.426-0.98-0.487-1.408-0.137
c-0.426,0.351-0.487,0.981-0.137,1.408c0.398,0.484,0.779,0.993,1.131,1.513c0.193,0.286,0.509,0.439,0.829,0.439
c0.193,0,0.388-0.056,0.56-0.172C47.921,16.355,48.041,15.734,47.731,15.277z"></path>
<path xmlns=ns d="M32,49.524c-0.621,0-1.247-0.037-1.857-0.11c-0.561-0.056-1.047,0.327-1.112,0.875c-0.065,0.548,0.326,1.046,0.874,1.112
c0.69,0.082,1.396,0.124,2.096,0.124h0.084c0.553,0,0.958-0.448,0.958-1S32.553,49.524,32,49.524z"></path>
<path xmlns=ns d="M45.021,43.02c-0.397,0.488-0.82,0.96-1.259,1.404c-0.388,0.393-0.384,1.026,0.01,1.414
c0.194,0.192,0.448,0.289,0.702,0.289c0.258,0,0.516-0.099,0.712-0.297c0.482-0.489,0.948-1.009,1.386-1.546
c0.349-0.429,0.284-1.059-0.145-1.407S45.37,42.593,45.021,43.02z"></path>
<path xmlns=ns d="M39.252,47.754c-0.555,0.285-1.129,0.54-1.706,0.757c-0.517,0.194-0.778,0.771-0.583,1.288
c0.15,0.401,0.531,0.648,0.936,0.648c0.117,0,0.236-0.021,0.353-0.065c0.648-0.244,1.293-0.53,1.915-0.849
c0.491-0.252,0.685-0.855,0.433-1.346C40.348,47.696,39.745,47.5,39.252,47.754z"></path>
<path xmlns=ns d="M41.857,9.45c-0.585-0.382-1.194-0.733-1.812-1.044c-0.494-0.249-1.095-0.05-1.343,0.443
c-0.249,0.493-0.051,1.095,0.442,1.343c0.552,0.278,1.097,0.592,1.62,0.933c0.168,0.11,0.357,0.163,0.545,0.163
c0.326,0,0.646-0.16,0.839-0.454C42.451,10.372,42.32,9.752,41.857,9.45z"></path>
<path xmlns=ns d="M50.833,27.968c-0.553,0-1,0.479-1,1.032c0,0.618-0.023,1.236-0.07,1.839c-0.043,0.551,0.369,1.032,0.92,1.074
c0.026,0.002,0.053,0.003,0.078,0.003c0.518,0,0.955-0.398,0.996-0.923c0.051-0.654,0.076-1.325,0.076-1.994v-0.064
C51.833,28.384,51.386,27.968,50.833,27.968z"></path>
<path xmlns=ns d="M14.167,29c0-0.623,0.025-1.256,0.074-1.88c0.043-0.551-0.368-1.032-0.919-1.076c-0.544-0.036-1.032,0.368-1.075,0.918
c-0.053,0.676-0.08,1.362-0.08,2.037v0.021c0,0.552,0.447,0.989,1,0.989S14.167,29.552,14.167,29z"></path>
<path xmlns=ns d="M49.901,35.783c-0.522-0.175-1.09,0.105-1.267,0.628c-0.202,0.598-0.432,1.193-0.683,1.77
c-0.221,0.506,0.012,1.096,0.518,1.316c0.13,0.057,0.266,0.083,0.398,0.083c0.387,0,0.754-0.225,0.918-0.601
c0.273-0.629,0.523-1.278,0.743-1.93C50.706,36.527,50.425,35.96,49.901,35.783z"></path>
<path xmlns=ns d="M15.55,18.465c-0.51-0.226-1.096,0.008-1.317,0.514c-0.274,0.626-0.526,1.275-0.748,1.929
c-0.177,0.523,0.104,1.091,0.626,1.269c0.106,0.036,0.215,0.053,0.321,0.053c0.417,0,0.806-0.263,0.947-0.679
c0.203-0.6,0.434-1.195,0.686-1.768C16.286,19.277,16.056,18.687,15.55,18.465z"></path>
<path xmlns=ns d="M17.902,41.567c-0.31-0.458-0.93-0.578-1.389-0.269c-0.457,0.31-0.577,0.931-0.268,1.389
c0.392,0.579,0.808,1.139,1.238,1.664c0.198,0.241,0.484,0.366,0.774,0.366c0.223,0,0.447-0.074,0.633-0.227
c0.428-0.351,0.489-0.98,0.14-1.408C18.64,42.605,18.26,42.095,17.902,41.567z"></path>
<path xmlns=ns d="M14.855,34.668c-0.134-0.536-0.679-0.863-1.212-0.729c-0.536,0.133-0.862,0.676-0.729,1.212
c0.166,0.666,0.363,1.333,0.585,1.983c0.142,0.416,0.53,0.677,0.946,0.677c0.107,0,0.216-0.018,0.323-0.054
c0.522-0.179,0.802-0.747,0.623-1.27C15.187,35.892,15.008,35.28,14.855,34.668z"></path>
<path xmlns=ns d="M18.846,12.14c-0.482,0.486-0.95,1.006-1.39,1.544c-0.35,0.428-0.286,1.058,0.142,1.407
c0.186,0.152,0.41,0.226,0.632,0.226c0.29,0,0.578-0.125,0.775-0.367c0.399-0.489,0.823-0.96,1.261-1.401
c0.389-0.392,0.387-1.025-0.006-1.414C19.868,11.746,19.235,11.747,18.846,12.14z"></path>
<path xmlns=ns d="M24.818,47.791c-0.552-0.28-1.097-0.596-1.618-0.938c-0.462-0.303-1.081-0.172-1.385,0.289
c-0.302,0.462-0.173,1.082,0.289,1.384c0.583,0.382,1.191,0.734,1.81,1.048c0.145,0.074,0.299,0.108,0.451,0.108
c0.365,0,0.717-0.2,0.893-0.547C25.508,48.643,25.312,48.041,24.818,47.791z"></path>
<path xmlns=ns d="M37.896,28.805c-1.01-0.5-2.146-0.916-3.375-1.236c-0.71-0.186-1.219-0.319-1.526-0.401v-8.936
c0.454,0.09,0.886,0.223,1.291,0.396c1.654,0.677,2.625,1.645,3.739,2.877v2.082c0,0.6,0.387,0.987,0.987,0.987
S40,24.187,40,23.587v-6.169c0-0.6-0.388-0.987-0.987-0.987s-0.987,0.388-0.987,0.987l0,1.315
c-0.229-0.179-0.075-0.385-0.402-0.618c-1.259-0.902-2.814-1.529-4.629-1.866V14.82c0-0.6-0.387-0.987-0.987-0.987
s-0.987,0.388-0.987,0.987v1.289c-2.561,0.043-4.482,0.631-5.713,1.75c-1.293,1.177-1.949,2.765-1.949,4.721
c0,1.037,0.301,1.968,0.894,2.766c0.576,0.776,1.401,1.433,2.453,1.952c0.951,0.471,2.401,0.978,4.316,1.509v9.564
c-0.52-0.089-1-0.216-1.432-0.38c-1.771-0.676-2.56-1.095-3.615-2.241v-2.243c0-0.6-0.388-0.987-0.987-0.987S24,32.907,24,33.507
v6.169c0,0.6,0.387,0.987,0.987,0.987s0.987-0.388,0.987-0.987v-1.316c1.727,1.385,2.996,1.723,5.047,2.005v1.405
c0,0.6,0.388,0.987,0.987,0.987s0.987-0.388,0.987-0.987v-1.287c2.586-0.031,4.611-0.649,6.023-1.837
c1.476-1.24,2.224-2.932,2.224-5.025c0-1.101-0.301-2.063-0.894-2.863C39.772,29.981,38.947,29.325,37.896,28.805z M31.021,26.617
c-0.344-0.102-0.722-0.212-1.132-0.329c-0.656-0.188-1.365-0.452-2.107-0.787c-0.696-0.313-1.292-0.76-1.771-1.328
c-0.456-0.539-0.677-1.155-0.677-1.885c0-1.179,0.463-2.148,1.416-2.965c0.916-0.784,2.352-1.2,4.272-1.24V26.617z M38.375,37.88
c-0.017,0.014-0.038,0.024-0.055,0.037c0.145-0.118,0.285-0.24,0.414-0.367C38.622,37.664,38.502,37.773,38.375,37.88z
M37.654,37.175c-1.068,0.856-2.635,1.305-4.658,1.334v-9.165l1.574,0.439c0.75,0.209,1.507,0.484,2.25,0.818
c0.696,0.313,1.292,0.761,1.771,1.327c0.456,0.541,0.678,1.157,0.678,1.886C39.269,35.204,38.74,36.304,37.654,37.175z"></path>
</g>
</svg>
)
}
}