]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
Fix bug: board not updated on win/lose/draw
authorStephen Seo <seo.disparate@gmail.com>
Wed, 4 May 2022 05:55:01 +0000 (14:55 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 4 May 2022 05:55:01 +0000 (14:55 +0900)
back_end/src/db_handler.rs
front_end/src/state.rs
front_end/src/yew_components.rs

index c79bb100863f5242852f596906941d0c8f6ba453..1b34db346fd7f8fc1c53dc5965db45975ee774eb 100644 (file)
@@ -928,7 +928,7 @@ impl DBHandler {
         }
 
         // get board state
-        let board = board_from_string(board_string);
+        let board = board_from_string(&board_string);
 
         // find placement position or return "illegal move" if unable to
         let mut final_pos = pos;
@@ -1064,7 +1064,7 @@ impl DBHandler {
         };
 
         let is_cyan = status == 0;
-        let board = board_from_string(board_string);
+        let board = board_from_string(&board_string);
         let mut ai_choice_pos: usize = get_ai_choice(
             AIDifficulty::Hard,
             if is_cyan {
index be8954ccf561c07ce3ad17eb799a991381157cd7..0642803298a0965562ac7a2787722ad706f037ce 100644 (file)
@@ -454,7 +454,7 @@ pub fn new_string_board() -> String {
 }
 
 #[allow(dead_code)]
-pub fn board_from_string(board_string: String) -> BoardType {
+pub fn board_from_string(board_string: &str) -> BoardType {
     let board = new_empty_board();
 
     for (idx, c) in board_string.chars().enumerate() {
index e2c05ae7450c6b357eec56d9ce20278d203c482f..19868eb05b45e5bf3b73ce2687ac7e61696151b1 100644 (file)
@@ -615,9 +615,9 @@ impl Wrapper {
         &mut self,
         shared: &SharedState,
         document: &Document,
-        board_string: String,
+        board_string: &str,
     ) {
-        let board = board_from_string(board_string.clone());
+        let board = board_from_string(board_string);
         for (idx, slot) in board.iter().enumerate() {
             let was_open =
                 element_has_class(document, &format!("slot{}", idx), "open").unwrap_or(false);
@@ -1579,7 +1579,7 @@ impl Component for Wrapper {
                         if self.board_updated_time != updated_time {
                             if let Some(updated_time) = updated_time {
                                 self.board_updated_time.replace(updated_time);
-                                if let Some(board_string) = board_string {
+                                if let Some(board_string) = board_string.as_ref() {
                                     self.update_board_from_string(&shared, &document, board_string);
                                 }
                             }
@@ -1635,6 +1635,9 @@ impl Component for Wrapper {
                                 }
                             }
                             NetworkedGameState::CyanWon => {
+                                if let Some(board_string) = board_string.as_ref() {
+                                    self.update_board_from_string(&shared, &document, board_string);
+                                }
                                 append_to_info_text(
                                     &document,
                                     "info_text1",
@@ -1648,6 +1651,9 @@ impl Component for Wrapper {
                                 self.do_backend_tick = false;
                             }
                             NetworkedGameState::MagentaWon => {
+                                if let Some(board_string) = board_string.as_ref() {
+                                    self.update_board_from_string(&shared, &document, board_string);
+                                }
                                 append_to_info_text(
                                     &document,
                                     "info_text1",
@@ -1661,6 +1667,9 @@ impl Component for Wrapper {
                                 self.do_backend_tick = false;
                             }
                             NetworkedGameState::Draw => {
+                                if let Some(board_string) = board_string.as_ref() {
+                                    self.update_board_from_string(&shared, &document, board_string);
+                                }
                                 append_to_info_text(
                                     &document,
                                     "info_text1",
@@ -1723,7 +1732,7 @@ impl Component for Wrapper {
                         }
                     }
                     BREnum::GotPlaced(placed_status, board_string) => {
-                        self.update_board_from_string(&shared, &document, board_string);
+                        self.update_board_from_string(&shared, &document, &board_string);
 
                         match placed_status {
                             PlacedEnum::Accepted => {