Skip to content

Commit

Permalink
Project cleanup (#7)
Browse files Browse the repository at this point in the history
* re/moved funcs from world handler & search

* fixed function calls on refresh

* cleaned up function names to make more sense
  • Loading branch information
JakePIXL authored Oct 26, 2023
1 parent e8cbfb3 commit ae44805
Show file tree
Hide file tree
Showing 17 changed files with 184 additions and 343 deletions.
8 changes: 4 additions & 4 deletions teller/src/handlers/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::io::AsyncSeek;
use tokio::io::AsyncWrite;

use crate::handlers::config::backup::get_backup_config;
use crate::handlers::search::worlds::world_path_from_id;
use crate::handlers::search::worlds::get_world_path_by_id;
use crate::types::backup::BackupMetadata;

use super::config::get_config_folder;
Expand Down Expand Up @@ -150,7 +150,7 @@ pub async fn create_backup_from_id(
vaults: Option<Vec<String>>,
) -> Result<String, String> {
info!("Creating backup for world id: {}", world_id);
match world_path_from_id(world_id, category, instance).await {
match get_world_path_by_id(world_id, category, instance).await {
Ok(world_path) => {
let world_backup_path = match create_world_backup(world_path.clone()).await {
Ok(backup_path) => backup_path,
Expand Down Expand Up @@ -259,7 +259,7 @@ pub async fn create_backup_from_id(
}
}

pub async fn grab_backup_metadata(backup_path: PathBuf) -> Result<BackupMetadata, String> {
pub async fn get_backup_meta_from_path(backup_path: PathBuf) -> Result<BackupMetadata, String> {
let mut zip =
match ZipFileReader::with_tokio(File::open(backup_path.clone()).await.unwrap()).await {
Ok(zip) => zip,
Expand Down Expand Up @@ -437,7 +437,7 @@ pub async fn delete_backup(
Ok(())
}

pub async fn delete_all_backups(world_id: &str, vault: Option<&str>) -> Result<(), String> {
pub async fn delete_world_backups(world_id: &str, vault: Option<&str>) -> Result<(), String> {
let backup_settings = get_backup_config().await?;

let vault_path = match vault {
Expand Down
55 changes: 42 additions & 13 deletions teller/src/handlers/config/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
env,
ops::Add,
path::{Path, PathBuf},
};

Expand All @@ -12,20 +13,48 @@ pub fn get_minecraft_save_location() -> Option<PathBuf> {
let os = env::consts::OS;

match os {
"windows" => Some(PathBuf::from(format!(
"{}\\.minecraft\\saves",
env::var("APPDATA").unwrap()
))),
"macos" => Some(PathBuf::from(format!(
"{}/Library/Application Support/minecraft/saves",
env::var("HOME").unwrap()
))),
"linux" => Some(PathBuf::from(format!(
"{}/.minecraft/saves",
env::var("HOME").unwrap()
))),
_ => None,
"windows" => {
let java_path = PathBuf::from(format!(
"{}\\.minecraft\\saves",
env::var("APPDATA").unwrap()
));

if java_path.exists() {
return Some(java_path);
} else {
let bedrock_path = PathBuf::from(format!(
"{}\\LocalState\\games\\com.mojang\\minecraftWorlds",
env::var("LOCALAPPDATA")
.unwrap()
.add("\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe")
));

if bedrock_path.exists() {
return Some(bedrock_path);
}
}
}
"macos" => {
let path = PathBuf::from(format!(
"{}/Library/Application Support/minecraft/saves",
env::var("HOME").unwrap()
));

if path.exists() {
return Some(path);
}
}
"linux" => {
let path = PathBuf::from(format!("{}/.minecraft/saves", env::var("HOME").unwrap()));

if path.exists() {
return Some(path);
}
}
_ => (),
}

None
}

pub fn get_local_directories_config<P: AsRef<Path>>(
Expand Down
40 changes: 11 additions & 29 deletions teller/src/handlers/player.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::info;
use serde_json::{json, Value};
use tokio::fs;
use uuid::Uuid;

use std::{collections::HashMap, path::PathBuf};
Expand All @@ -12,7 +13,7 @@ use crate::{
types::player::{Item, PlayerData},
};

pub async fn fetch_player_data_from_uuid(
pub async fn get_player_meta_from_uuid(
client: reqwest::Client,
player_uuid_str: String,
) -> Result<Value, String> {
Expand Down Expand Up @@ -74,7 +75,7 @@ pub async fn fetch_player_data_from_uuid(
}
}

pub async fn fetch_players_meta_data(
pub async fn get_players_meta_from_uuids(
player_data_list: Vec<PlayerData>,
) -> Result<HashMap<String, Value>, String> {
let mut player_data_map: HashMap<String, Value> = HashMap::new();
Expand All @@ -83,7 +84,7 @@ pub async fn fetch_players_meta_data(
for player_data in player_data_list {
let player_uuid = player_data.id;
let client = client.clone();
match fetch_player_data_from_uuid(client, player_uuid.clone()).await {
match get_player_meta_from_uuid(client, player_uuid.clone()).await {
Ok(player) => {
player_data_map.insert(player_uuid, player);
}
Expand Down Expand Up @@ -228,10 +229,7 @@ pub fn grab_player_from_uuid(player_uuid: String, path: &PathBuf) -> Result<Play
}
}

pub async fn get_player_data(
path: &PathBuf,
game_type: GameType,
) -> Result<Vec<Value>, Box<dyn std::error::Error>> {
pub async fn get_player_data(path: &PathBuf, game_type: GameType) -> Result<Vec<Value>, String> {
match game_type {
GameType::Bedrock => {
info!("Fetching Bedrock player data");
Expand All @@ -252,21 +250,17 @@ pub async fn get_player_data(
info!("Fetching player data for: {:?}", uuid);

let player_meta = json!({
"username": "Remote Player",
"id": uuid.strip_prefix("player_server_").unwrap_or(uuid),
"avatar": player_avatar,
"meta": {}
});

players.push(player_meta);
}
}

let local_player_data = json!({
"username": "Local Player",
"id": player_uuid,
"avatar": player_avatar,
"meta": {}
});

players.push(local_player_data);
Expand Down Expand Up @@ -320,16 +314,14 @@ pub async fn get_player_data(
};

let player_meta = json!({
"username": "Local Player",
"id": player_uuid,
"avatar": player_avatar,
"meta": {}
});

return Ok(vec![player_meta]);
}

let player_data = match std::fs::read_dir(&player_data_path) {
let mut player_data = match fs::read_dir(&player_data_path).await {
Ok(data) => data,
Err(e) => {
return Err(format!("Failed to read player data: {:?}", e).into());
Expand All @@ -338,14 +330,11 @@ pub async fn get_player_data(

let mut all_players: Vec<Value> = Vec::new();

for player in player_data {
let player = match player {
Ok(player) => player,
Err(e) => {
return Err(format!("Failed to read player data: {:?}", e).into());
}
};

while let Some(player) = player_data
.next_entry()
.await
.map_err(|e| format!("Failed to read player data: {:?}", e))?
{
let player = player.path();

if !player.is_file()
Expand All @@ -355,20 +344,13 @@ pub async fn get_player_data(
}

let player_uuid = player.file_stem().unwrap().to_str().unwrap().to_string();
// let player_meta = match fetch_player_data_from_uuid(client, player_uuid).await {
// Ok(data) => data,
// Err(e) => {
// return Err(format!("Failed to fetch player data: {:?}", e).into());
// }
// };

let player_avatar = format!(
"https://crafthead.net/avatar/{}?scale=32&overlay=false",
player_uuid
);

let player_meta = json!({
"username": "Remote Player",
"id": player_uuid,
"avatar": player_avatar,
"meta": {}
Expand Down
16 changes: 8 additions & 8 deletions teller/src/handlers/search/backups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use tokio::fs;

use crate::{
handlers::{backup::grab_backup_metadata, config::backup::get_backup_config},
handlers::{backup::get_backup_meta_from_path, config::backup::get_backup_config},
types::{
backup::{BackupMetadata, SnapshotInfo},
world::WorldData,
Expand Down Expand Up @@ -41,7 +41,7 @@ fn find_newest_backup(files: &[fs::DirEntry]) -> Option<PathBuf> {
newest_file
}

pub async fn fetch_backups_list(vault: &str) -> Result<Vec<WorldData>, String> {
pub async fn grab_local_backup_list(vault: &str) -> Result<Vec<WorldData>, String> {
let backup_settings = get_backup_config().await?;

let local_backups_path = if let Some(vault_path) = backup_settings.vaults.get(vault) {
Expand Down Expand Up @@ -71,7 +71,7 @@ pub async fn fetch_backups_list(vault: &str) -> Result<Vec<WorldData>, String> {
let newest_backup = find_newest_backup(&all_backups);

if let Some(newest_backup) = newest_backup {
let metadata = grab_backup_metadata(newest_backup).await;
let metadata = get_backup_meta_from_path(newest_backup).await;
if metadata.is_ok() {
let world_data = metadata.unwrap();
backups.push(world_data.entry);
Expand All @@ -85,7 +85,7 @@ pub async fn fetch_backups_list(vault: &str) -> Result<Vec<WorldData>, String> {
Ok(backups)
}

pub async fn fetch_backups_for_world(
pub async fn grab_world_backups(
world_id: &str,
selected_vault: Option<&str>,
) -> Result<Vec<SnapshotInfo>, String> {
Expand Down Expand Up @@ -140,7 +140,7 @@ pub async fn fetch_backups_for_world(
Ok(backups)
}

pub async fn fetch_metadata_for_world(
pub async fn get_world_metadata_from_id(
world_id: &str,
selected_vault: Option<&str>,
) -> Result<BackupMetadata, String> {
Expand All @@ -161,12 +161,12 @@ pub async fn fetch_metadata_for_world(
.map_err(|e| format!("Failed to read backups directory: {}", e))?;

match find_newest_backup(&files) {
Some(newest_backup) => return grab_backup_metadata(newest_backup).await,
Some(newest_backup) => return get_backup_meta_from_path(newest_backup).await,
None => Err("No backups found".to_string()),
}
}

pub async fn fetch_metadata_for_backup(
pub async fn grab_backup_metadata(
world_id: &str,
selected_vault: Option<&str>,
backup_id: &str,
Expand All @@ -186,7 +186,7 @@ pub async fn fetch_metadata_for_backup(
let backup_path = world_path.join(format!("{}.chunkvault-snapshot", backup_id));

if backup_path.exists() {
return grab_backup_metadata(backup_path).await;
return get_backup_meta_from_path(backup_path).await;
} else {
return Err("Backup does not exist".to_string());
}
Expand Down
61 changes: 3 additions & 58 deletions teller/src/handlers/search/worlds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,62 +97,7 @@ pub async fn fetch_worlds_from_instance(
Ok(worlds_list)
}

// pub fn world_path_from_id(world_id: &str, category: Option<&str>) -> Result<PathBuf, String> {
// let config_dir = get_config_folder();

// info!("Searching for world: {}", world_id);

// let mut paths: Vec<PathBuf> = Vec::new();

// match get_local_directories_config(&config_dir) {
// Ok(config) => {
// if let Some(category) = category {
// if category == "default" {
// match get_minecraft_save_location() {
// Some(path) => paths.push(path),
// None => {}
// };
// } else if let Some(vault_entries) = config.categories.get(category) {
// for (_, path) in vault_entries.paths.iter() {
// paths.push(path.clone());
// }
// }
// }
// }
// Err(_e) => {}
// };

// for save_location in paths {
// let world_folders = match std::fs::read_dir(&save_location) {
// Ok(folders) => folders,
// Err(_) => continue,
// };

// for entry in world_folders {
// if let Ok(world_folder) = entry {
// let world_folder = world_folder.path();

// if !world_folder.is_dir() {
// continue;
// }

// let vault_id = match get_vault_id(&world_folder) {
// Ok(id) => id,
// Err(_) => continue,
// };

// if vault_id == world_id {
// info!("Found world: {world_id}");
// return Ok(world_folder);
// }
// }
// }
// }

// Err("Could not find world".to_string())
// }

pub async fn world_path_from_id(
pub async fn get_world_path_by_id(
world_id: &str,
category: Option<&str>,
instance: Option<&str>,
Expand Down Expand Up @@ -217,12 +162,12 @@ pub async fn world_path_from_id(
Err("Could not find world".to_string())
}

pub async fn grab_world_by_id(
pub async fn get_world_by_id(
world_id: &str,
category: Option<&str>,
instance: Option<&str>,
) -> Result<WorldLevelData, String> {
match world_path_from_id(world_id, category, instance).await {
match get_world_path_by_id(world_id, category, instance).await {
Ok(path) => {
let game_type = is_minecraft_world(&path.clone());
match process_world_data(&path, game_type).await {
Expand Down
Loading

0 comments on commit ae44805

Please sign in to comment.