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
use crate::*;
use mem6_common::*;
use dodrio::VdomWeak;
use wasm_bindgen::{prelude::*, JsCast};
use wasm_bindgen_futures::spawn_local;
use unwrap::unwrap;
pub fn start_router(vdom: VdomWeak) {
let on_hash_change = move || {
let location = utilsmod::window().location();
let mut local_route = unwrap!(location.hash());
if local_route.is_empty() {
local_route = "index".to_owned();
}
wasm_bindgen_futures::spawn_local({
let vdom = vdom.clone();
async move {
let _ = vdom
.with_component({
let vdom = vdom.clone();
move |root| {
let rrc = root.unwrap_mut::<RootRenderingComponent>();
if rrc.local_route != local_route {
if local_route == "#p02" {
let vdom = vdom.clone();
fetchgameconfigmod::async_fetch_game_config_request(rrc, &vdom);
rrc.local_route = "p02_start_a_group.html".to_owned();
} else if local_route.starts_with("#p03") {
if local_route.contains('.') {
let group_id =
get_url_param_in_hash_after_dot(&local_route);
push_first_player_as_group_id(rrc, group_id);
}
rrc.local_route = "p03_join_a_group.html".to_owned();
} else if local_route.starts_with("#p04") {
let group_id = get_url_param_in_hash_after_dot(&local_route);
push_first_player_as_group_id(rrc, group_id);
statusjoinedmod::on_load_joined(rrc);
rrc.local_route = "p04_wait_to_start.html".to_owned();
} else if local_route == "#p05" {
rrc.local_route = "p05_choose_game.html".to_owned();
} else if local_route == "#p06" {
rrc.local_route = "p06_drink.html".to_owned();
} else if local_route == "#p07" {
rrc.local_route = "p07_do_not_drink.html".to_owned();
} else if local_route == "#p08" {
rrc.local_route = "p08_instructions.html".to_owned();
} else if local_route == "#p11" {
rrc.local_route = "p11_gameboard.html".to_owned();
} else {
rrc.local_route = "p01_start.html".to_owned();
}
let url = rrc.local_route.to_string();
spawn_local(async_fetch_and_write_to_rrc_html_template(url, vdom));
}
}
})
.await;
}
});
};
on_hash_change();
#[allow(clippy::as_conversions)]
let on_hash_change = Closure::wrap(Box::new(on_hash_change) as Box<dyn FnMut()>);
utilsmod::window()
.add_event_listener_with_callback("hashchange", on_hash_change.as_ref().unchecked_ref())
.unwrap_throw();
on_hash_change.forget();
}
fn get_url_param_in_hash_after_dot(local_route: &str) -> &str {
let mut spl = local_route.split('.');
unwrap!(spl.next());
unwrap!(spl.next())
}
pub fn push_first_player_as_group_id(rrc: &mut RootRenderingComponent, group_id: &str) {
let ws_uid = unwrap!(group_id.parse::<usize>());
rrc.game_data.players.clear();
rrc.game_data.players.push(Player {
ws_uid,
nickname: "FirstPlayer".to_string(),
points: 0,
});
rrc.game_data.players_ws_uid = gamedatamod::prepare_players_ws_uid(&rrc.game_data.players);
logmod::debug_write(&format!(
"players_ws_uid: {}",
&rrc.game_data.players_ws_uid
));
}
pub async fn async_fetch_and_write_to_rrc_html_template(url: String, vdom: VdomWeak) {
logmod::debug_write(&format!("fetch {}", &url));
let mut resp_body_text: String = fetchmod::async_spwloc_fetch_text(url).await;
async {
unwrap!(
vdom.with_component({
move |root| {
let rrc = root.unwrap_mut::<RootRenderingComponent>();
let pos1 = resp_body_text.find("<body>").unwrap_or(0);
let pos2 = resp_body_text.find("</body>").unwrap_or(0);
if pos1 != 0 {
#[allow(clippy::integer_arithmetic)]
{
resp_body_text =
unwrap!(resp_body_text.get(pos1 + 6..pos2)).to_string();
}
}
rrc.html_template = resp_body_text;
}
})
.await
);
vdom.schedule_render();
}
.await;
}