author | unc0rr |
Sat, 14 Jan 2017 22:30:09 +0300 | |
changeset 12137 | 193dfdcb0620 |
parent 12136 | e25a82ce2374 |
child 12141 | 78925eff02c2 |
permissions | -rw-r--r-- |
12130 | 1 |
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo}; |
12131 | 2 |
use std; |
3 |
use std::ops; |
|
4 |
use std::convert::From; |
|
12130 | 5 |
|
12131 | 6 |
#[derive(PartialEq, Debug)] |
12133 | 7 |
pub enum HWProtocolMessage<'a> { |
12130 | 8 |
// core |
9 |
Ping, |
|
10 |
Pong, |
|
12133 | 11 |
Quit(Option<&'a str>), |
12136 | 12 |
Bye(&'a str), |
13 |
LobbyLeft(&'a str), |
|
12133 | 14 |
//Cmd(&'a str, Vec<&'a str>), |
15 |
Global(&'a str), |
|
16 |
Watch(&'a str), |
|
12130 | 17 |
ToggleServerRegisteredOnly, |
18 |
SuperPower, |
|
12133 | 19 |
Info(&'a str), |
12130 | 20 |
// not entered state |
12133 | 21 |
Nick(&'a str), |
12130 | 22 |
Proto(u32), |
12133 | 23 |
Password(&'a str, &'a str), |
12135 | 24 |
Checker(u32, &'a str, &'a str), |
12130 | 25 |
// lobby |
26 |
List, |
|
12133 | 27 |
Chat(&'a str), |
28 |
CreateRoom(&'a str, Option<&'a str>), |
|
29 |
Join(&'a str, Option<&'a str>), |
|
30 |
Follow(&'a str), |
|
31 |
Rnd(Vec<&'a str>), |
|
32 |
Kick(&'a str), |
|
33 |
Ban(&'a str, &'a str, u32), |
|
34 |
BanIP(&'a str, &'a str, u32), |
|
35 |
BanNick(&'a str, &'a str, u32), |
|
12130 | 36 |
BanList, |
12133 | 37 |
Unban(&'a str), |
12130 | 38 |
SetServerVar(ServerVar), |
39 |
GetServerVar, |
|
40 |
RestartServer, |
|
41 |
Stats, |
|
42 |
// in room |
|
12133 | 43 |
Part(Option<&'a str>), |
12130 | 44 |
Cfg(GameCfg), |
45 |
AddTeam(TeamInfo), |
|
12133 | 46 |
RemoveTeam(&'a str), |
47 |
SetHedgehogsNumber(&'a str, u8), |
|
48 |
SetTeamColor(&'a str, u8), |
|
12130 | 49 |
ToggleReady, |
50 |
StartGame, |
|
12133 | 51 |
EngineMessage(&'a str), |
12130 | 52 |
RoundFinished, |
53 |
ToggleRestrictJoin, |
|
54 |
ToggleRestrictTeams, |
|
55 |
ToggleRegisteredOnly, |
|
12133 | 56 |
RoomName(&'a str), |
57 |
Delegate(&'a str), |
|
58 |
TeamChat(&'a str), |
|
12130 | 59 |
MaxTeams(u8), |
60 |
Fix, |
|
61 |
Unfix, |
|
12133 | 62 |
Greeting(&'a str), |
63 |
CallVote(Option<(&'a str, Option<&'a str>)>), |
|
64 |
Vote(&'a str), |
|
65 |
ForceVote(&'a str), |
|
66 |
Save(&'a str, &'a str), |
|
12135 | 67 |
Delete(&'a str), |
12133 | 68 |
SaveRoom(&'a str), |
69 |
LoadRoom(&'a str), |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
70 |
Connected(u32), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
71 |
Malformed, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
72 |
Empty, |
12130 | 73 |
} |
12131 | 74 |
|
75 |
pub fn number<T: From<u8> |
|
76 |
+ std::default::Default |
|
77 |
+ std::ops::MulAssign |
|
78 |
+ std::ops::AddAssign> |
|
79 |
(digits: Vec<u8>) -> T { |
|
80 |
let mut value: T = T::default(); |
|
81 |
for digit in digits { |
|
82 |
value *= T::from(10); |
|
83 |
value += T::from(digit); |
|
84 |
} |
|
85 |
value |
|
86 |
} |
|
12136 | 87 |
|
88 |
fn construct_message(msg: & [&str]) -> String { |
|
89 |
let mut m = String::with_capacity(64); |
|
90 |
||
91 |
for s in msg { |
|
92 |
m.push_str(s); |
|
93 |
m.push('\n'); |
|
94 |
} |
|
95 |
m.push('\n'); |
|
96 |
||
97 |
m |
|
98 |
} |
|
99 |
||
100 |
impl<'a> HWProtocolMessage<'a> { |
|
101 |
pub fn to_raw_protocol(&self) -> String { |
|
102 |
match self { |
|
103 |
&HWProtocolMessage::Ping |
|
104 |
=> "PING\n\n".to_string(), |
|
105 |
&HWProtocolMessage::Pong |
|
106 |
=> "PONG\n\n".to_string(), |
|
12137
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
107 |
&HWProtocolMessage::Connected(protocol_version) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
108 |
=> construct_message(&[ |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
109 |
"CONNECTED", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
110 |
"Hedgewars server http://www.hedgewars.org/", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
111 |
&protocol_version.to_string() |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12136
diff
changeset
|
112 |
]), |
12136 | 113 |
&HWProtocolMessage::Bye(msg) |
114 |
=> construct_message(&["BYE", msg]), |
|
115 |
&HWProtocolMessage::LobbyLeft(msg) |
|
116 |
=> construct_message(&["LOBBY_LEFT", msg]), |
|
117 |
_ => String::new() |
|
118 |
} |
|
119 |
} |
|
120 |
} |