Compare commits

..

No commits in common. "273a66fbff33539ce03a5819a4a29fde69ef51b8" and "637166ff58a4276ff8cf6f2af85a01e6cd69e5e5" have entirely different histories.

7 changed files with 34 additions and 19 deletions

View File

@ -1,7 +1,6 @@
use yew::prelude::*; use yew::prelude::*;
use crate::components::{EmptyRow, CurrentRow, AttemptRow}; use crate::components::{EmptyRow, CurrentRow, AttemptRow};
use crate::consts::{Attempts, MAX_ATTEMPTS}; use crate::consts::{Attempts, MAX_ATTEMPTS};
use std::cmp::Ordering;
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
pub struct BoardProps { pub struct BoardProps {
@ -24,10 +23,12 @@ pub fn Board(props: &BoardProps) -> Html {
<div class="board" onkeyup={onkeypress} tabindex="0"> <div class="board" onkeyup={onkeypress} tabindex="0">
{ {
for (0..MAX_ATTEMPTS).map(|i| { for (0..MAX_ATTEMPTS).map(|i| {
match i.cmp(attempt_index) { if i < *attempt_index {
Ordering::Less => html! { <AttemptRow attempt={ attempts.fields[i].clone() } /> }, html! { <AttemptRow attempt={ attempts.fields[i].clone() } /> }
Ordering::Equal => html! { <CurrentRow text={ current_attempt } /> }, } else if i == *attempt_index {
Ordering::Greater => html! { <EmptyRow /> } html! { <CurrentRow text={ current_attempt } /> }
} else {
html! { <EmptyRow /> }
} }
}) })
} }

View File

@ -1,4 +1,4 @@
use crate::consts::MAX_ATTEMPTS; use crate::consts::{GameResult, MAX_ATTEMPTS};
use yew::prelude::*; use yew::prelude::*;
use crate::consts::{Attempts, Status}; use crate::consts::{Attempts, Status};

View File

@ -1,4 +1,7 @@
use yew::html::IntoPropValue;
pub const MAX_ATTEMPTS: usize = 6; pub const MAX_ATTEMPTS: usize = 6;
pub const WORD_LEN: usize = 5;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub enum KeyboardKeyType { pub enum KeyboardKeyType {

View File

@ -99,13 +99,23 @@ impl UseBoardHandle {
#[hook] #[hook]
pub fn use_board() -> UseBoardHandle { pub fn use_board() -> UseBoardHandle {
let current_attempt = use_state(String::new); let current_attempt = use_state(|| "".to_string());
let attempts: UseStateHandle<Attempts> = use_state(Attempts::new); let attempts: UseStateHandle<Attempts> = use_state(|| Attempts::new());
let attempt_index = use_mut_ref(|| 0usize); let attempt_index = use_mut_ref(|| 0usize);
let answer = use_memo(|_| get_word_of_the_day(), None::<()>); let answer = use_memo(|_| get_word_of_the_day(), None::<()>);
let virtual_keyboard = use_state(|| new_empty_virtual_keyboard().into()); let virtual_keyboard = use_state(|| new_empty_virtual_keyboard().into());
let result = use_state(|| None::<GameResult>); let result = use_state(|| None::<GameResult>);
let send_key = {
let current_attempt = current_attempt.clone();
let current_attempt_len = current_attempt.chars().count();
let attempts = attempts.clone();
let answer = (*answer).clone();
let attempt_index = attempt_index.clone();
let result = result.clone();
move |k: Key| {}
};
UseBoardHandle { UseBoardHandle {
current_attempt, current_attempt,
attempts, attempts,

View File

@ -1,12 +1,13 @@
use yew::prelude::*; use yew::prelude::*;
use crate::components::Board; use crate::components::{AttemptRow, Board};
use crate::components::CurrentRow;
use crate::components::EmptyRow;
use crate::components::Keyboard; use crate::components::Keyboard;
use crate::components::ResultBoard; use crate::components::ResultBoard;
use crate::consts::{Key}; use crate::consts::{Key, MAX_ATTEMPTS};
use crate::hooks::use_board; use crate::hooks::use_board;
#[cfg(debug_assertions)]
use gloo::console; use gloo::console;
mod components; mod components;
@ -25,7 +26,6 @@ fn App() -> Html {
Callback::from(move |event: KeyboardEvent| { Callback::from(move |event: KeyboardEvent| {
let key_code = event.key_code(); let key_code = event.key_code();
board.send_key(Key::from(key_code)); board.send_key(Key::from(key_code));
#[cfg(debug_assertions)]
console::log!(&event); console::log!(&event);
}) })
}; };
@ -52,9 +52,9 @@ fn App() -> Html {
/> />
<div> <div>
<span> { <span> {
match *board.result { match (*board.result).clone() {
Some(_) => html! { <ResultBoard attempts={(*board.attempts).clone()} /> }, Some(res) => html! { <ResultBoard attempts={(*board.attempts).clone()} /> },
None => html! { <Keyboard _ => html! { <Keyboard
keyboard={ (board.virtual_keyboard).clone() } keyboard={ (board.virtual_keyboard).clone() }
onclick={ onclick } /> } onclick={ onclick } /> }
} }

View File

@ -3,12 +3,15 @@ use lazy_static::lazy_static;
lazy_static! { lazy_static! {
pub static ref WORDS: Vec<String> = include_str!("../../words.txt") pub static ref WORDS: Vec<String> = include_str!("../../words.txt")
.trim()
.lines() .lines()
.map(|w| w.trim().into()) .map(|w| w.to_string())
.collect(); .collect();
} }
pub fn get_all_words() -> &'static WORDS {
&WORDS
}
pub fn get_word_of_the_day() -> String { pub fn get_word_of_the_day() -> String {
let date = chrono::Utc::now(); let date = chrono::Utc::now();
let year = date.year() as usize; let year = date.year() as usize;

View File

@ -89,14 +89,12 @@ main {
.keyboard { .keyboard {
display: grid; display: grid;
grid-template-columns: repeat(20, 1fr); grid-template-columns: repeat(20, 1fr);
gap: 2px;
} }
.keyboard > button { .keyboard > button {
grid-column: span 2; grid-column: span 2;
width: auto; width: auto;
height: 60px; height: 60px;
border: 2px solid lightgray;
} }
.keyboard > button.key-big { .keyboard > button.key-big {