gameServer2/src/server/handlers/inroom.rs
changeset 13492 ba5211dddb21
parent 13486 1ee192f13456
child 13494 8c5dd562c9f7
equal deleted inserted replaced
13486:1ee192f13456 13492:ba5211dddb21
     4     HWProtocolMessage,
     4     HWProtocolMessage,
     5     HWServerMessage::*,
     5     HWServerMessage::*,
     6     server_chat
     6     server_chat
     7 };
     7 };
     8 use server::{
     8 use server::{
     9     coretypes::{ClientId, Voting, VoteType},
     9     coretypes::{ClientId, RoomId, Voting, VoteType},
    10     server::HWServer,
    10     server::HWServer,
    11     room::HWRoom,
    11     room::HWRoom,
    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};
    17 use super::common::rnd_action;
    17 use super::common::rnd_reply;
    18 
    18 
    19 #[derive(Clone)]
    19 #[derive(Clone)]
    20 struct ByMsg<'a> {
    20 struct ByMsg<'a> {
    21     messages: &'a[u8]
    21     messages: &'a[u8]
    22 }
    22 }
    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 }
    82 }
    83 
    83 
    84 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
    84 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
    85     use protocol::messages::HWProtocolMessage::*;
    85     use protocol::messages::HWProtocolMessage::*;
    86     match message {
    86     match message {
    87         Part(None) => server.react(client_id, vec![
    87         Part(None) => server.react(client_id, vec![
    88             MoveToLobby("part".to_string())]),
    88             MoveToLobby("part".to_string())]),
    89         Part(Some(msg)) => server.react(client_id, vec![
    89         Part(Some(msg)) => server.react(client_id, vec![
    90             MoveToLobby(format!("part: {}", msg))]),
    90             MoveToLobby(format!("part: {}", msg))]),
    91         Chat(msg) => {
    91         Chat(msg) => {
    92             let actions = {
    92             let actions = {
    93                 let c = &mut server.clients[client_id];
    93                 let c = &mut server.clients[client_id];
    94                 let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg};
    94                 let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg};
    95                 if let Some(room_id) = c.room_id {
    95                 vec![chat_msg.send_all().in_room(room_id).but_self().action()]
    96                     vec![chat_msg.send_all().in_room(room_id).but_self().action()]
       
    97                 } else {
       
    98                     Vec::new()
       
    99                 }
       
   100             };
    96             };
   101             server.react(client_id, actions);
    97             server.react(client_id, actions);
   102         },
    98         },
   103         Fix => {
    99         Fix => {
   104             if let (c, Some(r)) = server.client_and_room(client_id) {
   100             if let (c, Some(r)) = server.client_and_room(client_id) {
   160             server.react(client_id, actions);
   156             server.react(client_id, actions);
   161         }
   157         }
   162         AddTeam(info) => {
   158         AddTeam(info) => {
   163             let mut actions = Vec::new();
   159             let mut actions = Vec::new();
   164             if let (c, Some(r)) = server.client_and_room(client_id) {
   160             if let (c, Some(r)) = server.client_and_room(client_id) {
   165                 let room_id = r.id;
       
   166                 if r.teams.len() >= r.team_limit as usize {
   161                 if r.teams.len() >= r.team_limit as usize {
   167                     actions.push(Warn("Too many teams!".to_string()))
   162                     actions.push(Warn("Too many teams!".to_string()))
   168                 } else if r.addable_hedgehogs() == 0 {
   163                 } else if r.addable_hedgehogs() == 0 {
   169                     actions.push(Warn("Too many hedgehogs!".to_string()))
   164                     actions.push(Warn("Too many hedgehogs!".to_string()))
   170                 } else if r.find_team(|t| t.name == info.name) != None {
   165                 } else if r.find_team(|t| t.name == info.name) != None {
   205             };
   200             };
   206             server.react(client_id, actions);
   201             server.react(client_id, actions);
   207         },
   202         },
   208         SetHedgehogsNumber(team_name, number) => {
   203         SetHedgehogsNumber(team_name, number) => {
   209             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   204             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   210                 let room_id = r.id;
       
   211                 let addable_hedgehogs = r.addable_hedgehogs();
   205                 let addable_hedgehogs = r.addable_hedgehogs();
   212                 if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
   206                 if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
   213                     if !c.is_master() {
   207                     if !c.is_master() {
   214                         vec![ProtocolError("You're not the room master!".to_string())]
   208                         vec![ProtocolError("You're not the room master!".to_string())]
   215                     } else if number < 1 || number > 8
   209                     } else if number < 1 || number > 8
   230             server.react(client_id, actions);
   224             server.react(client_id, actions);
   231         },
   225         },
   232         SetTeamColor(team_name, color) => {
   226         SetTeamColor(team_name, color) => {
   233             let mut owner_id = None;
   227             let mut owner_id = None;
   234             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   228             let actions = if let (c, Some(r)) = server.client_and_room(client_id) {
   235                 let room_id = r.id;
       
   236                 if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
   229                 if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) {
   237                     if !c.is_master() {
   230                     if !c.is_master() {
   238                         vec![ProtocolError("You're not the room master!".to_string())]
   231                         vec![ProtocolError("You're not the room master!".to_string())]
   239                     } else if false  {
   232                     } else if false  {
   240                         Vec::new()
   233                         Vec::new()
   278             server.react(client_id, vec![
   271             server.react(client_id, vec![
   279                 server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>")
   272                 server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>")
   280                     .send_self().action()])
   273                     .send_self().action()])
   281         }
   274         }
   282         CallVote(Some(kind)) => {
   275         CallVote(Some(kind)) => {
   283             let (room_id, is_in_game) = server.room(client_id)
   276             let is_in_game = server.rooms[room_id].game_info.is_some();
   284                 .map(|r| (r.id, r.game_info.is_some())).unwrap();
       
   285             let error = match &kind {
   277             let error = match &kind {
   286                 VoteType::Kick(nick) => {
   278                 VoteType::Kick(nick) => {
   287                     if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() {
   279                     if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() {
   288                         None
   280                         None
   289                     } else {
   281                     } else {
   396                 }
   388                 }
   397             }
   389             }
   398             server.react(client_id, actions)
   390             server.react(client_id, actions)
   399         },
   391         },
   400         Rnd(v) => {
   392         Rnd(v) => {
   401             let actions = rnd_action(v, server.room(client_id));
   393             let result = rnd_reply(&v);
   402             server.react(client_id, actions)
   394             let mut echo = vec!["/rnd".to_string()];
       
   395             echo.extend(v.into_iter());
       
   396             let chat_msg = ChatMsg {
       
   397                 nick: server.clients[client_id].nick.clone(),
       
   398                 msg: echo.join(" ")
       
   399             };
       
   400             server.react(client_id, vec![
       
   401                 chat_msg.send_all().in_room(room_id).action(),
       
   402                 result.send_all().in_room(room_id).action()])
   403         },
   403         },
   404         _ => warn!("Unimplemented!")
   404         _ => warn!("Unimplemented!")
   405     }
   405     }
   406 }
   406 }