pending request system

This commit is contained in:
WanderingPenwing 2024-07-11 09:44:29 +02:00
parent a431f09c42
commit 2f2a9e40ae
4 changed files with 57 additions and 4 deletions

View file

@ -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":[]}]} {"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":[]}]}

View file

@ -103,6 +103,7 @@ async fn check_packets(context: &Context) {
sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet"); sender.send(postman::Packet::Channel(discord_channel)).expect("Failed to send packet");
} }
println!("bot : sent channels"); println!("bot : sent channels");
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} else { } else {
println!("bot : failed to retrieve sender"); 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) => { postman::Packet::FetchMessages(guild_id_str, channel_id_str) => {
println!("bot : received FetchMessages packet, for guild '{}', channel : '{}'", 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::<postman::Sender>() {
sender.send(postman::Packet::FinishedRequest).expect("Failed to send packet");
} else {
println!("bot : failed to retrieve sender");
}
} }
_ => { _ => {
println!("bot : unhandled packet"); println!("bot : unhandled packet");

View file

@ -9,6 +9,7 @@ mod postman;
mod discord_structure; mod discord_structure;
const MAX_FPS: f32 = 30.0; const MAX_FPS: f32 = 30.0;
const RUNNING_REQUEST_REFRESH_DELAY: f32 = 0.2;
fn main() { fn main() {
let (bot_tx, gui_rx) = mpsc::channel::<postman::Packet>(); //tx transmiter let (bot_tx, gui_rx) = mpsc::channel::<postman::Packet>(); //tx transmiter
@ -45,6 +46,8 @@ struct Jiji {
guilds: Vec<discord_structure::Guild>, guilds: Vec<discord_structure::Guild>,
selected_guild: Option<usize>, selected_guild: Option<usize>,
selected_channel: Option<usize>, selected_channel: Option<usize>,
time_watch: f32,
pending_bot_requests: usize,
} }
impl Jiji { impl Jiji {
@ -56,6 +59,8 @@ impl Jiji {
guilds: vec![], guilds: vec![],
selected_guild: None, selected_guild: None,
selected_channel: 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) => { postman::Packet::Message(message) => {
println!("gui : message received : '{}'", message.content); 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"); println!("unhandled packet");
} }
} }
} }
self.draw_selection(ctx);
self.draw_infobar(ctx);
self.draw_feed(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>) { fn on_exit(&mut self, _gl: std::option::Option<&eframe::glow::Context>) {
@ -101,7 +120,7 @@ impl eframe::App for Jiji {
} }
impl 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") egui::TopBottomPanel::top("server_selection")
.resizable(false) .resizable(false)
.show(ctx, |ui| { .show(ctx, |ui| {
@ -126,6 +145,8 @@ impl Jiji {
self.selected_guild = Some(i); self.selected_guild = Some(i);
if self.guilds[i].channels.len() == 0 { if self.guilds[i].channels.len() == 0 {
let _ = self.sender.send(postman::Packet::FetchChannels(self.guilds[i].id.clone())); 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 { 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())); 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(); 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| { egui::CentralPanel::default().show(ctx, |ui| {
ui.label("General Kenobi"); ui.label("");
}); });
} }
} }

View file

@ -8,7 +8,9 @@ pub enum Packet {
Channel(discord_structure::Channel), Channel(discord_structure::Channel),
Message(discord_structure::Message), Message(discord_structure::Message),
FetchChannels(String), FetchChannels(String),
FetchMessages(String, String) FetchMessages(String, String),
FinishedRequest,
} }
pub struct Sender; pub struct Sender;