# Include modules providing various useful staff
use Teeworlds::Common;
use Teeworlds::Random_maps;
use Teeworlds::Captains;

# Set chroot target directory
#$root = '/srv/teeworlds';

# Set passwords for predefined groups
$groups{'user'}{'password'}  = '.';
$groups{'admin'}{'password'} = 'admin password';
$groups{'root'}{'password'}  = 'root password!';

sv_gametype 'ictf';
sv_name "Test server";
sv_register 0; # if you change it, don't forget to change the passwords above!
sv_scorelimit 1000;
sv_timelimit 20;
sv_warmup 20;
sv_pause_warmup 5;
$sv_autolimit_players = 1; # Limit the number of active players according to the map
set_map 'ctf2';

set_datadir 'data';

# Maps available on the server
# probability: the probability for this map to appear during random
#   maprotation. These numbers are later normalized to sum up to 1.
# player: default limit on number of players in one team
%maps = (
 ctf2 => {
  probability => 0.8,
  players => 6
 },
 ctf4 => {
  probability => 0.7,
  players => 4
 },
 ctf4_new => {
  probability => 0.4,
  players => 6
 },
 'ctf_lava-1' => {
  probability => 0.2,
  players => 5
 },
 ctf_mars => {
  probability => 0.3,
  players => 4
 },
 test11 => {
  probability => 0.4,
  players => 6
 }
);

# Compute normalized probability for the random map rotation
normalize_maps;

# Apply players autolimit when changing the map
sub set_map {
 sv_map $_[0];
 sv_playerlimit 2*$maps{$_[0]}{'players'} 
   if $sv_autolimit_players && defined $maps{$_[0]};
};

# Introduce sv_autolimit_players rcon command
$commands{'sv_autolimit_players'} = {
 gid    => $g_all,
 help   => 'Limit the number of players according to a map',
 man    => 'Usage: sv_autolimit_players {0|1}',
 sub    => sub { 
  if ($_[0] ne '') { $sv_autolimit_players = $_[0] == 1 ? 1 : 0 }
  else { reply $sv_autolimit_players };
  return undef;
 },
 vgid   => $g_all,
 vcheck => sub {
  die "Expected value between 0 and 1\n"
    if $_[2] !~ /^ *\d+ *$/ && $_[2] < 0 || $_[2] > 1;
  die "Useless vote\n" if $_[2] == $sv_autolimit_players;
 }
};

# Tell the new player rcon user password
$hooks{'enter'}{'main'} = sub {
 send_chat -1, $_[0], 
           sprintf 'Welcome, %s. Rcon password is %s', 
                   client_name($_[0]), 
                   $groups{'user'}{'password'};
};

# Perform chroot
#init $root, 1000, 1000;
