custom refactor
This commit is contained in:
parent
0a74406bb3
commit
b2220dc224
4 changed files with 21 additions and 78 deletions
|
@ -116,7 +116,6 @@ pub struct Listener {
|
||||||
|
|
||||||
impl Listener {
|
impl Listener {
|
||||||
pub async fn execute(&self, m: BotAR, n: PrivmsgMessage) {
|
pub async fn execute(&self, m: BotAR, n: PrivmsgMessage) {
|
||||||
// ((*self).exec_body)(m, n).await;
|
|
||||||
(self.exec_body)(m, n).await;
|
(self.exec_body)(m, n).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,8 +123,6 @@ impl Listener {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl BotActionTrait for Listener {
|
impl BotActionTrait for Listener {
|
||||||
async fn add_to_bot(self, bot: BotInstance) {
|
async fn add_to_bot(self, bot: BotInstance) {
|
||||||
// println!("Adding action to bot");
|
|
||||||
// Log::trace("Adding action to bot");
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
"Adding action to bot",
|
"Adding action to bot",
|
||||||
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
|
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
|
||||||
|
@ -135,9 +132,6 @@ impl BotActionTrait for Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_to_modmgr(self, modmgr: Arc<ModulesManager>) {
|
async fn add_to_modmgr(self, modmgr: Arc<ModulesManager>) {
|
||||||
// let modmgr = *modmgr.lock().await;
|
|
||||||
// println!("Adding action to module manager");
|
|
||||||
// Log::trace("Adding action to module manager");
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
"Adding action to module manager",
|
"Adding action to module manager",
|
||||||
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
|
Some("BotModules > BotActionTrait > add_to_bot()".to_string()),
|
||||||
|
@ -153,8 +147,6 @@ impl BotActionTrait for Listener {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Routine {}
|
pub struct Routine {}
|
||||||
|
|
||||||
// #[derive(Clone)]
|
|
||||||
|
|
||||||
pub struct ModulesManager {
|
pub struct ModulesManager {
|
||||||
statusdb: Arc<RwLock<HashMap<ModType, Vec<ModStatusType>>>>,
|
statusdb: Arc<RwLock<HashMap<ModType, Vec<ModStatusType>>>>,
|
||||||
pub botactions: Arc<RwLock<HashMap<ModType, Vec<BotAction>>>>,
|
pub botactions: Arc<RwLock<HashMap<ModType, Vec<BotAction>>>>,
|
||||||
|
@ -176,35 +168,36 @@ botactions
|
||||||
|
|
||||||
impl ModulesManager {
|
impl ModulesManager {
|
||||||
pub async fn init() -> Arc<ModulesManager> {
|
pub async fn init() -> Arc<ModulesManager> {
|
||||||
let m = HashMap::new();
|
|
||||||
let act = HashMap::new();
|
|
||||||
|
|
||||||
let mgr = ModulesManager {
|
let mgr = ModulesManager {
|
||||||
statusdb: Arc::new(RwLock::new(m)),
|
statusdb: Arc::new(RwLock::new(HashMap::new())),
|
||||||
botactions: Arc::new(RwLock::new(act)),
|
botactions: Arc::new(RwLock::new(HashMap::new())),
|
||||||
};
|
};
|
||||||
|
|
||||||
// :: [x] initialize core modules
|
// :: [x] initialize core modules
|
||||||
|
|
||||||
// println!("ModulesManager > init() > Adding modules");
|
|
||||||
botlog::debug(
|
botlog::debug(
|
||||||
"ModulesManager > init() > Adding modules",
|
"ModulesManager > init() > Adding modules",
|
||||||
Some("ModulesManager > init()".to_string()),
|
Some("ModulesManager > init()".to_string()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
let mgra = Arc::new(mgr);
|
|
||||||
crate::core::identity::init(Arc::clone(&mgra)).await;
|
|
||||||
|
|
||||||
crate::modules::init(Arc::clone(&mgra)).await;
|
|
||||||
|
|
||||||
// println!(">> Modules Manager : End of Init");
|
|
||||||
|
let mgrarc = Arc::new(mgr);
|
||||||
|
|
||||||
|
// 1. load core modules
|
||||||
|
crate::core::identity::init(Arc::clone(&mgrarc)).await;
|
||||||
|
|
||||||
|
// 2. load custom modules
|
||||||
|
crate::custom::init(Arc::clone(&mgrarc)).await;
|
||||||
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
">> Modules Manager : End of Init",
|
">> Modules Manager : End of Init",
|
||||||
Some("ModulesManager > init()".to_string()),
|
Some("ModulesManager > init()".to_string()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
mgra
|
mgrarc
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn modstatus(&self, _: ModType, _: ChType) -> ModStatusType {
|
pub fn modstatus(&self, _: ModType, _: ChType) -> ModStatusType {
|
||||||
|
@ -230,7 +223,6 @@ impl ModulesManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_botaction(&self, in_module: ModType, in_action: BotAction) {
|
pub async fn add_botaction(&self, in_module: ModType, in_action: BotAction) {
|
||||||
// println!("Add botaction called");
|
|
||||||
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
"Add botaction called",
|
"Add botaction called",
|
||||||
|
@ -250,37 +242,17 @@ impl ModulesManager {
|
||||||
both would be called separately, even if they both have the same or different logic
|
both would be called separately, even if they both have the same or different logic
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// let newlistener = Listener {
|
|
||||||
// // module : BotModule(String::from("experiments").to_owned()),
|
|
||||||
// module : in_module.clone(),
|
|
||||||
// name : String::from("socklistener"),
|
|
||||||
// help : String::from("This will listen and react to sock randomly"),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// As a Demonstration, the listener's Module is added and Enabled at Instance level
|
|
||||||
|
|
||||||
// [x] Before Adding, validate the following :
|
// [x] Before Adding, validate the following :
|
||||||
// - If BotAction to Add is a BotCommand , In Module Manager DB (botactions),
|
// - If BotAction to Add is a BotCommand , In Module Manager DB (botactions),
|
||||||
// Check All Other BotAction Command Names & Aliases to ensure they don't conflict
|
// Check All Other BotAction Command Names & Aliases to ensure they don't conflict
|
||||||
|
|
||||||
async fn find_conflict_module(mgr: &ModulesManager, act: &BotAction) -> Option<ModType> {
|
async fn find_conflict_module(mgr: &ModulesManager, act: &BotAction) -> Option<ModType> {
|
||||||
// Some(BotModule(String::from("GambaCore")))
|
|
||||||
|
|
||||||
// match act {
|
|
||||||
// BotAction::C(c) => {
|
|
||||||
// Some(BotModule(String::from("GambaCore")))
|
|
||||||
// },
|
|
||||||
// BotAction::L(l) => None,
|
|
||||||
// BotAction::R(r) => None,
|
|
||||||
// }
|
|
||||||
|
|
||||||
if let BotAction::C(incmd) = act {
|
if let BotAction::C(incmd) = act {
|
||||||
// let n = & mgr.botactions;
|
|
||||||
|
|
||||||
let d = mgr.botactions.read().await;
|
let actdb = mgr.botactions.read().await;
|
||||||
let d = &(*d);
|
|
||||||
|
for (module, moduleactions) in &(*actdb) {
|
||||||
for (module, moduleactions) in d {
|
|
||||||
for modact in moduleactions.iter() {
|
for modact in moduleactions.iter() {
|
||||||
if let BotAction::C(dbcmd) = &modact {
|
if let BotAction::C(dbcmd) = &modact {
|
||||||
// At this point, there is an command incmd and looked up dbcmd
|
// At this point, there is an command incmd and looked up dbcmd
|
||||||
|
@ -289,17 +261,14 @@ impl ModulesManager {
|
||||||
|
|
||||||
if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
|
if incmd.command.to_lowercase() == dbcmd.command.to_lowercase() {
|
||||||
// Returning State - with the identified module
|
// Returning State - with the identified module
|
||||||
// return Some((module.clone(),BotAction::C(*dbcmd.clone())));
|
|
||||||
// return Some(incmd); // for some reason I keep getting issues
|
|
||||||
//return Some(BotModule(String::from("GambaCore"))); // works
|
|
||||||
return Some(module.clone()); // works
|
return Some(module.clone()); // works
|
||||||
// return Some(dbcmd.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for a in &dbcmd.alias {
|
for a in &dbcmd.alias {
|
||||||
if incmd.command.to_lowercase() == a.to_lowercase() {
|
if incmd.command.to_lowercase() == a.to_lowercase() {
|
||||||
// Returning State - with the identified module
|
// Returning State - with the identified module
|
||||||
// return Some((module.clone(),BotAction::C(dbcmd)));
|
|
||||||
return Some(module.clone()); // works
|
return Some(module.clone()); // works
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,14 +278,14 @@ impl ModulesManager {
|
||||||
for inalias in &incmd.alias {
|
for inalias in &incmd.alias {
|
||||||
if inalias.to_lowercase() == dbcmd.command.to_lowercase() {
|
if inalias.to_lowercase() == dbcmd.command.to_lowercase() {
|
||||||
// Returning State - with the identified module
|
// Returning State - with the identified module
|
||||||
// return Some((module.clone(),BotAction::C(dbcmd)));
|
|
||||||
return Some(module.clone()); // works
|
return Some(module.clone()); // works
|
||||||
}
|
}
|
||||||
|
|
||||||
for a in &dbcmd.alias {
|
for a in &dbcmd.alias {
|
||||||
if inalias.to_lowercase() == a.to_lowercase() {
|
if inalias.to_lowercase() == a.to_lowercase() {
|
||||||
// Returning State - with the identified module
|
// Returning State - with the identified module
|
||||||
// return Some((module.clone(),BotAction::C(dbcmd)));
|
|
||||||
return Some(module.clone()); // works
|
return Some(module.clone()); // works
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -324,28 +293,12 @@ impl ModulesManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return Some(BotModule(String::from("GambaCore")))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// for all other scenarios (e.g., Listener, Routine), find no conflicts
|
// for all other scenarios (e.g., Listener, Routine), find no conflicts
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
// if let probmod = find_conflict_module(&self, &in_action) {
|
|
||||||
// // () // return because there was a conflict?
|
|
||||||
// panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , probmod );
|
|
||||||
// }
|
|
||||||
// match find_conflict_module(&self, &in_action).await {
|
|
||||||
// match find_conflict_module(self, &in_action).await {
|
|
||||||
// // Some(c) => panic!("ERROR: Could not add {:?} ; there was a conflict with existing module {:?}", in_action , c ),
|
|
||||||
// Some(c) => panic!(
|
|
||||||
// "ERROR: Could not add module; there was a conflict with existing module {:?}",
|
|
||||||
// c
|
|
||||||
// ),
|
|
||||||
// None => (),
|
|
||||||
// }
|
|
||||||
|
|
||||||
if let Some(c) = find_conflict_module(self, &in_action).await {
|
if let Some(c) = find_conflict_module(self, &in_action).await {
|
||||||
panic!(
|
panic!(
|
||||||
"ERROR: Could not add module; there was a conflict with existing module {:?}",
|
"ERROR: Could not add module; there was a conflict with existing module {:?}",
|
||||||
|
@ -355,7 +308,6 @@ impl ModulesManager {
|
||||||
|
|
||||||
let mut dbt = self.statusdb.write().await;
|
let mut dbt = self.statusdb.write().await;
|
||||||
let statusvector = dbt
|
let statusvector = dbt
|
||||||
// .entry(BotModule(String::from("experiments")))
|
|
||||||
.entry(in_module.clone())
|
.entry(in_module.clone())
|
||||||
.or_insert(Vec::new());
|
.or_insert(Vec::new());
|
||||||
|
|
||||||
|
@ -363,22 +315,13 @@ impl ModulesManager {
|
||||||
|
|
||||||
let mut a = self.botactions.write().await;
|
let mut a = self.botactions.write().await;
|
||||||
let modactions = a
|
let modactions = a
|
||||||
//.entry( BotModule(String::from("experiments")))
|
|
||||||
.entry(in_module.clone())
|
.entry(in_module.clone())
|
||||||
.or_insert(Vec::new());
|
.or_insert(Vec::new());
|
||||||
|
|
||||||
// modactions.push(BotAction::L(newlistener));
|
|
||||||
modactions.push(in_action);
|
modactions.push(in_action);
|
||||||
|
|
||||||
// println!(">> Modules Manager : Called Add bot Action");
|
|
||||||
botlog::trace(
|
botlog::trace(
|
||||||
">> Modules Manager : Called Add bot Action",
|
format!("Modules Manager> add_botaction called - botactions size : {}", modactions.len()).as_str(),
|
||||||
Some("ModulesManager > init()".to_string()),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
// println!("add_botaction - botactions size : {}",modactions.len());
|
|
||||||
botlog::trace(
|
|
||||||
&format!("add_botaction - botactions size : {}", modactions.len()),
|
|
||||||
Some("ModulesManager > init()".to_string()),
|
Some("ModulesManager > init()".to_string()),
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod modules;
|
pub mod custom;
|
||||||
|
|
Loading…
Reference in a new issue