rust/hedgewars-server/src/server/handlers/inroom.rs
changeset 15026 a479916799ea
parent 14790 e94fbf6cad2b
equal deleted inserted replaced
15025:dc4a12a84c92 15026:a479916799ea
    15     },
    15     },
    16     utils::is_name_illegal,
    16     utils::is_name_illegal,
    17 };
    17 };
    18 use base64::{decode, encode};
    18 use base64::{decode, encode};
    19 use log::*;
    19 use log::*;
    20 use std::iter::once;
    20 use std::{cmp::min, iter::once, mem::swap};
    21 use std::mem::swap;
       
    22 
    21 
    23 #[derive(Clone)]
    22 #[derive(Clone)]
    24 struct ByMsg<'a> {
    23 struct ByMsg<'a> {
    25     messages: &'a [u8],
    24     messages: &'a [u8],
    26 }
    25 }
   164             }
   163             }
   165         }
   164         }
   166         MaxTeams(count) => {
   165         MaxTeams(count) => {
   167             if !client.is_master() {
   166             if !client.is_master() {
   168                 response.add(Warning("You're not the room master!".to_string()).send_self());
   167                 response.add(Warning("You're not the room master!".to_string()).send_self());
   169             } else if count < 2 || count > MAX_TEAMS_IN_ROOM {
   168             } else if !(2..=MAX_TEAMS_IN_ROOM).contains(&count) {
   170                 response
   169                 response
   171                     .add(Warning("/maxteams: specify number from 2 to 8".to_string()).send_self());
   170                     .add(Warning("/maxteams: specify number from 2 to 8".to_string()).send_self());
   172             } else {
   171             } else {
   173                 server.rooms[room_id].max_teams = count;
   172                 server.rooms[room_id].max_teams = count;
   174             }
   173             }
   289             }
   288             }
   290         },
   289         },
   291         SetHedgehogsNumber(team_name, number) => {
   290         SetHedgehogsNumber(team_name, number) => {
   292             let addable_hedgehogs = room.addable_hedgehogs();
   291             let addable_hedgehogs = room.addable_hedgehogs();
   293             if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
   292             if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
       
   293                 let max_hedgehogs = min(
       
   294                     MAX_HEDGEHOGS_PER_TEAM,
       
   295                     addable_hedgehogs + team.hedgehogs_number,
       
   296                 );
   294                 if !client.is_master() {
   297                 if !client.is_master() {
   295                     response.add(Error("You're not the room master!".to_string()).send_self());
   298                     response.add(Error("You're not the room master!".to_string()).send_self());
   296                 } else if number < 1
   299                 } else if !(1..=max_hedgehogs).contains(&number) {
   297                     || number > MAX_HEDGEHOGS_PER_TEAM
       
   298                     || number > addable_hedgehogs + team.hedgehogs_number
       
   299                 {
       
   300                     response
   300                     response
   301                         .add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self());
   301                         .add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send_self());
   302                 } else {
   302                 } else {
   303                     team.hedgehogs_number = number;
   303                     team.hedgehogs_number = number;
   304                     response.add(
   304                     response.add(