rust/hedgewars-server/src/server/actions.rs
changeset 14707 9f98086de1b6
parent 14704 932ff7683653
child 14708 5122c584804e
equal deleted inserted replaced
14706:669eb45bda72 14707:9f98086de1b6
   101     }
   101     }
   102 }
   102 }
   103 
   103 
   104 pub enum Action {
   104 pub enum Action {
   105     ChangeMaster(RoomId, Option<ClientId>),
   105     ChangeMaster(RoomId, Option<ClientId>),
   106     SendRoomUpdate(Option<String>),
       
   107     StartRoomGame(RoomId),
   106     StartRoomGame(RoomId),
   108     SendTeamRemovalMessage(String),
   107     SendTeamRemovalMessage(String),
   109     FinishRoomGame(RoomId),
   108     FinishRoomGame(RoomId),
   110     SendRoomData {
   109     SendRoomData {
   111         to: ClientId,
   110         to: ClientId,
   112         teams: bool,
   111         teams: bool,
   113         config: bool,
   112         config: bool,
   114         flags: bool,
   113         flags: bool,
   115     },
   114     },
   116     AddVote {
       
   117         vote: bool,
       
   118         is_forced: bool,
       
   119     },
       
   120     ApplyVoting(VoteType, RoomId),
   115     ApplyVoting(VoteType, RoomId),
   121 }
   116 }
   122 
   117 
   123 use self::Action::*;
   118 use self::Action::*;
   124 
   119 
   128             to,
   123             to,
   129             teams,
   124             teams,
   130             config,
   125             config,
   131             flags,
   126             flags,
   132         } => {
   127         } => {
   133             let mut actions = Vec::new();
       
   134             let room_id = server.clients[client_id].room_id;
   128             let room_id = server.clients[client_id].room_id;
   135             if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
   129             if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) {
   136                 if config {
   130                 if config {
   137                     /*                    actions.push(
   131                     /*                    actions.push(
   138                         ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
   132                         ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config())
   183                         /*actions.push(ClientFlags("+r".to_string(), nicks).send(to).action())*/
   177                         /*actions.push(ClientFlags("+r".to_string(), nicks).send(to).action())*/
   184 ;
   178 ;
   185                     }
   179                     }
   186                 }
   180                 }
   187             }
   181             }
   188             server.react(client_id, actions);
       
   189         }
       
   190         AddVote { vote, is_forced } => {
       
   191             let mut actions = Vec::new();
       
   192             if let Some(r) = server.room(client_id) {
       
   193                 let mut result = None;
       
   194                 if let Some(ref mut voting) = r.voting {
       
   195                     if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
       
   196                         /*                        actions.push(
       
   197                             server_chat("Your vote has been counted.".to_string())
       
   198                                 .send_self()
       
   199                                 .action(),
       
   200                         )*/
       
   201 ;
       
   202                         voting.votes.push((client_id, vote));
       
   203                         let i = voting.votes.iter();
       
   204                         let pro = i.clone().filter(|(_, v)| *v).count();
       
   205                         let contra = i.filter(|(_, v)| !*v).count();
       
   206                         let success_quota = voting.voters.len() / 2 + 1;
       
   207                         if is_forced && vote || pro >= success_quota {
       
   208                             result = Some(true);
       
   209                         } else if is_forced && !vote || contra > voting.voters.len() - success_quota
       
   210                         {
       
   211                             result = Some(false);
       
   212                         }
       
   213                     } else {
       
   214                         /*                        actions.push(
       
   215                             server_chat("You already have voted.".to_string())
       
   216                                 .send_self()
       
   217                                 .action(),
       
   218                         )*/
       
   219 ;
       
   220                     }
       
   221                 } else {
       
   222                     /*                    actions.push(
       
   223                         server_chat("There's no voting going on.".to_string())
       
   224                             .send_self()
       
   225                             .action(),
       
   226                     )*/
       
   227 ;
       
   228                 }
       
   229 
       
   230                 if let Some(res) = result {
       
   231                     /*actions.push(
       
   232                         server_chat("Voting closed.".to_string())
       
   233                             .send_all()
       
   234                             .in_room(r.id)
       
   235                             .action(),
       
   236                     );*/
       
   237                     let voting = replace(&mut r.voting, None).unwrap();
       
   238                     if res {
       
   239                         actions.push(ApplyVoting(voting.kind, r.id));
       
   240                     }
       
   241                 }
       
   242             }
       
   243 
       
   244             server.react(client_id, actions);
       
   245         }
   182         }
   246         ApplyVoting(kind, room_id) => {
   183         ApplyVoting(kind, room_id) => {
   247             let mut actions = Vec::new();
   184             let mut actions = Vec::new();
   248             let mut id = client_id;
   185             let mut id = client_id;
   249             match kind {
   186             match kind {
   263                             server_chat(location.to_string())
   200                             server_chat(location.to_string())
   264                                 .send_all()
   201                                 .send_all()
   265                                 .in_room(room_id)
   202                                 .in_room(room_id)
   266                                 .action(),
   203                                 .action(),
   267                         );*/
   204                         );*/
   268                         actions.push(SendRoomUpdate(None));
   205                         //actions.push(SendRoomUpdate(None));
   269                         for (_, c) in server.clients.iter() {
   206                         for (_, c) in server.clients.iter() {
   270                             if c.room_id == Some(room_id) {
   207                             if c.room_id == Some(room_id) {
   271                                 actions.push(SendRoomData {
   208                                 actions.push(SendRoomData {
   272                                     to: c.id,
   209                                     to: c.id,
   273                                     teams: false,
   210                                     teams: false,
   310                             .in_room(room_id)
   247                             .in_room(room_id)
   311                             .action()
   248                             .action()
   312                     }));*/
   249                     }));*/
   313                 }
   250                 }
   314             }
   251             }
   315             server.react(id, actions);
       
   316         }
   252         }
   317         ChangeMaster(room_id, new_id) => {
   253         ChangeMaster(room_id, new_id) => {
   318             let mut actions = Vec::new();
       
   319             let room_client_ids = server.room_clients(room_id);
   254             let room_client_ids = server.room_clients(room_id);
   320             let new_id = if server
   255             let new_id = if server
   321                 .room(client_id)
   256                 .room(client_id)
   322                 .map(|r| r.is_fixed())
   257                 .map(|r| r.is_fixed())
   323                 .unwrap_or(false)
   258                 .unwrap_or(false)
   362                 }
   297                 }
   363             }
   298             }
   364             if let Some(id) = new_id {
   299             if let Some(id) = new_id {
   365                 server.clients[id].set_is_master(true)
   300                 server.clients[id].set_is_master(true)
   366             }
   301             }
   367             server.react(client_id, actions);
       
   368         }
       
   369         SendRoomUpdate(old_name) => {
       
   370             if let (c, Some(r)) = server.client_and_room(client_id) {
       
   371                 let name = old_name.unwrap_or_else(|| r.name.clone());
       
   372                 /*let actions = vec![RoomUpdated(name, r.info(Some(&c)))
       
   373                     .send_all()
       
   374                     .with_protocol(r.protocol_number)
       
   375                     .action()];
       
   376                 server.react(client_id, actions);*/
       
   377             }
       
   378         }
   302         }
   379         StartRoomGame(room_id) => {
   303         StartRoomGame(room_id) => {
   380             let actions = {
   304             let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server
   381                 let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server
   305                 .clients
   382                     .clients
   306                 .iter()
   383                     .iter()
   307                 .map(|(id, c)| (id, c.nick.clone()))
   384                     .map(|(id, c)| (id, c.nick.clone()))
   308                 .unzip();
   385                     .unzip();
   309             let room = &mut server.rooms[room_id];
   386                 let room = &mut server.rooms[room_id];
   310 
   387 
   311             if !room.has_multiple_clans() {
   388                 if !room.has_multiple_clans() {
   312                 /*Warn(
   389                     vec![/*Warn(
   313                     "The game can't be started with less than two clans!".to_string(),
   390                         "The game can't be started with less than two clans!".to_string(),
   314                 )*/
   391                     )*/]
   315             } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number
   392                 } else if room.protocol_number <= 43
   316             {
   393                     && room.players_number != room.ready_players_number
   317                 /*Warn("Not all players are ready".to_string())*/
   394                 {
   318             } else if room.game_info.is_some() {
   395                     vec![/*Warn("Not all players are ready".to_string())*/]
   319                 /*Warn("The game is already in progress".to_string())*/
   396                 } else if room.game_info.is_some() {
   320             } else {
   397                     vec![/*Warn("The game is already in progress".to_string())*/]
   321                 room.start_round();
   398                 } else {
   322                 for id in room_clients {
   399                     room.start_round();
   323                     let c = &mut server.clients[id];
   400                     for id in room_clients {
   324                     c.set_is_in_game(false);
   401                         let c = &mut server.clients[id];
   325                     c.team_indices = room.client_team_indices(c.id);
   402                         c.set_is_in_game(false);
   326                 }
   403                         c.team_indices = room.client_team_indices(c.id);
   327                 /*RunGame.send_all().in_room(room.id).action(),*/
   404                     }
   328                 //SendRoomUpdate(None),
   405                     vec![
   329                 /*ClientFlags("+g".to_string(), room_nicks)
   406                         /*RunGame.send_all().in_room(room.id).action(),*/
   330                 .send_all()
   407                         SendRoomUpdate(None),
   331                 .in_room(room.id)
   408                         /*ClientFlags("+g".to_string(), room_nicks)
   332                 .action(),*/
   409                         .send_all()
   333             }
   410                         .in_room(room.id)
       
   411                         .action(),*/
       
   412                     ]
       
   413                 }
       
   414             };
       
   415             server.react(client_id, actions);
       
   416         }
   334         }
   417         SendTeamRemovalMessage(team_name) => {
   335         SendTeamRemovalMessage(team_name) => {
   418             let mut actions = Vec::new();
   336             let mut actions = Vec::new();
   419             if let Some(r) = server.room(client_id) {
   337             if let Some(r) = server.room(client_id) {
   420                 if let Some(ref mut info) = r.game_info {
   338                 if let Some(ref mut info) = r.game_info {
   445                             .but_self()
   363                             .but_self()
   446                             .action(),
   364                             .action(),
   447                     );*/
   365                     );*/
   448                 }
   366                 }
   449             }
   367             }
   450             server.react(client_id, actions);
       
   451         }
   368         }
   452         FinishRoomGame(room_id) => {
   369         FinishRoomGame(room_id) => {
   453             let mut actions = Vec::new();
   370             let mut actions = Vec::new();
   454 
   371 
   455             let r = &mut server.rooms[room_id];
   372             let r = &mut server.rooms[room_id];
   456             r.ready_players_number = 1;
   373             r.ready_players_number = 1;
   457             actions.push(SendRoomUpdate(None));
   374             //actions.push(SendRoomUpdate(None));
   458             //actions.push(RoundFinished.send_all().in_room(r.id).action());
   375             //actions.push(RoundFinished.send_all().in_room(r.id).action());
   459 
   376 
   460             if let Some(info) = replace(&mut r.game_info, None) {
   377             if let Some(info) = replace(&mut r.game_info, None) {
   461                 for (_, c) in server.clients.iter() {
   378                 for (_, c) in server.clients.iter() {
   462                     if c.room_id == Some(room_id) && c.is_joined_mid_game() {
   379                     if c.room_id == Some(room_id) && c.is_joined_mid_game() {
   497                 } else {
   414                 } else {
   498                     ClientFlags("-r".to_string(), nicks)
   415                     ClientFlags("-r".to_string(), nicks)
   499                 };
   416                 };
   500                 //actions.push(msg.send_all().in_room(room_id).action());
   417                 //actions.push(msg.send_all().in_room(room_id).action());
   501             }
   418             }
   502             server.react(client_id, actions);
   419         }
   503         }
   420     }
   504     }
   421 }
   505 }