Skip to content

Commit

Permalink
Replace unsafe code in points tree, update lapin
Browse files Browse the repository at this point in the history
Signed-off-by: Miezhiko <Miezhiko@gmail.com>
  • Loading branch information
Miezhiko committed Apr 12, 2022
1 parent a931534 commit 2bfa7e8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Amadeus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "amadeus"
version = "8.0.1"
version = "8.0.3"
authors = [ "Miezhiko" ]
repository = "https://github.com/Miezhiko/Amadeus"
readme = "README.md"
Expand Down
95 changes: 55 additions & 40 deletions Amadeus/src/common/db/trees/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ pub async fn add_points( guild_id: u64
if let Err(awhy) = task::spawn_blocking(move || {
match storage.get(&lump_id) {
Ok(mbdata) => {
if let Some(mut data) = mbdata {
let byte_data: &mut [u8] = data.as_bytes_mut();
if let Some(data) = mbdata {
let byte_data: &[u8] = data.as_bytes();
if let Ok((mut points, _len)) = bincode::decode_from_slice::<Points,_>(byte_data, BINCODE_CONFIG) {
points.count += new_points;
if let Ok(new_bytes) = bincode::encode_to_vec(&points, BINCODE_CONFIG) {
(*byte_data).copy_from_slice(&new_bytes[..]);
match storage.put(&lump_id, &data) {
Ok(added) => {
if added {
error!("error updating points");
if let Ok(lump_data_new) = LumpData::new(new_bytes) {
match storage.put(&lump_id, &lump_data_new) {
Ok(added) => {
if added {
error!("error updating points");
}
}, Err(not_added) => {
error!("failed to add points {not_added}");
}
}, Err(not_added) => {
error!("failed to add points {not_added}");
}
} else {
error!("LumpData error");
}
}
}
Expand Down Expand Up @@ -83,11 +86,14 @@ pub async fn give_points( guild_id: u64
if points.count >= points_count {
points.count -= points_count;
if let Ok(new_bytes) = bincode::encode_to_vec(&points, BINCODE_CONFIG) {
(*byte_data).copy_from_slice(&new_bytes[..]);
if let Ok(added) = storage.put(&lump_id, &data) {
if added {
error!("Some strange error updating giver points");
if let Ok(lump_data_new) = LumpData::new(new_bytes) {
if let Ok(added) = storage.put(&lump_id, &lump_data_new) {
if added {
error!("Some strange error updating giver points");
}
}
} else {
error!("LumpData Error");
}
} else {
error!("Error encoding ponts");
Expand All @@ -99,11 +105,14 @@ pub async fn give_points( guild_id: u64
if let Ok((mut tpoints, _len)) = bincode::decode_from_slice::<Points,_>(tbyte_data, BINCODE_CONFIG) {
tpoints.count += points_count;
if let Ok(tnew_bytes) = bincode::encode_to_vec(&tpoints, BINCODE_CONFIG) {
(*tbyte_data).copy_from_slice(&tnew_bytes[..]);
if let Ok(tadded) = storage.put(&target_lump_id, &tdata) {
if tadded {
error!("Some strange error updating receiver points");
if let Ok(lump_data_new) = LumpData::new(tnew_bytes) {
if let Ok(tadded) = storage.put(&target_lump_id, &lump_data_new) {
if tadded {
error!("Some strange error updating receiver points");
}
}
} else {
error!("LumpData Error");
}
} else {
error!("Error encoding ponts");
Expand Down Expand Up @@ -190,8 +199,8 @@ pub async fn add_win_points( guild_id: u64
match task::spawn_blocking(move || {
match storage.get(&lump_id) {
Ok(mbdata) => {
if let Some(mut data) = mbdata {
let byte_data: &mut [u8] = data.as_bytes_mut();
if let Some(data) = mbdata {
let byte_data: &[u8] = data.as_bytes();
if let Ok((mut points, _len)) = bincode::decode_from_slice::<Points,_>(byte_data, BINCODE_CONFIG) {
points.count += 10;
points.streak += 1;
Expand All @@ -200,20 +209,23 @@ pub async fn add_win_points( guild_id: u64
points.count += 5 * points_multiplier;
}
if let Ok(new_bytes) = bincode::encode_to_vec(&points, BINCODE_CONFIG) {
(*byte_data).copy_from_slice(&new_bytes[..]);
match storage.put(&lump_id, &data) {
Ok(added) => {
if added {
error!("error updating points");
}
if let Err(khm) = storage.journal_sync() {
error!("failed to sync {khm}");
if let Ok(lump_data_tnew) = LumpData::new(new_bytes) {
match storage.put(&lump_id, &lump_data_tnew) {
Ok(added) => {
if added {
error!("error updating points");
}
if let Err(khm) = storage.journal_sync() {
error!("failed to sync {khm}");
}
points.streak
}, Err(ecn) => {
error!("Something wrong with cannyls: {ecn}");
0
}
points.streak
}, Err(ecn) => {
error!("Something wrong with cannyls: {ecn}");
0
}
} else {
error!("Error putting new ponts to LumpData"); 0
}
} else {
error!("Error encoding ponts"); 0
Expand Down Expand Up @@ -261,19 +273,22 @@ pub async fn break_streak( guild_id: u64
if let Err(why) = task::spawn_blocking(move || {
match storage.get(&lump_id) {
Ok(mbdata) => {
if let Some(mut data) = mbdata {
let byte_data: &mut [u8] = data.as_bytes_mut();
if let Some(data) = mbdata {
let byte_data: &[u8] = data.as_bytes();
if let Ok((mut points, _len)) = bincode::decode_from_slice::<Points,_>(byte_data, BINCODE_CONFIG) {
points.streak = 0;
if let Ok(new_bytes) = bincode::encode_to_vec(&points, BINCODE_CONFIG) {
(*byte_data).copy_from_slice(&new_bytes[..]);
if let Ok(added) = storage.put(&lump_id, &data) {
if added {
error!("error updating points");
}
if let Err(khm) = storage.journal_sync() {
error!("failed to sync {khm}");
if let Ok(lump_data_new) = LumpData::new(new_bytes) {
if let Ok(added) = storage.put(&lump_id, &lump_data_new) {
if added {
error!("error updating points");
}
if let Err(khm) = storage.journal_sync() {
error!("failed to sync {khm}");
}
}
} else {
error!("LumpData Error")
}
} else {
error!("Error encoding ponts");
Expand Down
1 change: 1 addition & 0 deletions Amadeus/src/common/salieri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async fn handle_salieri(_ctx: &Context, stream: UnixStream) -> anyhow::Result<()
let s = String::from_utf8(buf)?;
match s.as_str() {
commands::ERROR_HANDLE => (),
commands::RESTART_HANDLE => (),
_ => ()
}
}
Expand Down
28 changes: 15 additions & 13 deletions Amadeus/src/steins/gate/tracking/social.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ pub async fn activate_social_skils(ctx: &Arc<Context>) {
let ctx_clone = Arc::clone(ctx);
tokio::spawn(async move {
loop {
let activity_level = cache::ACTIVITY_LEVEL.load(Ordering::Relaxed) + 20;
let rndx = rand::thread_rng().gen_range(0..activity_level);
if rndx < 2 {
let (chanz, ru) = match rndx {
0 => { (MAIN_CHANNEL, true) },
_ => { ( ChannelId(827151604053835807), true) } // mist
};
let ai_text = chain::generate_with_language(&ctx_clone, ru).await;
if let Err(why) = chanz.send_message(&ctx_clone, |m| {
m.content(ai_text)
}).await {
error!("Failed to post periodic message {why}");
tokio::time::sleep(time::Duration::from_secs(POLL_PERIOD_SECONDS)).await;
{
let activity_level = cache::ACTIVITY_LEVEL.load(Ordering::Relaxed) + 20;
let rndx = rand::thread_rng().gen_range(0..activity_level);
if rndx < 2 {
let (chanz, ru) = match rndx {
0 => { (MAIN_CHANNEL, true) },
_ => { ( ChannelId(827151604053835807), true) } // mist
};
let ai_text = chain::generate_with_language(&ctx_clone, ru).await;
if let Err(why) = chanz.send_message(&ctx_clone, |m| {
m.content(ai_text)
}).await {
error!("Failed to post periodic message {why}");
}
}
}
tokio::time::sleep(time::Duration::from_secs(POLL_PERIOD_SECONDS)).await;
}
});
}
2 changes: 1 addition & 1 deletion Amadeus/src/steins/gate/tracking/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub async fn activate_system_tracker(ctx: &Arc<Context>) {
let ctx_clone = Arc::clone(ctx);
tokio::spawn(async move {
loop {
tokio::time::sleep(time::Duration::from_secs(POLL_PERIOD_SECONDS)).await;
{ // this scope is needed for async locks!
// clean up old bert model conversation id-s
let salieri_lock = SALIERI.lock().await;
Expand Down Expand Up @@ -56,7 +57,6 @@ pub async fn activate_system_tracker(ctx: &Arc<Context>) {
}
}
}
tokio::time::sleep(time::Duration::from_secs(POLL_PERIOD_SECONDS)).await;
}
});
}
1 change: 1 addition & 0 deletions Mozart/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ use crate::SALIERI_SERVICE;
pub const SALIERI_SOCKET: &str = SALIERI_SERVICE;

pub const ERROR_HANDLE: &str = "handle_error";
pub const RESTART_HANDLE: &str = "handle_restart";
2 changes: 2 additions & 0 deletions aka.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3645,6 +3645,7 @@
"comeonhitme#51833": ~
"coolno2#2598": ~
"corax#51334": ~
"core#21549": ~
"coremix#2488": Coremix
"corvus#51685": ~
"craving#21662": ~
Expand Down Expand Up @@ -4545,6 +4546,7 @@
"Игрок#22287": ~
"Игрок#22667": ~
"Инат#2529": ~
"КвасныйДворф#231473": ~
"КвасныйДворф#29446": ~
"Колдунопрыг#26173": ~
"Купить#2616": ~
Expand Down
8 changes: 7 additions & 1 deletion hake.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ main = hake $ do
amadeusExecutable
cargo <| "build" : buildFlagsAmadeus

"install | install to system" [amadeusExecutable]
"install | install to system" [ salieriExecutable
, amadeusExecutable ]
cargo <| "install" : buildFlagsAmadeus

"test | build and test" [amadeusExecutable] do
Expand All @@ -28,6 +29,11 @@ main = hake $ do
rawSystem amadeusExecutable ["--version"]
>>= checkExitCode

"restart | restart services" [ salieriExecutable
, amadeusExecutable ] do
systemctl ["restart", appNameSalieri]
systemctl ["restart", appNameAmadeus]

where
appNameSalieri String
appNameSalieri = "salieri"
Expand Down

0 comments on commit 2bfa7e8

Please sign in to comment.