From 9e48af01ae56fe813869ada24de291ab98b361e2 Mon Sep 17 00:00:00 2001 From: "1992414357@qq.com" <1992414357@qq.com> Date: Wed, 11 Jun 2025 19:14:04 +0800 Subject: 组合模块中 structs.rs 和 implements.rs 文件 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/data/player/implements.rs | 119 -------------------------- core/src/data/player/mod.rs | 3 +- core/src/data/player/player_data.rs | 162 ++++++++++++++++++++++++++++++++++++ core/src/data/player/structs.rs | 45 ---------- 4 files changed, 163 insertions(+), 166 deletions(-) delete mode 100644 core/src/data/player/implements.rs create mode 100644 core/src/data/player/player_data.rs delete mode 100644 core/src/data/player/structs.rs (limited to 'core/src/data/player') diff --git a/core/src/data/player/implements.rs b/core/src/data/player/implements.rs deleted file mode 100644 index bbaefc4..0000000 --- a/core/src/data/player/implements.rs +++ /dev/null @@ -1,119 +0,0 @@ -use crate::data::player::structs::{Account, Customize, Player}; -use crate::data::player::ACCOUNT_HASH_SALT; -use hex::encode; -use sha1::{Digest, Sha1}; -use std::fmt::{Display, Formatter}; -use std::hash::{Hash, Hasher}; -use nogamepads::string_utils::process_id_text; - -impl Player { - - /// Create new player information using a username and password - pub fn register(id: String, password: String) -> Player { - let mut player = Player { - customize: None, - account: Account::default() - }; - - let processed_id = process_id_text(id); - - player.account.id = processed_id.clone(); - player.account.player_hash = Self::gen_hash(processed_id, password); - player - } - - pub fn check(&self, password: String) -> bool { - let hash = Self::gen_hash(self.account.id.clone(), password); - hash == self.account.player_hash - } - - fn gen_hash(processed_id: String, password: String) -> String { - let combined = format!("{}{}{}", processed_id, password, ACCOUNT_HASH_SALT); - let mut hasher = Sha1::new(); - hasher.update(combined); - let result = hasher.finalize(); - encode(&result[..]) - } -} - -// Customize implements -impl Player { - - /// Set player nickname - pub fn nickname(&mut self, name: &String) -> &mut Player { - self.change(|custom| { - custom.nickname = name.clone(); - custom - }) - } - - /// Set the hue of the player's color - pub fn hue(&mut self, mut hue: i32) -> &mut Player { - hue = hue.clamp(0, 360); - self.change(|custom| { - custom.color_hue = hue.clone(); - custom - }) - } - - /// Set the player's HSV values - pub fn hsv(&mut self, mut hue: i32, mut saturation: f64, mut value: f64) -> &mut Player { - hue = hue.clamp(0, 360); - saturation = saturation.clamp(0.0, 1.0); - value = value.clamp(0.0, 1.0); - self.change(|custom| { - custom.color_hue = hue.clone(); - custom.color_saturation = saturation.clone(); - custom.color_value = value.clone(); - custom - }) - } - - fn init(&mut self) { - if self.customize.is_none() { - self.customize = Some(Customize::default()); - } - } - - fn change(&mut self, f: F) -> &mut Player - where F: FnOnce(&mut Customize) -> &mut Customize { - self.init(); - let mut customize = self.customize.clone().unwrap(); - f(&mut customize); - self.customize = Some(customize); - self - } -} - -impl PartialEq for Player { - fn eq(&self, other: &Self) -> bool { - self.account == other.account - } -} - -impl Hash for Player { - fn hash(&self, state: &mut H) { - self.account.hash(state); - } -} - -impl Display for Player { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str(self.account.id.as_str()) - } -} - -impl From for Player { - fn from(account: Account) -> Self { - Player { - account, - customize: None - } - } -} - -impl Display for Account { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - f.write_str(self.id.as_str()) - } -} \ No newline at end of file diff --git a/core/src/data/player/mod.rs b/core/src/data/player/mod.rs index 7e8fe28..ff6ae85 100644 --- a/core/src/data/player/mod.rs +++ b/core/src/data/player/mod.rs @@ -1,4 +1,3 @@ -pub mod implements; -pub mod structs; +pub mod player_data; pub const ACCOUNT_HASH_SALT : &str = env!("TEST_PLAYER_ACCOUNT"); \ No newline at end of file diff --git a/core/src/data/player/player_data.rs b/core/src/data/player/player_data.rs new file mode 100644 index 0000000..1fe5ff0 --- /dev/null +++ b/core/src/data/player/player_data.rs @@ -0,0 +1,162 @@ +use crate::data::player::ACCOUNT_HASH_SALT; +use hex::encode; +use sha1::{Digest, Sha1}; +use std::fmt::{Display, Formatter}; +use std::hash::{Hash, Hasher}; +use bincode::{Decode, Encode}; +use serde::{Deserialize, Serialize}; +use nogamepads::string_utils::process_id_text; + +/// Player information +/// Describes a player's specific details, which are frequently exchanged between the controller and game pad_client. +#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, Debug)] +pub struct Player { + + /// Account information + pub account: Account, + + /// Custom information (Optional) + pub customize: Option +} + +/// Account information +/// Essential data for verifying player uniqueness, including the player's hash value and account ID. +#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, Eq, Hash, PartialEq, Debug)] +pub struct Account { + + /// Player name stored in data, allowing only lowercase letters and underscores + pub id: String, + + /// Player hash value proving player uniqueness + pub player_hash: String +} + +/// Custom information +/// Describes personalized player details displayed in-game, such as name, color, or other customizations. +#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, PartialEq, Debug)] +pub struct Customize { + + /// Player name displayed in the game + pub nickname: String, + + /// HSV Color - Hue (Range: 0 - 360) + pub color_hue: i32, + + /// HSV Color - Saturation (Range: 0 - 1) + pub color_saturation: f64, + + /// HSV Color - Value (Range: 0 - 1) + pub color_value: f64 +} + +impl Player { + + /// Create new player information using a username and password + pub fn register(id: String, password: String) -> Player { + let mut player = Player { + customize: None, + account: Account::default() + }; + + let processed_id = process_id_text(id); + + player.account.id = processed_id.clone(); + player.account.player_hash = Self::gen_hash(processed_id, password); + player + } + + pub fn check(&self, password: String) -> bool { + let hash = Self::gen_hash(self.account.id.clone(), password); + hash == self.account.player_hash + } + + fn gen_hash(processed_id: String, password: String) -> String { + let combined = format!("{}{}{}", processed_id, password, ACCOUNT_HASH_SALT); + let mut hasher = Sha1::new(); + hasher.update(combined); + let result = hasher.finalize(); + encode(&result[..]) + } +} + +// Customize implements +impl Player { + + /// Set player nickname + pub fn nickname(&mut self, name: &String) -> &mut Player { + self.change(|custom| { + custom.nickname = name.clone(); + custom + }) + } + + /// Set the hue of the player's color + pub fn hue(&mut self, mut hue: i32) -> &mut Player { + hue = hue.clamp(0, 360); + self.change(|custom| { + custom.color_hue = hue.clone(); + custom + }) + } + + /// Set the player's HSV values + pub fn hsv(&mut self, mut hue: i32, mut saturation: f64, mut value: f64) -> &mut Player { + hue = hue.clamp(0, 360); + saturation = saturation.clamp(0.0, 1.0); + value = value.clamp(0.0, 1.0); + self.change(|custom| { + custom.color_hue = hue.clone(); + custom.color_saturation = saturation.clone(); + custom.color_value = value.clone(); + custom + }) + } + + fn init(&mut self) { + if self.customize.is_none() { + self.customize = Some(Customize::default()); + } + } + + fn change(&mut self, f: F) -> &mut Player + where F: FnOnce(&mut Customize) -> &mut Customize { + self.init(); + let mut customize = self.customize.clone().unwrap(); + f(&mut customize); + self.customize = Some(customize); + self + } +} + +impl PartialEq for Player { + fn eq(&self, other: &Self) -> bool { + self.account == other.account + } +} + +impl Hash for Player { + fn hash(&self, state: &mut H) { + self.account.hash(state); + } +} + +impl Display for Player { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(self.account.id.as_str()) + } +} + +impl From for Player { + fn from(account: Account) -> Self { + Player { + account, + customize: None + } + } +} + +impl Display for Account { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(self.id.as_str()) + } +} \ No newline at end of file diff --git a/core/src/data/player/structs.rs b/core/src/data/player/structs.rs deleted file mode 100644 index cc3d7c9..0000000 --- a/core/src/data/player/structs.rs +++ /dev/null @@ -1,45 +0,0 @@ -use bincode::{Decode, Encode}; -use serde::{Deserialize, Serialize}; -use std::hash::{Hash}; - -/// Player information -/// Describes a player's specific details, which are frequently exchanged between the controller and game pad_client. -#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, Debug)] -pub struct Player { - - /// Account information - pub account: Account, - - /// Custom information (Optional) - pub customize: Option -} - -/// Account information -/// Essential data for verifying player uniqueness, including the player's hash value and account ID. -#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, Eq, Hash, PartialEq, Debug)] -pub struct Account { - - /// Player name stored in data, allowing only lowercase letters and underscores - pub id: String, - - /// Player hash value proving player uniqueness - pub player_hash: String -} - -/// Custom information -/// Describes personalized player details displayed in-game, such as name, color, or other customizations. -#[derive(Default, Clone, Encode, Decode, Serialize, Deserialize, PartialEq, Debug)] -pub struct Customize { - - /// Player name displayed in the game - pub nickname: String, - - /// HSV Color - Hue (Range: 0 - 360) - pub color_hue: i32, - - /// HSV Color - Saturation (Range: 0 - 1) - pub color_saturation: f64, - - /// HSV Color - Value (Range: 0 - 1) - pub color_value: f64 -} \ No newline at end of file -- cgit