VOTEENABLED = true

function NewMaps(userid)
	_PrintMessage(userid, 3, "Newest maps: zs_ovarycity, zs_buntshot, gm_build_noxctfnb_hai2u2")
end

MapCategory = {}
MapCategory["NoXious CTF (Build)"] = {
"gm_build_noxctf_tropical",
"gm_build_noxctf_oldremix",
"gm_build_noxctf_mountain",
--"gm_build_noxctf_snowhills",
"gm_build_noxctf_lavawars_v4",
"gm_build_noxctf_roadwars",
"gm_build_noxctf_battlefield",
"gm_build_noxctf_mountash",
"gm_build_noxctf_canyon",
"gm_build_noxctf_longrun",
"gm_build_noxctf_bigdesert2",
"gm_build_noxctf_river2",
"gm_build_noxctf_longbridge2",
"gm_build_noxctf_bigtowers",
"gm_build_noxctf_oldie",
"gm_build_fortwars_ctf_4towers3",
"gm_build_fortwars_ctf_4towers",
--"gm_build_fortwars_islands2",
--"gm_build_noxctf_urban",
"gm_build_noxctf_sanddunes2"
}

MapCategory["NoXious CTF (No Build)"] = {
"gm_build_noxctfnb_hai2u2",
"noxctfnb_broadside_v2",
"rp_build_noxctfnb_rambocity",
--"gm_build_noxctfnb_football",
"gm_nobuild_noxctf_bunkers",
"gm_build_noxctfnb_skychaos",
"gm_build_noxctfnb_2stores",
"gm_build_noxctfnb_swamp",
"gm_nobuild_noxctf_temple_v2",
"gm_nobuild_noxctf_raindance",
"gm_nobuild_noxctf_bloodgulch",
"gm_build_noxctfnb_chamber"}

MapCategory["Buggy Racer"] = {
"br_spillway",
"br_speedhax",
"br_toofuckinglong_v2",
"br_dirt",
"br_gokart",
"br_deathrally2",
"br_nightdrive",
"br_lake",
"br_river_v2",
"br_combine_v2",
"br_desert",
"br_underground_b2",
"br_egypt_v3",
"br_minikart",
"br_crisscrosscrash",
"br_figure8",
"br_nascar",
"br_toeffinlong_take2_v3",
"br_pipedream"}

MapCategory["NoXious Arena"] = {
"gm_noxarena_colosseum_v2",
"gm_noxarena_titans_b2",
"gm_noxarena_stadium_v2",
"gm_noxarena_darkarena",
"gm_noxarena_cage",
"gm_noxarena_castle",
"gm_noxarena_hell_b2"}

MapCategory["Zombie Survival"] = {
--"zs_aidstransit_v1",
"zs_ovarycity",
"zs_buntshot",
"zs_nastiesthouse",
"zs_raunchyhouse",
"zs_noir",
"zs_barrelfactory",
"zs_raunchierhouse",
"zs_raunchiesthouse",
"zs_forestofthedamned2",
"zs_nastyhouse",
"zs_nastierhouse",
"zs_cabin_v2",
"zs_asylum_v1"
}

MapCategory["Mario"] = {
"mario_castle2castle"}

MapCategory["Jet Boot Ball"] = {
"jbb_gooing_v2",
"jbb_skyballz"}

MapCategory["Top Gun 2"] = {
"topgun_bigocean",
"topgun_aircraftcarrier"}

MapCategory["The Stalker"] = {
"ts_waterworks",
"ts_steel",
"ts_constructionsite",
"ts_hdn_sewers",
"ts_subculture",
"ts_residue",
"ts_gunshot"
}

Muted = {}
for i=1, _MaxPlayers() do
	Muted[i] = false
end

BadList = {}
CorrectionsList = {}

BadList[1] = "your a "
CorrectionsList[1] = "I'm a "
BadList[2] = "your gay "
CorrectionsList[2] = "i'm bad at english "
BadList[3] = "wtf"
CorrectionsList[3] = "what the fuck"
BadList[4] = "wth"
CorrectionsList[4] = "what the hell"
BadList[5] = "your so "
CorrectionsList[5] = " I'm so "
BadList[6] = "hwo do "
CorrectionsList[6] = "how do "
BadList[7] = "omg"
CorrectionsList[7] = "oh my goodness"
BadList[8] = "omfg"
CorrectionsList[8] = "oh my fucking goodness"
BadList[9] = "stfu"
CorrectionsList[9] = "shut the fuck up"
BadList[10] = "sthu"
CorrectionsList[10] = "shut the hell up"
BadList[11] = "thats"
CorrectionsList[11] = "that's"
BadList[12] = " its "
CorrectionsList[12] = " it's "
BadList[13] = "jetbloom"
CorrectionsList[13] = " i can't read names. I fail at life. "
BadList[14] = "hes "
CorrectionsList[14] = "he's "
BadList[15] = "shes "
CorrectionsList[15] = "she's "
BadList[16] = "cant "
CorrectionsList[16] = "can't "
BadList[17] = "awsome"
CorrectionsList[17] = "awesome"
BadList[18] = "youre"
CorrectionsList[18] = "you're"
BadList[19] = "cuz "
CorrectionsList[19] = "because "
BadList[20] = " hav "
CorrectionsList[20] = " have "
BadList[21] = " urs"
CorrectionsList[22] = " yours"
BadList[23] = "i no "
CorrectionsList[24] = "I know "
BadList[25] = "fgt "
CorrectionsList[25] = "faggot "
BadList[26] = "doesnt"
CorrectionsList[26] = "doesn't"
BadList[30] = "chuck "
CorrectionsList[30] = "some "
BadList[31] = " norris"
CorrectionsList[31] = " smelly fag"
BadList[32] = "gmc"
CorrectionsList[32] = "gay manatee cultists"
BadList[33] = "gmodcentral"
CorrectionsList[33] = "gmodsluts"
BadList[34] = "gmod central"
CorrectionsList[34] = "gmod sluts"
BadList[35] = "roleplay"
CorrectionsList[35] = "radish picking"
BadList[36] = "votekick"
CorrectionsList[36] = "too bad this isn't a democracy or I would kick you"
BadList[37] = "voteban"
CorrectionsList[37] = "too bad this isn't a democracy or I would ban you"
BadList[38] = "rank "
CorrectionsList[38] = "scank "
BadList[39] = "votecop"
CorrectionsList[39] = "go to the radish farm"
BadList[40] = "melonbrew"
CorrectionsList[40] = "monkeyballs"
BadList[41] = "melon brew"
CorrectionsList[41] = "monkey balls"
BadList[42] = "jetboom"
CorrectionsList[42] = "God"
BadList[43] = "admin"
CorrectionsList[43] = "leeroy jenkins"
BadList[44] = "white"
CorrectionsList[44] = "black"
BadList[45] = "nigger"
CorrectionsList[45] = "lynch-bait"
BadList[46] = "cock"
CorrectionsList[46] = "pussy"
BadList[47] = "dick"
CorrectionsList[47] = "pussy"
BadList[48] = "gay"
CorrectionsList[48] = "straight"
BadList[49] = "faggot"
CorrectionsList[49] = "muscle man"

function VoteMapMenu(userid)
	local menu = 0
	if string.find(_GetConVar_String("hostname"), "CTF Only") ~= nil then
		menu = JBMenu.CreateMenu(false, "Vote Map", "Server #1 - CTF Only!", "255 255 255", "255 0 0")
		JBMenu.AddChoice(menu, "NoXious CTF (Build)", "", "255 255 255", VoteMapMenu2, userid, "NoXious CTF (Build)")
		JBMenu.AddChoice(menu, "NoXious CTF (No Build)", "", "255 255 255", VoteMapMenu2, userid, "NoXious CTF (No Build)")
	else
		menu = JBMenu.CreateMenu(false, "Vote Map", "Choose a gamemode.", "255 255 255", "255 0 0")
		for i in MapCategory do
			JBMenu.AddChoice(menu, i, "", "255 255 255", VoteMapMenu2, userid, i)
		end
	end
	JBMenu.SendMenu(userid, menu)
end

function VoteMapMenu2(userid, category)
	local thresh = GetNumPlayers()*0.6
	local menu = JBMenu.CreateMenu(false, "Vote Map", "Choose a map.", "255 255 255", "255 0 0")
	for i in MapCategory[category] do
		for x=1, table.getn(VoteMaps) do
			if VoteMaps[x].Name == MapCategory[category][i] then
				local tint = "255 255 255"
				if VoteMaps[x].Votes > thresh*0.75 then
					tint = "255 0 0"
				elseif VoteMaps[x].Votes > thresh*0.5 then
					tint = "255 255 0"
				elseif VoteMaps[x].Votes > 0 then
					tint = "0 255 0"
				end
				JBMenu.AddChoice(menu, VoteMaps[x].Name, VoteMaps[x].Votes.." votes.", tint, VoteMap, userid, VoteMaps[x].Name)
			end
		end
	end
	JBLastExit[userid] = _CurTime()-1
	JBMenu.SendMenu(userid, menu)
end

_OpenScript("crossserverchat.lua")

function eventPlayerSay(userid, strText, bTeam)
	if userid == 0 then 
		_PrintMessageAll(3, "[Console] JetBoom: "..string.sub(strText, 10).."\n")
		_PrintMessageAll(2, "[Console] JetBoom: "..string.sub(strText, 10).."\n")
		return ""
	end
	if Muted[userid] then
		_PlaySoundPlayer(userid, "stfu.wav")
		_PrintMessage(userid, 3, "Shut the fuck up.\n")
		return ""
	end
	if players[userid].LastMessage == strText then
		if _CurTime() > players[userid].LastChat+4 then
			players[userid].LastChat = _CurTime()
		else
			_PrintMessage(userid, 3, "Stop spamming.")
			_PlaySoundPlayer(userid, "stfu.wav")
			return ""
		end
	else
		if _CurTime() > players[userid].LastChat+1 then
			players[userid].LastChat = _CurTime()
		else
			_PrintMessage(userid, 3, "Stop spamming.")
			_PlaySoundPlayer(userid, "stfu.wav")
			return ""
		end
	end
	players[userid].LastMessage = strText
	if not bTeam then
		strText = string.sub(strText, string.len(_PlayerInfo(userid, "name"))+3)
	else
		strText = string.sub(strText, string.len(_PlayerInfo(userid, "name"))+10)
	end
	local lowertext = string.lower(strText)
	if string.sub(lowertext, 1, 9) == "givemoney" then TransferMoney(userid, string.sub(strText, 11)) return ""
	elseif lowertext == "votemap" or lowertext == "rockthevote" then VoteMapMenu(userid) return ""
	elseif string.sub(lowertext, 1, 8) == "voteskip" then VoteSkip(userid)
	elseif lowertext == "ratings" then RatingsMenu(userid) return ""
	elseif lowertext == "vote" then Vote.Vote(userid) return ""
	elseif lowertext == "newmaps" then NewMaps(userid)
	elseif lowertext == "/store" then GlobalStore(userid, "") return ""
	elseif string.sub(lowertext, 1, 4) == "/buy" then GlobalStoreBuy(userid, string.sub(strText, 6)) return ""
	elseif string.sub(lowertext, 1, 5) == "/free" then PickFree(userid, string.sub(strText, 7)) return ""
	elseif string.sub(lowertext, 1, 4) == "/csc" then CrossServerChat(userid, string.sub(strText, 6)) return ""
	elseif string.sub(lowertext, 1, 2) == "//" then CrossServerChat(userid, string.sub(strText, 4)) return ""
	elseif string.sub(lowertext, 1, 4) == "blah" then _TraceAttack(userid, userid, userid, 1500)
	elseif strText == "motd" then _player.ShowPanel(userid, "motd", true)
	elseif string.sub(lowertext, 1, 5) == "i bet" then DoBet(userid, string.gsub(string.sub(strText, 7), "$", "")) return "" end

	local founderrors = false
	for i=1, table.getn(BadList) do
		if string.find(lowertext, BadList[i]) then
			founderrors = true
			lowertext = string.gsub(lowertext, BadList[i], CorrectionsList[i])
		end
	end

	if founderrors then strText = lowertext end

	if not bTeam then
		return "The "..players[userid].Title..", ".._PlayerInfo(userid, "name")..": "..strText
	else
		return "(TEAM) The "..players[userid].Title..", ".._PlayerInfo(userid, "name")..": "..strText
	end
end

function DoBet(sender, args)
end

function TransferMoney(sender, args)
	if players[sender].DiamondMember then _PrintMessage(sender, 3, "Diamond Members don't need an economy!\n") return end
	if tonumber(args) ~= nil then args = tonumber(args) else _PrintMessage(sender, 3, "Syntax: givemoney #.\n") return end
	if players[sender].money < args then _PrintMessage(sender, 3, "You don't have that much money!\n") return end
	if args < 25 then _PrintMessage(sender, 3, "You can't give such a petty amount of money.\n") return end
	if args > 25000 then _PrintMessage(sender, 3, "For your security, limit money ammount to 25k at once.\n") return end
	_TraceLine(_PlayerGetShootPos(sender), _PlayerGetShootAng(sender), 100, sender)
	if not _TraceHitNonWorld() then _PrintMessage(sender, 3, "No player in range.\n") return end
	if _TraceGetEnt() <= _MaxPlayers() then
		local targ = _TraceGetEnt()
		if players[targ].DiamondMember then _PrintMessage(sender, 3, "Diamond Members don't need your money!\n") return end
		players[sender].money = players[sender].money-args
		players[targ].money = players[targ].money+args
		NDB.drawMoneyAmmount(sender)
		NDB.drawMoneyAmmount(targ)
		_PrintMessage(sender, 3, "You hand them $"..args..".\n")
		_PrintMessage(targ, 3, _PlayerInfo(sender,"name").." hands you $"..args..".\n")
		_EntEmitSound(sender, "ambient/levels/labs/coinslot1.wav")
		NDB.SaveInfo(sender)
		NDB.SaveInfo(targ)
		return true
	else
		_PrintMessage(sender, 3, "No player in range.\n")
		return false
	end
end
CONCOMMAND("givemoney", TransferMoney)

function VoteSkip(userid)
end

voted = {}
VoteMaps = {}
MapsAdded = {}
for i in MapCategory do
	for x=1, table.getn(MapCategory[i]) do
		if not MapsAdded[MapCategory[i][x]] then
			local y = table.getn(VoteMaps)+1
			VoteMaps[y] = {}
			VoteMaps[y].Votes = 0
			VoteMaps[y].Name = MapCategory[i][x]
			MapsAdded[MapCategory[i][x]] = true
		end
	end
end

MapsAdded = nil

function GetNumPlayers()
	local num = 0
	for i=1, _MaxPlayers() do
		if IsPlayerOnline(i) then
			num = num+1
		end
	end
	return num
end

function GetNextMap()
	local nextmap = _GetCurrentMap()
	local maxvotes = 0
	for i in VoteMaps do
		if VoteMaps[i].Votes > maxvotes then
			nextmap = VoteMaps[i].Name
			maxvotes = VoteMaps[i].Votes
		end
	end
	return nextmap
end

function VoteMap(userid, args)
	if not VOTEENABLED then _PrintMessage(userid, 3, "Voting is disabled at this time.\n") return end
	if voted[ConvertNet(_PlayerInfo(userid,"networkid"))] then _PrintMessage(userid, 3, "You already voted for a map.\n") return end
	if args == "" or args == nil then _PrintMessage(userid, 3, "You must supply a map name to vote for.\n") return end
	for i in VoteMaps do
		if VoteMaps[i].Name == args then
			voted[ConvertNet(_PlayerInfo(userid,"networkid"))] = true
			if GoldMembers[ConvertNet(_PlayerInfo(userid,"networkid"))] then
				VoteMaps[i].Votes = VoteMaps[i].Votes+1
			end
			VoteMaps[i].Votes = VoteMaps[i].Votes+1
			_PrintMessageAll(3, _PlayerInfo(userid,"name").." voted for "..VoteMaps[i].Name..". ("..VoteMaps[i].Votes.."/"..(GetNumPlayers()*0.6)..")\n")
			if VoteMaps[i].Votes >= GetNumPlayers()*0.6 then
				_PrintMessageAll(3, VoteMaps[i].Name.." has been voted for enough to change the map. Now changing...\n")
				_Msg(VoteMaps[i].Name.." has been voted for enough to change the map. Now changing...\n")
				NDB.GlobalSave()
				VOTEENABLED = false
				AddTimer(5, 1, _ServerCommand, "changelevel "..VoteMaps[i].Name.."\n")
			end
			return
		end
	end
	_PrintMessage(userid, 3, "Not a valid map name. Say maplist for a list of available maps.\n")
end
