gameServer2/src/protocol/messages.rs
changeset 13439 c4f917c6be51
parent 13433 fb104e150878
child 13440 30ee161d85d0
equal deleted inserted replaced
13438:da71e0d88a1c 13439:c4f917c6be51
     1 use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
     1 use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo};
     2 use std;
     2 use std::{ops, convert::From, iter::once};
     3 use std::ops;
       
     4 use std::convert::From;
       
     5 
     3 
     6 #[derive(PartialEq, Eq, Clone, Debug)]
     4 #[derive(PartialEq, Eq, Clone, Debug)]
     7 pub enum HWProtocolMessage {
     5 pub enum HWProtocolMessage {
     8     // core
     6     // core
     9     Ping,
     7     Ping,
   102     Connected(u32),
   100     Connected(u32),
   103     Unreachable,
   101     Unreachable,
   104 }
   102 }
   105 
   103 
   106 impl GameCfg {
   104 impl GameCfg {
   107     pub fn into_server_msg(self) -> HWServerMessage {
   105     pub fn to_protocol(&self) -> (String, Vec<String>) {
   108         use self::HWServerMessage::ConfigEntry;
       
   109         use server::coretypes::GameCfg::*;
   106         use server::coretypes::GameCfg::*;
   110         match self {
   107         match self {
   111             FeatureSize(s) => ConfigEntry("FEATURE_SIZE".to_string(), vec![s.to_string()]),
   108             FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]),
   112             MapType(t) => ConfigEntry("MAP".to_string(), vec![t.to_string()]),
   109             MapType(t) => ("MAP".to_string(), vec![t.to_string()]),
   113             MapGenerator(g) => ConfigEntry("MAPGEN".to_string(), vec![g.to_string()]),
   110             MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]),
   114             MazeSize(s) => ConfigEntry("MAZE_SIZE".to_string(), vec![s.to_string()]),
   111             MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]),
   115             Seed(s) => ConfigEntry("SEED".to_string(), vec![s.to_string()]),
   112             Seed(s) => ("SEED".to_string(), vec![s.to_string()]),
   116             Template(t) => ConfigEntry("TEMPLATE".to_string(), vec![t.to_string()]),
   113             Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]),
   117 
   114 
   118             Ammo(n, None) => ConfigEntry("AMMO".to_string(), vec![n.to_string()]),
   115             Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]),
   119             Ammo(n, Some(s)) => ConfigEntry("AMMO".to_string(), vec![n.to_string(), s.to_string()]),
   116             Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]),
   120             Scheme(n, None) => ConfigEntry("SCHEME".to_string(), vec![n.to_string()]),
   117             Scheme(n, None) => ("SCHEME".to_string(), vec![n.to_string()]),
   121             Scheme(n, Some(s)) => ConfigEntry("SCHEME".to_string(), {
   118             Scheme(n, Some(s)) => ("SCHEME".to_string(), {
   122                 let mut v = vec![n.to_string()];
   119                 let mut v = vec![n.to_string()];
   123                 v.extend(s.into_iter());
   120                 v.extend(s.clone().into_iter());
   124                 v
   121                 v
   125             }),
   122             }),
   126             Script(s) => ConfigEntry("SCRIPT".to_string(), vec![s.to_string()]),
   123             Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]),
   127             Theme(t) => ConfigEntry("THEME".to_string(), vec![t.to_string()]),
   124             Theme(t) => ("THEME".to_string(), vec![t.to_string()]),
   128             DrawnMap(m) => ConfigEntry("DRAWNMAP".to_string(), vec![m.to_string()])
   125             DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()])
   129         }
   126         }
       
   127     }
       
   128 
       
   129     pub fn to_server_msg(&self) -> HWServerMessage {
       
   130         use self::HWServerMessage::ConfigEntry;
       
   131         let (name, args) = self.to_protocol();
       
   132         HWServerMessage::ConfigEntry(name, args)
   130     }
   133     }
   131 }
   134 }
   132 
   135 
   133 macro_rules! const_braces {
   136 macro_rules! const_braces {
   134     ($e: expr) => { "{}\n" }
   137     ($e: expr) => { "{}\n" }
   136 
   139 
   137 macro_rules! msg {
   140 macro_rules! msg {
   138     [$($part: expr),*] => {
   141     [$($part: expr),*] => {
   139         format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
   142         format!(concat!($(const_braces!($part)),*, "\n"), $($part),*);
   140     };
   143     };
       
   144 }
       
   145 
       
   146 macro_rules! several {
       
   147     [$part: expr] => { once($part) };
       
   148     [$part: expr, $($other: expr),*] => { once($part).chain(several![$($other),*]) };
   141 }
   149 }
   142 
   150 
   143 impl HWProtocolMessage {
   151 impl HWProtocolMessage {
   144     /** Converts the message to a raw `String`, which can be sent over the network.
   152     /** Converts the message to a raw `String`, which can be sent over the network.
   145      *
   153      *
   186             GetServerVar => msg!["GET_SERVER_VAR"],
   194             GetServerVar => msg!["GET_SERVER_VAR"],
   187             RestartServer => msg!["CMD", "RESTART_SERVER YES"],
   195             RestartServer => msg!["CMD", "RESTART_SERVER YES"],
   188             Stats => msg!["CMD", "STATS"],
   196             Stats => msg!["CMD", "STATS"],
   189             Part(None) => msg!["PART"],
   197             Part(None) => msg!["PART"],
   190             Part(Some(msg)) => msg!["PART", msg],
   198             Part(Some(msg)) => msg!["PART", msg],
   191             //Cfg(GameCfg) =>
   199             Cfg(config) => {
   192             //AddTeam(info) =>
   200                 let (name, args) = config.to_protocol();
       
   201                 msg!["CFG", name, args.join("\n")]
       
   202             },
       
   203             AddTeam(info) =>
       
   204                 msg![info.name, info.color, info.grave, info.fort,
       
   205                      info.voice_pack, info.flag, info.difficulty,
       
   206                      info.hedgehogs.iter().flat_map(|h|
       
   207                         several![&h.name[..], "\n", &h.hat[..]]).collect::<String>()],
   193             RemoveTeam(name) => msg!["REMOVE_TEAM", name],
   208             RemoveTeam(name) => msg!["REMOVE_TEAM", name],
   194             //SetHedgehogsNumber(team, number), ??
   209             SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number],
   195             //SetTeamColor(team, color), ??
   210             SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color],
   196             ToggleReady => msg!["TOGGLE_READY"],
   211             ToggleReady => msg!["TOGGLE_READY"],
   197             StartGame => msg!["START_GAME"],
   212             StartGame => msg!["START_GAME"],
   198             EngineMessage(msg) => msg!["EM", msg],
   213             EngineMessage(msg) => msg!["EM", msg],
   199             RoundFinished => msg!["ROUNDFINISHED"],
   214             RoundFinished => msg!["ROUNDFINISHED"],
   200             ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],
   215             ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"],