From 9c984cef9a2d0fb223635617934959480e8ca2df Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 19 Feb 2023 11:49:23 +0100 Subject: Added adding of specific mod-version --- src/commands/modification.rs | 211 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 170 insertions(+), 41 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index 12a635f..c815155 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -1,8 +1,10 @@ -use crate::{modrinth::{project, versions, extract_current_version, Version, projects}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; +use crate::{modrinth::{versions, extract_current_version, Version, projects, get_raw_versions, project}, config::Cfg, db::{mods_insert, userlist_remove, mods_get_id, userlist_insert, userlist_get_all_ids, userlist_get_current_version, lists_get_all_ids, mods_remove}, input::{Input, ModOptions}, files::{delete_version, download_versions}, List, error::{MLE, ErrorType, MLError}}; //TODO DO IT -pub struct ModVer { - +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum IDSelector { + ModificationID(String), + VersionID(String) } pub async fn modification(config: Cfg, input: Input) -> MLE<()> { @@ -22,27 +24,148 @@ async fn add(config: Cfg, input: Input) -> MLE<()> { Ok(()) } -//TODO impl specific version -pub async fn mods_add(config: Cfg, mod_id: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { - + +#[derive(Debug, Clone)] +pub struct ProjectInfo { + pub mod_id: String, + pub slug: String, + pub title: String, + pub current_version: Option, + pub applicable_versions: Vec, + pub download_link: String, +} + +//TODO impl specific version... Rewrite yay +pub async fn mods_add(config: Cfg, ids: Vec, list: List, direct_download: bool, set_version: bool) -> MLE<()> { println!("Add mods to {}", list.id); println!(" └Add mods:"); - let projects = if mod_id.len() == 1 { - vec![project(String::from(&config.apis.modrinth), &mod_id[0]).await] - } else { - projects(String::from(&config.apis.modrinth), mod_id).await - }; + + let mut mod_ids: Vec = Vec::new(); + let mut ver_ids: Vec = Vec::new(); + + //"Sort" project ids from version ids to be able to handle them differently but in a batch + for id in ids { + match id { + IDSelector::ModificationID(pid) => mod_ids.push(pid), + IDSelector::VersionID(vid) => ver_ids.push(vid), + } + } - let mut downloadstack: Vec = Vec::new(); + let mut projectinfo: Vec = Vec::new(); + if !mod_ids.is_empty() { projectinfo.append(&mut get_mod_infos(config.clone(), mod_ids, list.clone()).await?) }; + if !ver_ids.is_empty() { projectinfo.append(&mut get_ver_info(config.clone(), ver_ids).await?) }; + + if projectinfo.is_empty() { return Err(MLError::new(ErrorType::ArgumentError, "NO_IDS?")) }; - for project in projects { + let mut downloadstack: Vec = Vec::new(); + + //Adding each mod to the lists and downloadstack + for project in projectinfo { + let current_version_id = if project.current_version.is_none() { String::from("NONE") } else { project.current_version.clone().unwrap().id }; + + match userlist_insert(config.clone(), &list.id, &project.mod_id, ¤t_version_id, project.clone().applicable_versions, &project.download_link, set_version) { + Err(e) => { + let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); + if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } + }, + Ok(..) => { Ok(..) }, + }?; + + match mods_insert(config.clone(), &project.mod_id, &project.slug, &project.title) { + Err(e) => { + if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } + }, + Ok(..) => Ok(..), + }?; + + if project.current_version.is_some() { downloadstack.push(project.current_version.unwrap()) }; + } + + //Download all the added mods + if direct_download { + download_versions(list.clone(), config.clone(), downloadstack).await?; + }; + + + // let projects = if id.len() == 1 { + // vec![project(String::from(&config.apis.modrinth), &mod_id).await] + // } else { + // projects(String::from(&config.apis.modrinth), &mod_id).await + // }; +// +// let mut downloadstack: Vec = Vec::new(); +// +// for project in projects { +// println!("\t└{}", project.title); +// println!("\t └Get versions"); +// let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; +// +// let mut available_versions_vec: Vec = Vec::new(); +// let current_version: Option; +// let current_version_id: String; +// let file: String; +// if !available_versions.is_empty() { +// let current_id = extract_current_version(available_versions.clone())?; +// println!("\t └Current version: {}", current_id); +// +// current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); +// +// current_version_id = current_version.clone().unwrap().id; +// +// file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; +// for ver in available_versions { +// available_versions_vec.push(ver.id); +// }; +// } else { +// println!("\t └There's currently no mod version for your specified target"); +// current_version = None; +// current_version_id = String::from("NONE"); +// file = String::from("NONE"); +// available_versions_vec.push(String::from("NONE")); +// } +// +// match userlist_insert(config.clone(), &list.id, &project.id, ¤t_version_id, available_versions_vec, &file, set_version) { +// Err(e) => { +// let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); +// if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } +// }, +// Ok(..) => { Ok(..) }, +// }?; +// +// match mods_insert(config.clone(), &project.id, &project.slug, &project.title) { +// Err(e) => { +// if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } +// }, +// Ok(..) => Ok(..), +// }?; +// +// downloadstack.push(current_version.unwrap()); +// }; +// +// //Download all the added mods +// if direct_download { +// download_versions(list.clone(), config.clone(), downloadstack).await?; +// }; +// + Ok(()) +} + +async fn get_mod_infos(config: Cfg, mod_ids: Vec, list: List) -> MLE> { + let mut projectinfo: Vec = Vec::new(); + + //Get required information from mod_ids + let m_projects = match mod_ids.len() { + 1 => vec![project(&config.apis.modrinth, &mod_ids[0]).await], + 2.. => projects(&config.apis.modrinth, mod_ids).await, + _ => panic!("PANIC"), + }; + for project in m_projects { println!("\t└{}", project.title); println!("\t └Get versions"); - let available_versions = versions(String::from(&config.apis.modrinth), String::from(&project.id), list.clone()).await; + let available_versions = versions(&config.apis.modrinth, String::from(&project.id), list.clone()).await; let mut available_versions_vec: Vec = Vec::new(); let current_version: Option; - let current_version_id: String; let file: String; if !available_versions.is_empty() { let current_id = extract_current_version(available_versions.clone())?; @@ -50,49 +173,55 @@ pub async fn mods_add(config: Cfg, mod_id: Vec, list: List, direct_downl current_version = Some(available_versions.clone().into_iter().find(|v| v.id == current_id).unwrap()); - current_version_id = current_version.clone().unwrap().id; - file = current_version.clone().ok_or("").unwrap().files.into_iter().find(|f| f.primary).unwrap().url; for ver in available_versions { available_versions_vec.push(ver.id); }; + + projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file }) + } else { println!("\t └There's currently no mod version for your specified target"); current_version = None; - current_version_id = String::from("NONE"); file = String::from("NONE"); available_versions_vec.push(String::from("NONE")); + projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version, applicable_versions: available_versions_vec, download_link: file }) } + }; - match userlist_insert(config.clone(), &list.id, &project.id, ¤t_version_id, available_versions_vec, &file, set_version) { - Err(e) => { - let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); - if e.to_string() == expected_err { Err(MLError::new(ErrorType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) } else { Err(e) } - }, - Ok(..) => { Ok(..) }, - }?; - - match mods_insert(config.clone(), &project.id, &project.slug, &project.title) { - Err(e) => { - if e.to_string() == "SQL: UNIQUE constraint failed: mods.id" { Ok(..) } else { Err(e) } - }, - Ok(..) => Ok(..), - }?; + Ok(projectinfo) +} - downloadstack.push(current_version.unwrap()); +async fn get_ver_info(config: Cfg, ver_ids: Vec) -> MLE> { + let mut projectinfo: Vec = Vec::new(); + + //Get required information from ver_ids + let mut v_versions = get_raw_versions(&config.apis.modrinth, ver_ids).await; + let mut v_mod_ids: Vec = Vec::new(); + for ver in v_versions.clone() { + v_mod_ids.push(ver.project_id); }; - - //Download all the added mods - if direct_download { - download_versions(list.clone(), config.clone(), downloadstack).await?; + let mut v_projects = projects(&config.apis.modrinth, v_mod_ids).await; + v_versions.sort_by(|a, b| a.project_id.cmp(&b.project_id)); + v_projects.sort_by(|a, b| a.id.cmp(&b.id)); + + for (i, project) in v_projects.into_iter().enumerate() { + let version = &v_versions[i]; + println!("\t└{}({})", project.title, version.id); + let file = version.clone().files.into_iter().find(|f| f.primary).unwrap().url; + projectinfo.push(ProjectInfo { mod_id: project.id, slug: project.slug, title: project.title, current_version: Some(version.clone()), applicable_versions: vec![String::from(&version.id)], download_link: file }) }; - - Ok(()) + Ok(projectinfo) } fn remove(config: Cfg, input: Input) -> MLE<()> { - - let mod_id = mods_get_id(&config.data, input.mod_id.as_ref().unwrap())?; + + let id = match input.clone().mod_id.unwrap() { + IDSelector::ModificationID(id) => id, + IDSelector::VersionID(..) => return Err(MLError::new(ErrorType::ArgumentError, "NO_MOD_ID")), + }; + + let mod_id = mods_get_id(&config.data, &id)?; let version = userlist_get_current_version(config.clone(), input.clone().list.unwrap().id, String::from(&mod_id))?; -- cgit v1.2.3