added client indexing to client manager

This commit is contained in:
michael-bailey 2022-03-03 12:54:17 +00:00
parent 5bf1d260ce
commit b29d055aa1
2 changed files with 18 additions and 2 deletions

View File

@ -2,4 +2,5 @@ print("Test Script")
print(Server.ClientManager:getCount())
print("Test Script")

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use futures::future::join_all;
@ -11,7 +12,8 @@ use uuid::Uuid;
use async_trait::async_trait;
use mlua::prelude::LuaUserData;
use mlua::{UserDataFields, UserDataMethods};
use mlua::{MetaMethod, Nil, ToLua, UserDataFields, UserDataMethods};
use mlua::Value::UserData;
use foundation::prelude::IManager;
use foundation::connection::Connection;
@ -222,7 +224,20 @@ impl<Out: 'static> LuaUserData for ClientManagerLua<Out>
.collect();
Ok(clients)
}
})
});
methods.add_async_meta_method(MetaMethod::Index, |lua, this, (index): (String)| {
let manager = this.0.clone();
async move {
if let Ok(id) = Uuid::from_str(&index) {
let map = manager.clients.lock().await;
if let Some(found) = map.get(&id) {
return Ok(ClientLua(found.clone()).to_lua(lua)?);
}
}
return Ok(Nil);
}
});
}
}