use std::net::SocketAddr; use sha1::{Digest, Sha1}; use crate::constants::SALT; pub struct PlayerInfo { addr: SocketAddr, name: &'static str, name_hash: &'static str, index: u8, // 颜色 HSV color_hue: i32, // 0 - 360 color_saturation: f32, // 0 - 1 color_value: f32 // 0 - 1 } pub enum PlayerMessage { Say(&'static str), PressedDown(char), PressedUp(char) } impl PlayerInfo { /// # 配置玩家 ID 和 名称 /// self : 自身 /// name : 玩家名称 /// number : 玩家序号 /// return -> 自身 pub fn name(&mut self, name : &'static str, number : &'static str) -> &mut Self { self.name = name; let combined = format!("{}{}{}", name, number, SALT); let mut hasher = Sha1::new(); hasher.update(combined); let result = hasher.finalize(); self.name_hash = &format!("{:x}", &result[..16]); self } /// # 配置玩家颜色 /// self : 自身 /// hue : 色调 0 -> RED; 120 -> GREEN; 240 -> BLUE /// saturation : 饱和度 /// value : 明度 /// return -> 自身 pub fn color_hue(&mut self, hue: i32, saturation: f32, value: f32) -> &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 } }