Эх сурвалжийг харах

feat(gsm): find game by port

Bryan Lee 1 жил өмнө
parent
commit
1a8b04c590

+ 39 - 0
game-server-manager/src/game/get.rs

@@ -0,0 +1,39 @@
+use crate::game::{GameDescription, GamesData};
+use crate::ServiceKey;
+use actix_web::{get, web, HttpResponse};
+
+#[get("/list/")]
+async fn list(
+    service_key: ServiceKey,
+    games_data: web::Data<GamesData>,
+) -> actix_web::Result<HttpResponse> {
+    service_key.validate()?;
+
+    let games = games_data
+        .games
+        .read()
+        .expect("Failed to get read lock on games");
+
+    Ok(HttpResponse::Ok().json(games.get_all_active_description()))
+}
+
+#[get("/port/{port}/")]
+async fn find_by_port(
+    port: web::Path<u16>,
+    service_key: ServiceKey,
+    games_data: web::Data<GamesData>,
+) -> actix_web::Result<HttpResponse> {
+    service_key.validate()?;
+
+    let games = games_data
+        .games
+        .read()
+        .expect("Failed to get read lock on games");
+
+    let Some(game) = games.find_by_port(port.into_inner()) else {
+        return Ok(HttpResponse::NotFound().finish());
+    };
+
+    let game: GameDescription = game.into();
+    Ok(HttpResponse::Ok().json(game))
+}

+ 0 - 34
game-server-manager/src/game/list.rs

@@ -1,34 +0,0 @@
-use crate::game::GamesData;
-use crate::ServiceKey;
-use actix_web::{get, web, HttpResponse};
-use serde::Serialize;
-
-#[derive(Debug, Clone, Serialize)]
-pub struct GameServerInfo {
-    id: String,
-    name: String,
-    internal_host: String,
-    internal_port: u16,
-    external_port: u16,
-}
-
-#[derive(Debug, Clone, Serialize)]
-struct SpawnResponse {
-    process_id: u32,
-    port: u16,
-}
-
-#[get("/list/")]
-async fn list(
-    service_key: ServiceKey,
-    games_data: web::Data<GamesData>,
-) -> actix_web::Result<HttpResponse> {
-    service_key.validate()?;
-
-    let games = games_data
-        .games
-        .read()
-        .expect("Failed to get read lock on games");
-
-    Ok(HttpResponse::Ok().json(games.get_all_active_description()))
-}

+ 7 - 5
game-server-manager/src/game/mod.rs

@@ -1,5 +1,5 @@
+mod get;
 mod kill;
-mod list;
 mod spawn;
 
 use actix_web::web;
@@ -9,7 +9,9 @@ use std::sync::RwLock;
 use subprocess::Popen;
 
 pub fn config_service(cfg: &mut web::ServiceConfig) {
-    cfg.service(list::list).service(spawn::spawn);
+    cfg.service(get::list)
+        .service(get::find_by_port)
+        .service(spawn::spawn);
 }
 
 pub struct GamesData {
@@ -42,7 +44,7 @@ impl Games {
 }
 
 impl Games {
-    pub fn get_for_port<'a>(&'a self, port: u16) -> Option<&'a Game> {
+    pub fn find_by_port(&self, port: u16) -> Option<&Game> {
         let game: Option<&Option<Game>> = self.0.get((port - BASE_PORT) as usize);
         let Some(game) = game else {
             return None;
@@ -54,7 +56,7 @@ impl Games {
         self.0.iter().filter(|p| p.is_some()).count()
     }
 
-    pub fn get_all_active<'a>(&'a self) -> Vec<&'a Game> {
+    pub fn get_all_active(&self) -> Vec<&Game> {
         self.0.iter().filter_map(|p| p.into()).collect()
     }
 
@@ -78,7 +80,7 @@ impl Games {
         Some(idx + BASE_PORT)
     }
 
-    pub fn get_available_entry<'a>(&'a mut self) -> Option<(u16, &'a mut Option<Game>)> {
+    pub fn get_available_entry(&mut self) -> Option<(u16, &mut Option<Game>)> {
         let idx = self.0.iter().position(|p| p.is_none());
         let Some(idx) = idx else {
             return None;