12152
|
1 |
use mio;
|
|
2 |
|
13421
|
3 |
use protocol::messages::{
|
|
4 |
HWProtocolMessage,
|
|
5 |
HWServerMessage::*
|
|
6 |
};
|
13424
|
7 |
use server::{
|
|
8 |
server::HWServer,
|
|
9 |
client::ClientId,
|
|
10 |
room::HWRoom,
|
|
11 |
actions::{Action, Action::*}
|
|
12 |
};
|
13421
|
13 |
use utils::is_name_illegal;
|
|
14 |
use std::mem::swap;
|
12152
|
15 |
|
13424
|
16 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
|
13421
|
17 |
use protocol::messages::HWProtocolMessage::*;
|
12152
|
18 |
match message {
|
13424
|
19 |
Part(None) => server.react(client_id, vec![
|
13421
|
20 |
MoveToLobby("part".to_string())]),
|
13424
|
21 |
Part(Some(msg)) => server.react(client_id, vec![
|
13421
|
22 |
MoveToLobby(format!("part: {}", msg))]),
|
|
23 |
Chat(msg) => {
|
13424
|
24 |
let actions = {
|
|
25 |
let c = &mut server.clients[client_id];
|
|
26 |
let chat_msg = ChatMsg(c.nick.clone(), msg);
|
|
27 |
if let Some(room_id) = c.room_id {
|
|
28 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()]
|
|
29 |
} else {
|
|
30 |
Vec::new()
|
|
31 |
}
|
|
32 |
};
|
|
33 |
server.react(client_id, actions);
|
13421
|
34 |
},
|
|
35 |
RoomName(new_name) => {
|
|
36 |
let actions =
|
|
37 |
if is_name_illegal(&new_name) {
|
|
38 |
vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
|
|
39 |
} else if server.has_room(&new_name) {
|
|
40 |
vec![Warn("A room with the same name already exists.".to_string())]
|
|
41 |
} else {
|
|
42 |
let mut old_name = new_name.clone();
|
13427
|
43 |
if let (_, Some(r)) = server.client_and_room(client_id) {
|
13421
|
44 |
swap(&mut r.name, &mut old_name);
|
|
45 |
vec![SendRoomUpdate(Some(old_name))]
|
|
46 |
} else {
|
|
47 |
Vec::new()
|
|
48 |
}
|
|
49 |
};
|
13424
|
50 |
server.react(client_id, actions);
|
|
51 |
},
|
|
52 |
ToggleReady => {
|
|
53 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
54 |
let flags = if c.is_ready {
|
|
55 |
r.ready_players_number -= 1;
|
|
56 |
"-r"
|
|
57 |
} else {
|
|
58 |
r.ready_players_number += 1;
|
|
59 |
"+r"
|
|
60 |
};
|
|
61 |
c.is_ready = !c.is_ready;
|
|
62 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
|
|
63 |
.send_all().in_room(r.id).action()]
|
|
64 |
} else {
|
|
65 |
Vec::new()
|
|
66 |
};
|
|
67 |
server.react(client_id, actions);
|
13421
|
68 |
}
|
13427
|
69 |
AddTeam(info) => {
|
13424
|
70 |
let mut actions = Vec::new();
|
|
71 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
72 |
let room_id = r.id;
|
|
73 |
if r.teams.len() >= r.team_limit as usize {
|
|
74 |
actions.push(Warn("Too many teams!".to_string()))
|
|
75 |
} else if r.addable_hedgehogs() == 0 {
|
|
76 |
actions.push(Warn("Too many hedgehogs!".to_string()))
|
|
77 |
} else if r.find_team(|t| t.name == info.name) != None {
|
|
78 |
actions.push(Warn("There's already a team with same name in the list.".to_string()))
|
|
79 |
} else if r.game_info != None {
|
|
80 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
|
|
81 |
} else {
|
|
82 |
let team = r.add_team(c.id, info);
|
|
83 |
c.teams_in_game += 1;
|
|
84 |
c.clan = Some(team.color);
|
|
85 |
actions.push(TeamAccepted(team.name.clone())
|
|
86 |
.send_self().action());
|
|
87 |
actions.push(TeamAdd(HWRoom::team_info(&c, team))
|
|
88 |
.send_all().in_room(room_id).but_self().action());
|
|
89 |
actions.push(TeamColor(team.name.clone(), team.color)
|
|
90 |
.send_all().in_room(room_id).action());
|
|
91 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
92 |
.send_all().in_room(room_id).action());
|
|
93 |
actions.push(SendRoomUpdate(None));
|
|
94 |
}
|
|
95 |
}
|
|
96 |
server.react(client_id, actions);
|
|
97 |
},
|
|
98 |
RemoveTeam(name) => {
|
|
99 |
let mut actions = Vec::new();
|
|
100 |
if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
101 |
match r.find_team_owner(&name) {
|
|
102 |
None =>
|
|
103 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())),
|
|
104 |
Some((id, _)) if id != client_id =>
|
|
105 |
actions.push(Warn("You can't remove a team you don't own.".to_string())),
|
|
106 |
Some((_, name)) => {
|
|
107 |
c.teams_in_game -= 1;
|
|
108 |
c.clan = r.find_team_color(c.id);
|
|
109 |
actions.push(Action::RemoveTeam(name.to_string()));
|
|
110 |
}
|
|
111 |
}
|
|
112 |
};
|
|
113 |
server.react(client_id, actions);
|
|
114 |
},
|
|
115 |
SetHedgehogsNumber(team_name, number) => {
|
|
116 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
117 |
let room_id = r.id;
|
|
118 |
let addable_hedgehogs = r.addable_hedgehogs();
|
|
119 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
120 |
if !c.is_master {
|
|
121 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
122 |
} else if number < 1 || number > 8
|
|
123 |
|| number > addable_hedgehogs + team.hedgehogs_number {
|
|
124 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
|
|
125 |
.send_self().action()]
|
|
126 |
} else {
|
|
127 |
team.hedgehogs_number = number;
|
|
128 |
vec![HedgehogsNumber(team.name.clone(), number)
|
|
129 |
.send_all().in_room(room_id).but_self().action()]
|
|
130 |
}
|
|
131 |
} else {
|
|
132 |
vec![(Warn("No such team.".to_string()))]
|
|
133 |
}
|
|
134 |
} else {
|
|
135 |
Vec::new()
|
|
136 |
};
|
|
137 |
server.react(client_id, actions);
|
|
138 |
},
|
|
139 |
SetTeamColor(team_name, color) => {
|
|
140 |
let mut owner_id = None;
|
|
141 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
142 |
let room_id = r.id;
|
|
143 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
|
|
144 |
if !c.is_master {
|
|
145 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
146 |
} else if false {
|
|
147 |
Vec::new()
|
|
148 |
} else {
|
|
149 |
owner_id = Some(owner);
|
|
150 |
team.color = color;
|
|
151 |
vec![TeamColor(team.name.clone(), color)
|
|
152 |
.send_all().in_room(room_id).but_self().action()]
|
|
153 |
}
|
|
154 |
} else {
|
|
155 |
vec![(Warn("No such team.".to_string()))]
|
|
156 |
}
|
|
157 |
} else {
|
|
158 |
Vec::new()
|
|
159 |
};
|
|
160 |
|
|
161 |
if let Some(id) = owner_id {
|
|
162 |
server.clients[id].clan = Some(color);
|
|
163 |
}
|
|
164 |
|
|
165 |
server.react(client_id, actions);
|
13427
|
166 |
},
|
|
167 |
Cfg(cfg) => {
|
|
168 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
|
|
169 |
if !c.is_master {
|
|
170 |
vec![ProtocolError("You're not the room master!".to_string())]
|
|
171 |
} else {
|
|
172 |
r.set_config(cfg.clone());
|
|
173 |
vec![cfg.into_server_msg()
|
|
174 |
.send_all().in_room(r.id).but_self().action()]
|
|
175 |
}
|
|
176 |
} else {
|
|
177 |
Vec::new()
|
|
178 |
};
|
|
179 |
server.react(client_id, actions);
|
13424
|
180 |
}
|
|
181 |
_ => warn!("Unimplemented!")
|
12152
|
182 |
}
|
|
183 |
}
|