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
use crate::client::Client;
use common::event::RequestSiteInfoEvent;
use common_net::msg::{world_msg::EconomyInfo, ServerGeneral};
#[cfg(feature = "plugins")]
use common_state::plugin::PluginMgr;
#[cfg(feature = "worldgen")]
use specs::ReadExpect;
use specs::{DispatcherBuilder, ReadStorage};
use std::collections::HashMap;
#[cfg(feature = "worldgen")]
use world::IndexOwned;

use super::{event_dispatch, ServerEvent};

pub(super) fn register_event_systems(builder: &mut DispatcherBuilder) {
    event_dispatch::<RequestSiteInfoEvent>(builder, &[]);
    #[cfg(feature = "plugins")]
    event_dispatch::<common::event::RequestPluginsEvent>(builder, &[]);
}

#[cfg(not(feature = "worldgen"))]
impl ServerEvent for RequestSiteInfoEvent {
    type SystemData<'a> = ReadStorage<'a, Client>;

    fn handle(events: impl ExactSizeIterator<Item = Self>, clients: Self::SystemData<'_>) {
        for ev in events {
            if let Some(client) = clients.get(ev.entity) {
                let info = EconomyInfo {
                    id: ev.id,
                    population: 0,
                    stock: HashMap::new(),
                    labor_values: HashMap::new(),
                    values: HashMap::new(),
                    labors: Vec::new(),
                    last_exports: HashMap::new(),
                    resources: HashMap::new(),
                };
                let msg = ServerGeneral::SiteEconomy(info);
                client.send_fallible(msg);
            }
        }
    }
}

#[cfg(feature = "worldgen")]
impl ServerEvent for RequestSiteInfoEvent {
    type SystemData<'a> = (ReadExpect<'a, IndexOwned>, ReadStorage<'a, Client>);

    fn handle(events: impl ExactSizeIterator<Item = Self>, (index, clients): Self::SystemData<'_>) {
        for ev in events {
            if let Some(client) = clients.get(ev.entity) {
                let site_id = index.sites.recreate_id(ev.id);
                let info = if let Some(site_id) = site_id {
                    let site = index.sites.get(site_id);
                    site.economy.get_information(site_id)
                } else {
                    EconomyInfo {
                        id: ev.id,
                        population: 0,
                        stock: HashMap::new(),
                        labor_values: HashMap::new(),
                        values: HashMap::new(),
                        labors: Vec::new(),
                        last_exports: HashMap::new(),
                        resources: HashMap::new(),
                    }
                };
                let msg = ServerGeneral::SiteEconomy(info);
                client.send_fallible(msg);
            }
        }
    }
}

/// Send missing plugins to the client
#[cfg(feature = "plugins")]
impl ServerEvent for common::event::RequestPluginsEvent {
    type SystemData<'a> = (ReadExpect<'a, PluginMgr>, ReadStorage<'a, Client>);

    fn handle(
        events: impl ExactSizeIterator<Item = Self>,
        (plugin_mgr, clients): Self::SystemData<'_>,
    ) {
        for mut ev in events {
            let Some(client) = clients.get(ev.entity) else {
                continue;
            };

            for hash in ev.plugins.drain(..) {
                if let Some(plugin) = plugin_mgr.find(&hash) {
                    let buf = Vec::from(plugin.data_buf());
                    // TODO: @perf We could possibly make this more performant by caching prepared
                    // messages for each plugin.
                    client
                        .send(ServerGeneral::PluginData(buf))
                        .unwrap_or_else(|e| {
                            tracing::warn!("Error {e} sending plugin {hash:?} to client")
                        });
                }
            }
        }
    }
}