gameServer2/src/protocol/messages.rs
changeset 13437 ee3fa3b8809d
parent 13433 c8425fbcf1d9
child 13438 fb104e150878
equal deleted inserted replaced
13436:6a818f9192f4 13437:ee3fa3b8809d
   128             DrawnMap(m) => ConfigEntry("DRAWNMAP".to_string(), vec![m.to_string()])
   128             DrawnMap(m) => ConfigEntry("DRAWNMAP".to_string(), vec![m.to_string()])
   129         }
   129         }
   130     }
   130     }
   131 }
   131 }
   132 
   132 
       
   133 macro_rules! const_braces {
       
   134     ($e: expr) => { "{}\n" }
       
   135 }
       
   136 
       
   137 macro_rules! msg {
       
   138     [$($part: expr),*] => {
       
   139         format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
       
   140     };
       
   141 }
       
   142 
   133 impl<'a> HWProtocolMessage {
   143 impl<'a> HWProtocolMessage {
   134     pub fn to_raw_protocol(&self) -> String {
   144     pub fn to_raw_protocol(&self) -> String {
   135         use self::HWProtocolMessage::*;
   145         use self::HWProtocolMessage::*;
   136         match self {
   146         match self {
   137             Ping => "PING\n\n".to_string(),
   147             Ping => msg!["PING"],
   138             Pong => "PONG\n\n".to_string(),
   148             Pong => msg!["PONG"],
   139             Quit(None) => format!("QUIT\n\n"),
   149             Quit(None) => msg!["QUIT"],
   140             Quit(Some(msg)) => format!("QUIT\n{}\n\n", msg),
   150             Quit(Some(msg)) => msg!["QUIT", msg],
   141             Global(msg) => format!("CMD\nGLOBAL\n{}\n\n", msg),
   151             Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)],
   142             Watch(name) => format!("CMD\nWATCH\n{}\n\n", name),
   152             Watch(name) => msg!["CMD", format!("WATCH {}", name)],
   143             ToggleServerRegisteredOnly => "CMD\nREGISTERED_ONLY\n\n".to_string(),
   153             ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"],
   144             SuperPower => "CMD\nSUPER_POWER\n\n".to_string(),
   154             SuperPower =>  msg!["CMD", "SUPER_POWER"],
   145             Info(info) => format!("CMD\nINFO\n{}\n\n", info),
   155             Info(info) => msg!["CMD", format!("INFO {}", info)],
   146             Nick(nick) => format!("NICK\n{}\n\n", nick),
   156             Nick(nick) => msg!("NICK", nick),
   147             Proto(version) => format!("PROTO\n{}\n\n", version),
   157             Proto(version) => msg!["PROTO", version],
   148             Password(p, s) => format!("PASSWORD\n{}\n{}\n\n", p, s), //?
   158             Password(p, s) => msg!["PASSWORD", p, s],
   149             Checker(i, n, p) =>
   159             Checker(i, n, p) => msg!["CHECKER", i, n, p],
   150                 format!("CHECKER\n{}\n{}\n{}\n\n", i, n, p), //?,
   160             List => msg!["LIST"],
   151             List => "LIST\n\n".to_string(),
   161             Chat(msg) => msg!["CHAT", msg],
   152             Chat(msg) => format!("CHAT\n{}\n\n", msg),
   162             CreateRoom(name, None) => msg!["CREATE_ROOM", name],
   153             CreateRoom(name, None) =>
       
   154                 format!("CREATE_ROOM\n{}\n\n", name),
       
   155             CreateRoom(name, Some(password)) =>
   163             CreateRoom(name, Some(password)) =>
   156                 format!("CREATE_ROOM\n{}\n{}\n\n", name, password),
   164                 msg!["CREATE_ROOM", name, password],
   157             JoinRoom(name, None) =>
   165             JoinRoom(name, None) => msg!["JOIN_ROOM", name],
   158                 format!("JOIN\n{}\n\n", name),
   166             JoinRoom(name, Some(password)) =>
   159             JoinRoom(name, Some(arg)) =>
   167                 msg!["JOIN_ROOM", name, password],
   160                 format!("JOIN\n{}\n{}\n\n", name, arg),
   168             Follow(name) => msg!["FOLLOW", name],
   161             Follow(name) =>
   169             Rnd(args) => msg!["RND", args.join(" ")],
   162                 format!("FOLLOW\n{}\n\n", name),
   170             Kick(name) => msg!["KICK", name],
   163             //Rnd(Vec<String>), ???
   171             Ban(name, reason, time) => msg!["BAN", name, reason, time],
   164             Kick(name) => format!("KICK\n{}\n\n", name),
   172             BanIP(ip, reason, time) => msg!["BAN_IP", ip, reason, time],
   165             Ban(name, reason, time) =>
       
   166                 format!("BAN\n{}\n{}\n{}\n\n", name, reason, time),
       
   167             BanIP(ip, reason, time) =>
       
   168                 format!("BAN_IP\n{}\n{}\n{}\n\n", ip, reason, time),
       
   169             BanNick(nick, reason, time) =>
   173             BanNick(nick, reason, time) =>
   170                 format!("BAN_NICK\n{}\n{}\n{}\n\n", nick, reason, time),
   174                 msg!("BAN_NICK", nick, reason, time),
   171             BanList => "BANLIST\n\n".to_string(),
   175             BanList => msg!["BANLIST"],
   172             Unban(name) => format!("UNBAN\n{}\n\n", name),
   176             Unban(name) => msg!["UNBAN", name],
   173             //SetServerVar(ServerVar), ???
   177             //SetServerVar(ServerVar), ???
   174             GetServerVar => "GET_SERVER_VAR\n\n".to_string(),
   178             GetServerVar => msg!["GET_SERVER_VAR"],
   175             RestartServer => "CMD\nRESTART_SERVER\nYES\n\n".to_string(),
   179             RestartServer => msg!["CMD", "RESTART_SERVER YES"],
   176             Stats => "CMD\nSTATS\n\n".to_string(),
   180             Stats => msg!["CMD", "STATS"],
   177             Part(None) => "CMD\nPART\n\n".to_string(),
   181             Part(None) => msg!["PART"],
   178             Part(Some(msg)) => format!("CMD\nPART\n{}\n\n", msg),
   182             Part(Some(msg)) => msg!["PART", msg],
   179             //Cfg(GameCfg) ??
   183             //Cfg(GameCfg) =>
   180             //AddTeam(TeamInfo) ??,
   184             //AddTeam(info) =>
   181             RemoveTeam(name) => format!("REMOVE_TEAM\n{}\n\n", name),
   185             RemoveTeam(name) => msg!["REMOVE_TEAM", name],
   182             //SetHedgehogsNumber(String, u8), ??
   186             //SetHedgehogsNumber(team, number), ??
   183             //SetTeamColor(String, u8), ??
   187             //SetTeamColor(team, color), ??
   184             ToggleReady => "TOGGLE_READY\n\n".to_string(),
   188             ToggleReady => msg!["TOGGLE_READY"],
   185             StartGame => "START_GAME\n\n".to_string(),
   189             StartGame => msg!["START_GAME"],
   186             EngineMessage(msg) => format!("EM\n{}\n\n", msg),
   190             EngineMessage(msg) => msg!["EM", msg],
   187             RoundFinished => "ROUNDFINISHED\n\n".to_string(),
   191             RoundFinished => msg!["ROUNDFINISHED"],
   188             ToggleRestrictJoin => "TOGGLE_RESTRICT_JOINS\n\n".to_string(),
   192             ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
   189             ToggleRestrictTeams => "TOGGLE_RESTRICT_TEAMS\n\n".to_string(),
   193             ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"],
   190             ToggleRegisteredOnly => "TOGGLE_REGISTERED_ONLY\n\n".to_string(),
   194             ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"],
   191             RoomName(name) => format!("ROOM_NAME\n{}\n\n", name),
   195             RoomName(name) => msg!["ROOM_NAME", name],
   192             Delegate(name) => format!("CMD\nDELEGATE\n{}\n\n", name),
   196             Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)],
   193             TeamChat(msg) => format!("TEAMCHAT\n{}\n\n", msg),
   197             TeamChat(msg) => msg!["TEAMCHAT", msg],
   194             MaxTeams(count) => format!("CMD\nMAXTEAMS\n{}\n\n", count) ,
   198             MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)] ,
   195             Fix => "CMD\nFIX\n\n".to_string(),
   199             Fix => msg!["CMD", "FIX"],
   196             Unfix => "CMD\nUNFIX\n\n".to_string(),
   200             Unfix => msg!["CMD", "UNFIX"],
   197             Greeting(msg) => format!("CMD\nGREETING\n{}\n\n", msg),
   201             Greeting(msg) => msg!["CMD", format!("GREETING {}", msg)],
   198             //CallVote(Option<(String, Option<String>)>) =>, ??
   202             //CallVote(Option<(String, Option<String>)>) =>, ??
   199             Vote(msg) => format!("CMD\nVOTE\n{}\n\n", msg),
   203             Vote(msg) => msg!["CMD", format!("VOTE {}", msg)],
   200             ForceVote(msg) => format!("CMD\nFORCE\n{}\n\n", msg),
   204             ForceVote(msg) => msg!["CMD", format!("FORCE {}", msg)],
   201             //Save(String, String), ??
   205             //Save(String, String), ??
   202             Delete(room) => format!("CMD\nDELETE\n{}\n\n", room),
   206             Delete(room) => msg!["CMD", format!("DELETE {}", room)],
   203             SaveRoom(room) => format!("CMD\nSAVEROOM\n{}\n\n", room),
   207             SaveRoom(room) => msg!["CMD", format!("SAVEROOM {}", room)],
   204             LoadRoom(room) => format!("CMD\nLOADROOM\n{}\n\n", room),
   208             LoadRoom(room) => msg!["CMD", format!("LOADROOM {}", room)],
   205             Malformed => "A\nQUICK\nBROWN\nHOG\nJUMPS\nOVER\nTHE\nLAZY\nDOG\n\n".to_string(),
   209             Malformed => msg!["A", "QUICK", "BROWN", "HOG", "JUMPS", "OVER", "THE", "LAZY", "DOG"],
   206             Empty => "\n\n".to_string(),
   210             Empty => msg![""],
   207             _ => panic!("Protocol message not yet implemented")
   211             _ => panic!("Protocol message not yet implemented")
   208         }
   212         }
   209     }
   213     }
   210 }
       
   211 
       
   212 macro_rules! const_braces {
       
   213     ($e: expr) => { "{}\n" }
       
   214 }
       
   215 
       
   216 macro_rules! msg {
       
   217     [$($part: expr),*] => {
       
   218         format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
       
   219     };
       
   220 }
   214 }
   221 
   215 
   222 fn construct_message(header: &[&str], msg: &Vec<String>) -> String {
   216 fn construct_message(header: &[&str], msg: &Vec<String>) -> String {
   223     let mut v: Vec<_> = header.iter().map(|s| *s).collect();
   217     let mut v: Vec<_> = header.iter().map(|s| *s).collect();
   224     v.extend(msg.iter().map(|s| &s[..]));
   218     v.extend(msg.iter().map(|s| &s[..]));