diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index 6332ab6..1bffc07 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -231,7 +231,7 @@ impl ModulesManager { mgrarc } - pub fn modstatus(&self, _: ModType, _: ChType) -> StatusType { + pub async fn modstatus(&self, in_module: ModType, in_chnl: ChType) -> StatusType { // Example usage : botmanager.modstatus( // BotModule("GambaCore"), // Channel("modulatingforce") @@ -239,7 +239,84 @@ impl ModulesManager { // - The ModStatusType checks in the context of the given channel , // but also validates based on wheher the module is disabled at a bot instance // level as well - StatusType::Enabled(StatusLvl::Instance) + + let dbt = self.statusdb.read().await; + + // let a = dbt.entry(in_module.clone()).; + let (mgrp,statusvector) = dbt.get(&in_module).unwrap(); + + match mgrp { + ModGroup::Core => { + StatusType::Enabled(StatusLvl::Instance) // This forces core to be validated as Enabled, even if undesired scenario of missing StatusLvl::Instance or empty vectors + }, + ModGroup::Custom => { + // // remove all instance level pattern for the module + // while let Some(index) = statusvector + // .iter() + // .position(|x| (*x == StatusType::Enabled(StatusLvl::Instance)) || (*x == StatusType::Disabled(StatusLvl::Instance))) { + + // statusvector.remove(index); + // } + // statusvector.push(StatusType::Disabled(StatusLvl::Instance)); + + // ( + // StatusType::Disabled(StatusLvl::Instance), + // ChangeResult::Success("Set Disabled at Instance".to_string()) + // ) + + /* + + [x] 1. If Disabled at Instance Level , + [x] a. And Enabled at a Channel Level > return Enabled(Channel) + [x] b. And Disabled at a Channel Level > return Disabled(Channel) + [x] c. And Not Defined at Channel Level > return Disabled(Instance) + [x] 2. If Enabled at Instance Level , + [x] a. And Enabled at a Channel Level > return Enabled(Channel) + [x] b. And Disabled at a Channel Level > return Disabled(Channel) + [x] c. And Not Defined at Channel Level > return Enabled(Instance) + */ + + + + if statusvector.contains(&StatusType::Disabled(StatusLvl::Instance)) { + // [x] 1. If Disabled at Instance Level , + + + if statusvector.contains(&StatusType::Enabled(StatusLvl::Ch(in_chnl.clone()))) { + // [x] a. And Enabled at a Channel Level > return Enabled(Channel) + StatusType::Enabled(StatusLvl::Ch(in_chnl.clone())) + } else if statusvector.contains(&StatusType::Disabled(StatusLvl::Ch(in_chnl.clone()))) { + // [x] b. And Disabled at a Channel Level > return Disabled(Channel) + StatusType::Disabled(StatusLvl::Ch(in_chnl.clone())) + } else { + // [x] c. And Not Defined at Channel Level > return Disabled(Instance) + StatusType::Disabled(StatusLvl::Instance) + } + + } else if statusvector.contains(&StatusType::Enabled(StatusLvl::Instance)) { + // [x] 2. If Enabled at Instance Level , + + if statusvector.contains(&StatusType::Enabled(StatusLvl::Ch(in_chnl.clone()))) { + // [x] a. And Enabled at a Channel Level > return Enabled(Channel) + StatusType::Enabled(StatusLvl::Ch(in_chnl.clone())) + } else if statusvector.contains(&StatusType::Disabled(StatusLvl::Ch(in_chnl.clone()))) { + // [x] b. And Disabled at a Channel Level > return Disabled(Channel) + StatusType::Disabled(StatusLvl::Ch(in_chnl.clone())) + } else { + // [x] c. And Not Defined at Channel Level > return Enabled(Instance) + StatusType::Enabled(StatusLvl::Instance) + } + + } else { + // ? In some unexpected scenario (e.g., not define at instance level), assume Disabled at Instance level and set as this way + self.set_instance_disabled(in_module).await; + StatusType::Disabled(StatusLvl::Instance) + } + }, + } + + + //StatusType::Enabled(StatusLvl::Instance) } // pub fn togglestatus(&self, _: ModType, _: ChType) -> StatusType { @@ -283,7 +360,7 @@ impl ModulesManager { ( StatusType::Disabled(StatusLvl::Instance), - ChangeResult::Success("Disabled at Instance".to_string()) + ChangeResult::Success("Set Disabled at Instance".to_string()) ) }, } @@ -336,22 +413,118 @@ impl ModulesManager { // (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) } - pub fn set_instance_enabled(&self, _in_module: ModType) -> (StatusType,ChangeResult) { + pub async fn set_instance_enabled(&self, in_module: ModType) -> (StatusType,ChangeResult) { // at Instance level // - If core module, do nothing - (StatusType::Enabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) + + let mut dbt = self.statusdb.write().await; + + // let a = dbt.entry(in_module.clone()).; + let (mgrp,statusvector) = dbt.get_mut(&in_module).unwrap(); + + match mgrp { + ModGroup::Core => { + ( + StatusType::Enabled(StatusLvl::Instance), + ChangeResult::NoChange("Core Modules are always Enabled".to_string()) + ) + }, + ModGroup::Custom => { + // remove all instance level pattern for the module + while let Some(index) = statusvector + .iter() + .position(|x| (*x == StatusType::Enabled(StatusLvl::Instance)) || (*x == StatusType::Disabled(StatusLvl::Instance))) { + + statusvector.remove(index); + } + statusvector.push(StatusType::Enabled(StatusLvl::Instance)); + + ( + StatusType::Enabled(StatusLvl::Instance), + ChangeResult::Success("Set Enabled at Instance".to_string()) + ) + }, + } + + // (StatusType::Enabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) } - pub fn set_ch_disabled(&self, _in_module: ModType , _in_chnl: ChType) -> (StatusType,ChangeResult) { + pub async fn set_ch_disabled(&self, in_module: ModType , in_chnl: ChType) -> (StatusType,ChangeResult) { // at Instance level // - If core module, do nothing - (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) + + + let mut dbt = self.statusdb.write().await; + + // let a = dbt.entry(in_module.clone()).; + let (mgrp,statusvector) = dbt.get_mut(&in_module).unwrap(); + + match mgrp { + ModGroup::Core => { + ( + StatusType::Enabled(StatusLvl::Instance), + ChangeResult::Failed("Core Modules cannot be disabled".to_string()) + ) + }, + ModGroup::Custom => { + // remove all channel level pattern for the module + while let Some(index) = statusvector + .iter() + .position(|x| + (*x == StatusType::Enabled(StatusLvl::Ch(in_chnl.clone()))) || (*x == StatusType::Disabled(StatusLvl::Ch(in_chnl.clone())))) + { + + statusvector.remove(index); + } + statusvector.push(StatusType::Disabled(StatusLvl::Ch(in_chnl.clone()))); + + ( + StatusType::Disabled(StatusLvl::Ch(in_chnl.clone())), + ChangeResult::Success("Set Disabled at Channel Level".to_string()) + ) + }, + } + + // (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) } - pub fn set_ch_enabled(&self, _in_module: ModType , _in_chnl: ChType) -> (StatusType,ChangeResult) { + pub async fn set_ch_enabled(&self, in_module: ModType , in_chnl: ChType) -> (StatusType,ChangeResult) { // at Instance level // - If core module, do nothing - (StatusType::Enabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) + + let mut dbt = self.statusdb.write().await; + + // let a = dbt.entry(in_module.clone()).; + let (mgrp,statusvector) = dbt.get_mut(&in_module).unwrap(); + + match mgrp { + ModGroup::Core => { + ( + StatusType::Enabled(StatusLvl::Instance), + ChangeResult::NoChange("Core Modules are always Enabled".to_string()) + ) + }, + ModGroup::Custom => { + // remove all channel level pattern for the module + while let Some(index) = statusvector + .iter() + .position(|x| + (*x == StatusType::Enabled(StatusLvl::Ch(in_chnl.clone()))) || (*x == StatusType::Disabled(StatusLvl::Ch(in_chnl.clone())))) + { + + statusvector.remove(index); + } + statusvector.push(StatusType::Enabled(StatusLvl::Ch(in_chnl.clone()))); + + ( + StatusType::Enabled(StatusLvl::Ch(in_chnl.clone())), + ChangeResult::Success("Set Enabled at Channel Level".to_string()) + ) + }, + } + + + // (StatusType::Enabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) }