author | unc0rr |
Wed, 18 Jan 2017 22:54:02 +0300 | |
changeset 12146 | 78925eff02c2 |
parent 12142 | 193dfdcb0620 |
child 12147 | 4d7d41be1993 |
permissions | -rw-r--r-- |
12135 | 1 |
use server::coretypes::{ServerVar, GameCfg, TeamInfo, HedgehogInfo}; |
12136 | 2 |
use std; |
3 |
use std::ops; |
|
4 |
use std::convert::From; |
|
12135 | 5 |
|
12136 | 6 |
#[derive(PartialEq, Debug)] |
12138 | 7 |
pub enum HWProtocolMessage<'a> { |
12135 | 8 |
// core |
9 |
Ping, |
|
10 |
Pong, |
|
12138 | 11 |
Quit(Option<&'a str>), |
12141 | 12 |
Bye(&'a str), |
13 |
LobbyLeft(&'a str), |
|
12138 | 14 |
//Cmd(&'a str, Vec<&'a str>), |
15 |
Global(&'a str), |
|
16 |
Watch(&'a str), |
|
12135 | 17 |
ToggleServerRegisteredOnly, |
18 |
SuperPower, |
|
12138 | 19 |
Info(&'a str), |
12135 | 20 |
// not entered state |
12138 | 21 |
Nick(&'a str), |
12135 | 22 |
Proto(u32), |
12138 | 23 |
Password(&'a str, &'a str), |
12140 | 24 |
Checker(u32, &'a str, &'a str), |
12135 | 25 |
// lobby |
26 |
List, |
|
12138 | 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), |
|
12135 | 36 |
BanList, |
12138 | 37 |
Unban(&'a str), |
12135 | 38 |
SetServerVar(ServerVar), |
39 |
GetServerVar, |
|
40 |
RestartServer, |
|
41 |
Stats, |
|
42 |
// in room |
|
12138 | 43 |
Part(Option<&'a str>), |
12135 | 44 |
Cfg(GameCfg), |
45 |
AddTeam(TeamInfo), |
|
12138 | 46 |
RemoveTeam(&'a str), |
47 |
SetHedgehogsNumber(&'a str, u8), |
|
48 |
SetTeamColor(&'a str, u8), |
|
12135 | 49 |
ToggleReady, |
50 |
StartGame, |
|
12138 | 51 |
EngineMessage(&'a str), |
12135 | 52 |
RoundFinished, |
53 |
ToggleRestrictJoin, |
|
54 |
ToggleRestrictTeams, |
|
55 |
ToggleRegisteredOnly, |
|
12138 | 56 |
RoomName(&'a str), |
57 |
Delegate(&'a str), |
|
58 |
TeamChat(&'a str), |
|
12135 | 59 |
MaxTeams(u8), |
60 |
Fix, |
|
61 |
Unfix, |
|
12138 | 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), |
|
12140 | 67 |
Delete(&'a str), |
12138 | 68 |
SaveRoom(&'a str), |
69 |
LoadRoom(&'a str), |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
70 |
Connected(u32), |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
71 |
Malformed, |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
72 |
Empty, |
12135 | 73 |
} |
12136 | 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 |
} |
|
12141 | 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(), |
|
12142
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
107 |
&HWProtocolMessage::Connected(protocol_version) |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
108 |
=> construct_message(&[ |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
109 |
"CONNECTED", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
110 |
"Hedgewars server http://www.hedgewars.org/", |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
111 |
&protocol_version.to_string() |
193dfdcb0620
- Use logging facilities instead of plain println!
unc0rr
parents:
12141
diff
changeset
|
112 |
]), |
12141 | 113 |
&HWProtocolMessage::Bye(msg) |
114 |
=> construct_message(&["BYE", msg]), |
|
12146 | 115 |
&HWProtocolMessage::Nick(nick) |
116 |
=> construct_message(&["NICK", nick]), |
|
12141 | 117 |
&HWProtocolMessage::LobbyLeft(msg) |
118 |
=> construct_message(&["LOBBY_LEFT", msg]), |
|
12146 | 119 |
_ => construct_message(&["ERROR", "UNIMPLEMENTED"]), |
12141 | 120 |
} |
121 |
} |
|
122 |
} |