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
use crate::hud::ChatTab;
use common::{
    comp::{ChatMsg, ChatType},
    uid::Uid,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

pub const MAX_CHAT_TABS: usize = 5;
pub const DEFAULT_CHAT_BOX_WIDTH: f64 = 470.0;
pub const DEFAULT_CHAT_BOX_HEIGHT: f64 = 150.0;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ChatFilter {
    //messages
    pub message_all: bool,
    pub message_world: bool,
    pub message_region: bool,
    pub message_say: bool,
    pub message_group: bool,
    pub message_faction: bool,
    //activity (login/logout)
    pub activity_all: bool,
    pub activity_group: bool,
    //deaths
    pub death_all: bool,
    pub death_group: bool,
}
impl ChatFilter {
    pub fn satisfies(&self, chat_msg: &ChatMsg, group_members: &HashSet<&Uid>) -> bool {
        match &chat_msg.chat_type {
            ChatType::Online(u) | ChatType::Offline(u) => {
                self.activity_all || (self.activity_group && group_members.contains(u))
            },
            ChatType::CommandInfo | ChatType::CommandError => true,
            ChatType::Kill(_, u) => self.death_all || self.death_group && group_members.contains(u),
            ChatType::GroupMeta(_) => true,   //todo
            ChatType::FactionMeta(_) => true, //todo
            ChatType::Tell(..) => true,
            ChatType::Say(_) => self.message_all || self.message_say,
            ChatType::Group(..) => self.message_all || self.message_group,
            ChatType::Faction(..) => self.message_all || self.message_faction,
            ChatType::Region(_) => self.message_all || self.message_region,
            ChatType::World(_) => self.message_all || self.message_world,
            ChatType::Npc(..) => true,
            ChatType::NpcSay(..) => true,
            ChatType::NpcTell(..) => true,
            ChatType::Meta => true,
        }
    }
}
impl Default for ChatFilter {
    fn default() -> Self {
        Self {
            message_all: true,
            message_world: true,
            message_region: true,
            message_say: true,
            message_group: true,
            message_faction: true,

            activity_all: false,
            activity_group: true,

            death_all: false,
            death_group: true,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct ChatSettings {
    pub chat_opacity: f32,
    pub chat_character_name: bool,
    pub chat_tabs: Vec<ChatTab>,
    pub chat_tab_index: Option<usize>,
    pub chat_cmd_prefix: char,
    pub chat_pos_x: f64,
    pub chat_pos_y: f64,
    pub chat_size_x: f64,
    pub chat_size_y: f64,
}

impl Default for ChatSettings {
    fn default() -> Self {
        Self {
            chat_opacity: 0.4,
            chat_character_name: true,
            chat_tabs: vec![ChatTab::default()],
            chat_tab_index: Some(0),
            chat_cmd_prefix: '/',
            chat_pos_x: 10.0,
            chat_pos_y: 10.0,
            chat_size_x: DEFAULT_CHAT_BOX_WIDTH,
            chat_size_y: DEFAULT_CHAT_BOX_HEIGHT,
        }
    }
}