/*************************************************
 * Decompiled by Serious and Edited by SyndiShanX
 * Script: cp\gametypes\_serversettings.gsc
*************************************************/

#using scripts\codescripts\struct;
#using scripts\shared\callbacks_shared;
#using scripts\shared\system_shared;
#namespace serversettings;

function autoexec __init__sytem__() {
  system::register("serversettings", &__init__, undefined, undefined);
}

function __init__() {
  callback::on_start_gametype(&init);
}

function init() {
  level.hostname = getdvarstring("sv_hostname");
  if(level.hostname == "") {
    level.hostname = "CoDHost";
  }
  setDvar("sv_hostname", level.hostname);
  setDvar("ui_hostname", level.hostname);
  level.motd = getdvarstring("scr_motd");
  if(level.motd == "") {
    level.motd = "";
  }
  setDvar("scr_motd", level.motd);
  setDvar("ui_motd", level.motd);
  level.allowvote = getdvarstring("g_allowvote");
  if(level.allowvote == "") {
    level.allowvote = "1";
  }
  setDvar("g_allowvote", level.allowvote);
  setDvar("ui_allowvote", level.allowvote);
  level.allow_teamchange = "0";
  if(sessionmodeisprivate() || !sessionmodeisonlinegame()) {
    level.allow_teamchange = "1";
  }
  setDvar("ui_allow_teamchange", level.allow_teamchange);
  level.friendlyfire = getgametypesetting("friendlyfiretype");
  setDvar("ui_friendlyfire", level.friendlyfire);
  if(getdvarstring("scr_mapsize") == "") {
    setDvar("scr_mapsize", "64");
  } else {
    if(getdvarfloat("scr_mapsize") >= 64) {
      setDvar("scr_mapsize", "64");
    } else {
      if(getdvarfloat("scr_mapsize") >= 32) {
        setDvar("scr_mapsize", "32");
      } else {
        if(getdvarfloat("scr_mapsize") >= 16) {
          setDvar("scr_mapsize", "16");
        } else {
          setDvar("scr_mapsize", "8");
        }
      }
    }
  }
  level.mapsize = getdvarfloat("scr_mapsize");
  constrain_gametype(getdvarstring("g_gametype"));
  constrain_map_size(level.mapsize);
  for(;;) {
    update();
    wait(5);
  }
}

function update() {
  sv_hostname = getdvarstring("sv_hostname");
  if(level.hostname != sv_hostname) {
    level.hostname = sv_hostname;
    setDvar("ui_hostname", level.hostname);
  }
  scr_motd = getdvarstring("scr_motd");
  if(level.motd != scr_motd) {
    level.motd = scr_motd;
    setDvar("ui_motd", level.motd);
  }
  g_allowvote = getdvarstring("g_allowvote");
  if(level.allowvote != g_allowvote) {
    level.allowvote = g_allowvote;
    setDvar("ui_allowvote", level.allowvote);
  }
  scr_friendlyfire = getgametypesetting("friendlyfiretype");
  if(level.friendlyfire != scr_friendlyfire) {
    level.friendlyfire = scr_friendlyfire;
    setDvar("ui_friendlyfire", level.friendlyfire);
  }
}

function constrain_gametype(gametype) {
  entities = getEntArray();
  for(i = 0; i < entities.size; i++) {
    entity = entities[i];
    if(gametype == "dm") {
      if(isDefined(entity.script_gametype_dm) && entity.script_gametype_dm != "1") {
        entity delete();
      }
      continue;
    }
    if(gametype == "tdm") {
      if(isDefined(entity.script_gametype_tdm) && entity.script_gametype_tdm != "1") {
        entity delete();
      }
      continue;
    }
    if(gametype == "ctf") {
      if(isDefined(entity.script_gametype_ctf) && entity.script_gametype_ctf != "1") {
        entity delete();
      }
      continue;
    }
    if(gametype == "hq") {
      if(isDefined(entity.script_gametype_hq) && entity.script_gametype_hq != "1") {
        entity delete();
      }
      continue;
    }
    if(gametype == "sd") {
      if(isDefined(entity.script_gametype_sd) && entity.script_gametype_sd != "1") {
        entity delete();
      }
      continue;
    }
    if(gametype == "koth") {
      if(isDefined(entity.script_gametype_koth) && entity.script_gametype_koth != "1") {
        entity delete();
      }
    }
  }
}

function constrain_map_size(mapsize) {
  entities = getEntArray();
  for(i = 0; i < entities.size; i++) {
    entity = entities[i];
    if(int(mapsize) == 8) {
      if(isDefined(entity.script_mapsize_08) && entity.script_mapsize_08 != "1") {
        entity delete();
      }
      continue;
    }
    if(int(mapsize) == 16) {
      if(isDefined(entity.script_mapsize_16) && entity.script_mapsize_16 != "1") {
        entity delete();
      }
      continue;
    }
    if(int(mapsize) == 32) {
      if(isDefined(entity.script_mapsize_32) && entity.script_mapsize_32 != "1") {
        entity delete();
      }
      continue;
    }
    if(int(mapsize) == 64) {
      if(isDefined(entity.script_mapsize_64) && entity.script_mapsize_64 != "1") {
        entity delete();
      }
    }
  }
}