Game:CreateVariable("gr_allow_teamswitch",1);
Game:CreateVariable("gr_allow_voting",1);
Game:CreateVariable("gr_team_balance",0);
Game:CreateVariable("gr_motd","");											
Game:CreateVariable("gr_Allow_AssaultUserModels",0);						
Game:CreateVariable("gr_DisplayStat", 1);
Game:CreateVariable("gr_discardclip", 0);			
Game:CreateVariable("gr_Disable_SniperRL",0);
Game:CreateVariable("gr_task_enable",1);
Game:CreateVariable("gr_task_autoload",1);			
Game:CreateVariable("gr_bwk_enable",0);
Game:CreateVariable("gr_bwk_words","");
Game:CreateVariable("gr_bwk_time",1);
Game:CreateVariable("gr_bwk_limit",2);
Game:CreateVariable("gr_auto_pickteam",0);
Game:CreateVariable("gl_flag_warmup_time",15); 
Game:CreateVariable("gl_allow_teamkilling",0);
Game:CreateVariable("gl_max_average_ping",180);
Game:CreateVariable("gl_keep_lock",0);
Game:CreateVariable("gl_fulltime",0);
Game:CreateVariable("gl_allow_spectators",1);
Game:CreateVariable("gl_keep_score",0);
Game:CreateVariable("gl_max_snipers",9999);  --max snipers in 1 team
Game:CreateVariable("gl_max_scouts",9999);  -- max scouts in 1 team
Game:CreateVariable("gl_max_engineers",9999);  --max engineers in 1 team
Game:CreateVariable("gl_forcebluejoin",0);
Game:CreateVariable("gl_forcebluejoin",0);
Game:CreateVariable("Botnumbers",0);

--
-- Handler for new sv_* commands
--
ServerCmds = {};
--
--
-- Handler for \sv_say command
--
function ServerCmds:Say(message)
	Server:BroadcastText(message);
end
Game:AddCommand("sv_say","ServerCmds:Say(%line);","Say in public");
--
-- Handler for \sv_listlevels
--
function ServerCmds:GetLevelList(mission)
	System:LogAlways("Listing levels...");
	local LevelList = Game:GetLevelList();

	-- go through all the levels
	for LevelIndex, Level in LevelList do

		if(mission) then
			-- List only specific mission
			for MissionIndex, MissionName in Level.MissionList do
				if(strlower(mission)==strlower(MissionName)) then
					System:LogAlways(Level.Name);
				end
			end
		else
			-- List all levels
			System:LogAlways(Level.Name);
		end
	end
end
Game:AddCommand("sv_listlevels","ServerCmds:GetLevelList(%line);","sv_listlevels [<mission>]");


--
-- Handler for \sv_checklevel
--
function ServerCmds:CheckLevel(level,mission)
	System:LogAlways("Checking for level...");
	local LevelList = Game:GetLevelList();
	local found = 0;

	-- search for specific level
	for LevelIndex, Level in LevelList do
		-- List only specific mission
		if(strlower(level) == strlower(Level.Name)) then
			for MissionIndex, MissionName in Level.MissionList do
				if(strlower(mission)==strlower(MissionName)) then
					found = 1;
				end
			end
		end
	end
	
	if(found==1) then
		System:LogAlways("Level "..level.." mission "..mission.." found");
	else
		System:LogAlways("Level "..level.." mission "..mission.." not found");
	end	
end

Game:AddCommand("sv_checklevel","ServerCmds:CheckLevel(%1,%2);","sv_checklevel <level> <mission>");
Game:AddCommand("sv_bw_add","ServerCmds:AddBadword(%line);","sv_bw_add badword");

function ServerCmds:nextMap()
	System:ExecuteCommand("sv_changemap "..tostring(getglobal("gr_NextMap")).." "..tostring(getglobal("g_GameType")));
end

function ServerCmds:forceTeamChangeById(id, team)
	local slot = Server:GetServerSlotBySSId(id);
	if slot and team then
		GameRules:HandleJoinTeamRequest(slot,team);
	else
		System:LogAlways("ServerCmds:forceTeamChangeById: invalid id");
	end
end


Game:AddCommand("sv_nextmap","MapCycle:LNextMap();","sv_nextmap");
Game:AddCommand("sv_swapteams","GameRules:DoSwitchTeams();","sv_swapteams");
Game:AddCommand("sv_chteam","ServerCmds:forceTeamChangeById(%1, %2);","sv_chteam <id> <team>");

function ServerCmds:tokenizeWithComma(str)
	local toks={}
	local cmd=gsub(str,"(%P+)",function (s) tinsert(%toks,s) end);
	toks.n=nil;
	return toks;
end

function ServerCmds:CheckBadlanguage(sText, sSender)
	local badwords = ServerCmds:tokenizeWithComma(tostring(getglobal("gr_bwk_words")));
	local nBwlimit =tonumber(getglobal("gr_bwk_limit"));
	local che=0;
	local found=0;
	local tm="";
	local slot = MultiplayerUtils:GetServerslotFromName(sSender);
	if sText and badwords then
			for a, bad in badwords do
					che=strfind(strlower(sText),strlower(bad));
					if che then
						MPStatistics:AddStatisticsDataSSId(slot:GetId(), "nBadWords", 1);
						found=found+1;
					end
			end
	end
	local nBadw = MPStatistics:GetStatisticsDataSS(slot, "nBadWords");
	if found>0 then
		slot:SendText("$4!!! BADWORDS USED "..nBadw.." of "..nBwlimit.." !!!",10);
	end
	if nBadw>nBwlimit then
		System:LogAlways("Kicked "..sSender.." for badwords");
		local pb = tonumber(getglobal("sv_punkbuster"));
		if(pb and pb==1) then
			local bwTime = tonumber(getglobal("gr_bwk_time"));
			System:ExecuteCommand("pb_sv_kick "..slot:GetName().." "..bwTime.." Badwords dude");
		else
			GameRules:Kick(sSender)
		end
	end

end

function ServerCmds:LoadBadwords()
	System:LogAlways("*** Reloading bwlist");
	local bwfile = openfile("bw.txt", "r");
	 
	if(bwfile) then
		local txt ="";
		local line = read(bwfile, "*l");
		while(line) do
			line = read(bwfile, "*l");
			if line then
				txt = txt..""..line;
			end
		end
		closefile(bwfile);
		setglobal("gr_bwk_words",txt);
	end
end

function ServerCmds:AddBadword(line)
	System:LogAlways("*** adding to bwlist");
	if line then
		local badwords = tostring(getglobal("gr_bwk_words"));
		setglobal("gr_bwk_words",badwords..""..line..",");
		ServerCmds:SaveBadwords();
	end
end

function ServerCmds:SaveBadwords()
	System:LogAlways("*** Saving bwlist");
	local bwfile = openfile("bw.txt", "w");
	local badwords = ServerCmds:tokenizeWithComma(tostring(getglobal("gr_bwk_words")));
	if(bwfile) then 
		for id, word in badwords do
			write(bwfile, word..",\n");
		end
		closefile(bwfile);
	end

end
ServerCmds:LoadBadwords();