This commit is contained in:
shenjack 2024-02-28 23:33:59 +08:00
parent efd4b521d2
commit edcc931402
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 141 additions and 31 deletions

7
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "base16384"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c81fb0b757d33aa2a07ee0f07800e5e529aac2a78a474f1816174516e23a1d62"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -39,6 +45,7 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
name = "namerena-rs" name = "namerena-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"base16384",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]

View File

@ -11,6 +11,8 @@ edition = "2021"
# serde = { version = "1.0", features = ["derive"] } # serde = { version = "1.0", features = ["derive"] }
# md-5 = "0.10.6" # md-5 = "0.10.6"
# toml = "0.8.10" # toml = "0.8.10"
# base64 = "0.21.7"
base16384 = "0.1.0"
tracing = "0.1.40" tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["time"] } tracing-subscriber = { version = "0.3.18", features = ["time"] }

View File

@ -1,6 +1,108 @@
mod data_struct; mod data_struct;
mod name; mod name;
fn main() { use std::sync::{atomic::AtomicU32, Arc};
println!("Hello, world!");
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();
}
} }

View File

@ -29,8 +29,8 @@ impl Namer {
} }
let mut name_base = [0_u8; 128]; let mut name_base = [0_u8; 128];
let mut name_prop = [0_u32; 8]; let mut name_prop = [0_u32; 8];
let mut skl_id = [0_u8; 40]; let skl_id = [0_u8; 40];
let mut skl_freq = [0_u8; 40]; let skl_freq = [0_u8; 40];
// name@team // name@team
// name // name
@ -89,7 +89,7 @@ impl Namer {
let m = ((val[i] as u32 * 181) + 160) % 256; let m = ((val[i] as u32 * 181) + 160) % 256;
if m >= 89 && m < 217 { if m >= 89 && m < 217 {
name_base[s as usize] = (m & 63) as u8; name_base[s as usize] = (m & 63) as u8;
s +=1; s += 1;
} }
} }
@ -97,19 +97,16 @@ impl Namer {
let mut prop_cnt = 0; let mut prop_cnt = 0;
let mut r = name_base[0..32].to_vec(); let mut r = name_base[0..32].to_vec();
for i in (10..31).step_by(3) { 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; name_prop[prop_cnt] = median(r[i], r[i + 1], r[i + 2]) as u32;
prop_cnt += 1; prop_cnt += 1;
} }
println!("r:{:?}", r); r[0..10].sort_unstable();
r[0..10].sort();
println!("r:{:?} {}", r, prop_cnt);
name_prop[prop_cnt] = 154; name_prop[prop_cnt] = 154;
prop_cnt += 1; prop_cnt += 1;
println!("name_prop:{:?}", name_prop);
for i in 3..7 { for i in 3..7 {
name_prop[prop_cnt - 1] += r[i] as u32; name_prop[prop_cnt - 1] += r[i] as u32;
} }
println!("name_prop:{:?}", name_prop);
for i in 0..7 { for i in 0..7 {
name_prop[i] += 36; name_prop[i] += 36;
} }
@ -127,12 +124,11 @@ impl Namer {
}) })
} }
pub fn name_len(&self) -> usize { pub fn get_property(&self) -> f32 {
self.name.len() + 1 // 除 prop[7] 外 加起来 + prop[7] / 3
} let sum1 = self.name_prop[0..7].iter().sum::<u32>();
let sum2 = self.name_prop[7] as u32;
pub fn team_len(&self) -> usize { sum1 as f32 + (sum2 as f32 / 3_f32)
self.team.len() + 1
} }
} }
@ -143,18 +139,19 @@ mod test {
#[test] #[test]
fn basic_new_test() { fn basic_new_test() {
let namer = Namer::new(&"x@x".to_string()); let namer = Namer::new(&"x@x".to_string());
assert!(namer.is_some()); assert!(namer.is_some());
let namer = namer.unwrap(); let namer = namer.unwrap();
// println!("{:#?}", namer);
assert_eq!(namer.name, "x"); let base_name_vec: Vec<u8> = vec![
assert_eq!(namer.team, "x"); 53, 0, 40, 4, 58, 61, 37, 46, 56, 51, 21, 20, 27, 17, 15, 26, 13, 30, 52, 63, 36, 30,
println!("val: {:?}", namer.val); 57, 34, 22, 37, 35, 6, 12, 25, 50, 49, 59, 23, 49, 27, 51, 58, 39, 28, 60, 20, 31, 36,
println!("name_base: {:?}", namer.name_base); 41, 11, 7, 29, 24, 24, 61, 62, 57, 4, 28, 48, 55, 50, 38, 29, 10, 40, 42, 15, 23, 47,
println!("name_bytes: {:?}", namer.name_bytes); 42, 62, 47, 1, 60, 5, 43, 21, 1, 46, 45, 9, 9, 14, 38, 13, 56, 0, 31, 59, 39, 6, 35,
println!("team_bytes: {:?}", namer.team_bytes); 41, 55, 5, 34, 3, 7, 33, 33, 45, 16, 16, 32, 43, 18, 44, 22, 14, 17, 10, 11, 53, 18,
println!("name_prop: {:?}", namer.name_prop); 44, 19, 52, 2, 32, 12, 8, 2, 54, 26, 48, 8, 3, 63, 54, 19, 25,
println!("skl_id: {:?}", namer.skl_id); ];
println!("skl_freq: {:?}", namer.skl_freq);
let val_vec: Vec<u8> = vec![ let val_vec: Vec<u8> = vec![
225, 96, 49, 232, 20, 47, 115, 245, 234, 23, 111, 178, 231, 100, 118, 197, 42, 98, 137, 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, 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, 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, 77, 152, 208, 214, 72, 55, 195, 62, 7, 217, 56, 181,
]; ];
let prop_vec: Vec<u32> = 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); assert_eq!(namer.val.to_vec(), val_vec);
// 57 53 66 72 70 71 61 344 assert_eq!(namer.name_prop.to_vec(), prop_vec);
let prop_vel: Vec<u32> = vec![57, 53, 66, 72, 70, 71, 61, 344]; assert_eq!(namer.name_base.to_vec(), base_name_vec);
assert_eq!(namer.name_prop.to_vec(), prop_vel);
} }
} }