1 use std::{ |
1 use std::{ |
2 io, io::Write |
2 io, io::Write, |
|
3 iter::once |
3 }; |
4 }; |
4 use super::{ |
5 use super::{ |
5 server::HWServer, |
6 server::HWServer, |
6 room::RoomId, |
7 room::{RoomId, GameInfo}, |
7 client::{ClientId, HWClient}, |
8 client::{ClientId, HWClient}, |
8 room::HWRoom, |
9 room::HWRoom, |
9 handlers |
10 handlers |
10 }; |
11 }; |
11 use protocol::messages::{ |
12 use protocol::messages::{ |
12 HWProtocolMessage, |
13 HWProtocolMessage, |
13 HWServerMessage, |
14 HWServerMessage, |
14 HWServerMessage::* |
15 HWServerMessage::* |
15 }; |
16 }; |
|
17 use utils::to_engine_msg; |
16 |
18 |
17 pub enum Destination { |
19 pub enum Destination { |
18 ToSelf, |
20 ToSelf, |
19 ToAll { |
21 ToAll { |
20 room_id: Option<RoomId>, |
22 room_id: Option<RoomId>, |
21 protocol: Option<u32>, |
23 protocol: Option<u32>, |
22 skip_self: bool |
24 skip_self: bool |
23 } |
25 } |
24 } |
26 } |
307 .send_all().with_protocol(r.protocol_number).action()] |
312 .send_all().with_protocol(r.protocol_number).action()] |
308 } else { |
313 } else { |
309 Vec::new() |
314 Vec::new() |
310 }; |
315 }; |
311 server.react(client_id, actions); |
316 server.react(client_id, actions); |
312 } |
317 }, |
313 |
318 StartRoomGame(room_id) => { |
|
319 let actions = { |
|
320 let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
321 .map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
322 let room = &mut server.rooms[room_id]; |
|
323 |
|
324 if !room.has_multiple_clans() { |
|
325 vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
326 } else if room.game_info.is_some() { |
|
327 vec![Warn("The game is already in progress".to_string())] |
|
328 } else { |
|
329 room.game_info = Some(GameInfo { |
|
330 teams_in_game: room.teams.len() as u8 |
|
331 }); |
|
332 for id in room_clients { |
|
333 let c = &mut server.clients[id]; |
|
334 c.is_in_game = true; |
|
335 c.team_indices = room.client_team_indices(c.id); |
|
336 } |
|
337 vec![RunGame.send_all().in_room(room.id).action(), |
|
338 SendRoomUpdate(None), |
|
339 ClientFlags("+g".to_string(), room_nicks) |
|
340 .send_all().in_room(room.id).action()] |
|
341 } |
|
342 }; |
|
343 server.react(client_id, actions); |
|
344 } |
|
345 SendTeamRemovalMessage(team_name) => { |
|
346 let mut actions = Vec::new(); |
|
347 if let (c, Some(r)) = server.client_and_room(client_id) { |
|
348 if let Some(ref mut info) = r.game_info { |
|
349 let msg = once(b'F').chain(team_name.bytes()); |
|
350 actions.push(ForwardEngineMessage(to_engine_msg(msg)). |
|
351 send_all().in_room(r.id).but_self().action()); |
|
352 info.teams_in_game -= 1; |
|
353 if info.teams_in_game == 0 { |
|
354 actions.push(FinishRoomGame(r.id)); |
|
355 } |
|
356 } |
|
357 } |
|
358 server.react(client_id, actions); |
|
359 } |
|
360 FinishRoomGame(room_id) => { |
|
361 let actions = { |
|
362 let r = &mut server.rooms[room_id]; |
|
363 r.game_info = None; |
|
364 r.ready_players_number = 0; |
|
365 vec![SendRoomUpdate(None), |
|
366 RoundFinished.send_all().in_room(r.id).action()] |
|
367 }; |
|
368 server.react(client_id, actions); |
|
369 } |
314 Warn(msg) => { |
370 Warn(msg) => { |
315 run_action(server, client_id, Warning(msg).send_self().action()); |
371 run_action(server, client_id, Warning(msg).send_self().action()); |
316 } |
372 } |
317 ProtocolError(msg) => { |
373 ProtocolError(msg) => { |
318 run_action(server, client_id, Error(msg).send_self().action()) |
374 run_action(server, client_id, Error(msg).send_self().action()) |