crystalorb-mock-network

Crates.iocrystalorb-mock-network
lib.rscrystalorb-mock-network
version0.3.0
sourcesrc
created_at2021-06-08 11:36:02.702789
updated_at2021-12-07 08:09:53.11957
descriptionMock, offline, in-memory transport layer for CrystalOrb
homepage
repositoryhttps://github.com/ErnWong/crystalorb
max_upload_size
id407780
size13,291
Ernest Wong (ErnWong)

documentation

README

crystalorb-mock-network

Provides an implementation of crystalorb::network_recource::NetworkResource that works offline, backed using VecDeque, allowing CrystalOrb to work offline for demo and testing purposes.

Usage

use crystalorb_mock_network::MockNetwork;
use crystalorb_demo::DemoWorld;
use crystalorb::{client::Client, server::Server, Config};

let (mut server_net, (mut client_1_net, mut client_2_net)) =
    MockNetwork::new_mock_network::<DemoWorld>();

let client_1 = Client::<DemoWorld>::new(Config::default());
let client_2 = Client::<DemoWorld>::new(Config::default());
let server = Server::<DemoWorld>::new(Config::default(), 0.0);

// You need to manually call connect.
client_1_net.connect();
client_2_net.connect();

// (You can also simulate a disconnection later on)
// client_1_net.disconnect();
// client_2_net.disconnect();

// (You can also set the latency of each connection)
// client_1_net.set_delay(number_of_seconds_as_f64);

// Later, in your update loop:
fn update_loop(
  delta_seconds: f64,
  seconds_since_startup: f64,
  client_1: &mut Client<DemoWorld>,
  client_2: &mut Client<DemoWorld>,
  server: &mut Server<DemoWorld>,
  client_1_net: &mut MockNetwork,
  client_2_net: &mut MockNetwork,
  server_net: &mut MockNetwork,
) {
    // You can then use these network resource instances to update
    // the `crystalorb` clients and server.
    client_1.update(delta_seconds, seconds_since_startup, client_1_net);
    client_2.update(delta_seconds, seconds_since_startup, client_2_net);
    server.update(delta_seconds, seconds_since_startup, server_net);

    // Call the `tick` method to simulate the flow of messages.
    // If you don't call `tick`, then no messages will be able to pass through the network.
    client_1_net.tick(delta_seconds);
    client_2_net.tick(delta_seconds);
    server_net.tick(delta_seconds);
}
Commit count: 267

cargo fmt