|
@@ -1,23 +1,22 @@
|
|
-use std::time::{Duration, Instant};
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
use serde::{Serialize, Deserialize};
|
|
-use actix::{Actor, Addr, Context};
|
|
|
|
|
|
+use std::collections::BTreeMap;
|
|
use crate::websocket;
|
|
use crate::websocket;
|
|
use crate::server;
|
|
use crate::server;
|
|
|
|
|
|
-#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
|
|
|
|
|
+#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
|
|
pub enum GameState {
|
|
pub enum GameState {
|
|
Starting,
|
|
Starting,
|
|
Creating,
|
|
Creating,
|
|
Guessing
|
|
Guessing
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
|
|
+#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
pub struct CreatingEx {
|
|
pub struct CreatingEx {
|
|
pub question: String,
|
|
pub question: String,
|
|
pub letters: Vec<char>
|
|
pub letters: Vec<char>
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Serialize, Clone)]
|
|
|
|
|
|
+#[derive(Serialize, Clone, Debug)]
|
|
pub struct Player {
|
|
pub struct Player {
|
|
pub nick: String,
|
|
pub nick: String,
|
|
pub creating_exercise: Option<CreatingEx>,
|
|
pub creating_exercise: Option<CreatingEx>,
|
|
@@ -25,7 +24,7 @@ pub struct Player {
|
|
pub submitted_guess: Option<Vec<(String, String)>>
|
|
pub submitted_guess: Option<Vec<(String, String)>>
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Serialize, Clone)]
|
|
|
|
|
|
+#[derive(Serialize, Clone, Debug)]
|
|
pub struct Game {
|
|
pub struct Game {
|
|
pub players: Vec<Player>,
|
|
pub players: Vec<Player>,
|
|
pub round_number: i32,
|
|
pub round_number: i32,
|
|
@@ -49,19 +48,42 @@ impl Game {
|
|
self.players.iter_mut().find(|x| x.nick == nick)
|
|
self.players.iter_mut().find(|x| x.nick == nick)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ pub fn check_letters(word: &str, letters: &Vec<char>) -> bool {
|
|
|
|
+ let mut countmap: BTreeMap<char, isize> = BTreeMap::new();
|
|
|
|
+ for c in letters {
|
|
|
|
+ countmap.insert(*c, countmap.get(c).unwrap_or(&0) + 1);
|
|
|
|
+ }
|
|
|
|
+ for c in word.chars() {
|
|
|
|
+ let count = countmap.get(&c);
|
|
|
|
+ if let Some(&v) = count {
|
|
|
|
+ if v < 0 {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ countmap.insert(c, v - 1);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ true
|
|
|
|
+ }
|
|
|
|
+
|
|
pub fn submit_creation(&mut self, player_nick: &str, word: String) -> bool {
|
|
pub fn submit_creation(&mut self, player_nick: &str, word: String) -> bool {
|
|
match self.state {
|
|
match self.state {
|
|
GameState::Creating => {
|
|
GameState::Creating => {
|
|
if let Some(player) = self.get_player(player_nick) {
|
|
if let Some(player) = self.get_player(player_nick) {
|
|
- player.submitted_word = Some(word);
|
|
|
|
- true
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- false
|
|
|
|
|
|
+ if let Some(ex) = &player.creating_exercise {
|
|
|
|
+ if Self::check_letters(&word, &ex.letters) {
|
|
|
|
+ player.submitted_word = Some(word);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
},
|
|
},
|
|
- _ => false
|
|
|
|
|
|
+ _ => {}
|
|
}
|
|
}
|
|
|
|
+ false
|
|
}
|
|
}
|
|
|
|
|
|
pub fn submit_guess(&mut self, player_nick: &str, guess: Vec<(String, String)>) -> bool {
|
|
pub fn submit_guess(&mut self, player_nick: &str, guess: Vec<(String, String)>) -> bool {
|
|
@@ -103,7 +125,7 @@ impl Game {
|
|
self.clear_submissions();
|
|
self.clear_submissions();
|
|
|
|
|
|
let mut exes = vec![
|
|
let mut exes = vec![
|
|
- CreatingEx{ question: "An Alphabet".to_owned(), letters: vec!['a', 'b', 'c'] },
|
|
|
|
|
|
+ CreatingEx{ question: "Delikatessfutter für Hunde".to_owned(), letters: vec!['a', 'b', 'c'] },
|
|
CreatingEx{ question: "A Betabet".to_owned(), letters: vec!['b', 'c', 'd'] },
|
|
CreatingEx{ question: "A Betabet".to_owned(), letters: vec!['b', 'c', 'd'] },
|
|
CreatingEx{ question: "A Gammaabet".to_owned(), letters: vec!['c', 'd', 'e'] },
|
|
CreatingEx{ question: "A Gammaabet".to_owned(), letters: vec!['c', 'd', 'e'] },
|
|
CreatingEx{ question: "A Deltaabet".to_owned(), letters: vec!['d', 'e', 'f'] },
|
|
CreatingEx{ question: "A Deltaabet".to_owned(), letters: vec!['d', 'e', 'f'] },
|