From 492c5e50443e505e4a97caf898916a309a42fc73 Mon Sep 17 00:00:00 2001 From: Philpax Date: Fri, 22 Sep 2023 10:36:43 +0200 Subject: [PATCH] feat(web): use random username --- Cargo.lock | 2 +- app/Cargo.toml | 1 - app/src/client/mod.rs | 57 +------------------------------- crates/client_shared/Cargo.toml | 1 + crates/client_shared/src/lib.rs | 1 + crates/client_shared/src/util.rs | 56 +++++++++++++++++++++++++++++++ web/Cargo.lock | 1 + web/client/src/app.rs | 5 ++- 8 files changed, 63 insertions(+), 61 deletions(-) create mode 100644 crates/client_shared/src/util.rs diff --git a/Cargo.lock b/Cargo.lock index 8d135c70a9..6cf90107db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,7 +155,6 @@ dependencies = [ "image_hasher", "log", "parking_lot", - "rand 0.8.5", "rpassword", "rustls-pemfile", "rusty-hook", @@ -429,6 +428,7 @@ dependencies = [ "ambient_shared_types", "ambient_ui_native", "glam 0.24.1", + "rand 0.8.5", ] [[package]] diff --git a/app/Cargo.toml b/app/Cargo.toml index ea41fe7d04..da008fa4f9 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -66,7 +66,6 @@ toml_edit = { workspace = true } rpassword = { workspace = true } sentry = { workspace = true } sentry-rust-minidump = { workspace = true } -rand = { workspace = true } [dev-dependencies] glam = { workspace = true } diff --git a/app/src/client/mod.rs b/app/src/client/mod.rs index 2ffb6b15af..19ad0b4ef1 100644 --- a/app/src/client/mod.rs +++ b/app/src/client/mod.rs @@ -26,7 +26,6 @@ use ambient_settings::SettingsKey; use ambient_sys::time::Instant; use ambient_ui_native::{Dock, WindowSized}; use glam::uvec2; -use rand::seq::SliceRandom; use crate::{ cli::{GoldenImageCommand, RunCli}, @@ -49,7 +48,7 @@ pub fn run( let user_id = match run.user_id.clone().or(settings.general.user_id) { Some(user_id) => user_id, None => { - let user_id = random_username(); + let user_id = ambient_client_shared::util::random_username(); log::warn!( "No `user_id` found in settings, using random username: {:?}", user_id @@ -391,57 +390,3 @@ fn systems() -> SystemGroup { ], ) } - -fn random_username() -> String { - const ADJECTIVES: &[&str] = &[ - "Quirky", - "Sneaky", - "Witty", - "Curious", - "Grumpy", - "Silly", - "Mischievous", - "Goofy", - "Hasty", - "Awkward", - "Zany", - "Peculiar", - "Whimsical", - "Bumbling", - "Absurd", - "Oddball", - "Clumsy", - "Nutty", - "Haphazard", - "Eccentric", - ]; - - const ANIMALS: &[&str] = &[ - "Penguin", - "Platypus", - "Lemur", - "Armadillo", - "Sloth", - "Ostrich", - "Tapir", - "Narwhal", - "Chameleon", - "Aardvark", - "Quokka", - "Wombat", - "Kakapo", - "Capybara", - "Mandrill", - "Axolotl", - "Blobfish", - "Echidna", - "Wallaby", - ]; - - let mut rng = rand::thread_rng(); - format!( - "{}{}", - ADJECTIVES.choose(&mut rng).unwrap(), - ANIMALS.choose(&mut rng).unwrap() - ) -} diff --git a/crates/client_shared/Cargo.toml b/crates/client_shared/Cargo.toml index 8228bef493..ebfb17046e 100644 --- a/crates/client_shared/Cargo.toml +++ b/crates/client_shared/Cargo.toml @@ -19,3 +19,4 @@ ambient_debugger = { path = "../debugger/" , version = "0.3.0-dev" } ambient_ecs_editor = { path = "../ecs_editor/" , version = "0.3.0-dev" } glam = { workspace = true } +rand = { workspace = true } diff --git a/crates/client_shared/src/lib.rs b/crates/client_shared/src/lib.rs index dc90382255..a9c3d4884a 100644 --- a/crates/client_shared/src/lib.rs +++ b/crates/client_shared/src/lib.rs @@ -1,2 +1,3 @@ pub mod game_view; pub mod player; +pub mod util; diff --git a/crates/client_shared/src/util.rs b/crates/client_shared/src/util.rs new file mode 100644 index 0000000000..d2286f17eb --- /dev/null +++ b/crates/client_shared/src/util.rs @@ -0,0 +1,56 @@ +use rand::seq::SliceRandom; + +/// Generates an adjective-animal username. +pub fn random_username() -> String { + const ADJECTIVES: &[&str] = &[ + "Quirky", + "Sneaky", + "Witty", + "Curious", + "Grumpy", + "Silly", + "Mischievous", + "Goofy", + "Hasty", + "Awkward", + "Zany", + "Peculiar", + "Whimsical", + "Bumbling", + "Absurd", + "Oddball", + "Clumsy", + "Nutty", + "Haphazard", + "Eccentric", + ]; + + const ANIMALS: &[&str] = &[ + "Penguin", + "Platypus", + "Lemur", + "Armadillo", + "Sloth", + "Ostrich", + "Tapir", + "Narwhal", + "Chameleon", + "Aardvark", + "Quokka", + "Wombat", + "Kakapo", + "Capybara", + "Mandrill", + "Axolotl", + "Blobfish", + "Echidna", + "Wallaby", + ]; + + let mut rng = rand::thread_rng(); + format!( + "{}{}", + ADJECTIVES.choose(&mut rng).unwrap(), + ANIMALS.choose(&mut rng).unwrap() + ) +} diff --git a/web/Cargo.lock b/web/Cargo.lock index 05e2be3508..ea715c6aa5 100644 --- a/web/Cargo.lock +++ b/web/Cargo.lock @@ -228,6 +228,7 @@ dependencies = [ "ambient_shared_types", "ambient_ui_native", "glam", + "rand 0.8.5", ] [[package]] diff --git a/web/client/src/app.rs b/web/client/src/app.rs index b1a547a232..4041f7cb6c 100644 --- a/web/client/src/app.rs +++ b/web/client/src/app.rs @@ -3,10 +3,9 @@ use ambient_cameras::UICamera; use ambient_client_shared::{game_view::GameView, player}; use ambient_ecs::{Entity, SystemGroup}; use ambient_element::{element_component, Element, ElementComponentExt, Hooks}; -use ambient_native_std::friendly_id; use ambient_network::{server::RpcArgs, web::client::GameClientView}; use ambient_rpc::RpcRegistry; -use ambient_ui_native::{cb}; +use ambient_ui_native::cb; use std::collections::HashMap; #[element_component] @@ -15,7 +14,7 @@ pub fn MainApp(_hooks: &mut Hooks, server_url: String) -> Element { GameClientView { url: server_url, - user_id: friendly_id(), + user_id: ambient_client_shared::util::random_username(), systems_and_resources: cb(|| { let mut resources = Entity::new();