gameServer2/src/protocol/messages.rs
author unc0rr
Sat, 14 Jan 2017 00:46:52 +0300
changeset 12141 e25a82ce2374
parent 12140 23ee939ba66a
child 12142 193dfdcb0620
permissions -rw-r--r--
- Render messages to string - Respond to PING messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     1
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
12136
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
     2
use std;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
     3
use std::ops;
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
     4
use std::convert::From;
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     5
12136
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
     6
#[derive(PartialEq, Debug)]
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
     7
pub enum HWProtocolMessage<'a> {
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     8
    // core
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
     9
    Ping,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    10
    Pong,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    11
    Quit(Option<&'a str>),
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    12
    Bye(&'a str),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    13
    LobbyLeft(&'a str),
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    14
    //Cmd(&'a str, Vec<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    15
    Global(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    16
    Watch(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    17
    ToggleServerRegisteredOnly,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    18
    SuperPower,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    19
    Info(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    20
    // not entered state
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    21
    Nick(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    22
    Proto(u32),
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    23
    Password(&'a str, &'a str),
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12138
diff changeset
    24
    Checker(u32, &'a str, &'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    25
    // lobby
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    26
    List,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    27
    Chat(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    28
    CreateRoom(&'a str, Option<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    29
    Join(&'a str, Option<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    30
    Follow(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    31
    Rnd(Vec<&'a str>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    32
    Kick(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    33
    Ban(&'a str, &'a str, u32),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    34
    BanIP(&'a str, &'a str, u32),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    35
    BanNick(&'a str, &'a str, u32),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    36
    BanList,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    37
    Unban(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    38
    SetServerVar(ServerVar),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    39
    GetServerVar,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    40
    RestartServer,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    41
    Stats,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    42
    // in room
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    43
    Part(Option<&'a str>),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    44
    Cfg(GameCfg),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    45
    AddTeam(TeamInfo),
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    46
    RemoveTeam(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    47
    SetHedgehogsNumber(&'a str, u8),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    48
    SetTeamColor(&'a str, u8),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    49
    ToggleReady,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    50
    StartGame,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    51
    EngineMessage(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    52
    RoundFinished,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    53
    ToggleRestrictJoin,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    54
    ToggleRestrictTeams,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    55
    ToggleRegisteredOnly,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    56
    RoomName(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    57
    Delegate(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    58
    TeamChat(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    59
    MaxTeams(u8),
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    60
    Fix,
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    61
    Unfix,
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    62
    Greeting(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    63
    CallVote(Option<(&'a str, Option<&'a str>)>),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    64
    Vote(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    65
    ForceVote(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    66
    Save(&'a str, &'a str),
12140
23ee939ba66a Add more messages to the parser
unc0rr
parents: 12138
diff changeset
    67
    Delete(&'a str),
12138
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    68
    SaveRoom(&'a str),
81df2e1f9ae9 Some parsing using nom
unc0rr
parents: 12136
diff changeset
    69
    LoadRoom(&'a str),
12135
6273f89ab13d Start on messages parser
unc0rr
parents:
diff changeset
    70
}
12136
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    71
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    72
pub fn number<T: From<u8>
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    73
                + std::default::Default
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    74
                + std::ops::MulAssign
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    75
                + std::ops::AddAssign>
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    76
    (digits: Vec<u8>) -> T {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    77
    let mut value: T = T::default();
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    78
    for digit in digits {
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    79
        value *= T::from(10);
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    80
        value += T::from(digit);
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    81
    }
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    82
    value
a4d22f197bd2 Still trying to make parser work
unc0rr
parents: 12135
diff changeset
    83
}
12141
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    84
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    85
fn construct_message(msg: & [&str]) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    86
    let mut m = String::with_capacity(64);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    87
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    88
    for s in msg {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    89
        m.push_str(s);
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    90
        m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    91
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    92
    m.push('\n');
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    93
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    94
    m
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    95
}
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    96
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    97
impl<'a> HWProtocolMessage<'a> {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    98
    pub fn to_raw_protocol(&self) -> String {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
    99
        match self {
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   100
            &HWProtocolMessage::Ping
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   101
                => "PING\n\n".to_string(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   102
            &HWProtocolMessage::Pong
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   103
                => "PONG\n\n".to_string(),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   104
            &HWProtocolMessage::Bye(msg)
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   105
                => construct_message(&["BYE", msg]),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   106
            &HWProtocolMessage::LobbyLeft(msg)
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   107
                => construct_message(&["LOBBY_LEFT", msg]),
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   108
            _ => String::new()
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   109
        }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   110
    }
e25a82ce2374 - Render messages to string
unc0rr
parents: 12140
diff changeset
   111
}