From 2f2a9e40aed64e04447e5f5e52c007aa4f9f6328 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 11 Jul 2024 09:44:29 +0200 Subject: [PATCH] pending request system --- jiji.project | 2 +- src/bot.rs | 6 ++++++ src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- src/postman.rs | 4 +++- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/jiji.project b/jiji.project index b2beb0a..a1f3c69 100644 --- a/jiji.project +++ b/jiji.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"get incoming messages","description":"read","id":2},{"name":"sort messages by origin","description":"// Hello there","id":3},{"name":"fetch previous messages","description":"// Hello there","id":4},{"name":"ability to write messages","description":"// Hello there","id":5}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1}]},{"name":"bugs","content":[]},{"name":"+","content":[]}]} \ No newline at end of file +{"categories":[{"name":"to do","content":[{"name":"get incoming messages","description":"read","id":2},{"name":"ability to write messages","description":"// Hello there","id":5},{"name":"ability to change token","description":"use a config file to store token so that \n\n1- it is away from github\n\n2- it is configurable if need be","id":1}]},{"name":"in progress","content":[{"name":"fetch previous messages","description":"// Hello there","id":4}]},{"name":"done","content":[{"name":"run discord bot","description":"make it so the bot is running","id":1},{"name":"fixed token in github","description":"// Hello there","id":1}]},{"name":"bugs","content":[]},{"name":"+","content":[]}]} \ No newline at end of file diff --git a/src/bot.rs b/src/bot.rs index 3714dd4..0177aee 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -103,6 +103,7 @@ async fn check_packets(context: &Context) { sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet"); } println!("bot : sent channels"); + sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet"); } else { println!("bot : failed to retrieve sender"); } @@ -124,6 +125,11 @@ async fn check_packets(context: &Context) { } postman::Packet::FetchMessages(guild_id_str, channel_id_str) => { println!("bot : received FetchMessages packet, for guild '{}', channel : '{}'", guild_id_str, channel_id_str); + if let Some(sender) = context.data.read().await.get::() { + sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet"); + } else { + println!("bot : failed to retrieve sender"); + } } _ => { println!("bot : unhandled packet"); diff --git a/src/main.rs b/src/main.rs index c5a369a..826386d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod postman; mod discord_structure; const MAX_FPS: f32 = 30.0; +const RUNNING_REQUEST_REFRESH_DELAY: f32 = 0.2; fn main() { let (bot_tx, gui_rx) = mpsc::channel::(); //tx transmiter @@ -45,6 +46,8 @@ struct Jiji { guilds: Vec, selected_guild: Option, selected_channel: Option, + time_watch: f32, + pending_bot_requests: usize, } impl Jiji { @@ -56,6 +59,8 @@ impl Jiji { guilds: vec![], selected_guild: None, selected_channel: None, + time_watch: 0.0, + pending_bot_requests: 0, } } } @@ -86,13 +91,27 @@ impl eframe::App for Jiji { postman::Packet::Message(message) => { println!("gui : message received : '{}'", message.content); } + postman::Packet::FinishedRequest => { + self.pending_bot_requests = self.pending_bot_requests.checked_sub(1).unwrap_or(0); + } _ => { println!("unhandled packet"); } } } + + self.draw_selection(ctx); + + self.draw_infobar(ctx); self.draw_feed(ctx); + + self.time_watch = self.next_frame.elapsed().as_micros() as f32 / 1000.0; + + if self.pending_bot_requests > 0 && !ctx.input(|i| i.wants_repaint()) { + thread::sleep(time::Duration::from_secs_f32(RUNNING_REQUEST_REFRESH_DELAY)); + egui::Context::request_repaint(ctx); + } } fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) { @@ -101,7 +120,7 @@ impl eframe::App for Jiji { } impl Jiji { - pub fn draw_feed(&mut self, ctx: &egui::Context) { + pub fn draw_selection(&mut self, ctx: &egui::Context) { egui::TopBottomPanel::top("server_selection") .resizable(false) .show(ctx, |ui| { @@ -126,6 +145,8 @@ impl Jiji { self.selected_guild = Some(i); if self.guilds[i].channels.len() == 0 { let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone())); + + self.pending_bot_requests += 1; } } } @@ -155,6 +176,8 @@ impl Jiji { if self.guilds[*selected_guild_index].channels[i].messages.len() == 0 { let _ = self.sender.send(postman::Packet::FetchMessages(self.guilds[*selected_guild_index].id.clone(), self.guilds[*selected_guild_index].channels[i].id.clone())); + self.pending_bot_requests += 1; + self.guilds[*selected_guild_index].channels[i].greetings(); } } @@ -165,8 +188,30 @@ impl Jiji { } }); }); + + } + + pub fn draw_infobar(&mut self, ctx: &egui::Context) { + egui::TopBottomPanel::bottom("infobar") + .resizable(false) + .show(ctx, |ui| { + ui.horizontal(|ui| { + ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| { + ui.label(&format!("time per frame : {:.1} ms", self.time_watch)); + + ui.with_layout(egui::Layout::left_to_right(egui::Align::TOP), |ui| { + if self.pending_bot_requests > 0 { + ui.label(&format!(" {} pending", self.pending_bot_requests)); + } + }); + }); + }); + }); + } + + pub fn draw_feed(&mut self, ctx: &egui::Context) { egui::CentralPanel::default().show(ctx, |ui| { - ui.label("General Kenobi"); + ui.label(""); }); } } diff --git a/src/postman.rs b/src/postman.rs index f1f94d5..4a06a09 100644 --- a/src/postman.rs +++ b/src/postman.rs @@ -8,7 +8,9 @@ pub enum Packet { Channel(discord_structure::Channel), Message(discord_structure::Message), FetchChannels(String), - FetchMessages(String, String) + FetchMessages(String, String), + FinishedRequest, + } pub struct Sender;