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
use std::fmt;

use serde::{de, Deserialize, Deserializer};

use crate::dofapi::carac::CaracLines;
use crate::dofapi::condition::{Condition, ConditionAtom};

//  _____            _                                 _
// | ____|__ _ _   _(_)_ __   ___ _ __ ___   ___ _ __ | |_
// |  _| / _` | | | | | '_ \ / _ \ '_ ` _ \ / _ \ '_ \| __|
// | |__| (_| | |_| | | |_) |  __/ | | | | |  __/ | | | |_
// |_____\__, |\__,_|_| .__/ \___|_| |_| |_|\___|_| |_|\__|
//          |_|       |_|

#[derive(Copy, Clone, Deserialize, Debug, Eq, Hash, PartialEq)]
pub enum ItemType {
    Amulet,
    Backpack,
    Belt,
    Boots,
    Cloak,
    Dofus,
    Hat,
    Ring,
    Shield,
    Trophy,
    Petsmount,
    Pet,

    #[serde(rename = "Living object")]
    LivingObject,

    #[serde(rename = "Mounts")]
    Mount,

    Axe,
    Sword,
    Staff,
    Wand,
    Bow,
    Dagger,
    Shovel,
    Hammer,
    Scythe,
    Pickaxe,
    Tool,

    #[serde(rename = "Soul stone")]
    SoulStone,
}

/// List of all kinds of weapons.
const WEAPON_TYPES: &[ItemType] = &[
    ItemType::Axe,
    ItemType::Sword,
    ItemType::Staff,
    ItemType::Wand,
    ItemType::Bow,
    ItemType::Dagger,
    ItemType::Shovel,
    ItemType::Hammer,
    ItemType::Scythe,
    ItemType::Pickaxe,
    ItemType::Tool,
    ItemType::SoulStone,
];

#[derive(Clone, Deserialize, Debug)]
pub struct Equipement {
    #[serde(rename = "type")]
    pub item_type: ItemType,

    #[serde(rename = "ankamaId")]
    pub ankama_id: u64,

    pub _id:   u64,
    pub name:  String,
    pub level: u8,
    pub url:   String,

    #[serde(
        default,
        rename = "setId",
        deserialize_with = "deserialize_set_id"
    )]
    pub set_id: Option<u64>,

    #[serde(default)]
    pub description: String,

    #[serde(rename = "imgUrl")]
    pub img_url: String,

    #[serde(default)]
    pub statistics: CaracLines,

    #[serde(default)]
    pub conditions: Condition,
}

impl Equipement {
    /// Check wether this equipement is a weapon
    pub fn is_weapon(&self) -> bool {
        WEAPON_TYPES.contains(&self.item_type)
    }
}

/// Fix trophy conditions as big trophy are not referenced with a condition to
/// use them in the website.
///
/// This will infer it by finding out if a trophy is strictly better than
/// another one in the database.
pub fn fix_all_trophy(db: &mut [Equipement]) {
    let trophy_list: Vec<Equipement> = db
        .iter()
        .filter(|item| item.item_type == ItemType::Trophy)
        .cloned()
        .collect();

    db.iter_mut()
        .filter(|item| item.item_type == ItemType::Trophy)
        .for_each(|item| {
            // A trophy is unique if no other trophy covers all its positive
            // bonuses.
            let has_no_malus = item
                .statistics
                .as_map()
                .iter()
                .all(|(_, bounds)| *bounds.start() >= 0);

            let is_unique = has_no_malus
                && trophy_list
                    .iter()
                    .filter(|other| other._id != item._id)
                    .filter(|other| other.level == item.level)
                    .all(|other| {
                        item.statistics
                            .as_map()
                            .iter()
                            .filter(|(_kind, bounds)| *bounds.start() >= 0)
                            .any(|(kind, _bounds)| {
                                other
                                    .statistics
                                    .as_map()
                                    .get(kind)
                                    .map(|bounds| *bounds.end() < 0)
                                    .unwrap_or(true)
                            })
                    });

            // A trophy is strong if it is unique or better than another trophy
            let is_strong = is_unique
                || trophy_list.iter().any(|other| {
                    item._id != other._id
                        && item.level <= other.level
                        && item.statistics.is_stronger_than(&other.statistics)
                });

            if is_strong {
                item.conditions = ConditionAtom::RestrictSetBonuses.into();
            }
        })
}

//  ____                      _       _ _
// |  _ \  ___  ___  ___ _ __(_) __ _| (_)_______ _ __
// | | | |/ _ \/ __|/ _ \ '__| |/ _` | | |_  / _ \ '__|
// | |_| |  __/\__ \  __/ |  | | (_| | | |/ /  __/ |
// |____/ \___||___/\___|_|  |_|\__,_|_|_/___\___|_|
//

fn deserialize_set_id<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
where
    D: Deserializer<'de>,
{
    deserializer.deserialize_any(SetIdVisitor)
}

struct SetIdVisitor;

impl<'de> de::Visitor<'de> for SetIdVisitor {
    type Value = Option<u64>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("An integer, 0 for no set")
    }

    fn visit_u64<E>(self, val: u64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(if val == 0 { None } else { Some(val) })
    }
}