1 use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType}; |
1 use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType}; |
2 use std::{convert::From, iter::once, ops}; |
2 use std::{convert::From, iter::once, ops}; |
3 |
3 |
4 #[derive(PartialEq, Eq, Clone, Debug)] |
4 #[derive(PartialEq, Eq, Clone, Debug)] |
5 pub enum HWProtocolMessage { |
5 pub enum HwProtocolMessage { |
6 // common messages |
6 // common messages |
7 Ping, |
7 Ping, |
8 Pong, |
8 Pong, |
9 Quit(Option<String>), |
9 Quit(Option<String>), |
10 Global(String), |
10 Global(String), |
154 |
154 |
155 //Deprecated messages |
155 //Deprecated messages |
156 LegacyReady(bool, Vec<String>), |
156 LegacyReady(bool, Vec<String>), |
157 } |
157 } |
158 |
158 |
159 fn special_chat(nick: &str, msg: String) -> HWServerMessage { |
159 fn special_chat(nick: &str, msg: String) -> HwServerMessage { |
160 HWServerMessage::ChatMsg { |
160 HwServerMessage::ChatMsg { |
161 nick: nick.to_string(), |
161 nick: nick.to_string(), |
162 msg, |
162 msg, |
163 } |
163 } |
164 } |
164 } |
165 |
165 |
166 pub fn server_chat(msg: String) -> HWServerMessage { |
166 pub fn server_chat(msg: String) -> HwServerMessage { |
167 special_chat("[server]", msg) |
167 special_chat("[server]", msg) |
168 } |
168 } |
169 |
169 |
170 pub fn global_chat(msg: String) -> HWServerMessage { |
170 pub fn global_chat(msg: String) -> HwServerMessage { |
171 special_chat("(global notice)", msg) |
171 special_chat("(global notice)", msg) |
172 } |
172 } |
173 |
173 |
174 impl ServerVar { |
174 impl ServerVar { |
175 pub fn to_protocol(&self) -> Vec<String> { |
175 pub fn to_protocol(&self) -> Vec<String> { |
204 Theme(t) => ("THEME".to_string(), vec![t.to_string()]), |
204 Theme(t) => ("THEME".to_string(), vec![t.to_string()]), |
205 DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]), |
205 DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]), |
206 } |
206 } |
207 } |
207 } |
208 |
208 |
209 pub fn to_server_msg(&self) -> HWServerMessage { |
209 pub fn to_server_msg(&self) -> HwServerMessage { |
210 use self::HWServerMessage::ConfigEntry; |
210 use self::HwServerMessage::ConfigEntry; |
211 let (name, args) = self.to_protocol(); |
211 let (name, args) = self.to_protocol(); |
212 HWServerMessage::ConfigEntry(name, args) |
212 HwServerMessage::ConfigEntry(name, args) |
213 } |
213 } |
214 } |
214 } |
215 |
215 |
216 impl TeamInfo { |
216 impl TeamInfo { |
217 pub fn to_protocol(&self) -> Vec<String> { |
217 pub fn to_protocol(&self) -> Vec<String> { |
249 macro_rules! several { |
249 macro_rules! several { |
250 [$part: expr] => { once($part) }; |
250 [$part: expr] => { once($part) }; |
251 [$part: expr, $($other: expr),*] => { once($part).chain(several![$($other),*]) }; |
251 [$part: expr, $($other: expr),*] => { once($part).chain(several![$($other),*]) }; |
252 } |
252 } |
253 |
253 |
254 impl HWProtocolMessage { |
254 impl HwProtocolMessage { |
255 /** Converts the message to a raw `String`, which can be sent over the network. |
255 /** Converts the message to a raw `String`, which can be sent over the network. |
256 * |
256 * |
257 * This is the inverse of the `message` parser. |
257 * This is the inverse of the `message` parser. |
258 */ |
258 */ |
259 #[cfg(test)] |
259 #[cfg(test)] |
260 pub(crate) fn to_raw_protocol(&self) -> String { |
260 pub(crate) fn to_raw_protocol(&self) -> String { |
261 use self::HWProtocolMessage::*; |
261 use self::HwProtocolMessage::*; |
262 match self { |
262 match self { |
263 Ping => msg!["PING"], |
263 Ping => msg!["PING"], |
264 Pong => msg!["PONG"], |
264 Pong => msg!["PONG"], |
265 Quit(None) => msg!["QUIT"], |
265 Quit(None) => msg!["QUIT"], |
266 Quit(Some(msg)) => msg!["QUIT", msg], |
266 Quit(Some(msg)) => msg!["QUIT", msg], |
353 v.extend(msg.iter().map(|s| &s[..])); |
353 v.extend(msg.iter().map(|s| &s[..])); |
354 v.push("\n"); |
354 v.push("\n"); |
355 v.join("\n") |
355 v.join("\n") |
356 } |
356 } |
357 |
357 |
358 impl HWServerMessage { |
358 impl HwServerMessage { |
359 pub fn to_raw_protocol(&self) -> String { |
359 pub fn to_raw_protocol(&self) -> String { |
360 use self::HWServerMessage::*; |
360 use self::HwServerMessage::*; |
361 match self { |
361 match self { |
362 Ping => msg!["PING"], |
362 Ping => msg!["PING"], |
363 Pong => msg!["PONG"], |
363 Pong => msg!["PONG"], |
364 Connected(protocol_version) => msg![ |
364 Connected(protocol_version) => msg![ |
365 "CONNECTED", |
365 "CONNECTED", |