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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use chrono::{DateTime, Utc};
use common::{
    comp,
    comp::{chat::KillType, ChatType, Content, Group, Player, UnresolvedChatMsg},
    uid::IdMaps,
    uuid::Uuid,
};
use serde::{Deserialize, Serialize};
use specs::{Join, World, WorldExt};
use std::{collections::VecDeque, ops::Sub, sync::Arc, time::Duration};
use tokio::sync::Mutex;
use tracing::{info_span, Instrument};

#[derive(Clone, Serialize, Deserialize)]
pub struct PlayerInfo {
    uuid: Uuid,
    alias: String,
}

/// Enum representing death reasons
///
/// All variants should be strictly typed, no string content.
#[derive(Clone, Serialize, Deserialize)]
pub enum KillSource {
    Player(PlayerInfo, KillType),
    NonPlayer(String, KillType),
    NonExistent(KillType),
    FallDamage,
    Suicide,
    Other,
}

#[derive(Clone, Serialize, Deserialize)]
/// partially mapped to common::comp::ChatMsg
pub enum ChatParties {
    Online(PlayerInfo),
    Offline(PlayerInfo),
    CommandInfo(PlayerInfo),
    CommandError(PlayerInfo),
    Kill(KillSource, PlayerInfo),
    GroupMeta(Vec<PlayerInfo>),
    Group(PlayerInfo, Vec<PlayerInfo>),
    Tell(PlayerInfo, PlayerInfo),
    Say(PlayerInfo),
    FactionMeta(String),
    Faction(PlayerInfo, String),
    Region(PlayerInfo),
    World(PlayerInfo),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub time: DateTime<Utc>,
    pub parties: ChatParties,
    pub content: Content,
}

type MessagesStore = Arc<Mutex<VecDeque<ChatMessage>>>;

/// The chat cache gets it data from the gameserver and will keep it for some
/// time It will be made available for its consumers, the REST Api
#[derive(Clone)]
pub struct ChatCache {
    pub messages: MessagesStore,
}

/// Will internally run on tokio and take stress from main loop
struct ChatForwarder {
    chat_r: tokio::sync::mpsc::Receiver<ChatMessage>,
    messages: MessagesStore,
    keep_duration: chrono::Duration,
}

pub struct ChatExporter {
    chat_s: tokio::sync::mpsc::Sender<ChatMessage>,
}

impl ChatMessage {
    fn new(chatmsg: &UnresolvedChatMsg, parties: ChatParties) -> Self {
        ChatMessage {
            time: Utc::now(),
            content: chatmsg.content().clone(),
            parties,
        }
    }
}

impl ChatExporter {
    pub fn generate(chatmsg: &UnresolvedChatMsg, ecs: &World) -> Option<ChatMessage> {
        let id_maps = ecs.read_resource::<IdMaps>();
        let players = ecs.read_storage::<Player>();
        let player_info_from_uid = |uid| {
            id_maps
                .uid_entity(uid)
                .and_then(|entry| players.get(entry))
                .map(|player| PlayerInfo {
                    alias: player.alias.clone(),
                    uuid: player.uuid(),
                })
        };
        let group_members_from_group = |g| -> Vec<_> {
            let groups = ecs.read_storage::<Group>();
            (&players, &groups)
                .join()
                .filter_map(|(player, group)| {
                    if g == group {
                        Some(PlayerInfo {
                            alias: player.alias.clone(),
                            uuid: player.uuid(),
                        })
                    } else {
                        None
                    }
                })
                .collect()
        };

        match &chatmsg.chat_type {
            ChatType::Offline(from) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(chatmsg, ChatParties::Offline(player_info)));
                }
            },
            ChatType::Online(from) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(chatmsg, ChatParties::Online(player_info)));
                }
            },
            ChatType::Region(from) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(chatmsg, ChatParties::Region(player_info)));
                }
            },
            ChatType::World(from) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(chatmsg, ChatParties::World(player_info)));
                }
            },
            ChatType::Say(from) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(chatmsg, ChatParties::Say(player_info)));
                }
            },
            ChatType::Tell(from, to) => {
                if let (Some(from_player_info), Some(to_player_info)) =
                    (player_info_from_uid(*from), player_info_from_uid(*to))
                {
                    return Some(ChatMessage::new(
                        chatmsg,
                        ChatParties::Tell(from_player_info, to_player_info),
                    ));
                }
            },
            ChatType::Kill(kill_source, from) => {
                let kill_source = match kill_source.clone() {
                    comp::chat::KillSource::Player(uid, t) => {
                        if let Some(player_info) = player_info_from_uid(uid) {
                            KillSource::Player(player_info, t)
                        } else {
                            return None;
                        }
                    },
                    comp::chat::KillSource::NonPlayer(str, t) => KillSource::NonPlayer(str, t),
                    comp::chat::KillSource::NonExistent(t) => KillSource::NonExistent(t),
                    comp::chat::KillSource::FallDamage => KillSource::FallDamage,
                    comp::chat::KillSource::Suicide => KillSource::Suicide,
                    comp::chat::KillSource::Other => KillSource::Other,
                };
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(
                        chatmsg,
                        ChatParties::Kill(kill_source, player_info),
                    ));
                }
            },
            ChatType::FactionMeta(s) => {
                return Some(ChatMessage::new(
                    chatmsg,
                    ChatParties::FactionMeta(s.clone()),
                ));
            },
            ChatType::Faction(from, s) => {
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(
                        chatmsg,
                        ChatParties::Faction(player_info, s.clone()),
                    ));
                }
            },
            ChatType::GroupMeta(g) => {
                let members = group_members_from_group(g);
                return Some(ChatMessage::new(chatmsg, ChatParties::GroupMeta(members)));
            },
            ChatType::Group(from, g) => {
                let members = group_members_from_group(g);
                if let Some(player_info) = player_info_from_uid(*from) {
                    return Some(ChatMessage::new(
                        chatmsg,
                        ChatParties::Group(player_info, members),
                    ));
                }
            },
            _ => (),
        };

        None
    }

    pub fn send(&self, msg: ChatMessage) {
        if let Err(e) = self.chat_s.blocking_send(msg) {
            tracing::warn!(
                ?e,
                "could not export chat message. the tokio sender seems to be broken"
            );
        }
    }
}

impl ChatForwarder {
    async fn run(mut self) {
        while let Some(msg) = self.chat_r.recv().await {
            let drop_older_than = msg.time.sub(self.keep_duration);
            let mut messages = self.messages.lock().await;
            while let Some(msg) = messages.front()
                && msg.time < drop_older_than
            {
                messages.pop_front();
            }
            messages.push_back(msg);
            const MAX_CACHE_MESSAGES: usize = 10_000; // in case we have a short spam of many many messages, we dont want to keep the capacity forever
            if messages.capacity() > messages.len() + MAX_CACHE_MESSAGES {
                let msg_count = messages.len();
                tracing::debug!(?msg_count, "shrinking cache");
                messages.shrink_to_fit();
            }
        }
    }
}

impl ChatCache {
    pub fn new(keep_duration: Duration, runtime: &tokio::runtime::Runtime) -> (Self, ChatExporter) {
        const BUFFER_SIZE: usize = 1_000;
        let (chat_s, chat_r) = tokio::sync::mpsc::channel(BUFFER_SIZE);
        let messages: Arc<Mutex<VecDeque<ChatMessage>>> = Default::default();
        let messages_clone = Arc::clone(&messages);
        let keep_duration = chrono::Duration::from_std(keep_duration).unwrap();

        let worker = ChatForwarder {
            keep_duration,
            chat_r,
            messages: messages_clone,
        };

        runtime.spawn(worker.run().instrument(info_span!("chat_forwarder")));

        (Self { messages }, ChatExporter { chat_s })
    }
}