diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs index 8fdf96c..78db77e 100644 --- a/src/core/botmodules.rs +++ b/src/core/botmodules.rs @@ -25,6 +25,7 @@ use std::collections::HashMap; // use std::error::Error; use std::sync::Arc; +use futures::stream::iter; use twitch_irc::message::PrivmsgMessage; use tokio::sync::RwLock; @@ -54,13 +55,13 @@ pub enum ModGroup { Custom, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum StatusLvl { Instance, - _Ch(ChType), + Ch(ChType), } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum StatusType { Enabled(StatusLvl), Disabled(StatusLvl), @@ -252,17 +253,87 @@ impl ModulesManager { // Ok("") // } - pub fn set_instance_disabled(&self, _in_module: ModType) -> (StatusType,ChangeResult) { + pub async fn set_instance_disabled(&self, in_module: ModType) -> (StatusType,ChangeResult) { // at Instance level // - If core module, do nothing - (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) + + // self.satusdb. + + 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 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("Disabled at Instance".to_string()) + ) + }, + } + + // (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) } - pub fn force_disabled(&self, _in_module: ModType) -> (StatusType,ChangeResult) { - // Disables the module at Instance level, and removes all Enabled/Disabled at Channel level + pub async fn force_disable(&self, in_module: ModType) -> (StatusType,ChangeResult) { + // Disables the module at Instance level, and removes all Enabled at Channel level // - Bot Moderators MUST Re-enable if they were enabled before // - 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 instance level pattern & Enabled Channel patterns for the module + // Disabled at Channel level might be fine? That way if it gets Enabled at instance level, channel level disables are uninterrupted + while let Some(index) = statusvector + .iter() + .position(|x| + if (*x == StatusType::Enabled(StatusLvl::Instance)) + || (*x == StatusType::Disabled(StatusLvl::Instance)) { + true + } else if let StatusType::Enabled(StatusLvl::Ch(_)) = (*x).clone() { + true + } else {false} + ) + { + statusvector.remove(index); + } + statusvector.push(StatusType::Disabled(StatusLvl::Instance)); + + ( + StatusType::Disabled(StatusLvl::Instance), + ChangeResult::Success("Disabled at Instance".to_string()) + ) + }, + } + + // (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string())) } pub fn set_instance_enabled(&self, _in_module: ModType) -> (StatusType,ChangeResult) { @@ -293,6 +364,23 @@ impl ModulesManager { self.int_add_botaction(in_module,ModGroup::Core,in_action).await; } + + pub async fn affirm_in_statusdb(&self,in_module:ModType,in_modgroup: ModGroup) { + + let mut dbt = self.statusdb.write().await; + + let (_,statusvector) = dbt.entry(in_module.clone()).or_insert((in_modgroup.clone(),Vec::new())); + + if !statusvector.contains(&StatusType::Enabled(StatusLvl::Instance)) && !statusvector.contains(&StatusType::Disabled(StatusLvl::Instance)) + { + match in_modgroup { + ModGroup::Core => statusvector.push(StatusType::Enabled(StatusLvl::Instance)) , // Pushes the Module as Enabled at Instance Level + ModGroup::Custom => statusvector.push(StatusType::Disabled(StatusLvl::Instance)), + } + } + + } + async fn int_add_botaction(&self, in_module: ModType, in_modgroup: ModGroup, in_action: BotAction) { botlog::trace( "Add botaction called", @@ -373,17 +461,19 @@ impl ModulesManager { ) } - let mut dbt = self.statusdb.write().await; - // - let statusvector = dbt.entry(in_module.clone()).or_insert((in_modgroup.clone(),Vec::new())); + // let mut dbt = self.statusdb.write().await; + // // + // let statusvector = dbt.entry(in_module.clone()).or_insert((in_modgroup.clone(),Vec::new())); - match in_modgroup { - ModGroup::Core => statusvector.1.push(StatusType::Enabled(StatusLvl::Instance)) , // Pushes the Module as Enabled at Instance Level - ModGroup::Custom => statusvector.1.push(StatusType::Disabled(StatusLvl::Instance)), - } + // match in_modgroup { + // ModGroup::Core => statusvector.1.push(StatusType::Enabled(StatusLvl::Instance)) , // Pushes the Module as Enabled at Instance Level + // ModGroup::Custom => statusvector.1.push(StatusType::Disabled(StatusLvl::Instance)), + // } // statusvector.push(ModStatusType::Enabled(StatusLvl::Instance)); // Pushes the Module as Enabled at Instance Level + self.affirm_in_statusdb(in_module.clone(),in_modgroup).await; + let mut a = self.botactions.write().await; let modactions = a.entry(in_module.clone()).or_insert(Vec::new());