From edcc931402d1070265bce3d587da775b3d6a3379 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 28 Feb 2024 23:33:59 +0800 Subject: [PATCH] go! --- Cargo.lock | 7 ++++ Cargo.toml | 2 + src/main.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/name.rs | 57 ++++++++++++++-------------- 4 files changed, 141 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12db1e2..5eb14e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "base16384" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c81fb0b757d33aa2a07ee0f07800e5e529aac2a78a474f1816174516e23a1d62" + [[package]] name = "cfg-if" version = "1.0.0" @@ -39,6 +45,7 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" name = "namerena-rs" version = "0.1.0" dependencies = [ + "base16384", "tracing", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index 0199fc8..67208a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ edition = "2021" # serde = { version = "1.0", features = ["derive"] } # md-5 = "0.10.6" # toml = "0.8.10" +# base64 = "0.21.7" +base16384 = "0.1.0" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["time"] } diff --git a/src/main.rs b/src/main.rs index b0b38af..c34549d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,108 @@ mod data_struct; mod name; -fn main() { - println!("Hello, world!"); +use std::sync::{atomic::AtomicU32, Arc}; + +use base16384::Base16384Utf8; +use tracing::{info, warn}; + +/// 根据 u64 生成对应的 name +/// 转换成 base 16384 +/// 禁用: +/// U00 ~ U1F ,换行,制表符 等 +/// ? , 问号 +/// U2000 - U202F , unicode特殊空格 等 +/// 不可以空格开头 +pub fn gen_name(id: u64) -> String { + // u64 -> [u8] + let id_bytes = id.to_be_bytes(); + Base16384Utf8::encode(id_bytes.as_slice()) +} + +pub fn show_name(namer: &name::Namer) -> String { + // var attributeNames = ["HP", "攻", "防", "速", "敏", "魔", "抗", "智"] + format!( + "HP|{} 攻|{} 防|{} 速|{} 敏|{} 魔|{} 抗|{} 智|{} 八围:{}", + namer.name_prop[7], + namer.name_prop[0], + namer.name_prop[1], + namer.name_prop[2], + namer.name_prop[3], + namer.name_prop[4], + namer.name_prop[5], + namer.name_prop[6], + namer.get_property() + ) +} + +#[allow(non_upper_case_globals)] +const allow_d: u32 = 100; + +fn main() { + tracing_subscriber::fmt::init(); + let team = "shenjack"; + // cli 获取 max + let max = match std::env::args().nth(1) { + Some(arg) => arg.parse().unwrap_or(1_0000_0000), + None => 1_0000_0000, + }; + // cli 获取线程数量 + let thread_count = match std::env::args().nth(2) { + Some(arg) => arg.parse().unwrap_or(10), + None => 10, + }; + + // 新建线程计算 + let top = AtomicU32::new(100); + let arc_top = Arc::new(top); + // 将数据量处理成可被 thread_count 整除 + // 在主线程计算剩余的 + let left = max % thread_count; + + let threads = (0..thread_count).map(|i| { + let top = arc_top.clone(); + let max = max / thread_count + if i < left { 1 } else { 0 }; + std::thread::spawn(move || { + for i in 0..max+1 { + let name = gen_name(i as u64); + let full_name = format!("{}@{}", name, team); + let namer = name::Namer::new(&full_name); + if let Some(namer) = namer { + let prop = namer.get_property(); + let tmp_top = top.as_ref(); + let top = tmp_top.load(std::sync::atomic::Ordering::Relaxed); + if (prop - allow_d as f32) > top as f32 { + if prop > top as f32 { + warn!("新的最高属性 {}", top); + tmp_top.store(prop as u32, std::sync::atomic::Ordering::Relaxed); + } + info!("{}|{}", full_name, show_name(&namer)); + } + } + } + }) + }); + info!("开始计算"); + + for i in max-left..max+1 { + let name = gen_name(i as u64); + let full_name = format!("{}@{}", name, team); + let namer = name::Namer::new(&full_name); + if let Some(namer) = namer { + let prop = namer.get_property(); + let tmp_top = arc_top.as_ref(); + let top = tmp_top.load(std::sync::atomic::Ordering::Relaxed); + if (prop - allow_d as f32) > top as f32 { + if prop > top as f32 { + warn!("新的最高属性 {}", top as f32); + tmp_top.store(prop as u32, std::sync::atomic::Ordering::Relaxed); + } + info!("{}|{}", full_name, show_name(&namer)); + } + } + } + + for t in threads { + t.join().unwrap(); + } } diff --git a/src/name.rs b/src/name.rs index 038d844..7655908 100644 --- a/src/name.rs +++ b/src/name.rs @@ -29,8 +29,8 @@ impl Namer { } let mut name_base = [0_u8; 128]; let mut name_prop = [0_u32; 8]; - let mut skl_id = [0_u8; 40]; - let mut skl_freq = [0_u8; 40]; + let skl_id = [0_u8; 40]; + let skl_freq = [0_u8; 40]; // name@team // name @@ -89,27 +89,24 @@ impl Namer { let m = ((val[i] as u32 * 181) + 160) % 256; if m >= 89 && m < 217 { name_base[s as usize] = (m & 63) as u8; - s +=1; + s += 1; } } - // 计算 name_prop + // 计算 name_prop let mut prop_cnt = 0; let mut r = name_base[0..32].to_vec(); for i in (10..31).step_by(3) { + r[i..i + 3].sort_unstable(); name_prop[prop_cnt] = median(r[i], r[i + 1], r[i + 2]) as u32; prop_cnt += 1; } - println!("r:{:?}", r); - r[0..10].sort(); - println!("r:{:?} {}", r, prop_cnt); + r[0..10].sort_unstable(); name_prop[prop_cnt] = 154; prop_cnt += 1; - println!("name_prop:{:?}", name_prop); for i in 3..7 { name_prop[prop_cnt - 1] += r[i] as u32; } - println!("name_prop:{:?}", name_prop); for i in 0..7 { name_prop[i] += 36; } @@ -127,13 +124,12 @@ impl Namer { }) } - pub fn name_len(&self) -> usize { - self.name.len() + 1 - } - - pub fn team_len(&self) -> usize { - self.team.len() + 1 - } + pub fn get_property(&self) -> f32 { + // 除 prop[7] 外 加起来 + prop[7] / 3 + let sum1 = self.name_prop[0..7].iter().sum::(); + let sum2 = self.name_prop[7] as u32; + sum1 as f32 + (sum2 as f32 / 3_f32) + } } #[cfg(test)] @@ -143,18 +139,19 @@ mod test { #[test] fn basic_new_test() { let namer = Namer::new(&"x@x".to_string()); + assert!(namer.is_some()); + let namer = namer.unwrap(); - // println!("{:#?}", namer); - assert_eq!(namer.name, "x"); - assert_eq!(namer.team, "x"); - println!("val: {:?}", namer.val); - println!("name_base: {:?}", namer.name_base); - println!("name_bytes: {:?}", namer.name_bytes); - println!("team_bytes: {:?}", namer.team_bytes); - println!("name_prop: {:?}", namer.name_prop); - println!("skl_id: {:?}", namer.skl_id); - println!("skl_freq: {:?}", namer.skl_freq); + + let base_name_vec: Vec = vec![ + 53, 0, 40, 4, 58, 61, 37, 46, 56, 51, 21, 20, 27, 17, 15, 26, 13, 30, 52, 63, 36, 30, + 57, 34, 22, 37, 35, 6, 12, 25, 50, 49, 59, 23, 49, 27, 51, 58, 39, 28, 60, 20, 31, 36, + 41, 11, 7, 29, 24, 24, 61, 62, 57, 4, 28, 48, 55, 50, 38, 29, 10, 40, 42, 15, 23, 47, + 42, 62, 47, 1, 60, 5, 43, 21, 1, 46, 45, 9, 9, 14, 38, 13, 56, 0, 31, 59, 39, 6, 35, + 41, 55, 5, 34, 3, 7, 33, 33, 45, 16, 16, 32, 43, 18, 44, 22, 14, 17, 10, 11, 53, 18, + 44, 19, 52, 2, 32, 12, 8, 2, 54, 26, 48, 8, 3, 63, 54, 19, 25, + ]; let val_vec: Vec = vec![ 225, 96, 49, 232, 20, 47, 115, 245, 234, 23, 111, 178, 231, 100, 118, 197, 42, 98, 137, 196, 209, 86, 114, 184, 167, 129, 164, 239, 205, 211, 82, 173, 189, 153, 198, 67, 4, 3, @@ -171,9 +168,11 @@ mod test { 53, 68, 218, 0, 252, 16, 136, 179, 158, 248, 2, 154, 12, 125, 126, 255, 18, 146, 104, 77, 152, 208, 214, 72, 55, 195, 62, 7, 217, 56, 181, ]; + let prop_vec: Vec = vec![57, 53, 66, 72, 70, 71, 61, 344]; + assert_eq!(namer.name, "x"); + assert_eq!(namer.team, "x"); assert_eq!(namer.val.to_vec(), val_vec); - // 57 53 66 72 70 71 61 344 - let prop_vel: Vec = vec![57, 53, 66, 72, 70, 71, 61, 344]; - assert_eq!(namer.name_prop.to_vec(), prop_vel); + assert_eq!(namer.name_prop.to_vec(), prop_vec); + assert_eq!(namer.name_base.to_vec(), base_name_vec); } }