gameServer2/src/server/actions.rs
changeset 13520 1ee192f13456
parent 13480 fb37745c5bca
child 13523 8c5dd562c9f7
equal deleted inserted replaced
13485:c85b324c4c2d 13520:1ee192f13456
     3     iter::once,
     3     iter::once,
     4     mem::replace
     4     mem::replace
     5 };
     5 };
     6 use super::{
     6 use super::{
     7     server::HWServer,
     7     server::HWServer,
     8     room::{GameInfo},
     8     room::GameInfo,
     9     client::HWClient,
     9     client::HWClient,
    10     coretypes::{ClientId, RoomId, GameCfg, VoteType},
    10     coretypes::{ClientId, RoomId, GameCfg, VoteType},
    11     room::HWRoom,
    11     room::HWRoom,
    12     handlers
    12     handlers
    13 };
    13 };
    23 pub enum Destination {
    23 pub enum Destination {
    24     ToId(ClientId),
    24     ToId(ClientId),
    25     ToSelf,
    25     ToSelf,
    26     ToAll {
    26     ToAll {
    27         room_id: Option<RoomId>,
    27         room_id: Option<RoomId>,
    28         protocol: Option<u32>,
    28         protocol: Option<u16>,
    29         skip_self: bool
    29         skip_self: bool
    30     }
    30     }
    31 }
    31 }
    32 
    32 
    33 pub struct PendingMessage {
    33 pub struct PendingMessage {
    58             *room_id = Some(clients_room_id)
    58             *room_id = Some(clients_room_id)
    59         }
    59         }
    60         self
    60         self
    61     }
    61     }
    62 
    62 
    63     pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage {
    63     pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
    64         if let Destination::ToAll {ref mut protocol, ..} = self.destination {
    64         if let Destination::ToAll {ref mut protocol, ..} = self.destination {
    65             *protocol = Some(protocol_number)
    65             *protocol = Some(protocol_number)
    66         }
    66         }
    67         self
    67         self
    68     }
    68     }
   216             let actions = {
   216             let actions = {
   217                 let r = &mut server.rooms[room_id];
   217                 let r = &mut server.rooms[room_id];
   218                 let c = &mut server.clients[client_id];
   218                 let c = &mut server.clients[client_id];
   219                 r.players_number += 1;
   219                 r.players_number += 1;
   220                 c.room_id = Some(room_id);
   220                 c.room_id = Some(room_id);
   221                 c.is_joined_mid_game = false;
   221 
   222                 if r.master_id == Some(c.id) {
   222                 let is_master = r.master_id == Some(c.id);
       
   223                 c.set_is_master(is_master);
       
   224                 c.set_is_ready(is_master);
       
   225                 c.set_is_joined_mid_game(false);
       
   226 
       
   227                 if is_master {
   223                     r.ready_players_number += 1;
   228                     r.ready_players_number += 1;
   224                     c.is_master = true;
       
   225                     c.is_ready = true;
       
   226                 } else {
       
   227                     c.is_ready = false;
       
   228                     c.is_master = false;
       
   229                 }
   229                 }
   230 
   230 
   231                 let mut v = vec![
   231                 let mut v = vec![
   232                     RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
   232                     RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(),
   233                     ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(),
   233                     ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(),
   234                     SendRoomUpdate(None)];
   234                     SendRoomUpdate(None)];
   235                 if !r.greeting.is_empty() {
   235                 if !r.greeting.is_empty() {
   236                     v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()}
   236                     v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()}
   237                         .send_self().action());
   237                         .send_self().action());
   238                 }
   238                 }
   239                 if !c.is_master {
   239                 if !c.is_master() {
   240                     let team_names: Vec<_>;
   240                     let team_names: Vec<_>;
   241                     if let Some(ref mut info) = r.game_info {
   241                     if let Some(ref mut info) = r.game_info {
   242                         c.is_in_game = true;
   242                         c.set_is_in_game(true);
   243                         c.is_joined_mid_game = true;
   243                         c.set_is_joined_mid_game(true);
   244 
   244 
   245                         {
   245                         {
   246                             let teams = info.client_teams(c.id);
   246                             let teams = info.client_teams(c.id);
   247                             c.teams_in_game = teams.clone().count() as u8;
   247                             c.teams_in_game = teams.clone().count() as u8;
   248                             c.clan = teams.clone().next().map(|t| t.color);
   248                             c.clan = teams.clone().next().map(|t| t.color);
   317                     if let Some(id) = r.master_id {
   317                     if let Some(id) = r.master_id {
   318                         actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
   318                         actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()])
   319                             .send(to).action());
   319                             .send(to).action());
   320                     }
   320                     }
   321                     let nicks: Vec<_> = server.clients.iter()
   321                     let nicks: Vec<_> = server.clients.iter()
   322                         .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready)
   322                         .filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready())
   323                         .map(|(_, c)| c.nick.clone()).collect();
   323                         .map(|(_, c)| c.nick.clone()).collect();
   324                     if !nicks.is_empty() {
   324                     if !nicks.is_empty() {
   325                         actions.push(ClientFlags("+r".to_string(), nicks)
   325                         actions.push(ClientFlags("+r".to_string(), nicks)
   326                             .send(to).action());
   326                             .send(to).action());
   327                     }
   327                     }
   409         MoveToLobby(msg) => {
   409         MoveToLobby(msg) => {
   410             let mut actions = Vec::new();
   410             let mut actions = Vec::new();
   411             let lobby_id = server.lobby_id;
   411             let lobby_id = server.lobby_id;
   412             if let (c, Some(r)) = server.client_and_room(client_id) {
   412             if let (c, Some(r)) = server.client_and_room(client_id) {
   413                 r.players_number -= 1;
   413                 r.players_number -= 1;
   414                 if c.is_ready && r.ready_players_number > 0 {
   414                 if c.is_ready() && r.ready_players_number > 0 {
   415                     r.ready_players_number -= 1;
   415                     r.ready_players_number -= 1;
   416                 }
   416                 }
   417                 if c.is_master && (r.players_number > 0 || r.is_fixed) {
   417                 if c.is_master() && (r.players_number > 0 || r.is_fixed) {
   418                     actions.push(ChangeMaster(r.id, None));
   418                     actions.push(ChangeMaster(r.id, None));
   419                 }
   419                 }
   420                 actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
   420                 actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()])
   421                     .send_all().action());
   421                     .send_all().action());
   422             }
   422             }
   448             let new_nick = new_id.map(|id| server.clients[id].nick.clone());
   448             let new_nick = new_id.map(|id| server.clients[id].nick.clone());
   449 
   449 
   450             if let (c, Some(r)) = server.client_and_room(client_id) {
   450             if let (c, Some(r)) = server.client_and_room(client_id) {
   451                 match r.master_id {
   451                 match r.master_id {
   452                     Some(id) if id == c.id => {
   452                     Some(id) if id == c.id => {
   453                         c.is_master = false;
   453                         c.set_is_master(false);
   454                         r.master_id = None;
   454                         r.master_id = None;
   455                         actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
   455                         actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()])
   456                             .send_all().in_room(r.id).action());
   456                             .send_all().in_room(r.id).action());
   457                     }
   457                     }
   458                     Some(_) => unreachable!(),
   458                     Some(_) => unreachable!(),
   462                 if let Some(nick) = new_nick {
   462                 if let Some(nick) = new_nick {
   463                     actions.push(ClientFlags("+h".to_string(), vec![nick])
   463                     actions.push(ClientFlags("+h".to_string(), vec![nick])
   464                         .send_all().in_room(r.id).action());
   464                         .send_all().in_room(r.id).action());
   465                 }
   465                 }
   466             }
   466             }
   467             new_id.map(|id| server.clients[id].is_master = true);
   467             new_id.map(|id| server.clients[id].set_is_master(true));
   468             server.react(client_id, actions);
   468             server.react(client_id, actions);
   469         }
   469         }
   470         RemoveTeam(name) => {
   470         RemoveTeam(name) => {
   471             let mut actions = Vec::new();
   471             let mut actions = Vec::new();
   472             if let (c, Some(r)) = server.client_and_room(client_id) {
   472             if let (c, Some(r)) = server.client_and_room(client_id) {
   474                 if let Some(ref mut info) = r.game_info {
   474                 if let Some(ref mut info) = r.game_info {
   475                     info.left_teams.push(name.clone());
   475                     info.left_teams.push(name.clone());
   476                 }
   476                 }
   477                 actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
   477                 actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action());
   478                 actions.push(SendRoomUpdate(None));
   478                 actions.push(SendRoomUpdate(None));
   479                 if r.game_info.is_some() && c.is_in_game {
   479                 if r.game_info.is_some() && c.is_in_game() {
   480                     actions.push(SendTeamRemovalMessage(name));
   480                     actions.push(SendTeamRemovalMessage(name));
   481                 }
   481                 }
   482             }
   482             }
   483             server.react(client_id, actions);
   483             server.react(client_id, actions);
   484         },
   484         },
   512                     vec![Warn("The game is already in progress".to_string())]
   512                     vec![Warn("The game is already in progress".to_string())]
   513                 } else {
   513                 } else {
   514                     room.start_round();
   514                     room.start_round();
   515                     for id in room_clients {
   515                     for id in room_clients {
   516                         let c = &mut server.clients[id];
   516                         let c = &mut server.clients[id];
   517                         c.is_in_game = true;
   517                         c.set_is_in_game(false);
   518                         c.team_indices = room.client_team_indices(c.id);
   518                         c.team_indices = room.client_team_indices(c.id);
   519                     }
   519                     }
   520                     vec![RunGame.send_all().in_room(room.id).action(),
   520                     vec![RunGame.send_all().in_room(room.id).action(),
   521                          SendRoomUpdate(None),
   521                          SendRoomUpdate(None),
   522                          ClientFlags("+g".to_string(), room_nicks)
   522                          ClientFlags("+g".to_string(), room_nicks)
   562                 actions.push(RoundFinished.send_all().in_room(r.id).action());
   562                 actions.push(RoundFinished.send_all().in_room(r.id).action());
   563             }
   563             }
   564 
   564 
   565             if let Some(info) = old_info {
   565             if let Some(info) = old_info {
   566                 for (_, c) in server.clients.iter() {
   566                 for (_, c) in server.clients.iter() {
   567                     if c.room_id == Some(room_id) && c.is_joined_mid_game {
   567                     if c.room_id == Some(room_id) && c.is_joined_mid_game() {
   568                         actions.push(SendRoomData{
   568                         actions.push(SendRoomData{
   569                             to: c.id, teams: false,
   569                             to: c.id, teams: false,
   570                             config: true, flags: false});
   570                             config: true, flags: false});
   571                         for name in info.left_teams.iter() {
   571                         for name in info.left_teams.iter() {
   572                             actions.push(TeamRemove(name.clone())
   572                             actions.push(TeamRemove(name.clone())
   577             }
   577             }
   578 
   578 
   579             let nicks: Vec<_> = server.clients.iter_mut()
   579             let nicks: Vec<_> = server.clients.iter_mut()
   580                 .filter(|(_, c)| c.room_id == Some(room_id))
   580                 .filter(|(_, c)| c.room_id == Some(room_id))
   581                 .map(|(_, c)| {
   581                 .map(|(_, c)| {
   582                     c.is_ready = c.is_master;
   582                     let is_master = c.is_master();
   583                     c.is_joined_mid_game = false;
   583                     c.set_is_ready(is_master);
       
   584                     c.set_is_joined_mid_game(false);
   584                     c
   585                     c
   585                 }).filter_map(|c| if !c.is_master {
   586                 }).filter_map(|c| if !c.is_master() {
   586                     Some(c.nick.clone())
   587                     Some(c.nick.clone())
   587                 } else {
   588                 } else {
   588                     None
   589                     None
   589                 }).collect();
   590                 }).collect();
   590             if !nicks.is_empty() {
   591             if !nicks.is_empty() {