offline mode, expanded logging

This commit is contained in:
2024-12-09 04:14:53 +01:00
parent 5244fa9549
commit 2d84c514a7
8 changed files with 296 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
use crate::structs::PrintPrefix;
use crate::misc;
use crate::structs::{PrintPrefix, StoredGameData};
use colored::Colorize;
use once_cell::sync::Lazy;
use std::collections::HashMap;
@@ -12,6 +13,8 @@ pub const GH_IW4X_REPO: &str = "iw4x-client";
pub static MASTER: Lazy<Mutex<String>> =
Lazy::new(|| Mutex::new("https://cdn.alterware.ovh".to_owned()));
pub static IS_OFFLINE: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
pub static PREFIXES: Lazy<HashMap<&'static str, PrintPrefix>> = Lazy::new(|| {
HashMap::from([
(
@@ -51,3 +54,29 @@ pub static PREFIXES: Lazy<HashMap<&'static str, PrintPrefix>> = Lazy::new(|| {
),
])
});
pub async fn check_connectivity() -> bool {
let master_url = MASTER.lock().unwrap().clone();
match crate::http_async::get_body_string(&master_url).await {
Ok(_) => true,
Err(_) => {
*IS_OFFLINE.lock().unwrap() = true;
false
}
}
}
pub fn get_stored_data() -> Option<StoredGameData> {
let dir = std::env::current_dir().ok()?;
let cache = misc::get_cache(&dir);
cache.stored_data
}
pub fn store_game_data(data: &StoredGameData) -> Result<(), Box<dyn std::error::Error>> {
let dir = std::env::current_dir()?;
let mut cache = misc::get_cache(&dir);
cache.stored_data = Some((*data).clone());
misc::save_cache(&dir, cache);
Ok(())
}