dragonfly_plugin/server/
server.rs

1use tokio::sync::mpsc;
2
3use crate::{
4    event::{EventContext, EventResultUpdate},
5    types::{self, PluginToHost},
6};
7
8#[derive(Clone)]
9pub struct Server {
10    pub plugin_id: String,
11    pub sender: mpsc::Sender<PluginToHost>,
12}
13
14impl Server {
15    /// Helper to build and send a single action.
16    pub async fn send_action(
17        &self,
18        kind: types::action::Kind,
19    ) -> Result<(), mpsc::error::SendError<PluginToHost>> {
20        let action = types::Action {
21            correlation_id: None,
22            kind: Some(kind),
23        };
24        let batch = types::ActionBatch {
25            actions: vec![action],
26        };
27        let msg = PluginToHost {
28            plugin_id: self.plugin_id.clone(),
29            payload: Some(types::PluginPayload::Actions(batch)),
30        };
31        self.sender.send(msg).await
32    }
33
34    /// Helper to send a batch of actions.
35    pub async fn send_actions(
36        &self,
37        actions: Vec<types::Action>,
38    ) -> Result<(), mpsc::error::SendError<PluginToHost>> {
39        let batch = types::ActionBatch { actions };
40        let msg = PluginToHost {
41            plugin_id: self.plugin_id.clone(),
42            payload: Some(types::PluginPayload::Actions(batch)),
43        };
44        self.sender.send(msg).await
45    }
46
47    /// Subscribe to a list of game events.
48    pub async fn subscribe(
49        &self,
50        events: Vec<types::EventType>,
51    ) -> Result<(), mpsc::error::SendError<PluginToHost>> {
52        let sub = types::EventSubscribe {
53            events: events.into_iter().map(|e| e.into()).collect(),
54        };
55        let msg = PluginToHost {
56            plugin_id: self.plugin_id.clone(),
57            payload: Some(types::PluginPayload::Subscribe(sub)),
58        };
59        self.sender.send(msg).await
60    }
61
62    /// Internal helper to send an event result (cancel/mutate)
63    /// This is called by the auto-generated `dispatch_event` function.
64    #[doc(hidden)]
65    pub(crate) async fn send_event_result(
66        &self,
67        context: EventContext<'_, impl Sized>,
68    ) -> Result<(), mpsc::error::SendError<types::PluginToHost>> {
69        let (event_id, result) = context.into_result();
70
71        let payload = match result {
72            // Do nothing if the handler didn't mutate or cancel
73            EventResultUpdate::None => return Ok(()),
74            EventResultUpdate::Cancelled => types::EventResult {
75                event_id,
76                cancel: Some(true),
77                update: None,
78            },
79            EventResultUpdate::Mutated(update) => types::EventResult {
80                event_id,
81                cancel: None,
82                update: Some(update),
83            },
84        };
85
86        let msg = types::PluginToHost {
87            plugin_id: self.plugin_id.clone(),
88            payload: Some(types::PluginPayload::EventResult(payload)),
89        };
90        self.sender.send(msg).await
91    }
92}
93
94mod helpers;