Fix player allocation (lma0)
This commit is contained in:
parent
7b4f6239ce
commit
f257027af1
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
28
src/main.rs
28
src/main.rs
@ -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,12 +59,34 @@ 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);
|
||||||
|
let mut insertion_attempts: u8 = 0;
|
||||||
|
while players_arc.lock()?[thread_number as usize].id != SpecialPlayers::SelfPlayer as u8
|
||||||
|
&& insertion_attempts < config.server.max_players
|
||||||
|
{
|
||||||
|
insertion_attempts += 1;
|
||||||
|
// One is reserved for communications
|
||||||
|
if thread_number < config.server.max_players - 1 {
|
||||||
|
thread_number += 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(
|
handle_client(
|
||||||
config.clone().server,
|
config.clone().server,
|
||||||
stream?,
|
stream?,
|
||||||
@ -71,7 +95,7 @@ fn run() -> Result<(), AppError> {
|
|||||||
world_arc_clone,
|
world_arc_clone,
|
||||||
extensions_arc_clone,
|
extensions_arc_clone,
|
||||||
);
|
);
|
||||||
thread_number = thread_number.wrapping_add(1);
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
||||||
|
Loading…
Reference in New Issue
Block a user