rust/hedgewars-server/src/utils.rs
author alfadur
Mon, 07 Oct 2019 23:46:49 +0300
changeset 15443 eb1c407a9e8f
parent 15026 a479916799ea
child 15444 c03b2e263488
permissions -rw-r--r--
add protocol version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     1
use base64::encode;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents: 12125
diff changeset
     2
use mio;
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     3
use std::iter::Iterator;
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents: 12125
diff changeset
     4
14783
b3adc030104b implement server vars
alfadur
parents: 14781
diff changeset
     5
pub const SERVER_VERSION: u32 = 3;
14779
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14457
diff changeset
     6
pub const SERVER_TOKEN: mio::Token = mio::Token(1_000_000_000);
14830
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
     7
pub const SECURE_SERVER_TOKEN: mio::Token = mio::Token(1_000_000_001);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
     8
pub const TIMER_TOKEN: mio::Token = mio::Token(1_000_000_002);
8ddb5842fe0b allow running plaintext and tls servers in parallel
alfadur
parents: 14803
diff changeset
     9
pub const IO_TOKEN: mio::Token = mio::Token(1_000_000_003);
13416
cdf69667593b partial room implementation
alfadur
parents: 12137
diff changeset
    10
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    11
pub fn is_name_illegal(name: &str) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    12
    name.len() > 40
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    13
        || name.trim().is_empty()
14781
01f8ab45f806 fix lobby joining
alfadur
parents: 14779
diff changeset
    14
        || name.trim() != name
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    15
        || name
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    16
            .chars()
15026
a479916799ea use contains where possible
alfadur
parents: 14830
diff changeset
    17
            .any(|c| "$()*+?[]^{|}\x7F".contains(c) || ('\x00'..='\x1F').contains(&c))
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    18
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    19
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    20
pub fn to_engine_msg<T>(msg: T) -> String
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    21
where
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    22
    T: Iterator<Item = u8> + Clone,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    23
{
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    24
    let mut tmp = Vec::new();
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    25
    tmp.push(msg.clone().count() as u8);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    26
    tmp.extend(msg);
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13416
diff changeset
    27
    encode(&tmp)
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    28
}
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    29
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    30
pub fn protocol_version_string(protocol_number: u16) -> &'static str {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    31
    match protocol_number {
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    32
        17 => "0.9.7-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    33
        19 => "0.9.7",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    34
        20 => "0.9.8-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    35
        21 => "0.9.8",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    36
        22 => "0.9.9-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    37
        23 => "0.9.9",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    38
        24 => "0.9.10-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    39
        25 => "0.9.10",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    40
        26 => "0.9.11-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    41
        27 => "0.9.11",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    42
        28 => "0.9.12-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    43
        29 => "0.9.12",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    44
        30 => "0.9.13-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    45
        31 => "0.9.13",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    46
        32 => "0.9.14-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    47
        33 => "0.9.14",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    48
        34 => "0.9.15-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    49
        35 => "0.9.14.1",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    50
        37 => "0.9.15",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    51
        38 => "0.9.16-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    52
        39 => "0.9.16",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    53
        40 => "0.9.17-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    54
        41 => "0.9.17",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    55
        42 => "0.9.18-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    56
        43 => "0.9.18",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    57
        44 => "0.9.19-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    58
        45 => "0.9.19",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    59
        46 => "0.9.20-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    60
        47 => "0.9.20",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    61
        48 => "0.9.21-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    62
        49 => "0.9.21",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    63
        50 => "0.9.22-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    64
        51 => "0.9.22",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    65
        52 => "0.9.23-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    66
        53 => "0.9.23",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    67
        54 => "0.9.24-dev",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    68
        55 => "0.9.24",
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    69
        56 => "0.9.25-dev",
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    70
        57 => "0.9.25",
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    71
        58 => "1.0.0-dev",
15443
eb1c407a9e8f add protocol version
alfadur
parents: 15026
diff changeset
    72
        59 => "1.0.0",
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    73
        _ => "Unknown",
13801
5fb40c8e5542 port some legacy protocol support
alfadur
parents: 13524
diff changeset
    74
    }
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    75
}