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),
|
12135
|
70 |
}
|
12136
|
71 |
|
|
72 |
pub fn number<T: From<u8>
|
|
73 |
+ std::default::Default
|
|
74 |
+ std::ops::MulAssign
|
|
75 |
+ std::ops::AddAssign>
|
|
76 |
(digits: Vec<u8>) -> T {
|
|
77 |
let mut value: T = T::default();
|
|
78 |
for digit in digits {
|
|
79 |
value *= T::from(10);
|
|
80 |
value += T::from(digit);
|
|
81 |
}
|
|
82 |
value
|
|
83 |
}
|
12141
|
84 |
|
|
85 |
fn construct_message(msg: & [&str]) -> String {
|
|
86 |
let mut m = String::with_capacity(64);
|
|
87 |
|
|
88 |
for s in msg {
|
|
89 |
m.push_str(s);
|
|
90 |
m.push('\n');
|
|
91 |
}
|
|
92 |
m.push('\n');
|
|
93 |
|
|
94 |
m
|
|
95 |
}
|
|
96 |
|
|
97 |
impl<'a> HWProtocolMessage<'a> {
|
|
98 |
pub fn to_raw_protocol(&self) -> String {
|
|
99 |
match self {
|
|
100 |
&HWProtocolMessage::Ping
|
|
101 |
=> "PING\n\n".to_string(),
|
|
102 |
&HWProtocolMessage::Pong
|
|
103 |
=> "PONG\n\n".to_string(),
|
|
104 |
&HWProtocolMessage::Bye(msg)
|
|
105 |
=> construct_message(&["BYE", msg]),
|
|
106 |
&HWProtocolMessage::LobbyLeft(msg)
|
|
107 |
=> construct_message(&["LOBBY_LEFT", msg]),
|
|
108 |
_ => String::new()
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|