pub trait Game {
fn handle_command(
&mut self,
delta_time: Duration,
commands: Vec<Vec<u8>>,
from: SocketAddr
) -> ContinueRunning;
fn draw(&mut self, delta_time: Duration) -> Vec<u8>;
fn allow_connect(&mut self, _from: &SocketAddr) -> bool { ... }
fn handle_server_event(&mut self, _event: ServerEvent) -> ContinueRunning { ... }
fn add_client(&mut self) -> Option<SocketAddr> { ... }
fn remove_client(&mut self) -> Option<SocketAddr> { ... }
}
Expand description
Game to use with server must implement this trait.
Required Methods
sourcefn handle_command(
&mut self,
delta_time: Duration,
commands: Vec<Vec<u8>>,
from: SocketAddr
) -> ContinueRunning
fn handle_command(
&mut self,
delta_time: Duration,
commands: Vec<Vec<u8>>,
from: SocketAddr
) -> ContinueRunning
delta_time: time elapsed from last call. command: ordered commands commands from server. from: Address of command sender. Returns bool value indicating should server continue running if false stops server. Called only when new commands come to server. Commands ordered and with some guarantees.
sourcefn draw(&mut self, delta_time: Duration) -> Vec<u8>
fn draw(&mut self, delta_time: Duration) -> Vec<u8>
Gets new state to send to client. delta_time: time elapsed throw last call. Returns bytes with new game state for client. Called once in about 30 milliseconds. Sends state only to clients connected to server. Ordered and without some guarantees.
Provided Methods
sourcefn allow_connect(&mut self, _from: &SocketAddr) -> bool
fn allow_connect(&mut self, _from: &SocketAddr) -> bool
Allow client with this IP Address work with server. If false server don’t send new state to this client. Usually don’t implement this method. Use default implementation.
sourcefn handle_server_event(&mut self, _event: ServerEvent) -> ContinueRunning
fn handle_server_event(&mut self, _event: ServerEvent) -> ContinueRunning
Handles events from server. Returns bool value. If returns false stops server. Usually don’t implement this method. Use default implementation.
sourcefn add_client(&mut self) -> Option<SocketAddr>
fn add_client(&mut self) -> Option<SocketAddr>
Client to add to recv state from server. If returns not None then servers on draw sends new state to this client. If client with this IP Address already connected then nothing happens. Usually don’t implement this method. Use default implementation.
sourcefn remove_client(&mut self) -> Option<SocketAddr>
fn remove_client(&mut self) -> Option<SocketAddr>
Disconnect this client from server and don’t send new state to them. Usually don’t implement this method. Use default implementation.