]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Minor tweaks (and `cargo fmt` got aggressive)
authorStephen Seo <seo.disparate@gmail.com>
Wed, 6 Apr 2022 10:38:37 +0000 (19:38 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 6 Apr 2022 10:38:59 +0000 (19:38 +0900)
front_end/src/state.rs
front_end/src/yew_components.rs

index caffe07b4d2d91ee3ff6635f5b321923ff4b1964..3c952889ebaa25a513a93ccd6e44280c7104a2d0 100644 (file)
@@ -84,6 +84,19 @@ impl GameState {
         }
     }
 
+    pub fn get_network_current_side(&self) -> Option<Turn> {
+        if let GameState::NetworkedMultiplayer {
+            paired: _,
+            current_side,
+            current_turn: _,
+        } = *self
+        {
+            current_side
+        } else {
+            None
+        }
+    }
+
     pub fn set_networked_current_turn(&mut self, turn: Turn) {
         if let GameState::NetworkedMultiplayer {
             paired: _,
index ac5a7f77847226e0f01f5c8c586f43be83d0bb88..64f61f2b373cc395b13bb8950b0d5e3c2388e52a 100644 (file)
@@ -702,12 +702,6 @@ impl Component for Wrapper {
                             current_side,
                             current_turn,
                         } => {
-                            log::warn!(
-                                "paired is {}, current_side is {:?}, current_turn is {:?}",
-                                paired,
-                                current_side,
-                                current_turn
-                            );
                             if paired {
                                 if let Some(current_side) = current_side {
                                     if current_side == current_turn {
@@ -715,7 +709,6 @@ impl Component for Wrapper {
                                     }
                                 }
                             }
-                            log::warn!("Set place request to {:?}", self.place_request);
                             return true;
                         }
                         GameState::PostGameResults(_) => (),
@@ -1212,210 +1205,135 @@ impl Component for Wrapper {
             WrapperMsg::BackendRequest { place } => {
                 self.place_request = Some(place);
             }
-            WrapperMsg::BackendResponse(br_enum) => match br_enum {
-                BREnum::Error(string) => {
-                    // TODO maybe suppress this for release builds
-                    log::warn!("{}", string);
-                }
-                BREnum::GotID(id, turn_opt) => {
-                    self.player_id = Some(id);
-                    let mut game_state = shared.game_state.get();
-                    game_state.set_networked_paired();
-                    game_state.set_networked_current_side(turn_opt);
-                    shared.game_state.set(game_state);
-                    if let Some(turn_type) = turn_opt {
-                        append_to_info_text(
-                            &document,
-                            "info_text0",
-                            &format!(
-                                "<b class=\"{}\">Paired with player, you are the {}</b>",
-                                turn_type.get_color(),
-                                turn_type
-                            ),
-                            INFO_TEXT_MAX_ITEMS,
-                        )
-                        .ok();
-                        append_to_info_text(
-                            &document,
-                            "info_text1",
-                            "<b class=\"cyan\">It is CyanPlayer's Turn</b>",
-                            1,
-                        )
-                        .ok();
-                    }
-                }
-                BREnum::GotPairing(turn_opt) => {
-                    let mut game_state = shared.game_state.get();
-                    game_state.set_networked_current_side(turn_opt);
-                    shared.game_state.set(game_state);
-                    if let Some(turn_type) = turn_opt {
-                        append_to_info_text(
-                            &document,
-                            "info_text0",
-                            &format!(
-                                "<b class=\"{}\">Paired with player, you are the {}</b>",
-                                turn_type.get_color(),
-                                turn_type
-                            ),
-                            INFO_TEXT_MAX_ITEMS,
-                        )
-                        .ok();
-                        append_to_info_text(
-                            &document,
-                            "info_text1",
-                            "<b class=\"cyan\">It is CyanPlayer's Turn</b>",
-                            1,
-                        )
-                        .ok();
+            WrapperMsg::BackendResponse(br_enum) => {
+                match br_enum {
+                    BREnum::Error(string) => {
+                        // TODO maybe suppress this for release builds
+                        log::warn!("{}", string);
                     }
-                }
-                BREnum::GotStatus(networked_game_state, board_opt) => {
-                    if let Some(board_string) = board_opt {
-                        self.update_board_from_string(&shared, &document, board_string);
-                    }
-
-                    let mut current_game_state = shared.game_state.get();
-                    match networked_game_state {
-                        NetworkedGameState::CyanTurn => {
-                            if current_game_state.get_current_turn() != Turn::CyanPlayer {
-                                current_game_state.set_networked_current_turn(Turn::CyanPlayer);
-                                shared.game_state.set(current_game_state);
-                                append_to_info_text(
-                                    &document,
-                                    "info_text1",
-                                    "<b class=\"cyan\">It is CyanPlayer's Turn</b>",
-                                    1,
-                                )
-                                .ok();
-                            }
-                        }
-                        NetworkedGameState::MagentaTurn => {
-                            if current_game_state.get_current_turn() != Turn::MagentaPlayer {
-                                current_game_state.set_networked_current_turn(Turn::MagentaPlayer);
-                                shared.game_state.set(current_game_state);
-                                append_to_info_text(
-                                    &document,
-                                    "info_text1",
-                                    "<b class=\"magenta\">It is MagentaPlayer's Turn</b>",
-                                    1,
-                                )
-                                .ok();
-                            }
-                        }
-                        NetworkedGameState::CyanWon => {
+                    BREnum::GotID(id, turn_opt) => {
+                        self.player_id = Some(id);
+                        let mut game_state = shared.game_state.get();
+                        game_state.set_networked_paired();
+                        game_state.set_networked_current_side(turn_opt);
+                        shared.game_state.set(game_state);
+                        if let Some(turn_type) = turn_opt {
                             append_to_info_text(
                                 &document,
-                                "info_text1",
-                                "<b class=\"cyan\">CyanPlayer won the game</b>",
-                                1,
+                                "info_text0",
+                                &format!(
+                                    "<b class=\"{}\">Paired with player, you are the {}</b>",
+                                    turn_type.get_color(),
+                                    turn_type
+                                ),
+                                INFO_TEXT_MAX_ITEMS,
                             )
                             .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::CyanWin));
-                            self.do_backend_tick = false;
-                        }
-                        NetworkedGameState::MagentaWon => {
                             append_to_info_text(
                                 &document,
                                 "info_text1",
-                                "<b class=\"magenta\">MagentaPlayer won the game</b>",
+                                &format!(
+                                    "<b class=\"cyan\">It is CyanPlayer's ({}) Turn</b>",
+                                    if turn_type == Turn::CyanPlayer {
+                                        "your"
+                                    } else {
+                                        "opponent's"
+                                    }
+                                ),
                                 1,
                             )
                             .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::MagentaWin));
-                            self.do_backend_tick = false;
                         }
-                        NetworkedGameState::Draw => {
+                    }
+                    BREnum::GotPairing(turn_opt) => {
+                        let mut game_state = shared.game_state.get();
+                        game_state.set_networked_current_side(turn_opt);
+                        shared.game_state.set(game_state);
+                        if let Some(turn_type) = turn_opt {
                             append_to_info_text(
                                 &document,
-                                "info_text1",
-                                "<b>The game ended in a draw</b>",
-                                1,
+                                "info_text0",
+                                &format!(
+                                    "<b class=\"{}\">Paired with player, you are the {}</b>",
+                                    turn_type.get_color(),
+                                    turn_type
+                                ),
+                                INFO_TEXT_MAX_ITEMS,
                             )
                             .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::Empty));
-                            self.do_backend_tick = false;
-                        }
-                        NetworkedGameState::Disconnected => {
                             append_to_info_text(
                                 &document,
                                 "info_text1",
-                                "<b>The opponent disconnected</b>",
+                                &format!(
+                                    "<b class=\"cyan\">It is CyanPlayer's ({}) Turn</b>",
+                                    if turn_type == Turn::CyanPlayer {
+                                        "your"
+                                    } else {
+                                        "opponent's"
+                                    }
+                                ),
                                 1,
                             )
                             .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::Empty));
-                            self.do_backend_tick = false;
                         }
-                        NetworkedGameState::InternalError => {
-                            append_to_info_text(
-                                &document,
-                                "info_text1",
-                                "<b>There was an internal error</b>",
-                                1,
-                            )
-                            .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::Empty));
-                            self.do_backend_tick = false;
+                    }
+                    BREnum::GotStatus(networked_game_state, board_opt) => {
+                        if let Some(board_string) = board_opt {
+                            self.update_board_from_string(&shared, &document, board_string);
                         }
-                        NetworkedGameState::NotPaired => (),
-                        NetworkedGameState::UnknownID => {
-                            append_to_info_text(
+
+                        let mut current_game_state = shared.game_state.get();
+                        match networked_game_state {
+                            NetworkedGameState::CyanTurn => {
+                                if current_game_state.get_current_turn() != Turn::CyanPlayer {
+                                    current_game_state.set_networked_current_turn(Turn::CyanPlayer);
+                                    shared.game_state.set(current_game_state);
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        &format!(
+                                            "<b class=\"cyan\">It is CyanPlayer's ({}) Turn</b>",
+                                            if current_game_state
+                                                .get_network_current_side()
+                                                .unwrap_or(Turn::CyanPlayer)
+                                                == Turn::CyanPlayer
+                                            {
+                                                "your"
+                                            } else {
+                                                "opponent's"
+                                            }
+                                        ),
+                                        1,
+                                    )
+                                    .ok();
+                                }
+                            }
+                            NetworkedGameState::MagentaTurn => {
+                                if current_game_state.get_current_turn() != Turn::MagentaPlayer {
+                                    current_game_state
+                                        .set_networked_current_turn(Turn::MagentaPlayer);
+                                    shared.game_state.set(current_game_state);
+                                    append_to_info_text(
                                 &document,
                                 "info_text1",
-                                "<b>The game has ended (disconnected?)</b>",
+                                &format!(
+                                    "<b class=\"magenta\">It is MagentaPlayer's ({}) Turn</b>",
+                                    if current_game_state.get_network_current_side().unwrap_or(Turn::CyanPlayer) == Turn::MagentaPlayer
+                                    {
+                                        "your"
+                                    } else {
+                                        "opponent's"
+                                    }),
                                 1,
                             )
                             .ok();
-                            shared
-                                .game_state
-                                .set(GameState::PostGameResults(BoardState::Empty));
-                            self.do_backend_tick = false;
-                        }
-                    }
-                }
-                BREnum::GotPlaced(placed_status, board_string) => {
-                    self.update_board_from_string(&shared, &document, board_string);
-
-                    match placed_status {
-                        PlacedEnum::Accepted => {
-                            // noop, handled by update_board_from_string
-                        }
-                        PlacedEnum::Illegal => {
-                            append_to_info_text(
-                                &document,
-                                "info_text0",
-                                "<b>Cannot place a token there</b>",
-                                INFO_TEXT_MAX_ITEMS,
-                            )
-                            .ok();
-                        }
-                        PlacedEnum::NotYourTurn => {
-                            append_to_info_text(
-                                &document,
-                                "info_text0",
-                                "<b>Cannot place a token, not your turn</b>",
-                                INFO_TEXT_MAX_ITEMS,
-                            )
-                            .ok();
-                        }
-                        PlacedEnum::Other(networked_game_state) => match networked_game_state {
-                            NetworkedGameState::CyanTurn => (),
-                            NetworkedGameState::MagentaTurn => (),
+                                }
+                            }
                             NetworkedGameState::CyanWon => {
                                 append_to_info_text(
                                     &document,
                                     "info_text1",
-                                    "<b class=\"cyan\">CyanPlayer has won the game</b>",
+                                    "<b class=\"cyan\">CyanPlayer won the game</b>",
                                     1,
                                 )
                                 .ok();
@@ -1428,7 +1346,7 @@ impl Component for Wrapper {
                                 append_to_info_text(
                                     &document,
                                     "info_text1",
-                                    "<b class=\"magenta\">MagentaPlayer has won the game</b>",
+                                    "<b class=\"magenta\">MagentaPlayer won the game</b>",
                                     1,
                                 )
                                 .ok();
@@ -1490,10 +1408,120 @@ impl Component for Wrapper {
                                     .set(GameState::PostGameResults(BoardState::Empty));
                                 self.do_backend_tick = false;
                             }
-                        },
+                        }
+                    }
+                    BREnum::GotPlaced(placed_status, board_string) => {
+                        self.update_board_from_string(&shared, &document, board_string);
+
+                        match placed_status {
+                            PlacedEnum::Accepted => {
+                                // noop, handled by update_board_from_string
+                            }
+                            PlacedEnum::Illegal => {
+                                append_to_info_text(
+                                    &document,
+                                    "info_text0",
+                                    "<b>Cannot place a token there</b>",
+                                    INFO_TEXT_MAX_ITEMS,
+                                )
+                                .ok();
+                            }
+                            PlacedEnum::NotYourTurn => {
+                                append_to_info_text(
+                                    &document,
+                                    "info_text0",
+                                    "<b>Cannot place a token, not your turn</b>",
+                                    INFO_TEXT_MAX_ITEMS,
+                                )
+                                .ok();
+                            }
+                            PlacedEnum::Other(networked_game_state) => match networked_game_state {
+                                NetworkedGameState::CyanTurn => (),
+                                NetworkedGameState::MagentaTurn => (),
+                                NetworkedGameState::CyanWon => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b class=\"cyan\">CyanPlayer has won the game</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::CyanWin));
+                                    self.do_backend_tick = false;
+                                }
+                                NetworkedGameState::MagentaWon => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b class=\"magenta\">MagentaPlayer has won the game</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::MagentaWin));
+                                    self.do_backend_tick = false;
+                                }
+                                NetworkedGameState::Draw => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b>The game ended in a draw</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::Empty));
+                                    self.do_backend_tick = false;
+                                }
+                                NetworkedGameState::Disconnected => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b>The opponent disconnected</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::Empty));
+                                    self.do_backend_tick = false;
+                                }
+                                NetworkedGameState::InternalError => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b>There was an internal error</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::Empty));
+                                    self.do_backend_tick = false;
+                                }
+                                NetworkedGameState::NotPaired => (),
+                                NetworkedGameState::UnknownID => {
+                                    append_to_info_text(
+                                        &document,
+                                        "info_text1",
+                                        "<b>The game has ended (disconnected?)</b>",
+                                        1,
+                                    )
+                                    .ok();
+                                    shared
+                                        .game_state
+                                        .set(GameState::PostGameResults(BoardState::Empty));
+                                    self.do_backend_tick = false;
+                                }
+                            },
+                        }
                     }
                 }
-            },
+            }
         } // match (msg)
 
         true