Made client manager Lua scriptable

This commit is contained in:
michael-bailey 2022-03-03 10:49:04 +00:00
parent 0681f2ea65
commit 43cafc2c4d
1 changed files with 36 additions and 1 deletions

View File

@ -10,11 +10,13 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
use uuid::Uuid;
use async_trait::async_trait;
use mlua::prelude::LuaUserData;
use mlua::{UserDataFields, UserDataMethods};
use foundation::prelude::IManager;
use foundation::connection::Connection;
use crate::client::Client;
use crate::client::{Client, ClientLua};
use crate::messages::ClientMessage;
#[derive(Debug)]
@ -190,6 +192,39 @@ impl<Out> IManager for ClientManager<Out>
}
}
#[derive(Clone)]
pub struct ClientManagerLua<Out: 'static>(pub Arc<ClientManager<Out>>)
where
Out: From<ClientMgrMessage> + Send;
impl<Out: 'static> LuaUserData for ClientManagerLua<Out>
where
Out: From<ClientMgrMessage> + Clone + Send
{
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
}
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method("getCount", |_lua,this,()| {
let this = this.0.clone();
async move {
Ok(this.clients.lock().await.len())
}
});
methods.add_async_method("getClientList", |_lua,this,()| {
let this = this.0.clone();
async move {
let clients = this.clients.lock().await;
let clients: Vec<ClientLua<ClientMgrMessage>> = clients.iter()
.map(|(_id,c)| ClientLua(c.clone()))
.collect();
Ok(clients)
}
})
}
}
#[cfg(test)]
mod test {