diff options
Diffstat (limited to 'karld/src/main.rs')
-rw-r--r-- | karld/src/main.rs | 91 |
1 files changed, 67 insertions, 24 deletions
diff --git a/karld/src/main.rs b/karld/src/main.rs index 0983554..2d0c9e9 100644 --- a/karld/src/main.rs +++ b/karld/src/main.rs @@ -1,12 +1,15 @@ pub mod condition; +pub mod helper; pub mod interface; +pub mod schedule; use chrono::NaiveDateTime; use condition::ConditionFind; use crossbeam_channel::Sender; +use helper::Overlaps; use interface::network_loop; use karlcommon::{ - ClientboundPacket, Condition, Instance, Property, ProtoError, ServerboundPacket, Task, + ClientboundPacket, Condition, Instance, Property, ProtoError, Schedule, ServerboundPacket, Task, }; use log::{debug, info}; use std::{collections::HashMap, sync::RwLock}; @@ -20,11 +23,8 @@ fn main() { id: 0, name: "Mittagessen im Februar".to_string(), description: None, - tags: vec![], - priority: 69.0, - completed: None, - scheduled: None, - occurence: Some(Condition::And(vec![ + tags: vec!["Essen".to_string(), "Unwichtig".to_string()], + schedule: Schedule::Condition(Condition::And(vec![ Condition::Equal { modulus: None, prop: Property::Monthofyear, @@ -36,7 +36,37 @@ fn main() { value: 12, }, ])), - deadline: None, + }, + ); + TASKS.write().unwrap().insert( + 1, + Task { + id: 1, + name: "Abendessen oder Frühstück".to_string(), + description: Some("Nom nom nom".to_string()), + tags: vec!["Essen".to_string()], + schedule: Schedule::Condition(Condition::Or(vec![ + Condition::Equal { + modulus: None, + prop: Property::Hour, + value: 18, + }, + Condition::Equal { + modulus: None, + prop: Property::Hour, + value: 7, + }, + ])), + }, + ); + TASKS.write().unwrap().insert( + 2, + Task { + id: 2, + description: None, + name: "Wichtiger termin™".to_string(), + tags: vec![], + schedule: Schedule::Static(1654997366..1655007366), }, ); network_loop(); @@ -77,25 +107,38 @@ pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<C }; 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; + match t.schedule { + Schedule::Never => (), + Schedule::Dynamic { .. } => (), // TODO + Schedule::Static(r) => { + if range.overlaps(r.clone()) { + ocs.push(Instance { + of: t.id, + at: Some(r.start)..Some(r.end), + }) + } + } + Schedule::Condition(o) => { + 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; } - time = s; - } else { - break; } } } |