gameServer2/src/protocol/messages.rs
changeset 13119 1e39b8749072
parent 12147 03ccb89820f3
child 13416 cdf69667593b
equal deleted inserted replaced
13118:1ddb8aac5e30 13119:1e39b8749072
     1 use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
     1 use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
     2 use std;
     2 use std;
     3 use std::ops;
     3 use std::ops;
     4 use std::convert::From;
     4 use std::convert::From;
     5 
     5 
     6 #[derive(PartialEq, Debug)]
     6 #[derive(PartialEq, Eq, Clone, Debug)]
     7 pub enum HWProtocolMessage {
     7 pub enum HWProtocolMessage {
     8     // core
     8     // core
     9     Ping,
     9     Ping,
    10     Pong,
    10     Pong,
    11     Quit(Option<String>),
    11     Quit(Option<String>),
    67     LoadRoom(String),
    67     LoadRoom(String),
    68     Malformed,
    68     Malformed,
    69     Empty,
    69     Empty,
    70 }
    70 }
    71 
    71 
    72 pub enum HWServerMessage<'a> {
    72 pub enum HWServerMessage {
    73     Ping,
    73     Ping,
    74     Pong,
    74     Pong,
    75     Bye(&'a str),
    75     Bye(String),
    76     Nick(&'a str),
    76     Nick(String),
    77     LobbyLeft(&'a str),
    77     LobbyLeft(String),
    78     LobbyJoined(&'a [&'a str]),
    78     LobbyJoined(Vec<String>),
    79     ChatMsg(&'a str, &'a str),
    79     ChatMsg(String, String),
    80     ClientFlags(&'a str, &'a [&'a str]),
    80     ClientFlags(String, Vec<String>),
    81 
    81 
    82     Warning(&'a str),
    82     Warning(String),
    83     Connected(u32),
    83     Connected(u32),
    84     Unreachable,
    84     Unreachable,
    85 }
    85 }
    86 
    86 
    87 fn construct_message(msg: & [&str]) -> String {
    87 fn construct_message(msg: & [&str]) -> String {
    94     m.push('\n');
    94     m.push('\n');
    95 
    95 
    96     m
    96     m
    97 }
    97 }
    98 
    98 
    99 impl<'a> HWServerMessage<'a> {
    99 impl<'a> HWProtocolMessage {
   100     pub fn to_raw_protocol(&self) -> String {
   100     pub fn to_raw_protocol(&self) -> String {
       
   101         use self::HWProtocolMessage::*;
       
   102         match *self {
       
   103             Ping => "PING\n\n".to_string(),
       
   104             Pong => "PONG\n\n".to_string(),
       
   105             Quit(None) => format!("QUIT\n\n"),
       
   106             Quit(Some(ref msg)) => format!("QUIT\n{}\n\n", msg),
       
   107             Global(ref msg) => format!("CMD\nGLOBAL\n{}\n\n", msg),
       
   108             Watch(ref name) => format!("CMD\nWATCH\n{}\n\n", name),
       
   109             ToggleServerRegisteredOnly => "CMD\nREGISTERED_ONLY\n\n".to_string(),
       
   110             SuperPower => "CMD\nSUPER_POWER\n\n".to_string(),
       
   111             Info(ref info) => format!("CMD\nINFO\n{}\n\n", info),
       
   112             Nick(ref nick) => format!("NICK\n{}\n\n", nick),
       
   113             Proto(version) => format!("PROTO\n{}\n\n", version),
       
   114             Password(ref p, ref s) => format!("PASSWORD\n{}\n{}\n\n", p, s), //?
       
   115             Checker(i, ref n, ref p) =>
       
   116                 format!("CHECKER\n{}\n{}\n{}\n\n", i, n, p), //?,
       
   117             List => "LIST\n\n".to_string(),
       
   118             Chat(ref msg) => format!("CHAT\n{}\n\n", msg),
       
   119             CreateRoom(ref name, None) =>
       
   120                 format!("CREATE_ROOM\n{}\n\n", name),
       
   121             CreateRoom(ref name, Some(ref password)) =>
       
   122                 format!("CREATE_ROOM\n{}\n{}\n\n", name, password),
       
   123             Join(ref name, None) =>
       
   124                 format!("JOIN\n{}\n\n", name),
       
   125             Join(ref name, Some(ref arg)) =>
       
   126                 format!("JOIN\n{}\n{}\n\n", name, arg),
       
   127             Follow(ref name) =>
       
   128                 format!("FOLLOW\n{}\n\n", name),
       
   129             //Rnd(Vec<String>), ???
       
   130             Kick(ref name) => format!("KICK\n{}\n\n", name),
       
   131             Ban(ref name, ref reason, time) =>
       
   132                 format!("BAN\n{}\n{}\n{}\n\n", name, reason, time),
       
   133             BanIP(ref ip, ref reason, time) =>
       
   134                 format!("BAN_IP\n{}\n{}\n{}\n\n", ip, reason, time),
       
   135             BanNick(ref nick, ref reason, time) =>
       
   136                 format!("BAN_NICK\n{}\n{}\n{}\n\n", nick, reason, time),
       
   137             BanList => "BANLIST\n\n".to_string(),
       
   138             Unban(ref name) => format!("UNBAN\n{}\n\n", name),
       
   139             //SetServerVar(ServerVar), ???
       
   140             GetServerVar => "GET_SERVER_VAR\n\n".to_string(),
       
   141             RestartServer => "CMD\nRESTART_SERVER\nYES\n\n".to_string(),
       
   142             Stats => "CMD\nSTATS\n\n".to_string(),
       
   143             Part(None) => "CMD\nPART\n\n".to_string(),
       
   144             Part(Some(ref msg)) => format!("CMD\nPART\n{}\n\n", msg),
       
   145             //Cfg(GameCfg) ??
       
   146             //AddTeam(TeamInfo) ??,
       
   147             RemoveTeam(ref name) => format!("REMOVE_TEAM\n{}\n\n", name),
       
   148             //SetHedgehogsNumber(String, u8), ??
       
   149             //SetTeamColor(String, u8), ??
       
   150             ToggleReady => "TOGGLE_READY\n\n".to_string(),
       
   151             StartGame => "START_GAME\n\n".to_string(),
       
   152             EngineMessage(ref msg) => format!("EM\n{}\n\n", msg),
       
   153             RoundFinished => "ROUNDFINISHED\n\n".to_string(),
       
   154             ToggleRestrictJoin => "TOGGLE_RESTRICT_JOINS\n\n".to_string(),
       
   155             ToggleRestrictTeams => "TOGGLE_RESTRICT_TEAMS\n\n".to_string(),
       
   156             ToggleRegisteredOnly => "TOGGLE_REGISTERED_ONLY\n\n".to_string(),
       
   157             RoomName(ref name) => format!("ROOM_NAME\n{}\n\n", name),
       
   158             Delegate(ref name) => format!("CMD\nDELEGATE\n{}\n\n", name),
       
   159             TeamChat(ref msg) => format!("TEAMCHAT\n{}\n\n", msg),
       
   160             MaxTeams(count) => format!("CMD\nMAXTEAMS\n{}\n\n", count) ,
       
   161             Fix => "CMD\nFIX\n\n".to_string(),
       
   162             Unfix => "CMD\nUNFIX\n\n".to_string(),
       
   163             Greeting(ref msg) => format!("CMD\nGREETING\n{}\n\n", msg),
       
   164             //CallVote(Option<(String, Option<String>)>) =>, ??
       
   165             Vote(ref msg) => format!("CMD\nVOTE\n{}\n\n", msg),
       
   166             ForceVote(ref msg) => format!("CMD\nFORCE\n{}\n\n", msg),
       
   167             //Save(String, String), ??
       
   168             Delete(ref room) => format!("CMD\nDELETE\n{}\n\n", room),
       
   169             SaveRoom(ref room) => format!("CMD\nSAVEROOM\n{}\n\n", room),
       
   170             LoadRoom(ref room) => format!("CMD\nLOADROOM\n{}\n\n", room),
       
   171             Malformed => "A\nQUICK\nBROWN\nHOG\nJUMPS\nOVER\nTHE\nLAZY\nDOG\n\n".to_string(),
       
   172             Empty => "\n\n".to_string(),
       
   173             _ => panic!("Protocol message not yet implemented")
       
   174         }
       
   175     }
       
   176 }
       
   177 
       
   178 impl HWServerMessage {
       
   179     pub fn to_raw_protocol(&self) -> String {
       
   180         use self::HWServerMessage::*;
   101         match self {
   181         match self {
   102             &HWServerMessage::Ping
   182             &Ping => "PING\n\n".to_string(),
   103                 => "PING\n\n".to_string(),
   183             &Pong => "PONG\n\n".to_string(),
   104             &HWServerMessage::Pong
   184             &Connected(protocol_version)
   105                 => "PONG\n\n".to_string(),
       
   106             &HWServerMessage::Connected(protocol_version)
       
   107                 => construct_message(&[
   185                 => construct_message(&[
   108                     "CONNECTED",
   186                     "CONNECTED",
   109                     "Hedgewars server http://www.hedgewars.org/",
   187                     "Hedgewars server http://www.hedgewars.org/",
   110                     &protocol_version.to_string()
   188                     &protocol_version.to_string()
   111                 ]),
   189                 ]),
   112             &HWServerMessage::Bye(msg)
   190             &Bye(ref msg) => construct_message(&["BYE", &msg]),
   113                 => construct_message(&["BYE", &msg]),
   191             &Nick(ref nick) => construct_message(&["NICK", &nick]),
   114             &HWServerMessage::Nick(nick)
   192             &LobbyLeft(ref nick)
   115                 => construct_message(&["NICK", &nick]),
       
   116             &HWServerMessage::LobbyLeft(nick)
       
   117                 => construct_message(&["LOBBY_LEFT", &nick]),
   193                 => construct_message(&["LOBBY_LEFT", &nick]),
   118             &HWServerMessage::LobbyJoined(nicks)
   194             &LobbyJoined(ref nicks)
   119                 => {
   195                 => {
   120                 let mut v = vec!["LOBBY:JOINED"];
   196                 let mut v = vec!["LOBBY:JOINED"];
   121                 v.extend_from_slice(nicks);
   197                 v.extend(nicks.iter().map(|n| { &n[..] }));
   122                 construct_message(&v)
   198                 construct_message(&v)
   123             },
   199             },
   124             &HWServerMessage::ClientFlags(flags, nicks)
   200             &ClientFlags(ref flags, ref nicks)
   125             => {
   201                 => {
   126                 let mut v = vec!["CLIENT_FLAGS"];
   202                 let mut v = vec!["CLIENT_FLAGS"];
   127                 v.push(flags);
   203                 v.push(&flags[..]);
   128                 v.extend_from_slice(nicks);
   204                 v.extend(nicks.iter().map(|n| { &n[..] }));
   129                 construct_message(&v)
   205                 construct_message(&v)
   130             },
   206             },
   131             &HWServerMessage::ChatMsg(nick, msg)
   207             &ChatMsg(ref nick, ref msg)
   132                 => construct_message(&["CHAT", &nick, &msg]),
   208                 => construct_message(&["CHAT", &nick, &msg]),
   133             &HWServerMessage::Warning(msg)
   209             &Warning(ref msg)
   134                 => construct_message(&["WARNING", &msg]),
   210                 => construct_message(&["WARNING", &msg]),
   135             _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
   211             _ => construct_message(&["ERROR", "UNIMPLEMENTED"]),
   136         }
   212         }
   137     }
   213     }
   138 }
   214 }