Fix player allocation (lma0)

This commit is contained in:
illegitimate-egg 2025-03-18 21:14:25 +00:00
parent 7b4f6239ce
commit f257027af1
5 changed files with 45 additions and 11 deletions

View File

@ -7,4 +7,10 @@ mæk ˈrrɪzᵊl ˈdrɪzᵊl
TODO: TODO:
- [x] ~~Fix error on disconnect~~ - [x] ~~Fix error on disconnect~~
- [x] ~~Command support~~ - [x] ~~Command support~~
- [ ] Operators
- [ ] Finish command support (Event Listeners)
- [ ] Finish the readme
- [ ] Fix player id/thread allocator
Backburner:
- [ ] classicube extensions - [ ] classicube extensions

View File

@ -2,6 +2,7 @@
name = "mcrizzledizzle default" name = "mcrizzledizzle default"
motd = "For shits and giggles" motd = "For shits and giggles"
port = 25565 port = 25565
max_players = 3
[world] [world]
world = "world.wrld" # Custom world type, not interchangable with other servers world = "world.wrld" # Custom world type, not interchangable with other servers

View File

@ -20,6 +20,7 @@ pub struct Config {
#[serde(default)] #[serde(default)]
pub struct ServerConfig { pub struct ServerConfig {
pub port: u16, pub port: u16,
pub max_players: u8, // Upper limit
pub name: String, pub name: String,
pub motd: String, pub motd: String,
} }
@ -28,6 +29,7 @@ impl Default for ServerConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
port: 25565, port: 25565,
max_players: 255,
name: "Default".to_string(), name: "Default".to_string(),
motd: "Default".to_string(), motd: "Default".to_string(),
} }
@ -64,6 +66,7 @@ impl Config {
name = "mcrizzledizzle default" name = "mcrizzledizzle default"
motd = "For shits and giggles" motd = "For shits and giggles"
port = 25565 port = 25565
max_players = 255
[world] [world]
world = "world.wrld" # Custom world type, not interchangable with other servers world = "world.wrld" # Custom world type, not interchangable with other servers

View File

@ -1,5 +1,6 @@
use log::{error, info}; use log::{error, info};
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use std::io::Write;
use std::net::{SocketAddr, TcpListener}; use std::net::{SocketAddr, TcpListener};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -17,6 +18,7 @@ use error::AppError;
use extensions::{Extensions, PlayersWrapper, WorldWrapper}; use extensions::{Extensions, PlayersWrapper, WorldWrapper};
use network::handle_client; use network::handle_client;
use player::{Player, SpecialPlayers}; use player::{Player, SpecialPlayers};
use utils::{client_disconnect, server_identification};
use world::World; use world::World;
fn main() { fn main() {
@ -57,21 +59,43 @@ fn run() -> Result<(), AppError> {
WorldWrapper::new(world_arc.clone()), WorldWrapper::new(world_arc.clone()),
)?); )?);
info!("Server listening on {}", 25565); info!("Server listening on {}", config.server.port);
for stream in listener.incoming() { for stream in listener.incoming() {
let players_arc_clone = Arc::clone(&players_arc); let players_arc_clone = Arc::clone(&players_arc);
let world_arc_clone = Arc::clone(&world_arc); let world_arc_clone = Arc::clone(&world_arc);
let extensions_arc_clone = Arc::clone(&extensions); let extensions_arc_clone = Arc::clone(&extensions);
handle_client( let mut insertion_attempts: u8 = 0;
config.clone().server, while players_arc.lock()?[thread_number as usize].id != SpecialPlayers::SelfPlayer as u8
stream?, && insertion_attempts < config.server.max_players
thread_number, {
players_arc_clone, insertion_attempts += 1;
world_arc_clone, // One is reserved for communications
extensions_arc_clone, if thread_number < config.server.max_players - 1 {
); thread_number += 1;
thread_number = thread_number.wrapping_add(1); } else {
thread_number = 0;
}
}
if insertion_attempts == config.server.max_players {
// Server must be full
// Seems silly that we have to ident to kick clients, but I didn't make the protocol
let mut disconnect_packets: Vec<u8> = Vec::new();
disconnect_packets
.extend_from_slice(&server_identification(config.server.clone(), false));
disconnect_packets
.extend_from_slice(&client_disconnect("Server is full! Try again later"));
stream?.write_all(&disconnect_packets)?;
} else {
handle_client(
config.clone().server,
stream?,
thread_number,
players_arc_clone,
world_arc_clone,
extensions_arc_clone,
);
}
} }
Ok(()) Ok(())
} }

View File

@ -282,7 +282,7 @@ pub fn handle_client(
break; break;
} }
sleep(Duration::from_millis(1)); // 1000 TPS TODO: Delta time sleep(Duration::from_millis(50)); // 1000 TPS TODO: Delta time
{ {
let mut players = players_arc_clone.lock().unwrap(); let mut players = players_arc_clone.lock().unwrap();
if !players[client_number as usize].outgoing_data.is_empty() { if !players[client_number as usize].outgoing_data.is_empty() {