feat: adds game struct

This commit is contained in:
itsscb 2024-08-22 15:15:40 +02:00
parent 5a7d66b52b
commit 8fb11f9bca

View File

@ -1,29 +1,30 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::CharStatus; use crate::CharStatus;
#[derive(Debug,Serialize, Deserialize, Clone)] #[derive(Debug,Serialize, Deserialize, Clone)]
struct Game { struct Game {
word: String, word: String,
submitted_words: Vec<Vec<CharStatus<String>>>, submitted_words: Vec<Vec<CharStatus<String>>>,
result: GameResult, result: GameResult,
} }
impl Game { impl Game {
pub fn new(word: String, submitted_words: Vec<Vec<CharStatus<String>>>) -> Self { #[allow(dead_code)]
let result = submitted_words.clone().into_iter().last().map_or(GameResult::Lose, |w| if w.iter().all(|v| matches!(v, CharStatus::Match(_))) { pub fn new(word: String, submitted_words: Vec<Vec<CharStatus<String>>>) -> Self {
GameResult::Win let result = submitted_words.clone().into_iter().last().map_or(GameResult::Lose, |w| if w.iter().all(|v| matches!(v, CharStatus::Match(_))) {
} else { GameResult::Win
GameResult::Lose } else {
}); GameResult::Lose
});
Self { word, submitted_words, result }
} Self { word, submitted_words, result }
} }
}
#[derive(Debug,Serialize, Deserialize, Clone, PartialEq, Eq)]
enum GameResult { #[derive(Debug,Serialize, Deserialize, Clone, PartialEq, Eq)]
Win, enum GameResult {
Lose, Win,
Lose,
} }