diff options
author | metamuffin <yvchraiqi@protonmail.com> | 2022-06-10 17:02:31 +0200 |
---|---|---|
committer | metamuffin <yvchraiqi@protonmail.com> | 2022-06-10 17:02:31 +0200 |
commit | d65b915f3dfda28aad6f2806df38c8ad77135d8b (patch) | |
tree | 46c57595ec7d64156b909e515a65f20fc6c0c7ff /karld/src/main.rs | |
parent | ee1116ffb12887d1ad985b67887c910f58202c1f (diff) | |
download | karlender-d65b915f3dfda28aad6f2806df38c8ad77135d8b.tar karlender-d65b915f3dfda28aad6f2806df38c8ad77135d8b.tar.bz2 karlender-d65b915f3dfda28aad6f2806df38c8ad77135d8b.tar.zst |
more code
Diffstat (limited to 'karld/src/main.rs')
-rw-r--r-- | karld/src/main.rs | 43 |
1 files changed, 40 insertions, 3 deletions
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)); + } } } |