2023-03-12 11:58:24 -05:00
|
|
|
use shipyard::{Unique, UniqueView, UniqueViewMut, Workload, IntoWorkload, AllStoragesView, View, Get, NonSendSync, IntoIter};
|
2023-03-06 21:50:06 -06:00
|
|
|
use glam::IVec3;
|
|
|
|
use hashbrown::HashMap;
|
2023-03-08 20:30:37 -06:00
|
|
|
use kubi_shared::networking::{
|
2023-03-12 11:58:24 -05:00
|
|
|
messages::{ClientToServerMessage, ServerToClientMessage, C_CHUNK_SUB_REQUEST, C_QUEUE_BLOCK},
|
|
|
|
channels::{CHANNEL_WORLD, CHANNEL_BLOCK},
|
2023-03-08 20:30:37 -06:00
|
|
|
client::Client,
|
|
|
|
};
|
2023-05-20 19:16:34 -05:00
|
|
|
use uflow::{server::RemoteClient, SendMode};
|
2023-03-08 21:06:46 -06:00
|
|
|
use lz4_flex::compress_prepend_size as lz4_compress;
|
|
|
|
use anyhow::Result;
|
|
|
|
use std::{rc::Rc, cell::RefCell};
|
2023-05-19 00:02:20 -05:00
|
|
|
use kubi_shared::networking::client::ClientIdMap;
|
2023-03-06 21:50:06 -06:00
|
|
|
use crate::{
|
2023-05-20 19:16:34 -05:00
|
|
|
server::{UdpServer, ServerEvents},
|
2023-03-06 21:50:06 -06:00
|
|
|
config::ConfigTable,
|
2023-05-19 00:02:20 -05:00
|
|
|
client::{ClientAddress, ClientAddressMap},
|
2023-05-19 06:22:32 -05:00
|
|
|
util::check_message_auth,
|
2023-03-06 21:50:06 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
pub mod chunk;
|
|
|
|
pub mod tasks;
|
|
|
|
|
|
|
|
use chunk::Chunk;
|
|
|
|
|
2023-03-06 21:56:51 -06:00
|
|
|
use self::{tasks::{ChunkTaskManager, ChunkTask, ChunkTaskResponse, init_chunk_task_manager}, chunk::ChunkState};
|
2023-03-06 21:50:06 -06:00
|
|
|
|
|
|
|
#[derive(Unique, Default)]
|
|
|
|
pub struct ChunkManager {
|
|
|
|
pub chunks: HashMap<IVec3, Chunk>
|
|
|
|
}
|
|
|
|
impl ChunkManager {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 21:06:46 -06:00
|
|
|
///Sends a compressed chunk packet
|
|
|
|
pub fn send_chunk_compressed(
|
|
|
|
client: &Rc<RefCell<RemoteClient>>,
|
|
|
|
message: &ServerToClientMessage
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut ser_message = postcard::to_allocvec(&message)?;
|
2023-03-08 21:21:59 -06:00
|
|
|
let mut compressed = lz4_compress(&ser_message[1..]);
|
2023-03-08 21:06:46 -06:00
|
|
|
ser_message.truncate(1);
|
|
|
|
ser_message.append(&mut compressed);
|
|
|
|
let ser_message = ser_message.into_boxed_slice();
|
|
|
|
client.borrow_mut().send(ser_message, CHANNEL_WORLD, SendMode::Reliable);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:50:06 -06:00
|
|
|
fn process_chunk_requests(
|
2023-03-08 21:06:46 -06:00
|
|
|
server: NonSendSync<UniqueView<UdpServer>>,
|
2023-03-06 21:50:06 -06:00
|
|
|
events: UniqueView<ServerEvents>,
|
|
|
|
mut chunk_manager: UniqueViewMut<ChunkManager>,
|
|
|
|
task_manager: UniqueView<ChunkTaskManager>,
|
2023-03-08 20:30:37 -06:00
|
|
|
config: UniqueView<ConfigTable>,
|
|
|
|
addr_map: UniqueView<ClientAddressMap>,
|
|
|
|
clients: View<Client>
|
2023-03-06 21:50:06 -06:00
|
|
|
) {
|
|
|
|
for event in &events.0 {
|
2023-05-19 06:22:32 -05:00
|
|
|
let Some(message) = check_message_auth::<C_CHUNK_SUB_REQUEST>(&server, event, &clients, &addr_map) else {
|
|
|
|
continue;
|
2023-03-08 20:30:37 -06:00
|
|
|
};
|
2023-05-19 06:22:32 -05:00
|
|
|
let ClientToServerMessage::ChunkSubRequest { chunk: chunk_position } = message.message else {
|
2023-03-08 20:30:37 -06:00
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(chunk) = chunk_manager.chunks.get_mut(&chunk_position) {
|
2023-05-19 06:22:32 -05:00
|
|
|
chunk.subscriptions.insert(message.client_id);
|
2023-03-08 20:30:37 -06:00
|
|
|
//TODO Start task here if status is "Nothing"
|
|
|
|
if let Some(blocks) = &chunk.blocks {
|
2023-03-08 21:06:46 -06:00
|
|
|
send_chunk_compressed(
|
2023-05-19 06:22:32 -05:00
|
|
|
&message.client,
|
2023-03-08 21:06:46 -06:00
|
|
|
&ServerToClientMessage::ChunkResponse {
|
2023-03-08 20:30:37 -06:00
|
|
|
chunk: chunk_position,
|
2023-03-06 21:50:06 -06:00
|
|
|
data: blocks.clone(),
|
|
|
|
queued: Vec::with_capacity(0)
|
2023-03-08 21:06:46 -06:00
|
|
|
}
|
|
|
|
).unwrap();
|
2023-03-06 21:50:06 -06:00
|
|
|
}
|
2023-03-08 20:30:37 -06:00
|
|
|
} else {
|
|
|
|
let mut chunk = Chunk::new(chunk_position);
|
|
|
|
chunk.state = ChunkState::Loading;
|
2023-05-19 06:22:32 -05:00
|
|
|
chunk.subscriptions.insert(message.client_id);
|
2023-03-08 20:30:37 -06:00
|
|
|
chunk_manager.chunks.insert(chunk_position, chunk);
|
|
|
|
task_manager.spawn_task(ChunkTask::LoadChunk {
|
|
|
|
position: chunk_position,
|
|
|
|
seed: config.world.seed,
|
|
|
|
});
|
2023-03-06 21:50:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_finished_tasks(
|
2023-03-08 21:06:46 -06:00
|
|
|
server: NonSendSync<UniqueView<UdpServer>>,
|
2023-03-06 21:50:06 -06:00
|
|
|
task_manager: UniqueView<ChunkTaskManager>,
|
|
|
|
mut chunk_manager: UniqueViewMut<ChunkManager>,
|
2023-03-08 20:30:37 -06:00
|
|
|
id_map: UniqueView<ClientIdMap>,
|
|
|
|
client_addr: View<ClientAddress>,
|
2023-03-06 21:50:06 -06:00
|
|
|
) {
|
2023-03-08 20:30:37 -06:00
|
|
|
'outer: while let Some(res) = task_manager.receive() {
|
2023-03-06 21:50:06 -06:00
|
|
|
let ChunkTaskResponse::ChunkLoaded { chunk_position, blocks, queue } = res;
|
|
|
|
let Some(chunk) = chunk_manager.chunks.get_mut(&chunk_position) else {
|
|
|
|
log::warn!("Chunk discarded: Doesn't exist");
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
if chunk.state != ChunkState::Loading {
|
|
|
|
log::warn!("Chunk discarded: Not Loading");
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
chunk.state = ChunkState::Loaded;
|
|
|
|
chunk.blocks = Some(blocks.clone());
|
2023-03-08 21:06:46 -06:00
|
|
|
|
2023-03-07 14:29:05 -06:00
|
|
|
log::debug!("Chunk {chunk_position} loaded, {} subs", chunk.subscriptions.len());
|
2023-03-08 21:06:46 -06:00
|
|
|
|
|
|
|
let chunk_packet = &ServerToClientMessage::ChunkResponse {
|
|
|
|
chunk: chunk_position,
|
|
|
|
data: blocks,
|
|
|
|
queued: queue
|
|
|
|
};
|
|
|
|
|
2023-03-08 20:30:37 -06:00
|
|
|
for &subscriber in &chunk.subscriptions {
|
|
|
|
let Some(&entity_id) = id_map.0.get(&subscriber) else {
|
|
|
|
log::error!("Invalid subscriber client id");
|
|
|
|
continue 'outer;
|
|
|
|
};
|
|
|
|
let Ok(&ClientAddress(client_addr)) = (&client_addr).get(entity_id) else {
|
|
|
|
log::error!("Invalid subscriber entity id");
|
|
|
|
continue 'outer;
|
|
|
|
};
|
|
|
|
let Some(client) = server.0.client(&client_addr) else {
|
|
|
|
log::error!("Client not connected");
|
|
|
|
continue 'outer;
|
|
|
|
};
|
2023-03-08 21:06:46 -06:00
|
|
|
send_chunk_compressed(client, chunk_packet).unwrap();
|
|
|
|
// client.borrow_mut().send(
|
|
|
|
// chunk_packet.clone(),
|
|
|
|
// CHANNEL_WORLD,
|
|
|
|
// SendMode::Reliable,
|
|
|
|
// );
|
2023-03-07 14:29:05 -06:00
|
|
|
}
|
2023-03-06 21:50:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-12 11:58:24 -05:00
|
|
|
fn process_block_queue_messages(
|
|
|
|
server: NonSendSync<UniqueView<UdpServer>>,
|
|
|
|
events: UniqueView<ServerEvents>,
|
|
|
|
addr_map: UniqueView<ClientAddressMap>,
|
|
|
|
clients: View<Client>,
|
|
|
|
addrs: View<ClientAddress>,
|
|
|
|
) {
|
|
|
|
for event in &events.0 {
|
2023-05-19 06:22:32 -05:00
|
|
|
let Some(message) = check_message_auth::<C_QUEUE_BLOCK>(&server, event, &clients, &addr_map) else {
|
|
|
|
continue;
|
2023-03-12 11:58:24 -05:00
|
|
|
};
|
2023-05-19 06:22:32 -05:00
|
|
|
let ClientToServerMessage::QueueBlock { item } = message.message else { unreachable!() };
|
2023-03-12 11:58:24 -05:00
|
|
|
//TODO place in our own queue, for now just send to other clients
|
|
|
|
log::info!("Placed block {:?} at {}", item.block_type, item.position);
|
|
|
|
for (other_client, other_client_address) in (&clients, &addrs).iter() {
|
|
|
|
//No need to send the event back
|
2023-05-19 06:22:32 -05:00
|
|
|
if message.client_id == other_client.0 {
|
2023-03-12 11:58:24 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
//Get client
|
|
|
|
let Some(client) = server.0.client(&other_client_address.0) else {
|
|
|
|
log::error!("Client with address not found");
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
//Send the message
|
|
|
|
client.borrow_mut().send(
|
|
|
|
postcard::to_allocvec(
|
|
|
|
&ServerToClientMessage::QueueBlock { item }
|
|
|
|
).unwrap().into_boxed_slice(),
|
|
|
|
CHANNEL_BLOCK,
|
|
|
|
SendMode::Reliable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:50:06 -06:00
|
|
|
fn init_chunk_manager(
|
|
|
|
storages: AllStoragesView
|
|
|
|
) {
|
|
|
|
storages.add_unique(ChunkManager::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_world() -> Workload {
|
|
|
|
(
|
2023-03-06 21:56:51 -06:00
|
|
|
init_chunk_manager,
|
|
|
|
init_chunk_task_manager,
|
2023-03-06 21:50:06 -06:00
|
|
|
).into_workload()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_world() -> Workload {
|
|
|
|
(
|
|
|
|
process_chunk_requests,
|
|
|
|
process_finished_tasks,
|
2023-03-12 11:58:24 -05:00
|
|
|
process_block_queue_messages,
|
2023-03-06 21:50:06 -06:00
|
|
|
).into_workload()
|
|
|
|
}
|