1 use mio; |
1 use mio; |
2 |
2 |
3 use server::{ |
|
4 server::HWServer, |
|
5 actions::{Action, Action::*} |
|
6 }; |
|
7 use protocol::messages::{ |
3 use protocol::messages::{ |
8 HWProtocolMessage, |
4 HWProtocolMessage, |
9 HWServerMessage::* |
5 HWServerMessage::* |
10 }; |
6 }; |
|
7 use server::{ |
|
8 server::HWServer, |
|
9 client::ClientId, |
|
10 room::HWRoom, |
|
11 actions::{Action, Action::*} |
|
12 }; |
11 use utils::is_name_illegal; |
13 use utils::is_name_illegal; |
12 use std::mem::swap; |
14 use std::mem::swap; |
13 |
15 |
14 pub fn handle(server: &mut HWServer, token: usize, message: HWProtocolMessage) { |
16 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
15 use protocol::messages::HWProtocolMessage::*; |
17 use protocol::messages::HWProtocolMessage::*; |
16 match message { |
18 match message { |
17 Part(None) => server.react(token, vec![ |
19 Part(None) => server.react(client_id, vec![ |
18 MoveToLobby("part".to_string())]), |
20 MoveToLobby("part".to_string())]), |
19 Part(Some(msg)) => server.react(token, vec![ |
21 Part(Some(msg)) => server.react(client_id, vec![ |
20 MoveToLobby(format!("part: {}", msg))]), |
22 MoveToLobby(format!("part: {}", msg))]), |
21 Chat(msg) => { |
23 Chat(msg) => { |
22 let chat_msg; |
24 let actions = { |
23 let room_id; |
25 let c = &mut server.clients[client_id]; |
24 { |
26 let chat_msg = ChatMsg(c.nick.clone(), msg); |
25 let c = &mut server.clients[token]; |
27 if let Some(room_id) = c.room_id { |
26 chat_msg = ChatMsg(c.nick.clone(), msg); |
28 vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
27 room_id = c.room_id; |
29 } else { |
28 } |
30 Vec::new() |
29 let client_ids = server.other_clients_in_room(token); |
31 } |
30 server.react(token, vec![ |
32 }; |
31 SendToSelected(client_ids, chat_msg)]); |
33 server.react(client_id, actions); |
32 }, |
34 }, |
33 RoomName(new_name) => { |
35 RoomName(new_name) => { |
34 let actions = |
36 let actions = |
35 if is_name_illegal(&new_name) { |
37 if is_name_illegal(&new_name) { |
36 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())] |
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())] |
37 } else if server.has_room(&new_name) { |
39 } else if server.has_room(&new_name) { |
38 vec![Warn("A room with the same name already exists.".to_string())] |
40 vec![Warn("A room with the same name already exists.".to_string())] |
39 } else { |
41 } else { |
40 let mut old_name = new_name.clone(); |
42 let mut old_name = new_name.clone(); |
41 if let (c, Some(r)) = server.client_and_room(token) { |
43 if let (c, Some(r)) = server.client_and_room(client_id) { |
42 swap(&mut r.name, &mut old_name); |
44 swap(&mut r.name, &mut old_name); |
43 vec![SendRoomUpdate(Some(old_name))] |
45 vec![SendRoomUpdate(Some(old_name))] |
44 } else { |
46 } else { |
45 Vec::new() |
47 Vec::new() |
46 } |
48 } |
47 }; |
49 }; |
48 server.react(token, actions); |
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); |
49 } |
68 } |
50 _ => warn!("Unimplemented!"), |
69 AddTeam(mut info) => { |
|
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); |
|
166 } |
|
167 _ => warn!("Unimplemented!") |
51 } |
168 } |
52 } |
169 } |