gameServer2/src/protocol/parser.rs
changeset 13424 81e0ed105f5d
parent 13421 cdf69667593b
child 13427 5fb27f94fc3b
equal deleted inserted replaced
13423:bb24c3414b0d 13424:81e0ed105f5d
     6 };
     6 };
     7 use super::{
     7 use super::{
     8     messages::{HWProtocolMessage, HWProtocolMessage::*},
     8     messages::{HWProtocolMessage, HWProtocolMessage::*},
     9     test::gen_proto_msg
     9     test::gen_proto_msg
    10 };
    10 };
       
    11 use server::coretypes::{
       
    12     HedgehogInfo, TeamInfo
       
    13 };
    11 
    14 
    12 named!(end_of_message, tag!("\n\n"));
    15 named!(end_of_message, tag!("\n\n"));
    13 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    16 named!(str_line<&[u8],   &str>, map_res!(not_line_ending, str::from_utf8));
    14 named!(  a_line<&[u8], String>, map!(str_line, String::from));
    17 named!(  a_line<&[u8], String>, map!(str_line, String::from));
    15 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
    18 named!( u8_line<&[u8],     u8>, map_res!(str_line, FromStr::from_str));
    16 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
    19 named!(u32_line<&[u8],    u32>, map_res!(str_line, FromStr::from_str));
    17 named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
    20 named!(opt_param<&[u8], Option<String> >, opt!(map!(flat_map!(preceded!(eol, str_line), non_empty), String::from)));
       
    21 named!(hog_line<&[u8], HedgehogInfo>,
       
    22     do_parse!(name: str_line >> eol >> hat: str_line >>
       
    23         (HedgehogInfo{name: name.to_string(), hat: hat.to_string()})));
       
    24 named!(_8_hogs<&[u8], [HedgehogInfo; 8]>,
       
    25     do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >>
       
    26               h3: hog_line >> eol >> h4: hog_line >> eol >>
       
    27               h5: hog_line >> eol >> h6: hog_line >> eol >>
       
    28               h7: hog_line >> eol >> h8: hog_line >>
       
    29               ([h1, h2, h3, h4, h5, h6, h7, h8])));
    18 
    30 
    19 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    31 named!(basic_message<&[u8], HWProtocolMessage>, alt!(
    20       do_parse!(tag!("PING") >> (Ping))
    32       do_parse!(tag!("PING") >> (Ping))
    21     | do_parse!(tag!("PONG") >> (Pong))
    33     | do_parse!(tag!("PONG") >> (Pong))
    22     | do_parse!(tag!("LIST") >> (List))
    34     | do_parse!(tag!("LIST") >> (List))
    86                     (CreateRoom(n, p)))
    98                     (CreateRoom(n, p)))
    87     | do_parse!(tag!("JOIN_ROOM")   >> eol >>
    99     | do_parse!(tag!("JOIN_ROOM")   >> eol >>
    88                     n: a_line       >>
   100                     n: a_line       >>
    89                     p: opt_param    >>
   101                     p: opt_param    >>
    90                     (JoinRoom(n, p)))
   102                     (JoinRoom(n, p)))
       
   103     | do_parse!(tag!("ADD_TEAM")    >> eol >>
       
   104                     name: a_line    >> eol >>
       
   105                     color: u8_line  >> eol >>
       
   106                     grave: a_line   >> eol >>
       
   107                     fort: a_line    >> eol >>
       
   108                     voice_pack: a_line >> eol >>
       
   109                     flag: a_line    >> eol >>
       
   110                     difficulty: u8_line >> eol >>
       
   111                     hedgehogs: _8_hogs >>
       
   112                     (AddTeam(TeamInfo{
       
   113                         name, color, grave, fort,
       
   114                         voice_pack, flag, difficulty,
       
   115                         hedgehogs, hedgehogs_number: 0
       
   116                      })))
       
   117     | do_parse!(tag!("HH_NUM")    >> eol >>
       
   118                     n: a_line     >> eol >>
       
   119                     c: u8_line    >>
       
   120                     (SetHedgehogsNumber(n, c)))
       
   121     | do_parse!(tag!("TEAM_COLOR")    >> eol >>
       
   122                     n: a_line     >> eol >>
       
   123                     c: u8_line    >>
       
   124                     (SetTeamColor(n, c)))
    91     | do_parse!(tag!("BAN")    >> eol >>
   125     | do_parse!(tag!("BAN")    >> eol >>
    92                     n: a_line     >> eol >>
   126                     n: a_line     >> eol >>
    93                     r: a_line     >> eol >>
   127                     r: a_line     >> eol >>
    94                     t: u32_line   >>
   128                     t: u32_line   >>
    95                     (Ban(n, r, t)))
   129                     (Ban(n, r, t)))