Quellcode durchsuchen

improving data flow

Nicolas Winkler vor 4 Jahren
Ursprung
Commit
b859f4c810
4 geänderte Dateien mit 60 neuen und 13 gelöschten Zeilen
  1. 24 0
      src/datasource.rs
  2. 16 7
      src/game.rs
  3. 1 0
      src/main.rs
  4. 19 6
      src/server.rs

+ 24 - 0
src/datasource.rs

@@ -0,0 +1,24 @@
+
+
+pub trait DataSource<T> {
+    fn get_ith<'a>(&'a self, i: usize) -> Option<&'a T>;
+}
+
+
+pub struct ArraySource<T> {
+    words: Vec<T>
+}
+
+impl<T> DataSource<T> for ArraySource<T> {
+    fn get_ith<'a>(&'a self, i: usize) -> Option<&'a T> {
+        self.words.get(i)
+    }
+}
+
+impl<T> ArraySource<T> {
+    pub fn create(words: Vec<T>) -> Self {
+        ArraySource {
+            words
+        }
+    }
+}

+ 16 - 7
src/game.rs

@@ -28,7 +28,7 @@ pub struct Player {
 pub struct Game {
     pub players: Vec<Player>,
     pub round_number: i32,
-    pub additional_questions: Vec<CreatingEx>,
+    pub additional_questions: Vec<String>,
 
     pub state: GameState,
 }
@@ -121,20 +121,29 @@ impl Game {
         }
     }
 
-    pub fn start_round(&mut self) {
+    pub fn start_round<F>(&mut self, mut word_generator: F)
+            where F: FnMut() -> String {
         self.clear_submissions();
 
-        let mut exes = vec![
+        /*let mut exes = vec![
             CreatingEx{ question: "Delikatessfutter für Hunde".to_owned(), letters: vec!['a', 'b', 'c'] },
             CreatingEx{ question: "Ein Hotel für die ganze Familie".to_owned(), letters: vec!['b', 'c', 'd'] },
             CreatingEx{ question: "Brasilianischer Superstar".to_owned(), letters: vec!['c', 'd', 'e'] },
             CreatingEx{ question: "Buchstabe des griechischen Alphabets".to_owned(), letters: vec!['d', 'e', 'f'] },
-        ];
+        ];*/
         for p in &mut self.players {
-            p.creating_exercise = exes.pop();
+            p.creating_exercise = Some(
+                CreatingEx{
+                    question: word_generator(),
+                    letters: vec!['a', 'b']
+                }
+            );
         }
         self.additional_questions.clear();
-        self.additional_questions.append(&mut exes);
+
+        self.additional_questions.push(word_generator());
+        self.additional_questions.push(word_generator());
+
         self.state = GameState::Creating;
     }
 
@@ -161,7 +170,7 @@ impl Game {
                         .map(|p| p.creating_exercise.clone().unwrap().question)
                         .collect::<Vec<_>>();
         questions.append(&mut self.additional_questions.iter()
-                                .map(|x| x.question.clone())
+                                .map(|x| x.clone())
                                 .collect::<Vec<_>>()
                         );
         for player in &self.players {

+ 1 - 0
src/main.rs

@@ -7,6 +7,7 @@ use actix_web::{middleware, web, App, HttpServer};
 mod websocket;
 mod game;
 mod server;
+mod datasource;
 //use crate::websocket::ws_initiate;
 
 

+ 19 - 6
src/server.rs

@@ -11,6 +11,7 @@ use rand::seq::SliceRandom;
 use crate::game;
 use crate::websocket;
 use crate::websocket::*;
+use crate::datasource;
 
 const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);
@@ -145,7 +146,8 @@ pub struct GameLobby {
     game: game::Game,
     waiting_players: BTreeMap<String, Addr<GameConnection>>,
     ready_players: Vec<String>,
-    lobby_state: LobbyState
+    lobby_state: LobbyState,
+    data_source: Box<dyn datasource::DataSource<String>>,
 }
 
 impl Actor for GameLobby {
@@ -228,10 +230,17 @@ impl GameLobby {
             waiting_players: BTreeMap::new(),
             ready_players: Vec::new(),
             lobby_state: LobbyState::Starting,
+            data_source: Box::new(datasource::ArraySource::create(vec![
+                "Delikatessfutter für Hunde".to_owned(),
+                "Ein Hotel für die ganze Familie".to_owned(),
+                "Brasilianischer Superstar".to_owned(),
+                "Buchstabe des griechischen Alphabets".to_owned(),
+            ]))
         }
     }
 
     fn set_state(&mut self, new_state: LobbyState) {
+
         match new_state {
             LobbyState::Starting => {
                 for (nick, _addr) in &self.waiting_players {
@@ -241,7 +250,12 @@ impl GameLobby {
                 self.ready_players.clear();
             },
             LobbyState::Creating => {
-                self.game.start_round();
+                let ds = &self.data_source;
+                let mut index = 0;
+                self.game.start_round(|| {
+                    index += 1;
+                    ds.get_ith(index - 1).unwrap().clone()
+                });
             },
             _ => {}
         }
@@ -262,9 +276,8 @@ impl GameLobby {
                         .map(|p| p.submitted_word.clone().unwrap())
                         .collect::<Vec<_>>();
         let questions = self.game.players.iter()
-                        .map(|x| x.creating_exercise.as_ref().unwrap())
-                        .chain(self.game.additional_questions.iter())
-                        .map(|x| x.question.clone())
+                        .map(|x| x.creating_exercise.as_ref().unwrap().question.clone())
+                        .chain(self.game.additional_questions.clone().into_iter())
                         .collect::<Vec<_>>();
         let solutions = words.iter()
                         .map(|x| x.clone())
@@ -331,7 +344,7 @@ impl GameLobby {
             .map(|p| p.creating_exercise.clone().unwrap().question)
             .collect::<Vec<_>>();
         questions.append(&mut self.game.additional_questions.iter()
-                            .map(|x| x.question.clone())
+                            .map(|x| x.clone())
                             .collect::<Vec<_>>()
                         );