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
//! User-related shared data structures.

/// Badge type - what shape a badge should be.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug)]
#[serde(untagged)]
pub enum BadgeType {
    /// Specific built-in badge pattern. Each number from 1 to 24 inclusive represents two SVG paths built in to the
    /// Screeps client.
    ///
    /// These paths can be scraped from the client, but are not included here for licensing reasons.
    Fixed(i32),
    /// Dynamic badge pattern defined by two SVG paths.
    Dynamic {
        /// The first SVG path string.
        path1: String,
        /// The second SVG path string.
        path2: String,
    },
}

/// Badge color - what color a specific part of a badge should be.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug)]
#[serde(untagged)]
pub enum BadgeColor {
    /// Specific built-in badge color. Each number from 0 to 79 inclusive represents a specific color.
    ///
    /// How these colors are generated can be figured out from the client, but this logic is not included here for
    /// licensing reasons.
    Set(i32),
    /// Hex badge color.
    Hex(String),
}

/// Description of a user badge, contains information on how that badge should be drawn/colored.
///
/// Badges can be rendered as SVG by first filling the entire image with `color1`, filling the first path (defined by
/// badge type) with `color2`, filling the second path with `color3`, and then cutting off corners to make the badge
/// a round circle.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash, Debug)]
pub struct Badge {
    /// Badge type, used for different badge formats
    #[serde(rename = "type")]
    pub badge_type: BadgeType,
    /// First color, use depends on badge type.
    pub color1: BadgeColor,
    /// Second color, use depends on badge type.
    pub color2: BadgeColor,
    /// Third color, use depends on badge type.
    pub color3: BadgeColor,
    /// Integer parameter to badge display, changes the shape of the badge in a different way depending on badge type.
    pub param: i32,
    /// Flips the badge either horizontally or vertically, depending on badge type.
    pub flip: bool,
}

#[cfg(test)]
mod tests {
    use super::Badge;
    use serde_json;

    #[test]
    fn parse_sample_parse_badge_simple_colors_simple_pattern() {
        let _: Badge = serde_json::from_value(json!({
            "type": 19,
            "color1": 37,
            "color2": 57,
            "color3": 1,
            "param": 0,
            "flip": false,
        }))
        .unwrap();
    }

    #[test]
    fn parse_sample_badge_hex_colors_simple_pattern() {
        let _: Badge = serde_json::from_value(json!({
            "type": 21,
            "color1": "#260d0d",
            "color2": "#6b2e41",
            "color3": "#ffe56d",
            "param": -100,
            "flip": false,
        }))
        .unwrap();
    }

    #[test]
    fn parse_sample_badge_custom_dissi() {
        let _: Badge = serde_json::from_value(json!({
            "type": {
                "path1": "M0,50c0,27.3,21.8,49.5,48.9,50v-9.6c-2.5-0.8-5.5-4.6-7.8-10.2c-0.9-2.3-1.7-4.7-2.4-7.4   \
                c3.3,0.3,6.7,0.5,10.2,0.5v-9.5c-4.2,0-8.2-0.3-12-0.8c-0.5-4.1-0.8-8.5-0.8-13c0-4.5,0.3-8.9,0.8-13c3.8-\
                0.5,7.9-0.8,12-0.8v-9.5   c-3.5,0-6.9,0.2-10.2,0.5c0.7-2.7,1.5-5.1,2.4-7.4c2.3-5.6,5.2-9.4,7.8-10.2V0C\
                21.8,0.5,0,22.7,0,50L0,50z M9.5,50   c0-2.7,4-6.2,10.2-8.7c2.3-0.9,4.7-1.7,7.4-2.4c-0.4,3.6-0.5,7.3-0.\
                5,11.1c0,3.8,0.2,7.5,0.5,11.1c-2.6-0.7-5.1-1.5-7.4-2.4   C13.5,56.2,9.5,52.7,9.5,50L9.5,50z \
                M12.8,66c1.1,0.5,2.2,1.1,3.4,1.5c3.7,1.5,7.9,2.8,12.5,3.7c0.9,4.6,2.2,8.8,3.7,12.5   \
                c0.5,1.2,1,2.3,1.5,3.4C24.4,83.1,16.8,75.5,12.8,66L12.8,66z \
                M33.9,12.8c-0.5,1.1-1,2.2-1.5,3.4c-1.5,3.7-2.8,8-3.7,12.5   \
                c-4.5,0.9-8.8,2.2-12.5,3.7c-1.2,0.5-2.3,1-3.4,1.5C16.8,24.5,24.4,16.9,33.9,12.8L33.9,12.8z",
                "path2": "M97,59l-6-3.4c0.3-1.8,0.4-3.7,0.4-5.6c0-1.9-0.1-3.7-0.4-5.6l6-3.4c2.8-1.6,3.8-5.2,2.2-8.1   \
                l-8.6-15.2c-0.8-1.4-2.1-2.4-3.6-2.8c-1.5-0.4-3.1-0.2-4.5,0.6l-6.1,3.5c-3.1-2.5-6.6-4.6-10.3-6v-7c0-3.3\
                -2.6-5.9-5.9-5.9h-7.7   v23.4C66.8,23.9,78.1,35.7,78.1,50c0,14.3-11.4,26.1-25.5,26.6V100h7.7c3.3,0,5.9\
                -2.7,5.9-5.9v-7c3.7-1.5,7.2-3.5,10.3-6l6.1,3.5   c0.9,0.5,1.9,0.8,2.9,0.8c0.5,0,1.1-0.1,1.6-0.2c1.5-0.\
                4,2.8-1.4,3.6-2.8l8.6-15.2C100.8,64.2,99.8,60.6,97,59L97,59z",
            },
            "color1": "#000000",
            "color2": "#028300",
            "color3": "#8b5c00",
            "param": 0,
            "flip": false,
        })).unwrap();
    }
}