gameServer2/src/server/actions.rs
changeset 13524 5359ff75da3a
parent 13523 8c5dd562c9f7
child 13527 e3ae9eea0689
equal deleted inserted replaced
13523:8c5dd562c9f7 13524:5359ff75da3a
   114 
   114 
   115 use self::Action::*;
   115 use self::Action::*;
   116 
   116 
   117 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
   117 pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) {
   118     match action {
   118     match action {
   119         Send(msg) => server.send(client_id, msg.destination, msg.message),
   119         Send(msg) => server.send(client_id, &msg.destination, msg.message),
   120         ByeClient(msg) => {
   120         ByeClient(msg) => {
   121             let room_id;
   121             let room_id;
   122             let nick;
   122             let nick;
   123             {
   123             {
   124                 let c = &server.clients[client_id];
   124                 let c = &server.clients[client_id];
   125                 room_id = c.room_id;
   125                 room_id = c.room_id;
   126                 nick = c.nick.clone();
   126                 nick = c.nick.clone();
   127             }
   127             }
   128 
   128 
   129             room_id.map (|id| {
   129             if let Some(id) = room_id{
   130                 if id != server.lobby_id {
   130                 if id != server.lobby_id {
   131                     server.react(client_id, vec![
   131                     server.react(client_id, vec![
   132                         MoveToLobby(format!("quit: {}", msg.clone()))]);
   132                         MoveToLobby(format!("quit: {}", msg.clone()))]);
   133                 }
   133                 }
   134             });
   134             }
   135 
   135 
   136             server.react(client_id, vec![
   136             server.react(client_id, vec![
   137                 LobbyLeft(nick, msg.clone()).send_all().action(),
   137                 LobbyLeft(nick, msg.clone()).send_all().action(),
   138                 Bye(msg).send_self().action(),
   138                 Bye(msg).send_self().action(),
   139                 RemoveClient]);
   139                 RemoveClient]);
   271                             vec![to_engine_msg("e$spectate 1".bytes())])
   271                             vec![to_engine_msg("e$spectate 1".bytes())])
   272                             .send_self().action());
   272                             .send_self().action());
   273                         v.push(ForwardEngineMessage(info.msg_log.clone())
   273                         v.push(ForwardEngineMessage(info.msg_log.clone())
   274                             .send_self().action());
   274                             .send_self().action());
   275 
   275 
   276                         for name in team_names.iter() {
   276                         for name in &team_names {
   277                             v.push(ForwardEngineMessage(
   277                             v.push(ForwardEngineMessage(
   278                                 vec![to_engine_msg(once(b'G').chain(name.bytes()))])
   278                                 vec![to_engine_msg(once(b'G').chain(name.bytes()))])
   279                                 .send_all().in_room(r.id).action());
   279                                 .send_all().in_room(r.id).action());
   280                         }
   280                         }
   281                         if info.is_paused {
   281                         if info.is_paused {
   293             let room_id = server.clients[client_id].room_id;
   293             let room_id = server.clients[client_id].room_id;
   294             if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
   294             if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
   295                 if config {
   295                 if config {
   296                     actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
   296                     actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
   297                         .send(to).action());
   297                         .send(to).action());
   298                     for cfg in r.game_config().into_iter() {
   298                     for cfg in r.game_config() {
   299                         actions.push(cfg.to_server_msg().send(to).action());
   299                         actions.push(cfg.to_server_msg().send(to).action());
   300                     }
   300                     }
   301                 }
   301                 }
   302                 if teams {
   302                 if teams {
   303                     let current_teams = match r.game_info {
   303                     let current_teams = match r.game_info {
   441             let room_client_ids = server.room_clients(room_id);
   441             let room_client_ids = server.room_clients(room_id);
   442             let new_id = if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) {
   442             let new_id = if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) {
   443                 new_id
   443                 new_id
   444             } else {
   444             } else {
   445                 new_id.or_else(||
   445                 new_id.or_else(||
   446                     room_client_ids.iter().find(|id| **id != client_id).map(|id| *id))
   446                     room_client_ids.iter().find(|id| **id != client_id).cloned())
   447             };
   447             };
   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 {
   466                 if let Some(nick) = new_nick {
   466                 if let Some(nick) = new_nick {
   467                     actions.push(ClientFlags("+h".to_string(), vec![nick])
   467                     actions.push(ClientFlags("+h".to_string(), vec![nick])
   468                         .send_all().in_room(r.id).action());
   468                         .send_all().in_room(r.id).action());
   469                 }
   469                 }
   470             }
   470             }
   471             new_id.map(|id| server.clients[id].set_is_master(true));
   471             if let Some(id) = new_id { server.clients[id].set_is_master(true) }
   472             server.react(client_id, actions);
   472             server.react(client_id, actions);
   473         }
   473         }
   474         RemoveTeam(name) => {
   474         RemoveTeam(name) => {
   475             let mut actions = Vec::new();
   475             let mut actions = Vec::new();
   476             if let (c, Some(r)) = server.client_and_room(client_id) {
   476             if let (c, Some(r)) = server.client_and_room(client_id) {
   570                 for (_, c) in server.clients.iter() {
   570                 for (_, c) in server.clients.iter() {
   571                     if c.room_id == Some(room_id) && c.is_joined_mid_game() {
   571                     if c.room_id == Some(room_id) && c.is_joined_mid_game() {
   572                         actions.push(SendRoomData{
   572                         actions.push(SendRoomData{
   573                             to: c.id, teams: false,
   573                             to: c.id, teams: false,
   574                             config: true, flags: false});
   574                             config: true, flags: false});
   575                         for name in info.left_teams.iter() {
   575                         for name in &info.left_teams {
   576                             actions.push(TeamRemove(name.clone())
   576                             actions.push(TeamRemove(name.clone())
   577                                 .send(c.id).action());
   577                                 .send(c.id).action());
   578                         }
   578                         }
   579                     }
   579                     }
   580                 }
   580                 }