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
#[cfg(feature = "redis-support")]
extern crate redis;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SteamId(u64);

impl SteamId {
    pub fn from_u64(u: u64) -> SteamId {
        SteamId(u)
    }

    pub fn to_u64(self) -> u64 {
        self.0
    }

    pub fn get_account_id(self) -> u32 {
        (self.0 & 0xFFFFFFFF) as u32 
    }

    pub fn get_instance(self) -> u32 {
        ((self.0 >> 32) & 0xFFFFF) as u32
    }

    pub fn get_type(self) -> Option<AccountType> {
        use AccountType::*;

        let int_rep = (self.0 >> 52) & 0xF;
        match int_rep {
            0 => Some(Invalid),
            1 => Some(Individual),
            2 => Some(Multiseat),
            3 => Some(GameServer),
            4 => Some(AnonGameServer),
            5 => Some(Pending),
            6 => Some(ContentServer),
            7 => Some(Clan),
            8 => Some(Chat),
            9 => Some(SuperSeeder),
            10 => Some(AnonUser),
            _ => None
        }
    }

    pub fn get_universe(self) -> Option<Universe> {
        use Universe::*;

        let int_rep = (self.0 >> 56) & 0xFF;
        match int_rep {
            0 => Some(Invalid),
            1 => Some(Public),
            2 => Some(Beta),
            3 => Some(Internal),
            4 => Some(Dev),
            _ => None
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum AccountType {
    Invalid = 0,
    Individual = 1,
    Multiseat = 2,
    GameServer = 3,
    AnonGameServer = 4,
    Pending = 5,
    ContentServer = 6,
    Clan = 7,
    Chat = 8,
    SuperSeeder = 9,
    AnonUser = 10
}


#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Universe {
    Invalid = 0,
    Public = 1,
    Beta = 2,
    Internal = 3,
    Dev = 4
}

#[cfg(feature = "redis-support")]
impl redis::FromRedisValue for SteamId {
    fn from_redis_value(v: &redis::Value) -> redis::RedisResult<SteamId> {
        use std::convert::From;
        use std::str::FromStr;
        use redis::Value::*;

        match *v {
            Int(i) => {
                Ok(SteamId::from_u64(i as u64))
            },
            Data(ref data) => {
                let string = std::str::from_utf8(data).ok();
                let number = string.and_then(|string| u64::from_str(string).ok());

                match number {
                    Some(number) => {
                        Ok(SteamId::from_u64(number))
                    },
                    None => {
                        Err(From::from((redis::ErrorKind::TypeError, "Not numeric")))
                    }
                }
            },
            _ => {
                Err(From::from((redis::ErrorKind::TypeError, "Not numeric")))
            }
        }
    }
}

#[cfg(feature = "redis-support")]
impl redis::ToRedisArgs for SteamId {
    fn to_redis_args(&self) -> Vec<Vec<u8>> {
        let s = self.0.to_string();
        vec![s.as_bytes().to_vec()]
    }
}