use crate::constants::SALT; use bincode::{Decode, Encode}; use hex::encode; use sha1::{Digest, Sha1}; /// 玩家信息 #[derive(Encode, Decode, PartialEq, Debug)] pub struct PlayerInfo { pub display_name: String, pub name_hash: String, // 颜色 HSV pub color_hue: i32, // 0 - 360 pub color_saturation: f64, // 0 - 1 pub color_value: f64, // 0 - 1 } impl Default for PlayerInfo { fn default() -> Self { PlayerInfo { display_name: String::from("Player"), name_hash: String::new(), color_hue: 86, color_saturation: 1.0, color_value: 1.0, } } } impl PlayerInfo { /// # 配置玩家 ID 和 名称 /// self : 自身 /// display_name : 玩家显示名称 /// id : 玩家 ID /// return -> 自身 pub fn name(&mut self, display_name: String, id: String) -> &mut Self { self.display_name = display_name; let combined = format!("{}{}{}", &self.display_name, id, SALT); let mut hasher = Sha1::new(); hasher.update(combined); let result = hasher.finalize(); self.name_hash = encode(&result[..]); self } /// # 配置玩家颜色 /// self : 自身 /// hue : 色调 0 -> RED; 120 -> GREEN; 240 -> BLUE /// saturation : 饱和度 /// value : 明度 /// return -> 自身 pub fn color_hsv(&mut self, hue: i32, saturation: f64, value: f64) -> &mut Self { self.color_hue = hue.clamp(0, 360); self.color_saturation = saturation.clamp(0.0, 1.0); self.color_value = value.clamp(0.0, 1.0); self } }