Files
ServerCrasherRust/src/utils.rs

62 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 一些杂七杂八的小东西可以放在这
use std::sync::Arc;
use rand::distributions::Alphanumeric;
use rand::Rng;
use crate::configuration::Configuration;
// 处理json组件(有些TextComponent里面一堆extra看的我头皮发麻,所以整个这个)
pub fn parse_json_component(json: &str) -> String {
let data: serde_json::Value = serde_json::from_str(json).unwrap();
let mut res: String = String::new();
let text = data.get("text");
match text {
Some(t) => {
if t.is_string(){
res += t.as_str().unwrap();
}
}
None => {
let translate = data.get("translate");
match translate {
Some(t) => {
if t.is_string(){
res += t.as_str().unwrap();
}
}
None => {}
}
}
}
let extra = data.get("extra");
match extra {
Some(t) =>{
for i in t.as_array().unwrap(){
if !i.is_object(){
continue;
}
res += parse_json_component(i.to_string().as_str()).as_str();
}
}
None => {}
}
return res;
}
// 生成一个随机的用户名
// author: 通义
pub fn generate_string(len_min: i64, len_max: i64) -> String {
let mut rng = rand::thread_rng();
let len = rng.gen_range(len_min..=len_max);
rng.sample_iter(&Alphanumeric)
.take(len as usize)
.map(char::from)
.collect()
}
pub fn generate_username(config: &Arc<Configuration>) -> String {
config.username_prefix().to_string() + generate_string(
config.name_len_limit[0], config.name_len_limit[1]
).to_string().as_str()
}