small improvements + players cmd + cleanup

This commit is contained in:
Luuk van Oijen
2023-11-23 16:03:31 +01:00
parent f0a5fa48b8
commit 39028ba4d7
4 changed files with 60 additions and 14 deletions

View File

@@ -24,17 +24,17 @@ use super::car::*;
use super::packet::*;
lazy_static! {
pub static ref TAKEN_PLAYER_IDS: Mutex<[bool;256]> = Mutex::new([false; 256]);
pub static ref TAKEN_PLAYER_IDS: Mutex<Vec<u8>> = Mutex::new(Vec::new());
pub static ref CLIENT_MOD_PROGRESS: Mutex<HashMap<u8, isize>> = Mutex::new(HashMap::new());
}
// TODO: Return a proper error?
async fn claim_id() -> Result<u8, ()> {
let mut lock = TAKEN_PLAYER_IDS.lock().await;
for index in 0..255 {
if !lock[index] {
lock[index] = true;
return Ok(index as u8);
for index in 0..=255 {
if !lock.contains(&index) {
lock.push(index);
return Ok(index);
}
}
// requires more then 255 players on the server to error
@@ -43,7 +43,7 @@ async fn claim_id() -> Result<u8, ()> {
async fn free_id(id: u8) {
let mut lock = TAKEN_PLAYER_IDS.lock().await;
lock[id as usize] = false;
*lock = lock.drain(..).filter(|i| *i != id).collect::<Vec<u8>>();
}
#[derive(PartialEq)]

View File

@@ -288,7 +288,6 @@ impl Server {
}
if set.is_empty() == false {
info!("what!");
// Because join_next() is cancel safe, we can simply cancel it after N duration
// so at worst this client acceptance loop blocks for N duration
tokio::select! {