From b6a75f8d91bcb8ceec9a8ce3b7d0d37c26c7957a Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Tue, 2 Apr 2024 23:35:20 -0400 Subject: [PATCH] enh chat --- src/core/botinstance.rs | 4 +- src/core/chat.rs | 57 ++++++++++++++++++------ src/core/ratelimiter.rs | 4 +- src/custom/experimental/experiment003.rs | 2 +- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/core/botinstance.rs b/src/core/botinstance.rs index 1cd1494..7198d01 100644 --- a/src/core/botinstance.rs +++ b/src/core/botinstance.rs @@ -207,13 +207,13 @@ impl BotInstance { let msgtext = msg.message_text.clone(); - dbg!("will spawn listener_main_prvmsg from runner",msgtext.clone()); + dbg!("will spawn listener_main_prvmsg from runner",chrono::offset::Local::now(),msgtext.clone()); tokio::spawn( async move { // let msgarc_c = msgarc.clone(); BotInstance::listener_main_prvmsg(botarc, &msg.clone()).await; }); // BotInstance::listener_main_prvmsg(Arc::clone(&bot), &msg).await; - dbg!("completed spawn listener_main_prvmsg from runner", msgtext); + dbg!("completed spawn listener_main_prvmsg from runner",chrono::offset::Local::now(), msgtext); } ServerMessage::Whisper(msg) => { botlog::debug( diff --git a/src/core/chat.rs b/src/core/chat.rs index 2ff55c3..5710d3a 100644 --- a/src/core/chat.rs +++ b/src/core/chat.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; +use std::i8; use std::sync::Arc; -use std::num::{self, Wrapping}; +use std::num::{self, Saturating, Wrapping}; +use std::time::Instant; use chrono::NaiveDateTime; use tokio::sync::{Mutex, RwLock}; @@ -68,8 +70,9 @@ pub struct Chat { // outqueue : Arc>> outqueue : (Sender<(BotMsgType,ExecBodyParams)>, Arc>>), - // https://doc.rust-lang.org/std/num/struct.Wrapping.html + // https://doc.rust-lang.org/std/num/struct.Saturating.html // spaceiter : Arc>>, + spaceiter : Arc>, } @@ -92,7 +95,7 @@ impl Chat { let r = Arc::new(RwLock::new(r)); - // let spaceiter = Arc::new(Mutex::new(Wrapping(3))); + let spaceiter = Arc::new(Mutex::new(true)); let chat = Chat { @@ -100,7 +103,7 @@ impl Chat { client, // outqueue : Arc::new(RwLock::new(vec![])), outqueue : (s,r.clone()), - // spaceiter , + spaceiter , }; let chat_ar = Arc::new(RwLock::new(chat)); @@ -679,21 +682,44 @@ impl Chat { ratelimiter::LimiterResp::Allow => { // let maxblanks = rand::thread_rng().gen_range(1..=20); - // let mut blanks_to_add_lock = self.spaceiter.lock().await; - // (*blanks_to_add_lock).0 += 1; + let chat_guard = chat_clone.read().await; + + let mut blanks_to_add_lock = chat_guard.spaceiter.lock().await; + + dbg!("Before blanks to add adjusted",*blanks_to_add_lock); + + // *blanks_to_add_lock += 1; + *blanks_to_add_lock = if *blanks_to_add_lock { false } else { true }; + + dbg!("After blanks to add adjusted",*blanks_to_add_lock); - // for _i in 1..(*blanks_to_add_lock).0 { + let mut outmsg = outmsg; + // for i in 0..*blanks_to_add_lock+1 { + // dbg!("Processing blank spaces : ",i,*blanks_to_add_lock); // // let blankspace: &str = " 󠀀"; // // let blankspace: &str = " ‎ ."; - // let blankspace: &str = "."; - // outmsg.push_str(blankspace); - // } + // if i > 0 { + // // let blankspace: &str = " \u{e0000}"; + // let blankspace: &str = " "; + // outmsg.push_str(blankspace); + // dbg!("Added 'blank' string",outmsg.clone(),blankspace); + // } + // }; - // drop(blanks_to_add_lock); + // let blankspace: &str = " "; + // let (firstword,restofstr) = outmsg.split_once(' '); + outmsg = if let Some((firstword,restofstr)) = outmsg.split_once(' ') { + if *blanks_to_add_lock { format!("{} {}\u{e0000}",firstword,restofstr,) } + else { format!("{} {}",firstword,restofstr) } + } else { outmsg.clone() } ; + + + + drop(blanks_to_add_lock); dbg!("Area that used to add blanks to out message"); - dbg!("Sending the following messag to chat > ", outmsg.clone()); + dbg!("OUT MESSAGE TO CHAT > ", chrono::offset::Local::now(), outmsg.clone()); botlog::trace( &format!("Out Message TO {} >> {}",channel_login.clone(),outmsg.clone()), @@ -708,7 +734,7 @@ impl Chat { BotMsgType::Say(a, _) => { chat_clone.read().await.client.say(a, outmsg.clone()).await.unwrap(); } - BotMsgType::Notif(outmsg) => { + BotMsgType::Notif(_) => { chat_clone.read().await.client.say_in_reply_to(¶ms.read().await.msg, outmsg.clone()).await.unwrap(); } } @@ -752,6 +778,11 @@ impl Chat { Some("Chat > send_botmsg".to_string()), None, ); + + + dbg!("Rate Limiter returned Sleep unexpetedly > Re-enqueueing message"); + // [x] Need to Re-Enqueue the message , but needs to be controlled + chat_clone.read().await.send_botmsg_inner(msginput.clone(),params).await; } } diff --git a/src/core/ratelimiter.rs b/src/core/ratelimiter.rs index dcd4f08..cdfa123 100644 --- a/src/core/ratelimiter.rs +++ b/src/core/ratelimiter.rs @@ -102,13 +102,15 @@ impl RateLimiter { dbg!(matches!(rsp.clone(),LimiterResp::Allow)); + // [-] Comment this area if removing Duplicate checking functionality + /* */ let rsp = if rl_guard.dupmsg(inmsg.clone()) && matches!(rsp,LimiterResp::Allow) { //self.lastmsgdup = String::new(); // LimiterResp::Sleep(DUPMSG_MIN_SEND_S) dbg!("Duplicate detected"); LimiterResp::Sleep(DUPMSG_MIN_SEND_S - rl_guard.lastmsgtimer.elapsed().as_secs_f64() + 0.1) } else { rsp.clone() }; - + /* */ // => 04.02 - Don't update here // // After Dup is checked, Set the lastmsgdup to latest message diff --git a/src/custom/experimental/experiment003.rs b/src/custom/experimental/experiment003.rs index 65ceae5..d76d8fb 100644 --- a/src/custom/experimental/experiment003.rs +++ b/src/custom/experimental/experiment003.rs @@ -158,7 +158,7 @@ async fn countdown_chnl_v1(params : ExecBodyParams) { let routine_attr = vec![ // RoutineAttr::RunOnce - RoutineAttr::MaxIterations(10), + RoutineAttr::MaxIterations(5), RoutineAttr::LoopDuration(Duration::from_secs(1)) ]; // let exec_body = actions_util::asyncbox(rtestbody);