aboutsummaryrefslogtreecommitdiffstats
path: root/src/web_server/bot_executor.rs
blob: fde3c080d33556bbd54cd40250f4d34f598bb78b (plain) (blame)
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
use std::sync::Arc;

use actix::{Actor, Handler, Message, SyncContext};

use crate::bot::MasterBot;
use crate::web_server::BotData;

pub struct BotExecutor(pub Arc<MasterBot>);

impl Actor for BotExecutor {
    type Context = SyncContext<Self>;
}

pub struct BotNameListRequest;

impl Message for BotNameListRequest {
    // A plain Vec does not work for some reason
    type Result = Result<Vec<String>, ()>;
}

impl Handler<BotNameListRequest> for BotExecutor {
    type Result = Result<Vec<String>, ()>;

    fn handle(&mut self, _: BotNameListRequest, _: &mut Self::Context) -> Self::Result {
        let bot = &self.0;

        Ok(bot.bot_names())
    }
}

pub struct BotDataListRequest;

impl Message for BotDataListRequest {
    // A plain Vec does not work for some reason
    type Result = Result<Vec<BotData>, ()>;
}

impl Handler<BotDataListRequest> for BotExecutor {
    type Result = Result<Vec<BotData>, ()>;

    fn handle(&mut self, _: BotDataListRequest, _: &mut Self::Context) -> Self::Result {
        let bot = &self.0;

        Ok(bot.bot_datas())
    }
}

pub struct BotDataRequest(pub String);

impl Message for BotDataRequest {
    type Result = Option<BotData>;
}

impl Handler<BotDataRequest> for BotExecutor {
    type Result = Option<BotData>;

    fn handle(&mut self, r: BotDataRequest, _: &mut Self::Context) -> Self::Result {
        let name = r.0;
        let bot = &self.0;

        bot.bot_data(name)
    }
}