gameServer2/src/server/handlers/inroom.rs
changeset 13528 8c5dd562c9f7
parent 13526 ba5211dddb21
child 13529 5359ff75da3a
equal deleted inserted replaced
13527:282e5e54386f 13528:8c5dd562c9f7
     6     server_chat
     6     server_chat
     7 };
     7 };
     8 use server::{
     8 use server::{
     9     coretypes::{ClientId, RoomId, Voting, VoteType},
     9     coretypes::{ClientId, RoomId, Voting, VoteType},
    10     server::HWServer,
    10     server::HWServer,
    11     room::HWRoom,
    11     room::{HWRoom, RoomFlags},
    12     actions::{Action, Action::*}
    12     actions::{Action, Action::*}
    13 };
    13 };
    14 use utils::is_name_illegal;
    14 use utils::is_name_illegal;
    15 use std::mem::swap;
    15 use std::mem::swap;
    16 use base64::{encode, decode};
    16 use base64::{encode, decode};
    77         VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
    77         VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()),
    78         VoteType::Pause => "pause".to_string(),
    78         VoteType::Pause => "pause".to_string(),
    79         VoteType::NewSeed => "new seed".to_string(),
    79         VoteType::NewSeed => "new seed".to_string(),
    80         VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number)
    80         VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number)
    81     })
    81     })
       
    82 }
       
    83 
       
    84 fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags {
       
    85     use protocol::messages::HWProtocolMessage::*;
       
    86     match msg {
       
    87         ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN,
       
    88         ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD,
       
    89         ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS,
       
    90         _ => RoomFlags::empty()
       
    91     }
    82 }
    92 }
    83 
    93 
    84 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
    94 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
    85     use protocol::messages::HWProtocolMessage::*;
    95     use protocol::messages::HWProtocolMessage::*;
    86     match message {
    96     match message {
    96             };
   106             };
    97             server.react(client_id, actions);
   107             server.react(client_id, actions);
    98         },
   108         },
    99         Fix => {
   109         Fix => {
   100             if let (c, Some(r)) = server.client_and_room(client_id) {
   110             if let (c, Some(r)) = server.client_and_room(client_id) {
   101                 if c.is_admin() { r.is_fixed = true }
   111                 if c.is_admin() { r.set_is_fixed(true) }
   102             }
   112             }
   103         }
   113         }
   104         Unfix => {
   114         Unfix => {
   105             if let (c, Some(r)) = server.client_and_room(client_id) {
   115             if let (c, Some(r)) = server.client_and_room(client_id) {
   106                 if c.is_admin() { r.is_fixed = false }
   116                 if c.is_admin() { r.set_is_fixed(false) }
   107             }
   117             }
   108         }
   118         }
   109         Greeting(text) => {
   119         Greeting(text) => {
   110             if let (c, Some(r)) = server.client_and_room(client_id) {
   120             if let (c, Some(r)) = server.client_and_room(client_id) {
   111                 if c.is_admin() || c.is_master() && !r.is_fixed {
   121                 if c.is_admin() || c.is_master() && !r.is_fixed() {
   112                     r.greeting = text
   122                     r.greeting = text
   113                 }
   123                 }
   114             }
   124             }
   115         }
   125         }
   116         RoomName(new_name) => {
   126         RoomName(new_name) => {
   117             let actions =
   127             let actions =
   118                 if is_name_illegal(&new_name) {
   128                 if is_name_illegal(&new_name) {
   119                     vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
   129                     vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())]
   120                 } else if server.room(client_id).map(|r| r.is_fixed).unwrap_or(false) {
   130                 } else if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) {
   121                     vec![Warn("Access denied.".to_string())]
   131                     vec![Warn("Access denied.".to_string())]
   122                 } else if server.has_room(&new_name) {
   132                 } else if server.has_room(&new_name) {
   123                     vec![Warn("A room with the same name already exists.".to_string())]
   133                     vec![Warn("A room with the same name already exists.".to_string())]
   124                 } else {
   134                 } else {
   125                     let mut old_name = new_name.clone();
   135                     let mut old_name = new_name.clone();
   144                 let is_ready = !c.is_ready();
   154                 let is_ready = !c.is_ready();
   145                 c.set_is_ready(is_ready);
   155                 c.set_is_ready(is_ready);
   146                 let mut v =
   156                 let mut v =
   147                     vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
   157                     vec![ClientFlags(flags.to_string(), vec![c.nick.clone()])
   148                         .send_all().in_room(r.id).action()];
   158                         .send_all().in_room(r.id).action()];
   149                 if r.is_fixed && r.ready_players_number as u32 == r.players_number {
   159                 if r.is_fixed() && r.ready_players_number == r.players_number {
   150                     v.push(StartRoomGame(r.id))
   160                     v.push(StartRoomGame(r.id))
   151                 }
   161                 }
   152                 v
   162                 v
   153             } else {
   163             } else {
   154                 Vec::new()
   164                 Vec::new()
   164                     actions.push(Warn("Too many hedgehogs!".to_string()))
   174                     actions.push(Warn("Too many hedgehogs!".to_string()))
   165                 } else if r.find_team(|t| t.name == info.name) != None {
   175                 } else if r.find_team(|t| t.name == info.name) != None {
   166                     actions.push(Warn("There's already a team with same name in the list.".to_string()))
   176                     actions.push(Warn("There's already a team with same name in the list.".to_string()))
   167                 } else if r.game_info.is_some() {
   177                 } else if r.game_info.is_some() {
   168                     actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
   178                     actions.push(Warn("Joining not possible: Round is in progress.".to_string()))
       
   179                 } else if r.is_team_add_restricted() {
       
   180                     actions.push(Warn("This room currently does not allow adding new teams.".to_string()));
   169                 } else {
   181                 } else {
   170                     let team = r.add_team(c.id, info);
   182                     let team = r.add_team(c.id, info);
   171                     c.teams_in_game += 1;
   183                     c.teams_in_game += 1;
   172                     c.clan = Some(team.color);
   184                     c.clan = Some(team.color);
   173                     actions.push(TeamAccepted(team.name.clone())
   185                     actions.push(TeamAccepted(team.name.clone())
   250 
   262 
   251             server.react(client_id, actions);
   263             server.react(client_id, actions);
   252         },
   264         },
   253         Cfg(cfg) => {
   265         Cfg(cfg) => {
   254             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   266             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   255                 if r.is_fixed {
   267                 if r.is_fixed() {
   256                     vec![Warn("Access denied.".to_string())]
   268                     vec![Warn("Access denied.".to_string())]
   257                 } else if !c.is_master() {
   269                 } else if !c.is_master() {
   258                     vec![ProtocolError("You're not the room master!".to_string())]
   270                     vec![ProtocolError("You're not the room master!".to_string())]
   259                 } else {
   271                 } else {
   260                     let v = vec![cfg.to_server_msg()
   272                     let v = vec![cfg.to_server_msg()
   334             } else {
   346             } else {
   335                 Vec::new()
   347                 Vec::new()
   336             };
   348             };
   337             server.react(client_id, actions);
   349             server.react(client_id, actions);
   338         }
   350         }
       
   351         ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly  => {
       
   352             if server.clients[client_id].is_master() {
       
   353                 server.rooms[room_id].flags.toggle(room_message_flag(&message));
       
   354             }
       
   355             server.react(client_id, vec![SendRoomUpdate(None)]);
       
   356         }
   339         StartGame => {
   357         StartGame => {
   340             let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
   358             let actions = if let (_, Some(r)) = server.client_and_room(client_id) {
   341                 vec![StartRoomGame(r.id)]
   359                 vec![StartRoomGame(r.id)]
   342             } else {
   360             } else {
   343                 Vec::new()
   361                 Vec::new()