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
|
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
}
}
|