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
use serde::{Deserialize, Serialize};

use crate::models::badge_urls::BadgeUrls;

use super::*;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Clan {
    pub tag: String,
    pub name: String,
    #[serde(rename = "type")]
    pub privacy: Privacy,
    pub description: Option<String>,
    pub location: Option<location::Location>,
    pub badge_urls: BadgeUrls,
    pub clan_level: i8,
    pub clan_points: i32,
    pub clan_versus_points: i32,
    pub required_trophies: i32,
    pub war_frequency: WarFrequency,
    pub war_win_streak: i32,
    pub war_wins: i32,
    pub war_ties: Option<i32>,
    pub war_losses: Option<i32>,
    pub is_war_log_public: bool,
    pub war_league: leagues::WarLeague,
    pub members: i32,
    pub member_list: Option<Vec<ClanMember>>,
    pub labels: Vec<labels::ClanLabel>,
    pub required_versus_trophies: i32,
    pub required_townhall_level: i8,
    pub clan_capital: ClanCapital,
    pub chat_language: Option<ChatLanguage>,
}

impl Clan {
    pub fn game_link(&self) -> String {
        format!(
            "https://link.clashofclans.com/en?action=OpenClanProfile&tag={}",
            self.tag.replace('#', ""),
        )
    }
    #[cfg(feature = "extra")]
    pub fn clash_of_stats_link(&self) -> String {
        format!("https://www.clashofstats.com/clans/{}/summary", self.tag.replace('#', ""))
    }
    #[cfg(feature = "extra")]
    pub fn chocolate_clash_link(&self) -> String {
        format!("https://cc.chocolateclash.com/cc_n/clan.php?tag={}", self.tag.replace('#', ""))
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone, Ord)]
pub enum Privacy {
    #[serde(rename = "open")]
    Open,
    #[serde(rename = "inviteOnly")]
    InviteOnly,
    #[serde(rename = "closed")]
    Closed,
}

impl Privacy {
    pub fn is_open(&self) -> bool {
        self == &Privacy::Open
    }
    pub fn is_invite_only(&self) -> bool {
        self == &Privacy::InviteOnly
    }
    pub fn is_closed(&self) -> bool {
        self == &Privacy::Closed
    }
}

impl ToString for Privacy {
    fn to_string(&self) -> String {
        match self {
            Privacy::Open => "Anyone Can Join".to_string(),
            Privacy::InviteOnly => "Invite Only".to_string(),
            Privacy::Closed => "Closed".to_string(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Clone, Ord)]
pub enum WarFrequency {
    #[serde(rename = "unknown")]
    Unknown,
    #[serde(rename = "always")]
    Always,
    #[serde(rename = "moreThanOncePerWeek")]
    MoreThanOncePerWeek,
    #[serde(rename = "oncePerWeek")]
    OncePerWeek,
    #[serde(rename = "lessThanOncePerWeek")]
    LessThanOncePerWeek,
    #[serde(rename = "never")]
    Never,
    #[serde(rename = "any")]
    Any,
}

impl WarFrequency {
    pub fn is_unknown(&self) -> bool {
        self == &WarFrequency::Unknown
    }
    pub fn is_always(&self) -> bool {
        self == &WarFrequency::Always
    }
    pub fn is_more_than_once_per_week(&self) -> bool {
        self == &WarFrequency::MoreThanOncePerWeek
    }
    pub fn is_once_per_week(&self) -> bool {
        self == &WarFrequency::OncePerWeek
    }
    pub fn is_less_than_once_per_week(&self) -> bool {
        self == &WarFrequency::LessThanOncePerWeek
    }
    pub fn is_never(&self) -> bool {
        self == &WarFrequency::Never
    }
    pub fn is_any(&self) -> bool {
        self == &WarFrequency::Any
    }
}

impl ToString for WarFrequency {
    fn to_string(&self) -> String {
        match self {
            WarFrequency::Unknown => "Not set".to_string(),
            WarFrequency::Always => "Always".to_string(),
            WarFrequency::MoreThanOncePerWeek => "Twice a week".to_string(),
            WarFrequency::OncePerWeek => "Once a week".to_string(),
            WarFrequency::LessThanOncePerWeek => "Rarely".to_string(),
            WarFrequency::Never => "Never".to_string(),
            WarFrequency::Any => "Any".to_string(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ChatLanguage {
    pub id: i32,
    pub name: String,
    pub language_code: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClanMember {
    pub tag: String,
    pub name: String,
    pub role: Role,
    pub exp_level: i32,
    pub league: leagues::League,
    pub trophies: i32,
    pub versus_trophies: i32,
    pub clan_rank: i32,
    pub previous_clan_rank: i32,
    pub donations: i32,
    pub donations_received: i32,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum Role {
    #[serde(rename = "notMember")]
    NotMember,
    #[serde(rename = "member")]
    Member,
    #[serde(rename = "admin")]
    Elder,
    #[serde(rename = "coLeader")]
    CoLeader,
    #[serde(rename = "leader")]
    Leader,
}

impl Role {
    pub fn is_not_member(&self) -> bool {
        self == &Role::NotMember
    }
    pub fn is_member(&self) -> bool {
        self == &Role::Member
    }
    pub fn is_elder(&self) -> bool {
        self == &Role::Elder
    }
    pub fn is_co_leader(&self) -> bool {
        self == &Role::CoLeader
    }
    pub fn is_leader(&self) -> bool {
        self == &Role::Leader
    }
}

impl ToString for Role {
    fn to_string(&self) -> String {
        match self {
            Role::NotMember => "Not Member".to_string(),
            Role::Member => "Member".to_string(),
            Role::Elder => "Elder".to_string(),
            Role::CoLeader => "Co-Leader".to_string(),
            Role::Leader => "Leader".to_string(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClanCapital {
    capital_hall_level: Option<i8>,
    districts: Option<Vec<District>>,
}

impl ClanCapital {
    pub fn capital_hall_level(&self) -> i8 {
        self.capital_hall_level.unwrap_or_default()
    }
    pub fn districts(&self) -> Vec<District> {
        self.districts.clone().unwrap_or_default()
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct District {
    pub id: i32,
    pub name: String,
    pub district_hall_level: i8,
}