From 8420721145b6464967f31570ca563beb663793d9 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Fri, 29 Sep 2023 21:15:31 -0600 Subject: [PATCH] Implement the keyboard to add color to the used chars --- src/components/keyboard.rs | 16 ++++++++++++++-- src/consts.rs | 9 +++++++++ src/hooks/use_board.rs | 20 +++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/components/keyboard.rs b/src/components/keyboard.rs index 0ced525..b3ebc36 100644 --- a/src/components/keyboard.rs +++ b/src/components/keyboard.rs @@ -1,4 +1,4 @@ -use crate::consts::{Key, KeyboardKeyType, VirtualKey}; +use crate::consts::{Key, KeyboardKeyType, VirtualKey, Status}; use yew::prelude::*; #[derive(Properties, PartialEq)] @@ -21,7 +21,19 @@ pub fn Keyboard(props: &KeyboardProps) -> Html {
{ for (props.keyboard).iter().map(|vk: &VirtualKey| { match vk.key { - KeyboardKeyType::CharKey(c) => html! { }, + KeyboardKeyType::CharKey(c) => html! { + + }, KeyboardKeyType::Backspace => html! { }, KeyboardKeyType::Enter => html! { } } diff --git a/src/consts.rs b/src/consts.rs index 65a89f4..b789a94 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -10,6 +10,15 @@ pub enum KeyboardKeyType { CharKey(char), } +impl KeyboardKeyType { + pub fn cmp_char(&self, char_to_compare: &char) -> bool { + match self { + KeyboardKeyType::CharKey(ch) => ch == char_to_compare, + _ => false + } + } +} + #[derive(Clone, PartialEq)] pub enum Status { NotFound, diff --git a/src/hooks/use_board.rs b/src/hooks/use_board.rs index 2a6da38..5675230 100644 --- a/src/hooks/use_board.rs +++ b/src/hooks/use_board.rs @@ -1,4 +1,4 @@ -use crate::consts::{AttemptField, Attempts, GameResult, Key, VirtualKey, MAX_ATTEMPTS}; +use crate::consts::{AttemptField, Attempts, GameResult, Key, VirtualKey, MAX_ATTEMPTS, Status}; use crate::services::words::{get_word_of_the_day, WORDS}; use crate::utils::{evaluate_status, new_empty_virtual_keyboard}; use std::cell::RefCell; @@ -64,8 +64,22 @@ impl UseBoardHandle { char_field: att, status, } - }).collect(); - new_attempts.fields.push(new_attempt_row); + }).collect::>(); + new_attempts.fields.push(new_attempt_row.clone()); + + let mut new_keyboard = (*self.virtual_keyboard).clone(); + for key in new_keyboard.iter_mut() { + for attempt_field in new_attempt_row.iter() { + if key.key.cmp_char(&attempt_field.char_field) { + if key.status.is_none() { + key.status = Some(attempt_field.status.clone()); + } else if key.status == Some(Status::Almost) && attempt_field.status == Status::Found { + key.status = Some(Status::Found); + } + } + } + } + self.virtual_keyboard.set(new_keyboard); self.attempts.set(new_attempts); *self.attempt_index.borrow_mut() += 1;