Răsfoiți Sursa

fix(gsm): replace killed games with None

Bryan Lee 1 an în urmă
părinte
comite
8d28ac62c6
2 a modificat fișierele cu 15 adăugiri și 17 ștergeri
  1. 9 5
      game-server-manager/src/game/kill.rs
  2. 6 12
      game-server-manager/src/game/mod.rs

+ 9 - 5
game-server-manager/src/game/kill.rs

@@ -15,13 +15,17 @@ async fn kill(
         .write()
         .expect("Failed to get write lock on games");
 
-    let Some(game) = games.find_mut_by_port(port.into_inner()) else {
-        return Ok(HttpResponse::NotFound().finish());
+    let game = games.find_mut_by_port(port.into_inner());
+
+    match game {
+        Some(game) => game
+            .process
+            .terminate()
+            .map_err(error::ErrorInternalServerError)?,
+        None => return Ok(HttpResponse::NotFound().finish()),
     };
 
-    game.process
-        .terminate()
-        .map_err(error::ErrorInternalServerError)?;
+    _ = std::mem::replace(game, None);
 
     Ok(HttpResponse::Ok().finish())
 }

+ 6 - 12
game-server-manager/src/game/mod.rs

@@ -49,22 +49,16 @@ impl Games {
         game.into()
     }
 
-    pub fn find_mut_by_port(&mut self, port: u16) -> Option<&mut Game> {
-        let game: Option<&mut Option<Game>> = self.0.get_mut((port - BASE_PORT) as usize);
-        let Some(game) = game else {
-            return None;
-        };
-        game.into()
+    pub fn find_mut_by_port(&mut self, port: u16) -> &mut Option<Game> {
+        self.0
+            .get_mut((port - BASE_PORT) as usize)
+            .expect("Failed to get mut game reference")
     }
 
     pub fn get_active_count(&self) -> usize {
         self.0.iter().filter(|p| p.is_some()).count()
     }
 
-    pub fn get_all_active(&self) -> Vec<&Game> {
-        self.0.iter().filter_map(|p| p.into()).collect()
-    }
-
     pub fn get_all_active_description(&self) -> Vec<GameDescription> {
         self.0
             .iter()
@@ -81,9 +75,9 @@ impl Games {
         let Some(idx) = idx else {
             return None;
         };
-        let game_process = self.0.get_mut(idx).unwrap();
+        let game = self.0.get_mut(idx).unwrap();
         let idx: u16 = idx.try_into().unwrap();
-        Some((idx + BASE_PORT, game_process))
+        Some((idx + BASE_PORT, game))
     }
 }