rust/hedgewars-server/src/core/server.rs
changeset 15525 16d3c9acd715
parent 15523 f4f6060b536c
child 15526 24f692e791d3
equal deleted inserted replaced
15524:fbcee515b946 15525:16d3c9acd715
     1 use super::{
     1 use super::{
     2     anteroom::HwAnteroomClient,
     2     anteroom::HwAnteroomClient,
     3     client::HwClient,
     3     client::HwClient,
     4     indexslab::IndexSlab,
     4     indexslab::IndexSlab,
     5     room::HwRoom,
     5     room::HwRoom,
     6     types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo},
     6     types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting},
     7 };
     7 };
     8 use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
     8 use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
     9 
     9 
    10 use bitflags::_core::hint::unreachable_unchecked;
    10 use bitflags::_core::hint::unreachable_unchecked;
    11 use bitflags::*;
    11 use bitflags::*;
    96 #[derive(Debug)]
    96 #[derive(Debug)]
    97 pub enum ModifyRoomNameError {
    97 pub enum ModifyRoomNameError {
    98     AccessDenied,
    98     AccessDenied,
    99     InvalidName,
    99     InvalidName,
   100     DuplicateName,
   100     DuplicateName,
       
   101 }
       
   102 
       
   103 #[derive(Debug)]
       
   104 pub enum StartVoteError {
       
   105     VotingInProgress,
       
   106 }
       
   107 
       
   108 #[derive(Debug)]
       
   109 pub enum VoteResult {
       
   110     Submitted,
       
   111     Succeeded(VoteType),
       
   112     Failed,
       
   113 }
       
   114 
       
   115 #[derive(Debug)]
       
   116 pub enum VoteError {
       
   117     NoVoting,
       
   118     AlreadyVoted,
   101 }
   119 }
   102 
   120 
   103 #[derive(Debug)]
   121 #[derive(Debug)]
   104 pub enum StartGameError {
   122 pub enum StartGameError {
   105     NotEnoughClans,
   123     NotEnoughClans,
   165     pub fn client(&self, client_id: ClientId) -> &HwClient {
   183     pub fn client(&self, client_id: ClientId) -> &HwClient {
   166         &self.clients[client_id]
   184         &self.clients[client_id]
   167     }
   185     }
   168 
   186 
   169     #[inline]
   187     #[inline]
   170     fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient {
       
   171         &mut self.clients[client_id]
       
   172     }
       
   173 
       
   174     #[inline]
       
   175     pub fn has_client(&self, client_id: ClientId) -> bool {
   188     pub fn has_client(&self, client_id: ClientId) -> bool {
   176         self.clients.contains(client_id)
   189         self.clients.contains(client_id)
   177     }
   190     }
   178 
   191 
   179     #[inline]
   192     #[inline]
   182     }
   195     }
   183 
   196 
   184     #[inline]
   197     #[inline]
   185     pub fn room(&self, room_id: RoomId) -> &HwRoom {
   198     pub fn room(&self, room_id: RoomId) -> &HwRoom {
   186         &self.rooms[room_id]
   199         &self.rooms[room_id]
   187     }
       
   188 
       
   189     #[inline]
       
   190     pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom {
       
   191         &mut self.rooms[room_id]
       
   192     }
   200     }
   193 
   201 
   194     #[inline]
   202     #[inline]
   195     pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> {
   203     pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> {
   196         self.rooms.get(room_id)
   204         self.rooms.get(room_id)
   536             &mut self.server.clients[self.client_id],
   544             &mut self.server.clients[self.client_id],
   537             &mut self.server.rooms[self.room_id],
   545             &mut self.server.rooms[self.room_id],
   538         )
   546         )
   539     }
   547     }
   540 
   548 
       
   549     pub fn change_client<'b: 'a>(self, client_id: ClientId) -> Option<HwRoomControl<'a>> {
       
   550         let room_id = self.room_id;
       
   551         HwRoomControl::new(self.server, client_id).filter(|c| c.room_id == room_id)
       
   552     }
       
   553 
   541     pub fn leave_room(&mut self) -> LeaveRoomResult {
   554     pub fn leave_room(&mut self) -> LeaveRoomResult {
   542         let (client, room) = self.get_mut();
   555         let (client, room) = self.get_mut();
   543         room.players_number -= 1;
   556         room.players_number -= 1;
   544         client.room_id = None;
   557         client.room_id = None;
   545 
   558 
   584                     room.master_id = Some(new_master_id);
   597                     room.master_id = Some(new_master_id);
   585                     let new_master = &mut self.server.clients[new_master_id];
   598                     let new_master = &mut self.server.clients[new_master_id];
   586                     new_master.set_is_master(true);
   599                     new_master.set_is_master(true);
   587 
   600 
   588                     if protocol_number < 42 {
   601                     if protocol_number < 42 {
   589                         todo!();
       
   590                         let nick = new_master.nick.clone();
   602                         let nick = new_master.nick.clone();
   591                         self.room_mut().name = nick;
   603                         self.room_mut().name = nick;
   592                     }
   604                     }
   593 
   605 
   594                     let room = self.room_mut();
   606                     let room = self.room_mut();
   653         } else {
   665         } else {
   654             Err(NoAccess)
   666             Err(NoAccess)
   655         }
   667         }
   656     }
   668     }
   657 
   669 
   658     pub fn vote(&mut self) {
   670     pub fn start_vote(&mut self, kind: VoteType) -> Result<(), StartVoteError> {
   659         todo!("port from the room handler")
   671         use StartVoteError::*;
       
   672         match self.room().voting {
       
   673             Some(_) => Err(VotingInProgress),
       
   674             None => {
       
   675                 let voting = Voting::new(kind, self.server.room_clients(self.room_id).collect());
       
   676                 self.room_mut().voting = Some(voting);
       
   677                 Ok(())
       
   678             }
       
   679         }
       
   680     }
       
   681 
       
   682     pub fn vote(&mut self, vote: Vote) -> Result<VoteResult, VoteError> {
       
   683         use self::{VoteError::*, VoteResult::*};
       
   684         let client_id = self.client_id;
       
   685         if let Some(ref mut voting) = self.room_mut().voting {
       
   686             if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
       
   687                 voting.votes.push((client_id, vote.is_pro));
       
   688                 let i = voting.votes.iter();
       
   689                 let pro = i.clone().filter(|(_, v)| *v).count();
       
   690                 let contra = i.filter(|(_, v)| !*v).count();
       
   691                 let success_quota = voting.voters.len() / 2 + 1;
       
   692                 if vote.is_forced && vote.is_pro || pro >= success_quota {
       
   693                     let voting = self.room_mut().voting.take().unwrap();
       
   694                     Ok(Succeeded(voting.kind))
       
   695                 } else if vote.is_forced && !vote.is_pro
       
   696                     || contra > voting.voters.len() - success_quota
       
   697                 {
       
   698                     Ok(Failed)
       
   699                 } else {
       
   700                     Ok(Submitted)
       
   701                 }
       
   702             } else {
       
   703                 Err(AlreadyVoted)
       
   704             }
       
   705         } else {
       
   706             Err(NoVoting)
       
   707         }
   660     }
   708     }
   661 
   709 
   662     pub fn add_engine_message(&mut self) {
   710     pub fn add_engine_message(&mut self) {
   663         todo!("port from the room handler")
   711         todo!("port from the room handler")
   664     }
   712     }
   757         } else {
   805         } else {
   758             Err(NoTeam)
   806             Err(NoTeam)
   759         }
   807         }
   760     }
   808     }
   761 
   809 
       
   810     pub fn set_hedgehogs_number(&mut self, number: u8) -> Vec<String> {
       
   811         self.room_mut().set_hedgehogs_number(number)
       
   812     }
       
   813 
   762     pub fn add_team(&mut self, mut info: Box<TeamInfo>) -> Result<&TeamInfo, AddTeamError> {
   814     pub fn add_team(&mut self, mut info: Box<TeamInfo>) -> Result<&TeamInfo, AddTeamError> {
   763         use AddTeamError::*;
   815         use AddTeamError::*;
   764         let (client, room) = self.get_mut();
   816         let (client, room) = self.get_mut();
   765         if room.teams.len() >= room.max_teams as usize {
   817         if room.teams.len() >= room.max_teams as usize {
   766             Err(TooManyTeams)
   818             Err(TooManyTeams)
   839 
   891 
   840     pub fn save_config(&mut self, name: String, location: String) {
   892     pub fn save_config(&mut self, name: String, location: String) {
   841         self.room_mut().save_config(name, location);
   893         self.room_mut().save_config(name, location);
   842     }
   894     }
   843 
   895 
       
   896     pub fn load_config(&mut self, name: &str) -> Option<&str> {
       
   897         self.room_mut().load_config(name)
       
   898     }
       
   899 
   844     pub fn delete_config(&mut self, name: &str) -> bool {
   900     pub fn delete_config(&mut self, name: &str) -> bool {
   845         self.room_mut().delete_config(name)
   901         self.room_mut().delete_config(name)
   846     }
   902     }
   847 
   903 
   848     pub fn toggle_ready(&mut self) -> bool {
   904     pub fn toggle_ready(&mut self) -> bool {
   881                 c.set_is_in_game(true);
   937                 c.set_is_in_game(true);
   882                 c.team_indices = team_indices;
   938                 c.team_indices = team_indices;
   883             }
   939             }
   884             Ok(room_nicks)
   940             Ok(room_nicks)
   885         }
   941         }
       
   942     }
       
   943 
       
   944     pub fn toggle_pause(&mut self) -> bool {
       
   945         if let Some(ref mut info) = self.room_mut().game_info {
       
   946             info.is_paused = !info.is_paused;
       
   947         }
       
   948         self.room_mut().game_info.is_some()
   886     }
   949     }
   887 
   950 
   888     pub fn leave_game(&mut self) -> Option<Vec<String>> {
   951     pub fn leave_game(&mut self) -> Option<Vec<String>> {
   889         let (client, room) = self.get_mut();
   952         let (client, room) = self.get_mut();
   890         let client_left = client.is_in_game();
   953         let client_left = client.is_in_game();