pub struct Swarm {
pub ecs_lock: Arc<Mutex<World>>,
pub resolved_address: Arc<RwLock<SocketAddr>>,
pub address: Arc<RwLock<ServerAddress>>,
pub instance_container: Arc<RwLock<InstanceContainer>>,
/* private fields */
}Expand description
A swarm is a way to conveniently control many bots at once, while also being able to control bots at an individual level when desired.
It can safely be cloned, so there should be no need to wrap them in a Mutex.
Swarms are created from SwarmBuilder.
Clients can be added to the swarm later via Swarm::add, and can be
removed with Client::disconnect.
Fields§
§ecs_lock: Arc<Mutex<World>>§resolved_address: Arc<RwLock<SocketAddr>>§address: Arc<RwLock<ServerAddress>>§instance_container: Arc<RwLock<InstanceContainer>>Implementations§
Source§impl Swarm
Make a bot Swarm.
impl Swarm
Make a bot Swarm.
§Examples
use azalea::{prelude::*, swarm::prelude::*};
use std::time::Duration;
#[derive(Default, Clone, Component)]
struct State {}
#[derive(Default, Clone, Resource)]
struct SwarmState {}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut accounts = Vec::new();
let mut states = Vec::new();
for i in 0..10 {
accounts.push(Account::offline(&format!("bot{i}")));
states.push(State::default());
}
SwarmBuilder::new()
.add_accounts(accounts.clone())
.set_handler(handle)
.set_swarm_handler(swarm_handle)
.join_delay(Duration::from_millis(1000))
.start("localhost")
.await
.unwrap();
}
async fn handle(bot: Client, event: Event, _state: State) -> anyhow::Result<()> {
match &event {
_ => {}
}
Ok(())
}
async fn swarm_handle(
mut swarm: Swarm,
event: SwarmEvent,
_state: SwarmState,
) -> anyhow::Result<()> {
match &event {
SwarmEvent::Chat(m) => {
println!("{}", m.message().to_ansi());
}
_ => {}
}
Ok(())
}Sourcepub async fn add<S: Component + Clone>(
&self,
account: &Account,
state: S,
) -> Result<Client, JoinError>
pub async fn add<S: Component + Clone>( &self, account: &Account, state: S, ) -> Result<Client, JoinError>
Add a new account to the swarm.
You can remove it later by calling Client::disconnect.
§Errors
Returns an error if the server’s address could not be resolved.
Sourcepub async fn add_with_opts<S: Component + Clone>(
&self,
account: &Account,
state: S,
join_opts: &JoinOpts,
) -> Result<Client, JoinError>
pub async fn add_with_opts<S: Component + Clone>( &self, account: &Account, state: S, join_opts: &JoinOpts, ) -> Result<Client, JoinError>
Sourcepub fn client_entities(&self) -> Box<[Entity]>
pub fn client_entities(&self) -> Box<[Entity]>
Get an array of ECS [Entity]s for all LocalEntitys in our world.
This will include clients that were disconnected without being removed
from the ECS.
Trait Implementations§
Source§impl IntoIterator for Swarm
impl IntoIterator for Swarm
impl Resource for Swarm
Auto Trait Implementations§
impl Freeze for Swarm
impl !RefUnwindSafe for Swarm
impl Send for Swarm
impl Sync for Swarm
impl Unpin for Swarm
impl !UnwindSafe for Swarm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSend for T
impl<T> DowncastSend for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Converts this type into the system output type.
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> TypeData for T
impl<T> TypeData for T
§fn clone_type_data(&self) -> Box<dyn TypeData>
fn clone_type_data(&self) -> Box<dyn TypeData>
Creates a type-erased clone of this value.