gameServer2/src/server/actions.rs
changeset 13478 d79795acaa73
parent 13477 f748a72432f2
child 13480 fb37745c5bca
equal deleted inserted replaced
13477:f748a72432f2 13478:d79795acaa73
     1 use std::{
     1 use std::{
     2     io, io::Write,
     2     io, io::Write,
     3     iter::once,
     3     iter::once,
     4     mem::swap
     4     mem::replace
     5 };
     5 };
     6 use super::{
     6 use super::{
     7     server::HWServer,
     7     server::HWServer,
     8     room::{RoomId, GameInfo},
     8     room::{GameInfo},
     9     client::{ClientId, HWClient},
     9     client::HWClient,
       
    10     coretypes::{ClientId, RoomId, VoteType},
    10     room::HWRoom,
    11     room::HWRoom,
    11     handlers
    12     handlers
    12 };
    13 };
    13 use protocol::messages::{
    14 use protocol::messages::{
    14     HWProtocolMessage,
    15     HWProtocolMessage,
    15     HWServerMessage,
    16     HWServerMessage,
    16     HWServerMessage::*
    17     HWServerMessage::*,
       
    18     server_chat
    17 };
    19 };
    18 use utils::to_engine_msg;
    20 use utils::to_engine_msg;
    19 
    21 
    20 pub enum Destination {
    22 pub enum Destination {
    21     ToId(ClientId),
    23     ToId(ClientId),
   101     SendRoomUpdate(Option<String>),
   103     SendRoomUpdate(Option<String>),
   102     StartRoomGame(RoomId),
   104     StartRoomGame(RoomId),
   103     SendTeamRemovalMessage(String),
   105     SendTeamRemovalMessage(String),
   104     FinishRoomGame(RoomId),
   106     FinishRoomGame(RoomId),
   105     SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool},
   107     SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool},
       
   108     AddVote{vote: bool, is_forced: bool},
       
   109     ApplyVoting(VoteType, RoomId),
   106     Warn(String),
   110     Warn(String),
   107     ProtocolError(String)
   111     ProtocolError(String)
   108 }
   112 }
   109 
   113 
   110 use self::Action::*;
   114 use self::Action::*;
   322                     }
   326                     }
   323                 }
   327                 }
   324             }
   328             }
   325             server.react(client_id, actions);
   329             server.react(client_id, actions);
   326         }
   330         }
       
   331         AddVote{vote, is_forced} => {
       
   332             let mut actions = Vec::new();
       
   333             if let (c, Some(r)) = server.client_and_room(client_id) {
       
   334                 let mut result = None;
       
   335                 if let Some(ref mut voting) = r.voting {
       
   336                     if is_forced || voting.votes.iter().find(|(id, _)| client_id == *id).is_none() {
       
   337                         actions.push(server_chat("Your vote has been counted.").send_self().action());
       
   338                         voting.votes.push((client_id, vote));
       
   339                         let i = voting.votes.iter();
       
   340                         let pro = i.clone().filter(|(_, v)| *v).count();
       
   341                         let contra = i.filter(|(_, v)| !*v).count();
       
   342                         let success_quota = voting.voters.len() / 2 + 1;
       
   343                         if is_forced && vote || pro >= success_quota {
       
   344                             result = Some(true);
       
   345                         } else if is_forced && !vote || contra > voting.voters.len() - success_quota {
       
   346                             result = Some(false);
       
   347                         }
       
   348                     } else {
       
   349                         actions.push(server_chat("You already have voted.").send_self().action());
       
   350                     }
       
   351                 } else {
       
   352                     actions.push(server_chat("There's no voting going on.").send_self().action());
       
   353                 }
       
   354 
       
   355                 if let Some(res) = result {
       
   356                     actions.push(server_chat("Voting closed.")
       
   357                         .send_all().in_room(r.id).action());
       
   358                     let voting = replace(&mut r.voting, None).unwrap();
       
   359                     if res {
       
   360                         actions.push(ApplyVoting(voting.kind, r.id));
       
   361                     }
       
   362                 }
       
   363             }
       
   364 
       
   365             server.react(client_id, actions);
       
   366         }
       
   367         ApplyVoting(kind, room_id) => {
       
   368             let mut actions = Vec::new();
       
   369             let mut id = client_id;
       
   370             match kind {
       
   371                 VoteType::Kick(nick) => {
       
   372                     if let Some(c) = server.find_client(&nick) {
       
   373                         if c.room_id == Some(room_id) {
       
   374                             id = c.id;
       
   375                             actions.push(Kicked.send_self().action());
       
   376                             actions.push(MoveToLobby("kicked".to_string()));
       
   377                         }
       
   378                     }
       
   379                 },
       
   380                 VoteType::Map(_) => {
       
   381                     unimplemented!();
       
   382                 },
       
   383                 VoteType::Pause => {
       
   384                     if let Some(ref mut info) = server.room(client_id).unwrap().game_info {
       
   385                         info.is_paused = !info.is_paused;
       
   386                         actions.push(server_chat("Pause toggled.")
       
   387                             .send_all().in_room(room_id).action());
       
   388                         actions.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))])
       
   389                             .send_all().in_room(room_id).action());
       
   390                     }
       
   391                 },
       
   392                 VoteType::NewSeed => {
       
   393                     unimplemented!();
       
   394                 },
       
   395                 VoteType::HedgehogsPerTeam(number) => {
       
   396                     let r = &mut server.rooms[room_id];
       
   397                     let nicks = r.set_hedgehogs_number(number);
       
   398                     actions.extend(nicks.into_iter().map(|n|
       
   399                         HedgehogsNumber(n, number).send_all().in_room(room_id).action()
       
   400                     ));
       
   401                 },
       
   402             }
       
   403             server.react(id, actions);
       
   404         }
   327         MoveToLobby(msg) => {
   405         MoveToLobby(msg) => {
   328             let mut actions = Vec::new();
   406             let mut actions = Vec::new();
   329             let lobby_id = server.lobby_id;
   407             let lobby_id = server.lobby_id;
   330             if let (c, Some(r)) = server.client_and_room(client_id) {
   408             if let (c, Some(r)) = server.client_and_room(client_id) {
   331                 r.players_number -= 1;
   409                 r.players_number -= 1;
   468             }
   546             }
   469             server.react(client_id, actions);
   547             server.react(client_id, actions);
   470         }
   548         }
   471         FinishRoomGame(room_id) => {
   549         FinishRoomGame(room_id) => {
   472             let mut actions = Vec::new();
   550             let mut actions = Vec::new();
   473             let mut old_info = None;
   551             let old_info;
   474             {
   552             {
   475                 let r = &mut server.rooms[room_id];
   553                 let r = &mut server.rooms[room_id];
   476                 swap(&mut old_info, &mut r.game_info);
   554                 old_info = replace(&mut r.game_info, None);
   477                 r.game_info = None;
   555                 r.game_info = None;
   478                 r.ready_players_number = 1;
   556                 r.ready_players_number = 1;
   479                 actions.push(SendRoomUpdate(None));
   557                 actions.push(SendRoomUpdate(None));
   480                 actions.push(RoundFinished.send_all().in_room(r.id).action());
   558                 actions.push(RoundFinished.send_all().in_room(r.id).action());
   481             }
   559             }