added Play state
This commit is contained in:
1 parent
8a5e69f8e3
commit
4b8720efdc
17 files changed
+522
-48
No files matched your search
+90
-9
@@ -1,20 +1,44 @@
|
||||
use std::io::Cursor;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::Cursor,
|
||||
rc::Rc,
|
||||
sync::{
|
||||
atomic::{AtomicI32, Ordering},
|
||||
Arc, Mutex,
|
||||
},
|
||||
};
|
||||
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use mio::{net::TcpStream, Token};
|
||||
use rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
|
||||
|
||||
use crate::protocol::{Players, Sample, StatusResponse, Version};
|
||||
use crate::{
|
||||
client::Client,
|
||||
entity::{
|
||||
player::{GameMode, Player},
|
||||
Entity, EntityId,
|
||||
},
|
||||
protocol::{client::play::CLogin, Players, Sample, StatusResponse, VarInt, Version},
|
||||
};
|
||||
|
||||
pub struct Server {
|
||||
pub compression_threshold: Option<u8>,
|
||||
|
||||
pub online_mode: bool,
|
||||
pub encriyption: bool, // encription is always required when online_mode is disabled
|
||||
pub encryption: bool, // encryptiony is always required when online_mode is disabled
|
||||
pub public_key: RsaPublicKey,
|
||||
pub private_key: RsaPrivateKey,
|
||||
pub public_key_der: Box<[u8]>,
|
||||
|
||||
pub max_players: u32,
|
||||
|
||||
pub status_response: StatusResponse,
|
||||
pub connections: HashMap<Token, Client>,
|
||||
|
||||
// todo replace with HashMap <World, Player>
|
||||
entity_id: AtomicI32, // todo: place this into every world
|
||||
pub players: Vec<Player>,
|
||||
pub difficulty: Difficulty,
|
||||
}
|
||||
|
||||
impl Default for Server {
|
||||
@@ -25,7 +49,8 @@ impl Default for Server {
|
||||
|
||||
impl Server {
|
||||
pub fn new() -> Self {
|
||||
let status_response = Self::default_response();
|
||||
let max_players = 20;
|
||||
let status_response = Self::default_response(max_players);
|
||||
|
||||
// todo, only create when needed
|
||||
let (public_key, private_key) = Self::generate_keys();
|
||||
@@ -37,17 +62,66 @@ impl Server {
|
||||
.into_boxed_slice();
|
||||
|
||||
Self {
|
||||
// 0 is invalid
|
||||
entity_id: 2.into(),
|
||||
online_mode: true,
|
||||
encriyption: true,
|
||||
encryption: true,
|
||||
compression_threshold: None, // 256
|
||||
public_key,
|
||||
private_key,
|
||||
max_players,
|
||||
status_response,
|
||||
public_key_der,
|
||||
connections: HashMap::new(),
|
||||
players: Vec::new(),
|
||||
difficulty: Difficulty::Normal,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_response() -> StatusResponse {
|
||||
pub fn new_client(&mut self, rc: Arc<Mutex<Server>>, connection: TcpStream, token: Token) {
|
||||
self.connections
|
||||
.insert(token, Client::new(rc, Rc::new(token), connection));
|
||||
}
|
||||
|
||||
pub fn spawn_player(&mut self, token: &Token) {
|
||||
let mut player = Player {
|
||||
entity: Entity::new(self.new_entity_id()),
|
||||
client: self.connections.remove(token).unwrap(),
|
||||
};
|
||||
player.send_packet(CLogin::new(
|
||||
player.entity_id(),
|
||||
self.difficulty == Difficulty::Hard,
|
||||
1,
|
||||
vec!["minecraft:overworld".into()],
|
||||
self.max_players as VarInt,
|
||||
8, // view distance todo
|
||||
8, // sim view dinstance todo
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
1,
|
||||
"minecraft:overworld".into(),
|
||||
0, // seed
|
||||
GameMode::Survival,
|
||||
GameMode::Undefined,
|
||||
false,
|
||||
false,
|
||||
false, // deth loc
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
false,
|
||||
));
|
||||
|
||||
self.players.push(player);
|
||||
}
|
||||
|
||||
// move to world
|
||||
pub fn new_entity_id(&self) -> EntityId {
|
||||
self.entity_id.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
|
||||
pub fn default_response(max_players: u32) -> StatusResponse {
|
||||
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
|
||||
|
||||
StatusResponse {
|
||||
@@ -56,12 +130,12 @@ impl Server {
|
||||
protocol: 767,
|
||||
},
|
||||
players: Players {
|
||||
max: 20,
|
||||
max: max_players,
|
||||
online: 0,
|
||||
sample: Sample {
|
||||
sample: vec![Sample {
|
||||
name: "".into(),
|
||||
id: "".into(),
|
||||
},
|
||||
}],
|
||||
},
|
||||
description: "Pumpkin Server".into(),
|
||||
favicon: Self::load_icon(path),
|
||||
@@ -88,3 +162,10 @@ impl Server {
|
||||
(pub_key, priv_key)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Difficulty {
|
||||
Easy,
|
||||
Normal,
|
||||
Hard,
|
||||
}
|
||||
Reference in new issue
Block a user