Add version check system
This commit is contained in:
		@@ -26,6 +26,7 @@ uuid = { version = "1.1.2", features = ["v4"] }
 | 
			
		||||
rand = "0.8.5"
 | 
			
		||||
serde_with = "2.0.1"
 | 
			
		||||
tokio = { version = "1", features = ["full"] }
 | 
			
		||||
semver = "1.0.14"
 | 
			
		||||
 | 
			
		||||
[dev-dependencies]
 | 
			
		||||
#reqwest = { version = "0.11.11", default-features = false, features = ["json", "rustls-tls"] }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,8 @@
 | 
			
		||||
//! # Project constants
 | 
			
		||||
 | 
			
		||||
pub const MIN_REQUIRED_VERSION: &str = "0.1.0";
 | 
			
		||||
pub const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
 | 
			
		||||
 | 
			
		||||
pub const MIN_BOATS_NUMBER: usize = 1;
 | 
			
		||||
pub const MAX_BOATS_NUMBER: usize = 10;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ pub use game_map::*;
 | 
			
		||||
pub use game_rules::*;
 | 
			
		||||
pub use play_config::*;
 | 
			
		||||
pub use printable_map::*;
 | 
			
		||||
pub use version::*;
 | 
			
		||||
 | 
			
		||||
mod boats_layout;
 | 
			
		||||
mod current_game_status;
 | 
			
		||||
@@ -11,3 +12,4 @@ mod game_map;
 | 
			
		||||
mod game_rules;
 | 
			
		||||
mod play_config;
 | 
			
		||||
mod printable_map;
 | 
			
		||||
mod version;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								rust/sea_battle_backend/src/data/version.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								rust/sea_battle_backend/src/data/version.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
//! # Version Info
 | 
			
		||||
//!
 | 
			
		||||
//! Contains server version requirements information
 | 
			
		||||
 | 
			
		||||
use crate::consts;
 | 
			
		||||
use crate::utils::res_utils::Res;
 | 
			
		||||
use semver::Version;
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct VersionInfo {
 | 
			
		||||
    current: String,
 | 
			
		||||
    min_required: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl VersionInfo {
 | 
			
		||||
    pub fn load_static() -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            current: consts::CURRENT_VERSION.to_string(),
 | 
			
		||||
            min_required: consts::MIN_REQUIRED_VERSION.to_string(),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// Check if builtin version is compatible with a remote version or not
 | 
			
		||||
    pub fn is_compatible_with_static_version(&self) -> Res<bool> {
 | 
			
		||||
        let static_version = Self::load_static();
 | 
			
		||||
 | 
			
		||||
        let local_current = Version::parse(&static_version.current)?;
 | 
			
		||||
        let min_required = Version::parse(&self.min_required)?;
 | 
			
		||||
 | 
			
		||||
        Ok(min_required <= local_current)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@ use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
 | 
			
		||||
use crate::args::Args;
 | 
			
		||||
use crate::data::{GameRules, PlayConfiguration};
 | 
			
		||||
use crate::data::{GameRules, PlayConfiguration, VersionInfo};
 | 
			
		||||
use crate::dispatcher_actor::DispatcherActor;
 | 
			
		||||
use crate::human_player_ws::{HumanPlayerWS, StartMode};
 | 
			
		||||
 | 
			
		||||
@@ -18,7 +18,12 @@ async fn not_found() -> impl Responder {
 | 
			
		||||
    HttpResponse::NotFound().json("You missed your strike lol")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get bot configuration
 | 
			
		||||
/// Get version information
 | 
			
		||||
async fn version_information() -> impl Responder {
 | 
			
		||||
    HttpResponse::Ok().json(VersionInfo::load_static())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get game configuration
 | 
			
		||||
async fn game_configuration() -> impl Responder {
 | 
			
		||||
    HttpResponse::Ok().json(PlayConfiguration::default())
 | 
			
		||||
}
 | 
			
		||||
@@ -148,6 +153,7 @@ pub async fn start_server(args: Args) -> std::io::Result<()> {
 | 
			
		||||
        App::new()
 | 
			
		||||
            .app_data(web::Data::new(dispatcher_actor.clone()))
 | 
			
		||||
            .wrap(cors)
 | 
			
		||||
            .route("/version", web::get().to(version_information))
 | 
			
		||||
            .route("/config", web::get().to(game_configuration))
 | 
			
		||||
            .route("/play/bot", web::get().to(start_bot_play))
 | 
			
		||||
            .route("/play/create_invite", web::get().to(start_create_invite))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user