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

View File

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

View File

@ -99,13 +99,23 @@ impl UseBoardHandle {
#[hook]
pub fn use_board() -> UseBoardHandle {
let current_attempt = use_state(String::new);
let attempts: UseStateHandle<Attempts> = use_state(Attempts::new);
let current_attempt = use_state(|| "".to_string());
let attempts: UseStateHandle<Attempts> = use_state(|| Attempts::new());
let attempt_index = use_mut_ref(|| 0usize);
let answer = use_memo(|_| get_word_of_the_day(), None::<()>);
let virtual_keyboard = use_state(|| new_empty_virtual_keyboard().into());
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 {
current_attempt,
attempts,

View File

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

View File

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

View File

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