Skip to content

Commit

Permalink
forward to swarm for easier use
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Jan 4, 2024
1 parent ef8be97 commit 487f9df
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions azalea/src/swarm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Swarm {
pub ecs_lock: Arc<Mutex<World>>,

bots: Arc<Mutex<HashMap<Entity, Client>>>,
proxies: Vec<Proxy>,

// the address is public and mutable so plugins can change it
pub resolved_address: Arc<RwLock<SocketAddr>>,
Expand Down Expand Up @@ -344,6 +345,7 @@ where
let swarm = Swarm {
ecs_lock: ecs_lock.clone(),
bots: Arc::new(Mutex::new(HashMap::new())),
proxies: self.proxies,

resolved_address: Arc::new(RwLock::new(resolved_address)),
address: Arc::new(RwLock::new(address)),
Expand All @@ -369,15 +371,13 @@ where
let mut swarm_clone = swarm.clone();
let join_delay = self.join_delay;
let accounts = self.accounts.clone();
let proxies = self.proxies.clone();
let states = self.states.clone();

tokio::spawn(async move {
if let Some(join_delay) = join_delay {
// if there's a join delay, then join one by one
for (account, state) in accounts.iter().zip(states) {
let proxy = proxies.choose(&mut thread_rng()).map(|p| p.clone());
swarm_clone.add_and_retry_forever(account, proxy, state).await;
swarm_clone.add_and_retry_forever(account, state).await;
tokio::time::sleep(join_delay).await;
}
} else {
Expand All @@ -387,12 +387,10 @@ where
accounts
.iter()
.zip(states)
.zip(proxies.iter().cycle())
.map(move |((account, state), proxy)| async {
// todo: fix when no proxies available + make proxies random
.map(move |(account, state)| async {
swarm_borrow
.clone()
.add_and_retry_forever(account, Some(proxy.clone()), state)
.add_and_retry_forever(account, state)
.await;
}),
)
Expand Down Expand Up @@ -597,12 +595,12 @@ impl Swarm {
pub async fn add_and_retry_forever<S: Component + Clone>(
&mut self,
account: &Account,
proxy: Option<Proxy>,
state: S,
) -> Client {
let mut disconnects = 0;
loop {
match self.add(account, proxy.clone(), state.clone()).await {
let proxy = self.proxies.choose(&mut thread_rng()).map(|p| p.clone());

Check warning on line 602 in azalea/src/swarm/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

you are using an explicit closure for cloning elements

warning: you are using an explicit closure for cloning elements --> azalea/src/swarm/mod.rs:602:25 | 602 | let proxy = self.proxies.choose(&mut thread_rng()).map(|p| p.clone()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `self.proxies.choose(&mut thread_rng()).cloned()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone = note: `#[warn(clippy::map_clone)]` on by default
match self.add(account, proxy, state.clone()).await {
Ok(bot) => return bot,
Err(e) => {
disconnects += 1;
Expand Down

0 comments on commit 487f9df

Please sign in to comment.