aboutsummaryrefslogtreecommitdiff
path: root/karld/src/main.rs
blob: 0983554532bba033ceed53cd7a8838c0d5012a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
pub mod condition;
pub mod interface;

use chrono::NaiveDateTime;
use condition::ConditionFind;
use crossbeam_channel::Sender;
use interface::network_loop;
use karlcommon::{
    ClientboundPacket, Condition, Instance, Property, ProtoError, ServerboundPacket, Task,
};
use log::{debug, info};
use std::{collections::HashMap, sync::RwLock};

fn main() {
    env_logger::init();
    info!("logging");
    TASKS.write().unwrap().insert(
        0,
        Task {
            id: 0,
            name: "Mittagessen im Februar".to_string(),
            description: None,
            tags: vec![],
            priority: 69.0,
            completed: None,
            scheduled: None,
            occurence: Some(Condition::And(vec![
                Condition::Equal {
                    modulus: None,
                    prop: Property::Monthofyear,
                    value: 1,
                },
                Condition::Equal {
                    modulus: None,
                    prop: Property::Hour,
                    value: 12,
                },
            ])),
            deadline: None,
        },
    );
    network_loop();
}

lazy_static::lazy_static! {
    static ref TASKS: RwLock<HashMap<u64, Task>> = RwLock::new(HashMap::new());
}

pub fn handle_packet(client: u32, packet: ServerboundPacket, responder: Sender<ClientboundPacket>) {
    match packet {
        ServerboundPacket::Sync => {
            let _ = responder.send(ClientboundPacket::Sync);
        }
        ServerboundPacket::ListTasks => {
            let _ = responder.send(ClientboundPacket::TaskList(
                TASKS.read().unwrap().values().map(|e| e.clone()).collect(),
            ));
        }
        ServerboundPacket::UpdateTask(t) => {
            TASKS.write().unwrap().insert(t.id, t);
        }
        ServerboundPacket::RemoveTask(i) => {
            if TASKS.write().unwrap().remove(&i).is_none() {
                let _ = responder.send(ClientboundPacket::Error(ProtoError::UnknownTask));
            }
        }
        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(ProtoError::UnknownTask));
                    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));
        }
    }
}