1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

start simplifying swarm internals

This commit is contained in:
mat 2025-04-21 16:33:52 -02:00
parent 8b92b668d6
commit b1cd39615e
3 changed files with 20 additions and 37 deletions

View file

@ -107,20 +107,20 @@ pub enum JoinError {
Disconnect { reason: FormattedText },
}
pub struct StartClientOpts<'a> {
pub struct StartClientOpts {
pub ecs_lock: Arc<Mutex<World>>,
pub account: &'a Account,
pub account: Account,
pub connect_opts: ConnectOpts,
pub event_sender: Option<mpsc::UnboundedSender<Event>>,
}
impl<'a> StartClientOpts<'a> {
impl StartClientOpts {
pub fn new(
account: &'a Account,
address: &'a ServerAddress,
account: Account,
address: ServerAddress,
resolved_address: SocketAddr,
event_sender: Option<mpsc::UnboundedSender<Event>>,
) -> StartClientOpts<'a> {
) -> StartClientOpts {
let mut app = App::new();
app.add_plugins(DefaultPlugins);
@ -131,7 +131,7 @@ impl<'a> StartClientOpts<'a> {
ecs_lock,
account,
connect_opts: ConnectOpts {
address: address.clone(),
address,
resolved_address,
proxy: None,
},
@ -180,7 +180,7 @@ impl Client {
/// }
/// ```
pub async fn join(
account: &Account,
account: Account,
address: impl TryInto<ServerAddress>,
) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> {
let address: ServerAddress = address.try_into().map_err(|_| JoinError::InvalidAddress)?;
@ -189,7 +189,7 @@ impl Client {
let client = Self::start_client(StartClientOpts::new(
account,
&address,
address,
resolved_address,
Some(tx),
))
@ -198,7 +198,7 @@ impl Client {
}
pub async fn join_with_proxy(
account: &Account,
account: Account,
address: impl TryInto<ServerAddress>,
proxy: Proxy,
) -> Result<(Self, mpsc::UnboundedReceiver<Event>), JoinError> {
@ -207,7 +207,7 @@ impl Client {
let (tx, rx) = mpsc::unbounded_channel();
let client = Self::start_client(
StartClientOpts::new(account, &address, resolved_address, Some(tx)).proxy(proxy),
StartClientOpts::new(account, address, resolved_address, Some(tx)).proxy(proxy),
)
.await?;
Ok((client, rx))
@ -221,22 +221,19 @@ impl Client {
account,
connect_opts,
event_sender,
}: StartClientOpts<'_>,
}: StartClientOpts,
) -> Result<Self, JoinError> {
// send a StartJoinServerEvent
let (start_join_callback_tx, mut start_join_callback_rx) =
mpsc::unbounded_channel::<Result<Entity, JoinError>>();
{
let mut ecs = ecs_lock.lock();
ecs.send_event(StartJoinServerEvent {
account: account.clone(),
connect_opts,
event_sender: event_sender.clone(),
start_join_callback_tx: Some(StartJoinCallback(start_join_callback_tx)),
});
}
ecs_lock.lock().send_event(StartJoinServerEvent {
account,
connect_opts,
event_sender,
start_join_callback_tx: Some(StartJoinCallback(start_join_callback_tx)),
});
let entity = start_join_callback_rx.recv().await.expect(
"StartJoinCallback should not be dropped before sending a message, this is a bug in Azalea",

View file

@ -192,7 +192,7 @@ async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Resu
Ok(())
}
async fn swarm_handle(swarm: Swarm, event: SwarmEvent, _state: SwarmState) -> anyhow::Result<()> {
async fn swarm_handle(_swarm: Swarm, event: SwarmEvent, _state: SwarmState) -> anyhow::Result<()> {
match &event {
SwarmEvent::Disconnect(account, join_opts) => {
println!("bot got kicked! {}", account.username);

View file

@ -45,8 +45,6 @@ use crate::{BoxHandleFn, DefaultBotPlugins, HandleFn, JoinOpts, NoState, StartEr
pub struct Swarm {
pub ecs_lock: Arc<Mutex<World>>,
bots: Arc<Mutex<HashMap<Entity, Client>>>,
// the address is public and mutable so plugins can change it
pub resolved_address: Arc<RwLock<SocketAddr>>,
pub address: Arc<RwLock<ServerAddress>>,
@ -400,7 +398,6 @@ where
let swarm = Swarm {
ecs_lock: ecs_lock.clone(),
bots: Arc::new(Mutex::new(HashMap::new())),
resolved_address: Arc::new(RwLock::new(resolved_address)),
address: Arc::new(RwLock::new(address)),
@ -679,20 +676,11 @@ impl Swarm {
ecs.entity_mut(bot.entity).insert(state);
}
self.bots.lock().insert(bot.entity, bot.clone());
let cloned_bots = self.bots.clone();
let cloned_bots_tx = self.bots_tx.clone();
let cloned_bot = bot.clone();
let swarm_tx = self.swarm_tx.clone();
let join_opts = join_opts.clone();
tokio::spawn(Self::event_copying_task(
rx,
cloned_bots,
cloned_bots_tx,
cloned_bot,
swarm_tx,
join_opts,
rx, cloned_bot, swarm_tx, join_opts,
));
Ok(bot)
@ -700,8 +688,6 @@ impl Swarm {
async fn event_copying_task(
mut rx: mpsc::UnboundedReceiver<Event>,
cloned_bots: Arc<Mutex<HashMap<Entity, Client>>>,
cloned_bots_tx: mpsc::UnboundedSender<(Option<Event>, Client)>,
cloned_bot: Client,
swarm_tx: mpsc::UnboundedSender<SwarmEvent>,
join_opts: JoinOpts,