fn card_on_click(&mut self) {
        //get this_click_card_index from game_data
        let this_click_card_index = if self.game_data.count_click_inside_one_turn == 1 {
            self.game_data.card_index_of_first_click
        } else {
            self.game_data.card_index_of_second_click
        };

        if self.game_data.count_click_inside_one_turn == 1
            || self.game_data.count_click_inside_one_turn == 2
        {
            //region: audio play
            //prepare the audio element with src filename of mp3
            let audio_element = web_sys::HtmlAudioElement::new_with_src(
                format!(
                    "content/{}/sound/mem_sound_{:02}.mp3",
                    self.game_data.content_folder_name,
                    self.game_data
                        .vec_cards
                        .get(this_click_card_index)
                        .expect("error this_click_card_index")
                        .card_number_and_img_src
                )
                .as_str(),
            );

            //play() return a Promise in JSValue. That is too hard for me to deal with now.
            audio_element
                .expect("Error: HtmlAudioElement new.")
                .play()
                .expect("Error: HtmlAudioElement.play() ");
            //endregion

            //flip the card up
            self.game_data
                .vec_cards
                .get_mut(this_click_card_index)
                .expect("error this_click_card_index")
                .status = CardStatusCardFace::UpTemporary;

            if self.game_data.count_click_inside_one_turn == 2 {
                //if is the second click, flip the card and then check for card match

                //if the cards match, player get one point and continues another turn
                if self
                    .game_data
                    .vec_cards
                    .get(self.game_data.card_index_of_first_click)
                    .expect("error game_data.card_index_of_first_click")
                    .card_number_and_img_src
                    == self
                        .game_data
                        .vec_cards
                        .get(self.game_data.card_index_of_second_click)
                        .expect("error game_data.card_index_of_second_click")
                        .card_number_and_img_src
                {
                    //give points
                    if self.game_data.player_turn == 1 {
                        self.game_data.player1_points += 1;
                    } else {
                        self.game_data.player2_points += 1
                    }

                    // the two cards matches. make them permanent FaceUp
                    let x1 = self.game_data.card_index_of_first_click;
                    let x2 = self.game_data.card_index_of_second_click;
                    self.game_data
                        .vec_cards
                        .get_mut(x1)
                        .expect("error game_data.card_index_of_first_click")
                        .status = CardStatusCardFace::UpPermanently;
                    self.game_data
                        .vec_cards
                        .get_mut(x2)
                        .expect("error game_data.card_index_of_second_click")
                        .status = CardStatusCardFace::UpPermanently;
                    self.game_data.count_click_inside_one_turn = 0;
                    //the sum of points is 8, the game is over
                    if self.game_data.player1_points + self.game_data.player2_points == 8 {
                        self.game_data.game_state = GameState::EndGame;
                        //send message
                        self.game_data
                            .ws
                            .send_with_str(
                                &serde_json::to_string(&WsMessage::EndGame {
                                    my_ws_uid: self.game_data.my_ws_uid,
                                    other_ws_uid: self.game_data.other_ws_uid,
                                })
                                .expect("error sending EndGame"),
                            )
                            .expect("Failed to send EndGame");
                    }
                }
            }
        }
        self.check_invalidate_for_all_components();
    }
///The onclick event passed by javascript executes all the logic
///and changes only the fields of the Card Grid struct.
///That stuct is the only permanent data storage for later render the virtual dom.
The onclick event passed by javascript executes all the logic
and changes only the fields of the Card Grid struct.
That stuct is the only permanent data storage for later render the virtual dom.
The onclick event passed by javascript executes all the logic and changes only the fields of the Card Grid struct . That stuct is the only permanent data storage for later render the virtual dom .
fn card_on_click(&mut self) {
        //get this_click_card_index from game_data
        let this_click_card_index = if self.game_data.count_click_inside_one_turn == 1 {
            self.game_data.card_index_of_first_click
        } else {
            self.game_data.card_index_of_second_click
        };
        if self.game_data.count_click_inside_one_turn == 1
            || self.game_data.count_click_inside_one_turn == 2
        {
            //region: audio play
            //prepare the audio element with src filename of mp3
            let audio_element = web_sys::HtmlAudioElement::new_with_src(
                format!(
                    "content/{}/sound/mem_sound_{:02}.mp3",
                    self.game_data.content_folder_name,
                    self.game_data
                        .vec_cards
                        .get(this_click_card_index)
                        .expect("error this_click_card_index")
                        .card_number_and_img_src
                )
                .as_str(),
            );
            //play() return a Promise in JSValue. That is too hard for me to deal with now.
            audio_element
                .expect("Error: HtmlAudioElement new.")
                .play()
                .expect("Error: HtmlAudioElement.play() ");
            //endregion
            //flip the card up
            self.game_data
                .vec_cards
                .get_mut(this_click_card_index)
                .expect("error this_click_card_index")
                .status = CardStatusCardFace::UpTemporary;
            if self.game_data.count_click_inside_one_turn == 2 {
                //if is the second click, flip the card and then check for card match
                //if the cards match, player get one point and continues another turn
                if self
                    .game_data
                    .vec_cards
                    .get(self.game_data.card_index_of_first_click)
                    .expect("error game_data.card_index_of_first_click")
                    .card_number_and_img_src
                    == self
                        .game_data
                        .vec_cards
                        .get(self.game_data.card_index_of_second_click)
                        .expect("error game_data.card_index_of_second_click")
                        .card_number_and_img_src
                {
                    //give points
                    if self.game_data.player_turn == 1 {
                        self.game_data.player1_points += 1;
                    } else {
                        self.game_data.player2_points += 1
                    }
                    // the two cards matches. make them permanent FaceUp
                    let x1 = self.game_data.card_index_of_first_click;
                    let x2 = self.game_data.card_index_of_second_click;
                    self.game_data
                        .vec_cards
                        .get_mut(x1)
                        .expect("error game_data.card_index_of_first_click")
                        .status = CardStatusCardFace::UpPermanently;
                    self.game_data
                        .vec_cards
                        .get_mut(x2)
                        .expect("error game_data.card_index_of_second_click")
                        .status = CardStatusCardFace::UpPermanently;
                    self.game_data.count_click_inside_one_turn = 0;
                    //the sum of points is 8, the game is over
                    if self.game_data.player1_points + self.game_data.player2_points == 8 {
                        self.game_data.game_state = GameState::EndGame;
                        //send message
                        self.game_data
                            .ws
                            .send_with_str(
                                &serde_json::to_string(&WsMessage::EndGame {
                                    my_ws_uid: self.game_data.my_ws_uid,
                                    other_ws_uid: self.game_data.other_ws_uid,
                                })
                                .expect("error sending EndGame"),
                            )
                            .expect("Failed to send EndGame");
                    }
                }
            }
        }
        self.check_invalidate_for_all_components();
    }
fn card_on_click ( & mut self ) { //get this_click_card_index from game_data let this_click_card_index = if self . game_data . count_click_inside_one_turn == 1 { self . game_data . card_index_of_first_click } else { self . game_data . card_index_of_second_click } ; if self . game_data . count_click_inside_one_turn == 1 || self . game_data . count_click_inside_one_turn == 2 { //region: audio play //prepare the audio element with src filename of mp3 let audio_element = web_sys :: HtmlAudioElement :: new_with_src ( format ! ( " " self game_data content_folder_name self game_data vec_cards get ( this_click_card_index ) expect ( " " ) card_number_and_img_src ) . as_str ( ) , ) ; //play() return a Promise in JSValue. That is too hard for me to deal with now. audio_element . expect ( " " ) . play ( ) . expect ( " " ) ; //endregion //flip the card up self . game_data . vec_cards . get_mut ( this_click_card_index ) . expect ( " " ) . status = CardStatusCardFace :: UpTemporary ; if self . game_data . count_click_inside_one_turn == 2 { //if is the second click, flip the card and then check for card match //if the cards match, player get one point and continues another turn if self . game_data . vec_cards . get ( self . game_data . card_index_of_first_click ) . expect ( " " ) . card_number_and_img_src == self . game_data . vec_cards . get ( self . game_data . card_index_of_second_click ) . expect ( " " ) . card_number_and_img_src { //give points if self . game_data . player_turn == 1 { self . game_data . player1_points += 1 ; } else { self . game_data . player2_points += 1 } // the two cards matches. make them permanent FaceUp let x1 = self . game_data . card_index_of_first_click ; let x2 = self . game_data . card_index_of_second_click ; self . game_data . vec_cards . get_mut ( x1 ) . expect ( " " ) . status = CardStatusCardFace :: UpPermanently ; self . game_data . vec_cards . get_mut ( x2 ) . expect ( " " ) . status = CardStatusCardFace :: UpPermanently ; self . game_data . count_click_inside_one_turn = 0 ; //the sum of points is 8, the game is over if self . game_data . player1_points + self . game_data . player2_points == 8 { self . game_data . game_state = GameState :: EndGame ; //send message self . game_data . ws . send_with_str ( & serde_json :: to_string ( & WsMessage :: EndGame { my_ws_uid : self . game_data . my_ws_uid , other_ws_uid : self . game_data . other_ws_uid , } ) . expect ( " " ) , ) . expect ( " " ) ; } } } } self . check_invalidate_for_all_components ( ) ; }
The onclick event passed by javascript executes all the logic
and changes only the fields of the Card Grid struct.
The onclick event passed by javascript executes all the logic and changes only the fields of the Card Grid struct .
&mut self
&mut self