From 2a1a7f8503e4d8273cae133b7db32c581a3f2cad Mon Sep 17 00:00:00 2001
From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com>
Date: Thu, 28 Mar 2024 04:39:03 -0400
Subject: [PATCH] ExecBody & routine addl references

---
 src/core/bot_actions.rs                  |  2 +-
 src/core/botinstance.rs                  |  5 +++
 src/core/botmodules.rs                   | 57 +++++++++++++++++++++---
 src/custom/experimental/experiment003.rs | 36 +++++++--------
 4 files changed, 74 insertions(+), 26 deletions(-)

diff --git a/src/core/bot_actions.rs b/src/core/bot_actions.rs
index 9ff41b5..130be94 100644
--- a/src/core/bot_actions.rs
+++ b/src/core/bot_actions.rs
@@ -16,7 +16,7 @@ pub type RoutineAR = Arc<RwLock<Routine>>;
 pub struct ExecBodyParams {
     pub bot : BotAR,
     pub msg : PrivmsgMessage,
-    // pub parent_act : ActAR , 
+    pub parent_act : Option<ActAR> , 
     pub curr_act : ActAR ,
 }
 
diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs
index 563856f..86ac8bb 100644
--- a/src/core/botinstance.rs
+++ b/src/core/botinstance.rs
@@ -404,6 +404,7 @@ impl BotInstance {
                                 let params = ExecBodyParams {
                                     bot : Arc::clone(&bot),
                                     msg : (*msg).clone(),
+                                    parent_act : None,
                                     curr_act : Arc::clone(&act_clone),
                                 };
 
@@ -461,6 +462,7 @@ impl BotInstance {
                                     let params = ExecBodyParams {
                                         bot : Arc::clone(&bot),
                                         msg : (*msg).clone(),
+                                        parent_act : None,
                                         curr_act : Arc::clone(&act_clone),
 
                                     };
@@ -491,6 +493,7 @@ impl BotInstance {
                                     let params = ExecBodyParams {
                                         bot : Arc::clone(&bot),
                                         msg : (*msg).clone(),
+                                        parent_act : None,
                                         curr_act : Arc::clone(&act_clone),
 
                                     };
@@ -516,6 +519,7 @@ impl BotInstance {
                                     c.execute(ExecBodyParams { 
                                         bot : a, 
                                         msg : msg.clone() ,
+                                        parent_act : None,
                                         curr_act : Arc::clone(&act_clone),
                                     }).await;
 
@@ -564,6 +568,7 @@ impl BotInstance {
                             l.execute(ExecBodyParams { 
                                 bot : a, 
                                 msg : msg.clone() , 
+                                parent_act : None,
                                 curr_act : Arc::clone(&act_clone),
                             } ).await;
                         }
diff --git a/src/core/botmodules.rs b/src/core/botmodules.rs
index e320e7f..17c570a 100644
--- a/src/core/botmodules.rs
+++ b/src/core/botmodules.rs
@@ -637,12 +637,39 @@ pub struct Routine {
     remaining_iterations : Option<i64> ,
     routine_attr : Vec<RoutineAttr> ,
     pub internal_signal : RoutineSignal ,
+    pub self_routine_ar : Option<RoutineAR> ,
+    pub self_act_ar : Option<ActAR> ,
 }
 
 
 impl Routine {
 
 
+    // pub fn set 
+    // pub async fn refresh_self_ref(self) {
+    pub async fn refresh_routine_internal(routine_ar : RoutineAR) 
+    {
+
+        /*
+        Execute after the Routine is constructed
+        - If not, a start() will also call this 
+
+        */
+
+        // 1. Update the self reference to itself
+
+        let mut mut_lock = routine_ar.write().await;
+        mut_lock.self_routine_ar = Some(routine_ar.clone());
+
+        // 2. Update the current self_act_ar
+        mut_lock.self_act_ar = Some(Arc::new(RwLock::new(BotAction::R(routine_ar.clone()))));
+
+
+
+        
+
+    }
+
 
     pub async fn validate_attr(routine_attr : &Vec<RoutineAttr>) 
     -> Result<String,String> 
@@ -819,6 +846,8 @@ impl Routine {
             remaining_iterations : None ,
             routine_attr : routine_attr ,
             internal_signal : RoutineSignal::NotStarted ,
+            self_routine_ar : None ,
+            self_act_ar : None ,
         }))) ;
 
 
@@ -831,6 +860,10 @@ impl Routine {
     // [ ] => 03.27 - REVIEW FOR COMPLETION
     {
 
+
+        // [x] Prep by updating it's own self reference
+        Routine::refresh_routine_internal(trg_routine_ar.clone()).await;
+
         // [x] Asyncio Spawn likely around here 
         // [x] & Assigns self.join_handle
 
@@ -1005,7 +1038,8 @@ impl Routine {
 
                 
                 // [x] execution body
-                trg_routine_ar.read().await.loopbody().await;
+                // trg_routine_ar.read().await.loopbody().await;
+                trg_routine_ar.write().await.loopbody().await;
 
 
                 { // [x] End of Loop iteration
@@ -1100,7 +1134,7 @@ impl Routine {
             
     }
 
-    async fn loopbody(&self)
+    async fn loopbody(&mut self)
     // [x] => 03.27 - COMPLETED 
     {
         botlog::trace(
@@ -1112,10 +1146,23 @@ impl Routine {
         );
 
         Log::flush();
-        
-        (self.exec_body)(
-            self.parent_params.clone()
+
+        let self_ar = Arc::new(RwLock::new(self));
+
+        {
+            let mut mutlock = self_ar.write().await;
+            
+            mutlock.parent_params.parent_act = Some(mutlock.parent_params.curr_act.clone());
+            mutlock.parent_params.curr_act = mutlock.self_act_ar.to_owned().unwrap();
+        }
+
+        (self_ar.read().await.exec_body)(
+            self_ar.read().await.parent_params.clone()
         ).await;
+        
+        // (self.exec_body)(
+        //     self.parent_params.clone()
+        // ).await;
     }
 
     pub async fn stop(&mut self) -> Result<String,String> 
diff --git a/src/custom/experimental/experiment003.rs b/src/custom/experimental/experiment003.rs
index 9b8f91e..343cece 100644
--- a/src/custom/experimental/experiment003.rs
+++ b/src/custom/experimental/experiment003.rs
@@ -141,28 +141,24 @@ async fn test3_body(params : ExecBodyParams) {
 
     if let Ok(newr) = a {
 
+        // NOTE : The below is a "Playing aound" feature
+        // In the below, we're unnecessarily adjusting the ExecBodyParams of the parent
+
+        // To get the reference to the Routine or the BotAction there are now refernces to itself
+
         // [ ] before execute , be sure to adjust curr_act
 
-        // let mut params_mut = params.clone();
-        let mut params_mut = params;
-        // let newr_ar = Arc::new(RwLock::new(newr));
+        // let mut params_mut = params;
         let newr_ar = newr.clone();
 
-        params_mut.curr_act = Arc::new(RwLock::new(
-            BotAction::R(newr_ar.clone())
-        ));
-
-        // let a = newr_ar.read().await;
-        // let rslt = (&*a).start().await;
-
-        // let mut newr_lock = newr_ar.write().await;
-
-        // let rslt = newr_ar.write().await.start().await;
+        // params_mut.curr_act = Arc::new(RwLock::new(
+        //     BotAction::R(newr_ar.clone())
+        // ));
 
 
-        {
-            newr_ar.write().await.parent_params = params_mut.clone();
-        }
+        // {
+        //     newr_ar.write().await.parent_params = params_mut.clone();
+        // }
 
         let rslt = Routine::start(newr_ar.clone()).await;
 
@@ -179,12 +175,12 @@ async fn test3_body(params : ExecBodyParams) {
                 rsltstr
                 ).as_str(),
             Some("experiment003 > test3_body".to_string()),
-            Some(&params_mut.msg),
+            Some(&params.msg),
         );
 
         Log::flush();
 
-        let bot = Arc::clone(&params_mut.bot);
+        let bot = Arc::clone(&params.bot);
 
         let botlock = bot.read().await;
 
@@ -193,9 +189,9 @@ async fn test3_body(params : ExecBodyParams) {
         .botmgrs
         .chat
         .say_in_reply_to(
-            &params_mut.msg, 
+            &params.msg, 
             format!("Routine Result : {:?}",rsltstr),
-            params_mut.clone()
+            params.clone()
         ).await;
 
         // [x] Will not be handling JoinHandles here . If immediate abort() handling is required, below is an example that works