aboutsummaryrefslogtreecommitdiff
path: root/core/src/player.rs
blob: 78e4d23a8c55fef7ea32437be0ea18d81bea2eb3 (plain) (blame)
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
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
    }
}