]> git.seodisparate.com - EN605.607.81.SP22_ASDM_Project/commitdiff
front-end: Minor refactorings
authorStephen Seo <seo.disparate@gmail.com>
Fri, 29 Apr 2022 02:37:15 +0000 (11:37 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 29 Apr 2022 02:37:15 +0000 (11:37 +0900)
front_end/src/yew_components.rs

index 04094d0d42def6b5d3495a71bafc8ebae998737b..f305c42c7b8c3ae0c21234f7c0569ce7ed8271c6 100644 (file)
@@ -630,6 +630,20 @@ impl Wrapper {
             }
         }
     }
+
+    fn send_disconnect(&mut self) {
+        if let Some(id) = self.player_id.take() {
+            let function = Function::new_no_args(&format!(
+                "
+                let xhr = new XMLHttpRequest();
+                xhr.open('POST', '{}');
+                xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}');
+            ",
+                BACKEND_URL, id
+            ));
+            function.call0(&function).ok();
+        }
+    }
 }
 
 #[derive(Clone, Debug, PartialEq, Eq)]
@@ -1264,17 +1278,7 @@ impl Component for Wrapper {
             WrapperMsg::StartBackendTickImpl => {
                 // If previous id is still stored, request disconnect so that a
                 // new id can be received
-                if let Some(id) = self.player_id.take() {
-                    let function = Function::new_no_args(&format!(
-                        "
-                        let xhr = new XMLHttpRequest();
-                        xhr.open('POST', '{}');
-                        xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}');
-                    ",
-                        BACKEND_URL, id
-                    ));
-                    function.call0(&function).ok();
-                }
+                self.send_disconnect();
                 self.do_backend_tick = true;
                 ctx.link().send_message(WrapperMsg::BackendTick);
             }
@@ -1283,17 +1287,7 @@ impl Component for Wrapper {
                     shared.game_state.borrow().is_networked_multiplayer();
                 if !self.do_backend_tick || !is_networked_multiplayer {
                     // disconnect id if backend tick is to be stopped
-                    if let Some(id) = self.player_id.take() {
-                        let function = Function::new_no_args(&format!(
-                            "
-                            let xhr = new XMLHttpRequest();
-                            xhr.open('POST', '{}');
-                            xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}');
-                        ",
-                            BACKEND_URL, id
-                        ));
-                        function.call0(&function).ok();
-                    }
+                    self.send_disconnect();
                     return false;
                 }
 
@@ -1714,17 +1708,7 @@ impl Component for Wrapper {
                     element_remove_class(&document, &format!("slot{}", idx), "magenta").ok();
                     element_append_class(&document, &format!("slot{}", idx), "open").ok();
                 }
-                if let Some(id) = self.player_id.take() {
-                    let function = Function::new_no_args(&format!(
-                        "
-                        let xhr = new XMLHttpRequest();
-                        xhr.open('POST', '{}');
-                        xhr.send('{{\"type\": \"disconnect\", \"id\": {}}}');
-                    ",
-                        BACKEND_URL, id
-                    ));
-                    function.call0(&function).ok();
-                }
+                self.send_disconnect();
                 self.place_request = None;
                 element_remove_class(&document, "mainmenu", "hidden_menu").ok();
                 element_append_class(&document, "mainmenu", "menu").ok();