Add unproven letters

main
kirbylife 2024-09-04 23:12:11 -06:00
parent 1304fab237
commit cd05510570
5 changed files with 39 additions and 8 deletions

View File

@ -1,4 +1,5 @@
use crate::attempt::Attempt; use crate::attempt::Attempt;
use crate::misc::generate_valid_letters;
use crate::status::Status; use crate::status::Status;
use crate::words::get_word_of_the_day; use crate::words::get_word_of_the_day;
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
@ -30,6 +31,7 @@ pub struct Game {
pub board: Board, pub board: Board,
pub status: GameStatus, pub status: GameStatus,
pub last_insertion_status: InsertionStatus, pub last_insertion_status: InsertionStatus,
pub unproven_letters: HashSet<char>,
} }
impl Game { impl Game {
@ -41,16 +43,13 @@ impl Game {
board: Default::default(), board: Default::default(),
status: GameStatus::Playing, status: GameStatus::Playing,
last_insertion_status: InsertionStatus::Ok, last_insertion_status: InsertionStatus::Ok,
unproven_letters: generate_valid_letters(),
} }
} }
pub fn add_attempt(&mut self, word: &String) -> InsertionStatus { pub fn add_attempt(&mut self, word: &String) -> InsertionStatus {
let word = word.to_uppercase(); let word = word.to_uppercase();
let valid_letters = { let valid_letters = generate_valid_letters();
let mut output: HashSet<char> = ('A'..='Z').collect();
output.insert('Ñ');
output
};
let win = word.eq(&self.answer); let win = word.eq(&self.answer);
let has_valid_chars = word let has_valid_chars = word
@ -66,6 +65,11 @@ impl Game {
) { ) {
(0..=5, 5, &GameStatus::Playing, true) => { (0..=5, 5, &GameStatus::Playing, true) => {
let new_attempt = Attempt::from_string(&word, &self.answer); let new_attempt = Attempt::from_string(&word, &self.answer);
for attempt_letter in &new_attempt.chars {
if attempt_letter.status == Status::NotFound {
self.unproven_letters.remove(&attempt_letter.char);
}
}
self.board[self.attempt_index] = Some(new_attempt); self.board[self.attempt_index] = Some(new_attempt);
self.attempt_index += 1; self.attempt_index += 1;

View File

@ -1,12 +1,15 @@
mod attempt; mod attempt;
mod gameslot; mod gameslot;
mod misc;
mod status; mod status;
mod words; mod words;
use crate::attempt::Attempt; use std::collections::HashSet;
use crate::gameslot::{Board, Game}; use crate::gameslot::{Board, Game};
use crate::words::get_word_of_the_day;
use gameslot::{GameStatus, InsertionStatus, SlotManager}; use gameslot::{GameStatus, InsertionStatus, SlotManager};
use misc::generate_valid_letters;
use misc::sort_hashset;
use tera::Tera; use tera::Tera;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio::sync::OnceCell; use tokio::sync::OnceCell;
@ -94,10 +97,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let board: Board; let board: Board;
let error: Option<String>; let error: Option<String>;
let result_pattern: Option<String>; let result_pattern: Option<String>;
let unproven_letters: HashSet<char>;
match Uuid::parse_str(possible_uuid) { match Uuid::parse_str(possible_uuid) {
Ok(uuid) => match get_game(uuid).await { Ok(uuid) => match get_game(uuid).await {
Ok(game) => { Ok(game) => {
unproven_letters = game.unproven_letters;
board = game.board; board = game.board;
match game.status { match game.status {
GameStatus::Playing => { GameStatus::Playing => {
@ -128,6 +133,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
Err(_) => { Err(_) => {
board = Default::default(); board = Default::default();
unproven_letters = generate_valid_letters();
error = Some( error = Some(
"La partida a la que intentas acceder ya no existe o nunca existió" "La partida a la que intentas acceder ya no existe o nunca existió"
.to_string(), .to_string(),
@ -137,14 +143,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}, },
Err(_) => { Err(_) => {
board = Default::default(); board = Default::default();
unproven_letters = generate_valid_letters();
error = Some("Identificador incorrecto".to_string()); error = Some("Identificador incorrecto".to_string());
result_pattern = None; result_pattern = None;
} }
} }
let unproven_letters_sorted = sort_hashset(unproven_letters);
context.insert("board", &board); context.insert("board", &board);
context.insert("error", &error); context.insert("error", &error);
context.insert("result_pattern", &result_pattern); context.insert("result_pattern", &result_pattern);
context.insert("unproven_letters", &unproven_letters_sorted);
windmark::response::Response::success(render("index", &context).await.unwrap()) windmark::response::Response::success(render("index", &context).await.unwrap())
}) })
.mount("/:uuid/:attempt", move |request| async move { .mount("/:uuid/:attempt", move |request| async move {
@ -172,6 +182,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
context.insert("board", &board); context.insert("board", &board);
context.insert("error", &error); context.insert("error", &error);
context.insert("result_pattern", &result_pattern); context.insert("result_pattern", &result_pattern);
context.insert("unproven_letters", &sort_hashset(generate_valid_letters()));
windmark::response::Response::success(render("index", &context).await.unwrap()) windmark::response::Response::success(render("index", &context).await.unwrap())
} }
}) })

13
src/misc.rs 100644
View File

@ -0,0 +1,13 @@
use std::collections::HashSet;
pub fn generate_valid_letters() -> HashSet<char> {
let mut output: HashSet<char> = ('A'..='Z').collect();
output.insert('Ñ');
output
}
pub fn sort_hashset<T: Ord>(hashset: HashSet<T>) -> Vec<T> {
let mut output = hashset.into_iter().collect::<Vec<_>>();
output.sort();
output
}

View File

@ -1,6 +1,6 @@
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
#[derive(Clone)] #[derive(Clone, PartialEq)]
pub enum Status { pub enum Status {
NotFound, NotFound,
Found, Found,

View File

@ -23,6 +23,9 @@
{% if result_pattern -%} {% if result_pattern -%}
{{ result_pattern }} {{ result_pattern }}
{% else -%}
{{unproven_letters | join(sep=", ")}}
{% endif -%} {% endif -%}
## Indicaciones: ## Indicaciones: