Merge pull request #32 from itsscb/feature
feat(frontend): improves current_status chore: updates nix flake
This commit is contained in:
commit
540b86ba1f
12
flake.lock
generated
12
flake.lock
generated
@ -20,11 +20,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1724748588,
|
"lastModified": 1725534445,
|
||||||
"narHash": "sha256-NlpGA4+AIf1dKNq76ps90rxowlFXUsV9x7vK/mN37JM=",
|
"narHash": "sha256-Yd0FK9SkWy+ZPuNqUgmVPXokxDgMJoGuNpMEtkfcf84=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "a6292e34000dc93d43bccf78338770c1c5ec8a99",
|
"rev": "9bb1e7571aadf31ddb4af77fc64b2d59580f9a39",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@ -62,11 +62,11 @@
|
|||||||
"nixpkgs": "nixpkgs_2"
|
"nixpkgs": "nixpkgs_2"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1724811750,
|
"lastModified": 1725589472,
|
||||||
"narHash": "sha256-PvhVgQ1rm3gfhK7ts4emprhh/KMkFwXogmgsQ3srR7g=",
|
"narHash": "sha256-+OB00N6Yql/ZRQQkQ0PNnxfW2tH89DHnv29hBS7tXMM=",
|
||||||
"owner": "oxalica",
|
"owner": "oxalica",
|
||||||
"repo": "rust-overlay",
|
"repo": "rust-overlay",
|
||||||
"rev": "6a1c4915dca7149e7258d8c7f3ac634d8c65f6c6",
|
"rev": "2b00881d2ff72174cffdc007238cb6bedd6e1d8e",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@ -1,124 +1,122 @@
|
|||||||
// use rand::seq::SliceRandom;
|
// use rand::seq::SliceRandom;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::CharStatus;
|
use crate::CharStatus;
|
||||||
|
|
||||||
const MAX_TRIES: usize = 5;
|
const MAX_TRIES: usize = 5;
|
||||||
|
|
||||||
// #[derive(Debug, Serialize, Deserialize, Clone)]
|
// #[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
// struct Games(Vec<Game>);
|
// struct Games(Vec<Game>);
|
||||||
|
|
||||||
// impl Games {
|
// impl Games {
|
||||||
// pub const fn new() -> Self {
|
// pub const fn new() -> Self {
|
||||||
// Self(Vec::new())
|
// Self(Vec::new())
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// pub fn new_game(&mut self, word: String) {
|
// pub fn new_game(&mut self, word: String) {
|
||||||
// let game = Game::new();
|
// let game = Game::new();
|
||||||
// self.0.push(game);
|
// self.0.push(game);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// pub fn current_game(&self) -> Option<&Game> {
|
// pub fn current_game(&self) -> Option<&Game> {
|
||||||
// self.0.last()
|
// self.0.last()
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct WordList {
|
pub struct WordList {
|
||||||
words: Vec<String>,
|
words: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WordList {
|
impl WordList {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self { words: Vec::new() }
|
Self { words: Vec::new() }
|
||||||
}
|
}
|
||||||
pub fn from_json(s: &str) -> Self {
|
pub fn from_json(s: &str) -> Self {
|
||||||
serde_json::from_str(s).map_or(Self::new(), |w| w)
|
serde_json::from_str(s).map_or(Self::new(), |w| w)
|
||||||
}
|
}
|
||||||
// pub fn to_json(&self) -> String {
|
// pub fn to_json(&self) -> String {
|
||||||
// serde_json::to_string_pretty(self).map_or(String::new(), |w| w)
|
// serde_json::to_string_pretty(self).map_or(String::new(), |w| w)
|
||||||
// }
|
// }
|
||||||
// pub fn get_word(&self) -> String {
|
// pub fn get_word(&self) -> String {
|
||||||
// let mut rng = rand::thread_rng();
|
// let mut rng = rand::thread_rng();
|
||||||
// self.words
|
// self.words
|
||||||
// .choose(&mut rng)
|
// .choose(&mut rng)
|
||||||
// .map_or_else(String::new, |w| (*w).to_string())
|
// .map_or_else(String::new, |w| (*w).to_string())
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
pub word: Option<String>,
|
pub word: Option<String>,
|
||||||
pub submitted_words: Vec<Vec<CharStatus<String>>>,
|
pub submitted_words: Vec<Vec<CharStatus<String>>>,
|
||||||
tries: usize,
|
tries: usize,
|
||||||
status: Status,
|
status: Status,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Game {
|
impl Game {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
word: None,
|
word: None,
|
||||||
tries: 0,
|
tries: 0,
|
||||||
submitted_words: Vec::new(),
|
submitted_words: Vec::new(),
|
||||||
status: Status::New,
|
status: Status::New,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&mut self, word: String) {
|
pub fn start(&mut self, word: String) {
|
||||||
if self.word.is_none() && self.status == Status::New {
|
if self.word.is_none() && self.status == Status::New {
|
||||||
self.status = Status::InProgress;
|
self.status = Status::InProgress;
|
||||||
self.word = Some(word);
|
self.word = Some(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn submit_answer(&mut self, answer: &[String]) {
|
pub fn submit_answer(&mut self, answer: &[String]) {
|
||||||
if let Some(ref word) = self.word {
|
if let Some(ref word) = self.word {
|
||||||
let res = crate::compare_strings(word, &answer.join(""));
|
let res = crate::compare_strings(word, &answer.join(""));
|
||||||
self.submitted_words.push(res);
|
self.submitted_words.push(res);
|
||||||
self.tries += 1;
|
self.tries += 1;
|
||||||
self.status = self.current_status();
|
self.status = self.current_status();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_status(&self) -> Status {
|
pub fn current_status(&self) -> Status {
|
||||||
self.word.as_ref().map_or(Status::New, |_| {
|
self.word.as_ref().map_or(Status::New, |_| {
|
||||||
let word_count = self.submitted_words.len();
|
let word_count = self.submitted_words.len();
|
||||||
if self.tries == 0 {
|
match self.tries {
|
||||||
Status::New
|
0 => Status::New,
|
||||||
} else if self.tries < MAX_TRIES {
|
1..MAX_TRIES => self
|
||||||
if self
|
.submitted_words
|
||||||
.submitted_words
|
.last()
|
||||||
.last()
|
.map_or(Status::InProgress, |words| {
|
||||||
.unwrap()
|
if words.iter().all(|v| matches!(v, CharStatus::Match(_))) {
|
||||||
.iter()
|
Status::Win(word_count)
|
||||||
.all(|v| matches!(v, CharStatus::Match(_)))
|
} else {
|
||||||
{
|
Status::InProgress
|
||||||
Status::Win(word_count)
|
}
|
||||||
} else {
|
}),
|
||||||
Status::InProgress
|
_ => self
|
||||||
}
|
.submitted_words
|
||||||
} else if self
|
.last()
|
||||||
.submitted_words
|
.map_or(Status::Lose(word_count), |words| {
|
||||||
.last()
|
if words.iter().all(|v| matches!(v, CharStatus::Match(_))) {
|
||||||
.unwrap()
|
Status::Win(word_count)
|
||||||
.iter()
|
} else {
|
||||||
.all(|v| matches!(v, CharStatus::Match(_)))
|
Status::Lose(word_count)
|
||||||
{
|
}
|
||||||
Status::Win(word_count)
|
}),
|
||||||
} else {
|
}
|
||||||
Status::Lose(word_count)
|
})
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
|
||||||
}
|
type Tries = usize;
|
||||||
|
|
||||||
type Tries = usize;
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
|
#[allow(clippy::module_name_repetitions)]
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
pub enum Status {
|
||||||
#[allow(clippy::module_name_repetitions)]
|
New,
|
||||||
pub enum Status {
|
Win(Tries),
|
||||||
New,
|
Lose(Tries),
|
||||||
Win(Tries),
|
InProgress,
|
||||||
Lose(Tries),
|
}
|
||||||
InProgress,
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user