Implement the keyboard to add color to the used chars

main
kirbylife 2023-09-29 21:15:31 -06:00
parent d521d69eac
commit 8420721145
3 changed files with 40 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use crate::consts::{Key, KeyboardKeyType, VirtualKey}; use crate::consts::{Key, KeyboardKeyType, VirtualKey, Status};
use yew::prelude::*; use yew::prelude::*;
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
@ -21,7 +21,19 @@ pub fn Keyboard(props: &KeyboardProps) -> Html {
<div class="keyboard"> { <div class="keyboard"> {
for (props.keyboard).iter().map(|vk: &VirtualKey| { for (props.keyboard).iter().map(|vk: &VirtualKey| {
match vk.key { match vk.key {
KeyboardKeyType::CharKey(c) => html! { <button onclick={ handle_click(Key::CharKey(c)) }> { c } </button> }, KeyboardKeyType::CharKey(c) => html! {
<button
class={
match vk.status {
Some(Status::Found) => "correct",
Some(Status::NotFound) => "missed",
Some(Status::Almost) => "almost",
None => ""
}
}
onclick={ handle_click(Key::CharKey(c)) }
> { c } </button>
},
KeyboardKeyType::Backspace => html! { <button onclick={ handle_click(Key::Backspace) } class="key-big"><i class="las la-undo"></i></button> }, KeyboardKeyType::Backspace => html! { <button onclick={ handle_click(Key::Backspace) } class="key-big"><i class="las la-undo"></i></button> },
KeyboardKeyType::Enter => html! { <button onclick={ handle_click(Key::Enter) } class="key-big"> { "Enviar" } </button> } KeyboardKeyType::Enter => html! { <button onclick={ handle_click(Key::Enter) } class="key-big"> { "Enviar" } </button> }
} }

View File

@ -10,6 +10,15 @@ pub enum KeyboardKeyType {
CharKey(char), 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)] #[derive(Clone, PartialEq)]
pub enum Status { pub enum Status {
NotFound, NotFound,

View File

@ -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::services::words::{get_word_of_the_day, WORDS};
use crate::utils::{evaluate_status, new_empty_virtual_keyboard}; use crate::utils::{evaluate_status, new_empty_virtual_keyboard};
use std::cell::RefCell; use std::cell::RefCell;
@ -64,8 +64,22 @@ impl UseBoardHandle {
char_field: att, char_field: att,
status, status,
} }
}).collect(); }).collect::<Vec<AttemptField>>();
new_attempts.fields.push(new_attempt_row); 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.attempts.set(new_attempts);
*self.attempt_index.borrow_mut() += 1; *self.attempt_index.borrow_mut() += 1;