cont helper fns

This commit is contained in:
ModulatingForce 2024-03-21 02:13:23 -04:00
parent 1b44ec4f4c
commit 5280d18702

View file

@ -25,6 +25,7 @@ use std::collections::HashMap;
// use std::error::Error; // use std::error::Error;
use std::sync::Arc; use std::sync::Arc;
use futures::stream::iter;
use twitch_irc::message::PrivmsgMessage; use twitch_irc::message::PrivmsgMessage;
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -54,13 +55,13 @@ pub enum ModGroup {
Custom, Custom,
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum StatusLvl { pub enum StatusLvl {
Instance, Instance,
_Ch(ChType), Ch(ChType),
} }
#[derive(Debug)] #[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum StatusType { pub enum StatusType {
Enabled(StatusLvl), Enabled(StatusLvl),
Disabled(StatusLvl), Disabled(StatusLvl),
@ -252,17 +253,87 @@ impl ModulesManager {
// Ok("") // 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 // at Instance level
// - If core module, do nothing // - 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())
)
},
} }
pub fn force_disabled(&self, _in_module: ModType) -> (StatusType,ChangeResult) { // (StatusType::Disabled(StatusLvl::Instance),ChangeResult::NoChange("Nothing needed".to_string()))
// 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 // - Bot Moderators MUST Re-enable if they were enabled before
// - If core module, do nothing // - 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) { 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; 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) { async fn int_add_botaction(&self, in_module: ModType, in_modgroup: ModGroup, in_action: BotAction) {
botlog::trace( botlog::trace(
"Add botaction called", "Add botaction called",
@ -373,17 +461,19 @@ impl ModulesManager {
) )
} }
let mut dbt = self.statusdb.write().await; // let mut dbt = self.statusdb.write().await;
// // //
let statusvector = dbt.entry(in_module.clone()).or_insert((in_modgroup.clone(),Vec::new())); // let statusvector = dbt.entry(in_module.clone()).or_insert((in_modgroup.clone(),Vec::new()));
match in_modgroup { // match in_modgroup {
ModGroup::Core => statusvector.1.push(StatusType::Enabled(StatusLvl::Instance)) , // Pushes the Module as Enabled at Instance Level // 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)), // ModGroup::Custom => statusvector.1.push(StatusType::Disabled(StatusLvl::Instance)),
} // }
// statusvector.push(ModStatusType::Enabled(StatusLvl::Instance)); // Pushes the Module as Enabled at Instance Level // 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 mut a = self.botactions.write().await;
let modactions = a.entry(in_module.clone()).or_insert(Vec::new()); let modactions = a.entry(in_module.clone()).or_insert(Vec::new());