rust/hedgewars-server/src/handlers/common.rs
changeset 15516 b907b9071ec5
parent 15492 395be40faa51
child 15519 b3157d218ae2
equal deleted inserted replaced
15509:6ddfde71ba6a 15516:b907b9071ec5
     1 use crate::{
     1 use crate::{
     2     core::{
     2     core::{
     3         client::HwClient,
     3         client::HwClient,
     4         room::HwRoom,
     4         room::HwRoom,
     5         server::{HwServer, JoinRoomError, LeaveRoomError, LeaveRoomResult, StartGameError},
     5         server::{
       
     6             EndGameResult, HwServer, JoinRoomError, LeaveRoomError, LeaveRoomResult, StartGameError,
       
     7         },
     6         types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
     8         types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
     7     },
     9     },
     8     protocol::messages::{
    10     protocol::messages::{
     9         add_flags, remove_flags, server_chat,
    11         add_flags, remove_flags, server_chat,
    10         HwProtocolMessage::{self, Rnd},
    12         HwProtocolMessage::{self, Rnd},
   202     response: &mut Response,
   204     response: &mut Response,
   203 ) {
   205 ) {
   204     let client = server.client(response.client_id);
   206     let client = server.client(response.client_id);
   205     response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
   207     response.add(ClientFlags(remove_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_all());
   206 
   208 
   207     match (result) {
   209     match result {
   208         LeaveRoomResult::RoomRemoved => {
   210         LeaveRoomResult::RoomRemoved => {
   209             response.add(
   211             response.add(
   210                 RoomRemove(room.name.clone())
   212                 RoomRemove(room.name.clone())
   211                     .send_all()
   213                     .send_all()
   212                     .with_protocol(room.protocol_number),
   214                     .with_protocol(room.protocol_number),
   502         Err(StartGameError::NotEnoughClans) => {
   504         Err(StartGameError::NotEnoughClans) => {
   503             response.warn("The game can't be started with less than two clans!")
   505             response.warn("The game can't be started with less than two clans!")
   504         }
   506         }
   505         Err(StartGameError::NotEnoughTeams) => (),
   507         Err(StartGameError::NotEnoughTeams) => (),
   506         Err(StartGameError::NotReady) => response.warn("Not all players are ready"),
   508         Err(StartGameError::NotReady) => response.warn("Not all players are ready"),
   507         Err(StartGameErrror) => response.warn("The game is already in progress"),
   509         Err(StartGameError::AlreadyInGame) => response.warn("The game is already in progress"),
   508     }
   510     }
   509 }
   511 }
   510 
   512 
   511 pub fn end_game(server: &mut HwServer, room_id: RoomId, response: &mut Response) {
   513 pub fn get_end_game_result(
   512     let room = &mut server.rooms[room_id];
   514     server: &HwServer,
   513     room.ready_players_number = 1;
   515     room_id: RoomId,
       
   516     result: EndGameResult,
       
   517     response: &mut Response,
       
   518 ) {
       
   519     let room = server.room(room_id);
   514     let room_master = if let Some(id) = room.master_id {
   520     let room_master = if let Some(id) = room.master_id {
   515         Some(&server.clients[id])
   521         Some(server.client(id))
   516     } else {
   522     } else {
   517         None
   523         None
   518     };
   524     };
       
   525 
   519     get_room_update(None, room, room_master, response);
   526     get_room_update(None, room, room_master, response);
   520     response.add(RoundFinished.send_all().in_room(room_id));
   527     response.add(RoundFinished.send_all().in_room(room_id));
   521 
   528 
   522     if let Some(info) = replace(&mut room.game_info, None) {
   529     for client_id in result.joined_mid_game_clients {
   523         for (_, client) in server.clients.iter() {
   530         super::common::get_room_config(room, client_id, response);
   524             if client.room_id == Some(room_id) && client.is_joined_mid_game() {
   531         response.extend(
   525                 super::common::get_room_config(room, client.id, response);
   532             result
   526                 response.extend(
   533                 .left_teams
   527                     info.left_teams
   534                 .iter()
   528                         .iter()
   535                 .map(|name| TeamRemove(name.clone()).send(client_id)),
   529                         .map(|name| TeamRemove(name.clone()).send(client.id)),
   536         );
   530                 );
   537     }
   531             }
   538 
   532         }
   539     if !result.unreadied_nicks.is_empty() {
   533     }
       
   534 
       
   535     let nicks: Vec<_> = server
       
   536         .clients
       
   537         .iter_mut()
       
   538         .filter(|(_, c)| c.room_id == Some(room_id))
       
   539         .map(|(_, c)| {
       
   540             c.set_is_ready(c.is_master());
       
   541             c.set_is_joined_mid_game(false);
       
   542             c
       
   543         })
       
   544         .filter_map(|c| {
       
   545             if !c.is_master() {
       
   546                 Some(c.nick.clone())
       
   547             } else {
       
   548                 None
       
   549             }
       
   550         })
       
   551         .collect();
       
   552 
       
   553     if !nicks.is_empty() {
       
   554         let msg = if room.protocol_number < 38 {
   540         let msg = if room.protocol_number < 38 {
   555             LegacyReady(false, nicks)
   541             LegacyReady(false, result.unreadied_nicks)
   556         } else {
   542         } else {
   557             ClientFlags(remove_flags(&[Flags::Ready]), nicks)
   543             ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)
   558         };
   544         };
   559         response.add(msg.send_all().in_room(room_id));
   545         response.add(msg.send_all().in_room(room_id));
   560     }
   546     }
   561 }
   547 }
   562 
   548