1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub mod character_screen;
pub mod general;
pub mod in_game;
pub mod network_events;
pub mod ping;
pub mod register;
pub mod terrain;

use crate::{
    client::Client,
    sys::{loot, pets},
};
use common_ecs::{dispatch, System};
use serde::de::DeserializeOwned;
use specs::DispatcherBuilder;

pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
    //run ping after general, as its super fast anyway. also don't get duplicate
    // disconnect then.
    dispatch::<character_screen::Sys>(dispatch_builder, &[]);
    dispatch::<general::Sys>(dispatch_builder, &[]);
    dispatch::<in_game::Sys>(dispatch_builder, &[]);
    dispatch::<ping::Sys>(dispatch_builder, &[&general::Sys::sys_name()]);
    dispatch::<register::Sys>(dispatch_builder, &[]);
    dispatch::<terrain::Sys>(dispatch_builder, &[]);
    dispatch::<pets::Sys>(dispatch_builder, &[]);
    dispatch::<loot::Sys>(dispatch_builder, &[]);
    dispatch::<network_events::Sys>(dispatch_builder, &[]);
}

/// handles all send msg and calls a handle fn
/// Aborts when a error occurred returns cnt of successful msg otherwise
pub(crate) fn try_recv_all<M, F>(
    client: &mut Client,
    stream_id: u8,
    mut f: F,
) -> Result<u64, crate::error::Error>
where
    M: DeserializeOwned,
    F: FnMut(&Client, M) -> Result<(), crate::error::Error>,
{
    let mut cnt = 0u64;
    loop {
        let msg = match client.recv(stream_id) {
            Ok(Some(msg)) => msg,
            Ok(None) => break Ok(cnt),
            Err(e) => break Err(e.into()),
        };
        if let Err(e) = f(client, msg) {
            break Err(e);
        }
        cnt += 1;
    }
}