[INIT] modules manager
This commit is contained in:
parent
09308a407a
commit
ad137f5530
3 changed files with 163 additions and 1 deletions
|
@ -1,2 +1,3 @@
|
|||
pub mod botinstance;
|
||||
pub mod ratelimiter;
|
||||
pub mod botmodules;
|
|
@ -22,6 +22,10 @@ use crate::core::ratelimiter::RateLimiter;
|
|||
use crate::core::ratelimiter;
|
||||
// use crate::core::ratelimiter;
|
||||
|
||||
|
||||
use crate::core::botmodules;
|
||||
use crate::core::botmodules::ModulesManager;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ChType {
|
||||
Channel(String),
|
||||
|
@ -42,6 +46,13 @@ pub enum EnType {
|
|||
|
||||
pub use EnType::Enabled;
|
||||
|
||||
// pub enum ModStatusType {
|
||||
// Enabled(EnType),
|
||||
// Disabled(EnType),
|
||||
// Enabled(ModType),
|
||||
// Disabled(ModType),
|
||||
// }
|
||||
|
||||
|
||||
pub struct BotInstance {
|
||||
prefix : char,
|
||||
|
@ -50,6 +61,7 @@ pub struct BotInstance {
|
|||
pub incoming_messages : UnboundedReceiver<ServerMessage>,
|
||||
pub ratelimiters : HashMap<ChType,RateLimiter>, // used to limit messages sent per channel
|
||||
// botmodules : HashMap<ModType,Vec<EnType>>,
|
||||
botmodules : ModulesManager,
|
||||
twitch_oauth : String,
|
||||
pub bot_channels : Vec<ChType>,
|
||||
/*bot_commands : Vec[BotCommand],
|
||||
|
@ -109,6 +121,7 @@ impl BotInstance {
|
|||
client : client,
|
||||
ratelimiters : ratelimiters, // used to limit messages sent per channel
|
||||
// botmodules : HashMap::new(),
|
||||
botmodules : ModulesManager::init(),
|
||||
twitch_oauth : oauth_token,
|
||||
bot_channels : botchannels,
|
||||
/*bot_commands : Vec[BotCommand],
|
||||
|
|
148
src/core/botmodules.rs
Normal file
148
src/core/botmodules.rs
Normal file
|
@ -0,0 +1,148 @@
|
|||
use std::error::Error;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
ModulesManager is made of modulesdb , a HashMap of BotModules with a Vector representing their enabled/disabled status based on channel and instance
|
||||
|
||||
Example
|
||||
{
|
||||
BotModule("Experiments") , [Enabled(Channel("modulatingforce")) , Disabled(Channel("modulatingforce")), Enabled(Instance)]
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ModType {
|
||||
BotModule(String),
|
||||
}
|
||||
|
||||
pub use ModType::BotModule;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ChType {
|
||||
Channel(String),
|
||||
}
|
||||
|
||||
|
||||
pub use ChType::Channel;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
enum StatusLvl {
|
||||
Instance,
|
||||
Ch(ChType),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ModStatusType {
|
||||
Enabled(StatusLvl),
|
||||
Disabled(StatusLvl),
|
||||
}
|
||||
|
||||
// pub use EnType::Enabled;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum BotAction {
|
||||
C(BotCommand),
|
||||
L(Listener),
|
||||
R(Routine),
|
||||
}
|
||||
#[derive(Debug)]
|
||||
struct BotCommand {}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Listener {
|
||||
module : ModType,
|
||||
name : String,
|
||||
// exec_body : fn,
|
||||
help : String
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Routine {}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModulesManager {
|
||||
modulesdb: HashMap<ModType,Vec<ModStatusType>>,
|
||||
botactions: HashMap<ModType,Vec<BotAction>>,
|
||||
}
|
||||
|
||||
impl ModulesManager {
|
||||
|
||||
pub fn init() -> ModulesManager {
|
||||
|
||||
// initializes the modulers manager
|
||||
// Ideally, this should have added known modules based on
|
||||
// directory structure and API user recommendations
|
||||
|
||||
let mut m = HashMap::new();
|
||||
let mut act = HashMap::new();
|
||||
|
||||
// -- some processing including adding into the hashmap
|
||||
|
||||
// let newmodule = BotModule(String::from("GambaCore"));
|
||||
|
||||
let newlistener = Listener {
|
||||
module : BotModule(String::from("experiments").to_owned()),
|
||||
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
|
||||
let statusvector = m
|
||||
.entry(BotModule(String::from("experiments")))
|
||||
.or_insert(Vec::new());
|
||||
|
||||
statusvector.push(ModStatusType::Enabled(StatusLvl::Instance));
|
||||
|
||||
let modactions = act
|
||||
.entry( BotModule(String::from("experiments")))
|
||||
.or_insert(Vec::new());
|
||||
|
||||
modactions.push(BotAction::L(newlistener));
|
||||
|
||||
|
||||
let mgr = ModulesManager {
|
||||
modulesdb : m,
|
||||
botactions : act,
|
||||
};
|
||||
|
||||
println!(">> Modules Manager : {:?}",mgr);
|
||||
|
||||
mgr
|
||||
}
|
||||
|
||||
pub fn modstatus(&self, _:ModType, _:ChType) -> ModStatusType {
|
||||
// Example usage : botmanager.modstatus(
|
||||
// BotModule("GambaCore"),
|
||||
// Channel("modulatingforce")
|
||||
// )
|
||||
// - 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
|
||||
ModStatusType::Enabled(StatusLvl::Instance)
|
||||
}
|
||||
|
||||
|
||||
pub fn togglestatus(&self, _:ModType, _:ChType) -> ModStatusType {
|
||||
// enables or disables based on current status
|
||||
ModStatusType::Enabled(StatusLvl::Instance)
|
||||
}
|
||||
|
||||
|
||||
pub fn setstatus(&self, _:ModType, _:ModStatusType) -> Result<&str,Box<dyn Error>> {
|
||||
// sets the status based given ModSatusType
|
||||
// e.g., b.setstatus(BodModule("GambaCore"), Enabled(Channel("modulatingforce"))).expect("ERROR")
|
||||
Ok("")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in a new issue