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
use std::time::{Duration, Instant};

use async_trait::async_trait;

use crate::{
    api::Client,
    error::APIError,
    models::{clan, player},
    war::War,
};

#[async_trait]
#[allow(unused_variables)]
pub trait EventHandler {
    async fn player(&self, old_player: Option<player::Player>, new_player: player::Player) {}
    async fn clan(&self, old_clan: Option<clan::Clan>, new_clan: clan::Clan) {}
    async fn war(&self, old_war: Option<War>, new_war: War) {}
    async fn on_error(&self, error: APIError, tag: String, event_type: EventType);
}

#[derive(Debug)]
pub struct EventsListenerBuilder {
    event_type: Vec<EventType>,
    client: Client,
}

#[derive(Debug, Clone)]
pub enum EventType {
    Player(String, Instant, Option<player::Player>),
    Clan(String, Instant, Option<clan::Clan>),
    War(String, Instant, Option<War>),
}

impl std::fmt::Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Player(tag, _, _) => write!(f, "PlayerEvent({tag})"),
            Self::Clan(tag, _, _) => write!(f, "ClanEvent({tag})"),
            Self::War(tag, _, _) => write!(f, "WarEvent({tag})"),
        }
    }
}

impl EventsListenerBuilder {
    #[must_use]
    pub const fn new(client: Client) -> Self {
        Self { event_type: vec![], client }
    }

    #[must_use]
    pub fn add_clan(mut self, tag: &str) -> Self {
        self.event_type.push(EventType::Clan(tag.to_string(), Instant::now(), None));
        self
    }

    #[must_use]
    pub fn add_player(mut self, tag: &str) -> Self {
        self.event_type.push(EventType::Player(tag.to_string(), Instant::now(), None));
        self
    }

    #[must_use]
    pub fn add_war(mut self, tag: &str) -> Self {
        self.event_type.push(EventType::War(tag.to_string(), Instant::now(), None));
        self
    }

    #[must_use]
    pub fn add_clans(mut self, tags: Vec<impl ToString>) -> Self {
        // since add_clan takes self by value, we have to use a for loop
        for tag in tags {
            self.event_type.push(EventType::Clan(tag.to_string(), Instant::now(), None));
        }
        self
    }

    #[must_use]
    pub fn add_players(mut self, tags: Vec<impl ToString>) -> Self {
        // since add_player takes self by value, we have to use a for loop
        for tag in tags {
            self.event_type.push(EventType::Player(tag.to_string(), Instant::now(), None));
        }

        self
    }

    #[must_use]
    pub fn add_wars(&mut self, tags: Vec<impl ToString>) -> &mut Self {
        // since add_war takes self by value, we have to use a for loop
        for tag in tags {
            self.event_type.push(EventType::War(tag.to_string(), Instant::now(), None));
        }
        self
    }

    pub fn build<T>(self, handler: T) -> EventsListener<T>
    where
        T: EventHandler + Sync + Send,
    {
        EventsListener { event_type: self.event_type, client: self.client, handler }
    }
}

pub struct EventsListener<T>
where
    T: EventHandler + Sync + Send,
{
    event_type: Vec<EventType>,
    client: Client,
    handler: T,
}

pub struct EventsError {
    api_error: APIError,
    tag: String,
    event_type: EventType,
    index: usize,
}

impl std::fmt::Display for EventsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error while handling event `{}`: [{}]", self.event_type, self.api_error)
    }
}

impl std::fmt::Debug for EventsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Error while handling event `{}`: [{}]", self.event_type, self.api_error)
    }
}

impl std::error::Error for EventsError {}

impl<T> EventsListener<T>
where
    T: EventHandler + Sync + Send,
{
    /// Start the events listener, note that if duration is None, it will run forever
    ///
    /// # Errors
    ///
    /// This function will return an error if [`Client::get_player`], [`Client::get_clan`], or [`Client::get_current_war`] fails
    pub async fn start(mut self, duration: Option<Duration>) -> Result<(), EventsError> {
        if let Some(duration) = duration {
            let start = Instant::now();
            while start.elapsed() < duration {
                if let Err(e) = self.fire_events().await {
                    self.event_type.remove(e.index);
                    self.handler.on_error(e.api_error, e.tag, e.event_type).await;
                };
            }
        } else {
            loop {
                if let Err(e) = self.fire_events().await {
                    self.event_type.remove(e.index);
                    self.handler.on_error(e.api_error, e.tag, e.event_type).await;
                };
            }
        }

        Ok(())
    }

    #[inline(always)]
    const fn should_fire_again(duration: Duration, seconds: u64) -> bool {
        duration.as_secs() >= seconds
    }

    /// Returns true if a new event was fired for any [`EventType`]
    ///
    /// # Errors
    ///
    /// This function will return an error if [`crate::api::Client::get_player`] [`crate::api::Client::get_clan`], or [`crate::api::Client::get_current_war`] fails
    async fn fire_events(&mut self) -> Result<bool, EventsError> {
        for (i, event) in self.event_type.iter().enumerate() {
            match event {
                EventType::Player(tag, last_fired, old) => {
                    if let Some(duration) = Instant::now().checked_duration_since(*last_fired) {
                        if Self::should_fire_again(duration, 10) {
                            return match self.client.get_player(tag).await {
                                Ok(new) => {
                                    self.handler.player(old.clone(), new.clone()).await; // invoking the handler function the user defined
                                    self.event_type[i] =
                                        EventType::Player(tag.clone(), Instant::now(), Some(new));
                                    Ok(true)
                                }
                                Err(err) => Err(EventsError {
                                    api_error: err,
                                    tag: tag.clone(),
                                    event_type: event.clone(),
                                    index: i,
                                }),
                            };
                        }
                    };
                }
                EventType::Clan(tag, last_fired, old) => {
                    if let Some(duration) = Instant::now().checked_duration_since(*last_fired) {
                        if Self::should_fire_again(duration, 10) {
                            return match self.client.get_clan(tag).await {
                                Ok(new) => {
                                    self.handler.clan(old.clone(), new.clone()).await; // invoking the handler function the user defined
                                    self.event_type[i] =
                                        EventType::Clan(tag.clone(), Instant::now(), Some(new));
                                    Ok(true)
                                }
                                Err(err) => Err(EventsError {
                                    api_error: err,
                                    tag: tag.clone(),
                                    event_type: event.clone(),
                                    index: i,
                                }),
                            };
                        }
                    };
                }
                EventType::War(tag, last_fired, old) => {
                    if let Some(duration) = Instant::now().checked_duration_since(*last_fired) {
                        if Self::should_fire_again(duration, 60 * 10) {
                            return match self.client.get_current_war(tag).await {
                                Ok(new) => {
                                    self.handler.war(old.clone(), new.clone()).await; // invoking the handler function the user defined
                                    self.event_type[i] =
                                        EventType::War(tag.clone(), Instant::now(), Some(new));
                                    Ok(true)
                                }
                                Err(err) => Err(EventsError {
                                    api_error: err,
                                    tag: tag.clone(),
                                    event_type: event.clone(),
                                    index: i,
                                }),
                            };
                        }
                    };
                }
            }
        }

        Ok(false)
    }
}