diff options
Diffstat (limited to 'karld/src')
-rw-r--r-- | karld/src/condition.rs | 2 | ||||
-rw-r--r-- | karld/src/main.rs | 43 |
2 files changed, 41 insertions, 4 deletions
diff --git a/karld/src/condition.rs b/karld/src/condition.rs index 5874e7b..a989a25 100644 --- a/karld/src/condition.rs +++ b/karld/src/condition.rs @@ -24,7 +24,7 @@ pub enum Direction { Backward, } -trait ConditionFind { +pub trait ConditionFind { fn find(&self, edge: Edge, dir: Direction, from: NaiveDateTime) -> Option<NaiveDateTime>; } diff --git a/karld/src/main.rs b/karld/src/main.rs index f70b7e7..1bc5798 100644 --- a/karld/src/main.rs +++ b/karld/src/main.rs @@ -1,9 +1,11 @@ pub mod condition; pub mod interface; +use chrono::NaiveDateTime; +use condition::ConditionFind; use crossbeam_channel::Sender; use interface::network_loop; -use karlcommon::{ClientboundPacket, Condition, Property, ServerboundPacket, Task}; +use karlcommon::{ClientboundPacket, Condition, Instance, Property, ServerboundPacket, Task}; use log::{debug, info}; use std::{collections::HashMap, sync::RwLock}; @@ -44,8 +46,8 @@ lazy_static::lazy_static! { pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<ClientboundPacket>) { match packet { - ServerboundPacket::Download => { - let _ = responder.send(ClientboundPacket::DownloadResponse( + ServerboundPacket::ListTasks => { + let _ = responder.send(ClientboundPacket::TaskList( TASKS.read().unwrap().values().map(|e| e.clone()).collect(), )); } @@ -58,5 +60,40 @@ pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<C ServerboundPacket::Handshake { version } => { debug!("{client}: version {version}"); } + ServerboundPacket::ListInstances { range, task, limit } => { + let t = match TASKS.read().unwrap().get(&task).cloned() { + Some(t) => t, + None => { + let _ = + responder.send(ClientboundPacket::Error("task does not exist".to_string())); + return; + } + }; + + let mut ocs = vec![]; + if let Some(o) = &t.occurence { + let mut time = NaiveDateTime::from_timestamp(range.start.unwrap_or(0), 0); + let end_time = range.end.map(|e| NaiveDateTime::from_timestamp(e, 0)); + for _ in 0..limit { + let start = o.find(condition::Edge::Start, condition::Direction::Forward, time); + let end = o.find(condition::Edge::End, condition::Direction::Forward, time); + ocs.push(Instance { + of: t.id, + at: start.map(|e| e.timestamp())..end.map(|e| e.timestamp()), + }); + if let Some(s) = end { + if let Some(e) = end_time { + if s > e { + break; + } + } + time = s; + } else { + break; + } + } + } + let _ = responder.send(ClientboundPacket::InstanceList(ocs)); + } } } |