Add version check system
This commit is contained in:
parent
915426849b
commit
eea2ecbf63
78
rust/Cargo.lock
generated
78
rust/Cargo.lock
generated
@ -940,6 +940,7 @@ dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"h2",
|
||||
"http",
|
||||
"http-body",
|
||||
"httparse",
|
||||
@ -1019,6 +1020,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnet"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.4"
|
||||
@ -1421,6 +1428,45 @@ version = "0.6.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.11.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "431949c384f4e2ae07605ccaa56d1d9d2ecdb5cadd4f9577ccfab29f2e5149fc"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"h2",
|
||||
"http",
|
||||
"http-body",
|
||||
"hyper",
|
||||
"hyper-rustls",
|
||||
"ipnet",
|
||||
"js-sys",
|
||||
"log",
|
||||
"mime",
|
||||
"once_cell",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"rustls",
|
||||
"rustls-pemfile",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tower-service",
|
||||
"url",
|
||||
"wasm-bindgen",
|
||||
"wasm-bindgen-futures",
|
||||
"web-sys",
|
||||
"webpki-roots",
|
||||
"winreg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ring"
|
||||
version = "0.16.20"
|
||||
@ -1530,6 +1576,7 @@ dependencies = [
|
||||
"futures",
|
||||
"log",
|
||||
"rand",
|
||||
"semver",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
@ -1554,6 +1601,7 @@ dependencies = [
|
||||
"num",
|
||||
"num-derive",
|
||||
"num-traits",
|
||||
"reqwest",
|
||||
"rustls",
|
||||
"sea_battle_backend",
|
||||
"serde_json",
|
||||
@ -2111,6 +2159,18 @@ dependencies = [
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.83"
|
||||
@ -2160,6 +2220,15 @@ dependencies = [
|
||||
"untrusted",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "webpki-roots"
|
||||
version = "0.22.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "368bfe657969fb01238bb756d351dcade285e0f6fcbd36dcb23359a5169975be"
|
||||
dependencies = [
|
||||
"webpki",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
@ -2234,6 +2303,15 @@ version = "0.36.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zstd"
|
||||
version = "0.11.2+zstd.1.5.2"
|
||||
|
@ -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))
|
||||
|
@ -31,3 +31,4 @@ serde_json = "1.0.85"
|
||||
hostname = "0.3.1"
|
||||
rustls = "0.20.6"
|
||||
hyper-rustls = { version = "0.23.0", features = ["rustls-native-certs"] }
|
||||
reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features = false }
|
@ -20,7 +20,7 @@ pub struct CliArgs {
|
||||
value_parser,
|
||||
default_value = "https://seabattleapi.communiquons.org"
|
||||
)]
|
||||
pub remote_server_uri: String,
|
||||
pub remote_server: String,
|
||||
|
||||
/// Local server listen address
|
||||
#[clap(short, long, default_value = "127.0.0.1:5679")]
|
||||
|
@ -3,12 +3,13 @@ use crate::server;
|
||||
use futures::stream::{SplitSink, SplitStream};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use hyper_rustls::ConfigBuilderExt;
|
||||
use sea_battle_backend::data::GameRules;
|
||||
use sea_battle_backend::data::*;
|
||||
use sea_battle_backend::human_player_ws::{ClientMessage, ServerMessage};
|
||||
use sea_battle_backend::server::{
|
||||
AcceptInviteQuery, BotPlayQuery, CreateInviteQuery, PlayRandomQuery,
|
||||
};
|
||||
use sea_battle_backend::utils::res_utils::{boxed_error, Res};
|
||||
use std::error::Error;
|
||||
use std::fmt::Display;
|
||||
use std::sync::mpsc::TryRecvError;
|
||||
use std::sync::{mpsc, Arc};
|
||||
@ -18,6 +19,11 @@ use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
|
||||
|
||||
type WsStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
|
||||
|
||||
pub enum GetRemoteVersionError {
|
||||
ConnectionFailed,
|
||||
Other(Box<dyn Error>),
|
||||
}
|
||||
|
||||
/// Connection client
|
||||
///
|
||||
/// This structure acts as a wrapper around websocket connection that handles automatically parsing
|
||||
@ -28,6 +34,24 @@ pub struct Client {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Get remote server version
|
||||
pub async fn get_server_version() -> Result<VersionInfo, GetRemoteVersionError> {
|
||||
let url = format!("{}/version", cli_args().remote_server);
|
||||
log::debug!("Getting remote information from {} ...", url);
|
||||
|
||||
let res = match reqwest::get(url).await {
|
||||
Ok(r) => r,
|
||||
Err(e) if e.is_timeout() || e.is_connect() => {
|
||||
return Err(GetRemoteVersionError::ConnectionFailed)
|
||||
}
|
||||
Err(e) => return Err(GetRemoteVersionError::Other(Box::new(e))),
|
||||
};
|
||||
|
||||
res.json()
|
||||
.await
|
||||
.map_err(|e| GetRemoteVersionError::Other(Box::new(e)))
|
||||
}
|
||||
|
||||
/// Start to play against a bot
|
||||
///
|
||||
/// When playing against a bot, local server is always used
|
||||
@ -51,7 +75,7 @@ impl Client {
|
||||
/// Start to play against a random player
|
||||
pub async fn start_random_play<D: Display>(player_name: D) -> Res<Self> {
|
||||
Self::connect_url(
|
||||
&cli_args().remote_server_uri,
|
||||
&cli_args().remote_server,
|
||||
&format!(
|
||||
"/play/random?{}",
|
||||
serde_urlencoded::to_string(&PlayRandomQuery {
|
||||
@ -66,7 +90,7 @@ impl Client {
|
||||
/// Start a play by creating an invite
|
||||
pub async fn start_create_invite<D: Display>(rules: &GameRules, player_name: D) -> Res<Self> {
|
||||
Self::connect_url(
|
||||
&cli_args().remote_server_uri,
|
||||
&cli_args().remote_server,
|
||||
&format!(
|
||||
"/play/create_invite?{}",
|
||||
serde_urlencoded::to_string(&CreateInviteQuery {
|
||||
@ -82,7 +106,7 @@ impl Client {
|
||||
/// Start a play by accepting an invite
|
||||
pub async fn start_accept_invite<D: Display>(code: String, player_name: D) -> Res<Self> {
|
||||
Self::connect_url(
|
||||
&cli_args().remote_server_uri,
|
||||
&cli_args().remote_server,
|
||||
&format!(
|
||||
"/play/accept_invite?{}",
|
||||
serde_urlencoded::to_string(&AcceptInviteQuery {
|
||||
|
@ -2,7 +2,7 @@ extern crate core;
|
||||
|
||||
pub mod cli_args;
|
||||
pub mod client;
|
||||
pub mod constants;
|
||||
pub mod consts;
|
||||
pub mod server;
|
||||
pub mod ui_screens;
|
||||
pub mod ui_widgets;
|
||||
|
@ -18,7 +18,7 @@ use sea_battle_backend::consts::{
|
||||
use sea_battle_backend::data::GameRules;
|
||||
use sea_battle_backend::utils::res_utils::Res;
|
||||
use sea_battle_cli_player::cli_args::{cli_args, TestDevScreen};
|
||||
use sea_battle_cli_player::client::Client;
|
||||
use sea_battle_cli_player::client::{Client, GetRemoteVersionError};
|
||||
use sea_battle_cli_player::server::run_server;
|
||||
use sea_battle_cli_player::ui_screens::configure_game_rules::GameRulesConfigurationScreen;
|
||||
use sea_battle_cli_player::ui_screens::game_screen::GameScreen;
|
||||
@ -35,10 +35,10 @@ async fn run_dev<B: Backend>(
|
||||
d: TestDevScreen,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let res = match d {
|
||||
TestDevScreen::Popup => popup_screen::PopupScreen::new("Welcome there!!")
|
||||
TestDevScreen::Popup => PopupScreen::new("Welcome there!!")
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
TestDevScreen::Input => input_screen::InputScreen::new("What it your name ?")
|
||||
TestDevScreen::Input => InputScreen::new("What it your name ?")
|
||||
.set_title("A custom title")
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
@ -50,9 +50,9 @@ async fn run_dev<B: Backend>(
|
||||
TestDevScreen::SelectBotType => select_bot_type_screen::SelectBotTypeScreen::default()
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
TestDevScreen::SelectPlayMode => select_play_mode_screen::SelectPlayModeScreen::default()
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
TestDevScreen::SelectPlayMode => {
|
||||
SelectPlayModeScreen::default().show(terminal)?.as_string()
|
||||
}
|
||||
TestDevScreen::SetBoatsLayout => {
|
||||
let rules = GameRules {
|
||||
boats_can_touch: true,
|
||||
@ -64,7 +64,7 @@ async fn run_dev<B: Backend>(
|
||||
.as_string()
|
||||
}
|
||||
TestDevScreen::ConfigureGameRules => {
|
||||
configure_game_rules::GameRulesConfigurationScreen::new(GameRules::default())
|
||||
GameRulesConfigurationScreen::new(GameRules::default())
|
||||
.show(terminal)?
|
||||
.as_string()
|
||||
}
|
||||
@ -99,6 +99,8 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
|
||||
return run_dev(terminal, d).await;
|
||||
}
|
||||
|
||||
let mut checked_online_compatibility = false;
|
||||
|
||||
let mut rules = GameRules::default();
|
||||
|
||||
let mut username = "".to_string();
|
||||
@ -107,6 +109,30 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Res {
|
||||
let choice = SelectPlayModeScreen::default().show(terminal)?;
|
||||
|
||||
if let ScreenResult::Ok(c) = choice {
|
||||
// Check compatibility
|
||||
if c.is_online_play_mode() && !checked_online_compatibility {
|
||||
PopupScreen::new("🖥 Checking remote server version...").show_once(terminal)?;
|
||||
let valid_version = match Client::get_server_version().await {
|
||||
Ok(v) => v.is_compatible_with_static_version().unwrap_or(false),
|
||||
Err(GetRemoteVersionError::ConnectionFailed) => {
|
||||
PopupScreen::new("❌ Could not connect to remote server!")
|
||||
.show(terminal)?;
|
||||
continue;
|
||||
}
|
||||
Err(GetRemoteVersionError::Other(e)) => {
|
||||
log::error!("Could not load remote server information! {:?}", e);
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if !valid_version {
|
||||
PopupScreen::new("❌ Unfortunately, it seems that your version of Sea Battle Cli player is too old to be used online...\n\nPlease update it before trying to play online...").show(terminal)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
checked_online_compatibility = true;
|
||||
}
|
||||
|
||||
if c.need_player_name() && username.is_empty() {
|
||||
username = query_player_name(terminal)?;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ use tui::{Frame, Terminal};
|
||||
|
||||
use sea_battle_backend::data::GameRules;
|
||||
|
||||
use crate::constants::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::consts::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::ui_screens::popup_screen::show_screen_too_small_popup;
|
||||
use crate::ui_screens::select_bot_type_screen::SelectBotTypeScreen;
|
||||
use crate::ui_screens::utils::centered_rect_size;
|
||||
|
@ -9,7 +9,7 @@ use tui::text::*;
|
||||
use tui::widgets::*;
|
||||
use tui::{Frame, Terminal};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::consts::*;
|
||||
use crate::ui_screens::utils::centered_rect_size;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
use crate::ui_widgets::button_widget::ButtonWidget;
|
||||
|
@ -16,7 +16,7 @@ use sea_battle_backend::utils::res_utils::Res;
|
||||
use sea_battle_backend::utils::time_utils::time;
|
||||
|
||||
use crate::client::Client;
|
||||
use crate::constants::*;
|
||||
use crate::consts::*;
|
||||
use crate::ui_screens::confirm_dialog_screen::confirm;
|
||||
use crate::ui_screens::popup_screen::{show_screen_too_small_popup, PopupScreen};
|
||||
use crate::ui_screens::set_boats_layout_screen::SetBoatsLayoutScreen;
|
||||
|
@ -10,7 +10,7 @@ use tui::layout::*;
|
||||
use tui::widgets::*;
|
||||
use tui::{Frame, Terminal};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::consts::*;
|
||||
use crate::ui_screens::utils::*;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
use crate::ui_widgets::button_widget::ButtonWidget;
|
||||
|
@ -9,7 +9,7 @@ use tui::text::*;
|
||||
use tui::widgets::*;
|
||||
use tui::{Frame, Terminal};
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::consts::*;
|
||||
use crate::ui_screens::utils::centered_rect_size;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
use crate::ui_widgets::button_widget::ButtonWidget;
|
||||
|
@ -11,7 +11,7 @@ use tui::{Frame, Terminal};
|
||||
|
||||
use sea_battle_backend::data::{BotDescription, BotType, PlayConfiguration};
|
||||
|
||||
use crate::constants::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::consts::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::ui_screens::utils::centered_rect_size;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::io;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::constants::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::consts::{HIGHLIGHT_COLOR, TICK_RATE};
|
||||
use crate::ui_screens::utils::centered_rect_size;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
use crossterm::event;
|
||||
@ -33,6 +33,11 @@ impl SelectPlayModeResult {
|
||||
pub fn need_custom_rules(&self) -> bool {
|
||||
self == &SelectPlayModeResult::PlayAgainstBot || self == &SelectPlayModeResult::CreateInvite
|
||||
}
|
||||
|
||||
/// Specify whether a play mode is to be played online or not
|
||||
pub fn is_online_play_mode(&self) -> bool {
|
||||
self != &SelectPlayModeResult::PlayAgainstBot && self != &SelectPlayModeResult::Exit
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -12,7 +12,7 @@ use tui::{Frame, Terminal};
|
||||
|
||||
use sea_battle_backend::data::*;
|
||||
|
||||
use crate::constants::*;
|
||||
use crate::consts::*;
|
||||
use crate::ui_screens::confirm_dialog_screen::confirm;
|
||||
use crate::ui_screens::popup_screen::show_screen_too_small_popup;
|
||||
use crate::ui_screens::utils::{centered_rect_size, centered_text};
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::constants::HIGHLIGHT_COLOR;
|
||||
use crate::consts::HIGHLIGHT_COLOR;
|
||||
use tui::buffer::Buffer;
|
||||
use tui::layout::Rect;
|
||||
use tui::style::{Color, Style};
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::constants::HIGHLIGHT_COLOR;
|
||||
use crate::consts::HIGHLIGHT_COLOR;
|
||||
use std::fmt::Display;
|
||||
use tui::buffer::Buffer;
|
||||
use tui::layout::Rect;
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::constants::HIGHLIGHT_COLOR;
|
||||
use crate::consts::HIGHLIGHT_COLOR;
|
||||
use tui::buffer::Buffer;
|
||||
use tui::layout::Rect;
|
||||
use tui::style::*;
|
||||
|
Loading…
Reference in New Issue
Block a user