Skip to content

Commit 6cdad10

Browse files
authored
Merge pull request #301 from TruncateGame/fix/npc-winning
Fix bug where NPC couldn't win by touching an artifact on WASM builds
2 parents d5c82a9 + dfc769e commit 6cdad10

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

Cargo.lock

+7-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

truncate_client/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ impl WebHandle {
126126
}
127127
}
128128

129+
#[cfg(target_arch = "wasm32")]
130+
#[wasm_bindgen]
131+
pub fn backchannel_setup() {
132+
use web_sys::console;
133+
134+
// Make sure panics are logged using `console.error`.
135+
console_error_panic_hook::set_once();
136+
137+
// Redirect tracing to console.log and friends:
138+
tracing_wasm::set_as_global_default();
139+
}
140+
129141
// Functions used in the web worker
130142
// Used to evaluate games as the NPC using a separate thread,
131143
// preventing the UI from hanging during computation.

truncate_client/src/regions/native_menu.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ pub fn render_native_menu_if_required(
9898
}
9999
ui.text_edit_singleline(room_code);
100100
if ui.button("Join Game").clicked() {
101-
send_to_server(PlayerMessage::JoinGame(
102-
room_code.clone(),
103-
outer.name.clone(),
104-
token.clone(),
105-
));
101+
outer.launched_code = Some(room_code.clone());
106102
return Some(GameStatus::PendingJoin(room_code.clone()));
107103
}
108104
if let Some(existing_token) = token {

truncate_core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ chksum-hash-sha2 = { version = "0.0.0", default-features = false, features = [
1919
"256",
2020
] }
2121
noise = "0.8"
22+
tracing = "0.1.41"
2223

2324
[dev-dependencies]
2425
insta = { version = "1.29.0", features = ["yaml"] }

truncate_core/src/game.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,15 @@ impl Game {
567567
}
568568

569569
let neighbors = self.board.neighbouring_squares(position);
570+
if !neighbors.iter().any(|&(_, square)| match square {
571+
Square::Occupied { player: p, .. } => p == player,
572+
Square::Artifact { player: p, .. } => p == player,
573+
_ => false,
574+
}) {
575+
if neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
576+
return Err(GamePlayError::OpponentStartPlace);
577+
}
570578

571-
if self.turn_count == 0 && neighbors.iter().any(|&(_, square)| matches!(square, Square::Artifact { player: p, .. } if p != player)) {
572-
return Err(GamePlayError::OpponentStartPlace);
573-
}
574-
575-
if !neighbors.iter().any(
576-
|&(_, square)| match square {
577-
Square::Occupied { player: p, .. } => p == player,
578-
Square::Artifact { player: p, .. } => p == player,
579-
_ => false,
580-
},
581-
) {
582579
return Err(GamePlayError::NonAdjacentPlace);
583580
}
584581

truncate_core/src/moves/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ mod tests {
10141014
players,
10151015
player_turn_count: vec![0, 0],
10161016
judge: short_dict(),
1017-
turn_count: 1, // any non zero value will do to avoid hitting OpponentStartPlace error
1017+
turn_count: 0,
10181018
..Game::new_legacy(3, 1, None, GameRules::generation(0))
10191019
};
10201020
game.start();

truncate_core/src/npc/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ impl Game {
8989
let evaluation_player = game
9090
.next_player
9191
.expect("Minimax only works in non-periodic playmodes");
92-
9392
let mut internal_arborist = if npc_params.pruning {
9493
Arborist::pruning()
9594
} else {
@@ -140,15 +139,15 @@ impl Game {
140139
};
141140

142141
if log {
143-
println!(
142+
tracing::debug!(
144143
"Bot checked {} boards, going to a depth of {looked}",
145144
arborist.assessed()
146145
);
147-
println!("Bot has the hand: {}", game.players[evaluation_player].hand);
146+
tracing::debug!("Bot has the hand: {}", game.players[evaluation_player].hand);
148147

149-
println!("Chosen tree has the score {best_score:#?}");
148+
tracing::debug!("Chosen tree has the score {best_score:#?}");
150149
if let Some(board) = &best_score.board {
151-
println!("Bot is aiming for the board {board}");
150+
tracing::debug!("Bot is aiming for the board {board}");
152151
}
153152
}
154153

@@ -717,10 +716,8 @@ mod tests {
717716
initial_board: &'a str,
718717
depth: usize,
719718
dict: &WordDict,
720-
turn_count: u32,
721719
) -> (&'a str, String) {
722720
let mut game = test_game(initial_board, hand);
723-
game.turn_count = turn_count;
724721

725722
let (best_move, pruned_checks, total_checks) = best_test_move(&game, &dict, depth);
726723

@@ -989,7 +986,6 @@ mod tests {
989986
"###,
990987
3,
991988
&dict,
992-
0,
993989
);
994990

995991
insta::with_settings!({
@@ -1031,7 +1027,6 @@ mod tests {
10311027
"###,
10321028
3,
10331029
&dict,
1034-
0,
10351030
);
10361031

10371032
insta::with_settings!({
@@ -1073,7 +1068,6 @@ mod tests {
10731068
"###,
10741069
3,
10751070
&dict,
1076-
0,
10771071
);
10781072

10791073
insta::with_settings!({
@@ -1115,7 +1109,6 @@ mod tests {
11151109
"###,
11161110
3,
11171111
&dict,
1118-
0,
11191112
);
11201113

11211114
insta::with_settings!({
@@ -1157,7 +1150,6 @@ mod tests {
11571150
"###,
11581151
3,
11591152
&dict,
1160-
0,
11611153
);
11621154

11631155
insta::with_settings!({
@@ -1202,7 +1194,6 @@ mod tests {
12021194
"###,
12031195
3,
12041196
&dict,
1205-
0,
12061197
);
12071198

12081199
insta::with_settings!({
@@ -1250,7 +1241,6 @@ mod tests {
12501241
"###,
12511242
4,
12521243
&dict,
1253-
0,
12541244
);
12551245

12561246
insta::with_settings!({
@@ -1304,7 +1294,6 @@ mod tests {
13041294
"###,
13051295
2,
13061296
&dict,
1307-
3, // any non zero turn count value will do
13081297
);
13091298

13101299
insta::with_settings!({

web_client/src/static/worker.js

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ self.onmessage = function (e) {
2525
console.log("[WORKER] Successfully loaded Truncate");
2626

2727
loadedWasm = true;
28+
wasm_bindgen.backchannel_setup();
29+
2830
}
2931

3032
function on_wasm_error(error) {

0 commit comments

Comments
 (0)