Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include("sh_gangs.lua")
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gang Wars & Gang Bases
- util.AddNetworkString("GangBaseDialog")
- util.AddNetworkString("GangWarStartDialog")
- util.AddNetworkString("GangWarOverDialog")
- util.AddNetworkString("ds_gang_points") -- set gang points on the client
- util.AddNetworkString("set_gang_news") -- set gang news msg from client
- util.AddNetworkString("sv_refresh_topgangs")
- GangWars.GangBases = {}
- GangWars.GangWars = {}
- local meta = FindMetaTable("Player")
- function meta:GetGangBase()
- return GangWars.GangBases[self:Gang()]
- end
- local function GiveGangMembersXP(gang, xp)
- for k, v in pairs(player.GetAll()) do
- if v:Gang() == gang then
- GangWars.AddXP(v, xp)
- end
- end
- end
- GangWars.GiveGangMembersXP = GiveGangMembersXP
- -- this function will give a % of required XP
- local function GiveGangMembersXPAuto(gang, perc)
- for k, v in pairs(player.GetAll()) do
- if v:Gang() == gang then
- local lvl = GangWars.GetLevel(v) + v:Tier()
- local xp2l = GangWars.GetXP2L(lvl)
- local xp = math.floor((xp2l / 100) * (perc or 50))
- GangWars.AddXP(v, xp, true)
- end
- end
- end
- GangWars.GiveGangMembersXPAuto = GiveGangMembersXPAuto
- local function ServerHasGangVariance()
- local gangs = {}
- for k, v in pairs(player.GetAll()) do
- if v:Gang() ~= "" then
- gangs[v:Gang()] = 1
- end
- end
- return table.Count(gangs) > 1
- end
- local MIN_TIME_BETWEEN_GANG_BASES = (3600 + 1800)
- function GangWars.SetGangBase(ply, base_building)
- local gang = ply:Gang()
- local bb = GangWars.FindBuildingByID(base_building)
- -- is gang ok?
- if gang == "" then
- Notify(ply, NOTIFY_ERROR, 4, "You are not in a gang")
- return
- end
- -- is points ok?
- if not ply.GangInfo or not ply.GangInfo.points or ply.GangInfo.points < 1000 then
- Notify(ply, NOTIFY_ERROR, 4, "You need at least 1,000 points to make a Gang Base")
- return
- end
- -- is this building inside a territory owned by our gang?
- local building_is_in_territory = false
- local territory = nil
- for k, v in pairs(GangWars.GangTerritories) do
- if v:ObjectIsInside(bb) and v.owner and v.owner == gang then
- building_is_in_territory = true
- territory = v
- break
- end
- end
- if not building_is_in_territory then
- Notify(ply, NOTIFY_ERROR, 4, "You can only make a gang base within territory you own")
- return
- end
- -- can we base?
- isql:QueryValue("SELECT TIME_TO_SEC(TIMEDIFF(NOW(), `last_base`)) FROM `gw_gangs` WHERE `name` = " .. isql:Escape(gang), function(timediff)
- -- is base ok?
- if not bb or not bb.bdata or not bb.capture_point then
- GangNotify(ply, NOTIFY_ERROR, 4, "You can't make a gang base here - " .. base_building)
- --MsgN("bb = " .. tostring(bb))
- return
- end
- -- are we at war?
- if GangWars.GangWars[gang] then
- GangNotify(ply, NOTIFY_ERROR, 4, "You can't change base while at war!")
- return
- end
- -- do we have access?
- if ply:GetNW2Int("gang_rank") > 2 then
- GangNotify(ply, NOTIFY_ERROR, 4, "Gang Base can only be set by leader & vice leader")
- return
- end
- -- is there already a base set? compare access levels
- if GangWars.GangBases[gang] and IsValid(GangWars.GangBases[gang].owner) and GangWars.GangBases[gang].owner:GetNW2Int("gang_rank") < ply:GetNW2Int("gang_rank") then
- GangNotify(ply, NOTIFY_ERROR, 4, "Your gang already has a base set")
- return
- end
- -- Cancel / Un-set base
- if GangWars.GangBases[gang] and GangWars.GangBases[gang].base == base_building then
- NotifyGang(ply, NOTIFY_ERROR, 4, "Your Gang Base has been un-set. You have no base!")
- -- update building doors
- for k, v in pairs(bb.doors) do
- v:SetNW2String("gang", "")
- end
- -- set data
- GangWars.GangBases[gang] = nil
- return
- end
- -- timediff is the number of seconds since we last based. it must be at least 60 mins.
- -- is time ok?
- if not timediff or timediff < MIN_TIME_BETWEEN_GANG_BASES then
- GangNotify(ply, NOTIFY_ERROR, 4, "You can't make a gang base for " .. math.floor((MIN_TIME_BETWEEN_GANG_BASES - (timediff or 60)) / 60) .. " minutes")
- return
- end
- local newbase = GangWars.GangBases[gang] == nil and false or true
- -- set database entry for last base
- isql:Query("UPDATE `gw_gangs` SET `last_base` = NOW() WHERE `name` = " .. isql:Escape(gang))
- -- set base entry
- GangWars.GangBases[gang] = {
- owner = ply,
- gang = gang,
- base = base_building,
- territory = territory
- }
- -- set a gangbase flag here. this is used in UnOwn() in entity.lua to automaticly unset the GangBase entry, capture points use it too.
- local building = bb
- building.GangBase = gang
- --[[
- // create the points tick timer
- timer.Create("GangBase" .. gang, 60, 0, function()
- if GangWars.GangBases[gang] then
- // how many points should they earn? 50 base
- local points = 50
- // launderers, printers, forges boost points by 5
- local contraband_owned_by_gang = 0;
- // count contraband which are plugged in, whos owner is in this gang and are a reasonable distance from base
- local counters = { "money_printer", "money_launderer", "material_forge" }
- for x, y in pairs(counters) do
- for k,v in pairs(ents.FindByClass(y) or {}) do
- if v:GetNW2Bool("connected") and v:GetNWEntity("owning_ent"):Gang() == gang and v:GetPos():DistToSqr( building:GetPos() ) < 1000000 then
- contraband_owned_by_gang = contraband_owned_by_gang + 1
- end
- end
- end
- // boost points - +5 per contraband
- points = points + contraband_owned_by_gang * 5
- if table.Count(player.GetAll()) < 10 or not ServerHasGangVariance() then
- NotifyGang(gang, NOTIFY_ERROR, 4, "No Gang Points with less than 10 people / no other Gangs on the server")
- else
- GangWars.DB.AddGangPoints( gang, points, function() end )
- NotifyGang(gang, NOTIFY_ERROR, 4, "Your base earned +" .. points .. " Gang Points")
- end
- else
- timer.Remove("GangBase" .. gang)
- end
- end)
- ]]
- -- update clients? not needed because they only need to know this during war?
- -- update building doors
- for k, v in pairs(building.doors) do
- v:SetNW2String("gang", gang)
- end
- -- tell gang members
- if newbase then
- net.Start("GangBaseDialog")
- net.Send(ply)
- NotifyGang(gang, 4, 4, "Your Gang Base is set to " .. bb.bdata.name, true)
- else
- NotifyGang(gang, 4, 4, "Your Gang Base changed to " .. bb.bdata.name, true)
- end
- end)
- end
- AddChatCommand("/base", function(ply)
- local trace = ply:GetEyeTrace()
- if IsValid(trace.Entity) and trace.Entity:IsOwnable() and trace.Entity:IsDoor() and ply:GetPos():Distance(trace.Entity:GetPos()) < 200 then
- if RPArrestedPlayers[ply:SteamID()] then
- Notify(ply, 1, 5, "You can't do that while arrested!")
- return ""
- end
- if trace.Entity:GetNW2Bool("nonOwnable") or trace.Entity:GetNW2Bool("BankerOwnable") then
- Notify(ply, 1, 5, "This door can not be owned")
- return ""
- end
- if not trace.Entity:OwnedBy(ply) then
- Notify(ply, 1, 5, "You don't own this building")
- return ""
- end
- GangWars.SetGangBase(ply, trace.Entity.building.bdata.id)
- end
- return ""
- end)
- -- This is fking lazy and should be rewritten using a proper net message
- function GangWars.ClientUpdateGangWarData(ply)
- local war = GangWars.GangWars[ply:Gang()]
- if war then
- if war.role == "attacking" then
- local ebase = GangWars.GangBases[war.rival]
- ply:SendLua("GangWars.GangWar = { role = '" .. war.role .. "', rival = '" .. war.rival .. "', base = '" .. ebase.base .. "', tokens = " .. war.tokens .. " }")
- else
- local base = GangWars.GangBases[war.gang]
- -- I've seen here there be no base entry! So we're getting an update on a war where the defense no longer has a base? def should lose?
- if not base then
- GangWars.FinishGangWar(war.rival)
- return
- end
- ply:SendLua("GangWars.GangWar = { role = '" .. war.role .. "', rival = '" .. war.rival .. "', base = '" .. base.base .. "', tokens = " .. GangWars.GangWars[war.rival].tokens .. " }")
- end
- else
- ply:SendLua("GangWars.GangWar = nil")
- end
- end
- --AddChatCommand("/testwar", function(ply, args)
- -- GangWars.StartGangWar( ply, ply:Gang() )
- -- return ""
- --end)
- AddChatCommand("/war", function(ply, args)
- local trace = ply:GetEyeTrace()
- if IsValid(trace.Entity) and trace.Entity:IsPlayer() then
- GangWars.StartGangWar(ply, trace.Entity:Gang())
- elseif IsValid(trace.Entity) and trace.Entity.building and trace.Entity.building.bdata and trace.Entity.building.bdata.id and trace.Entity.building.GangBase then
- -- search for a gang base at this ID
- for k, v in pairs(GangWars.GangBases) do
- if v.base == trace.Entity.building.bdata.id then
- GangWars.StartGangWar(ply, v.gang)
- return ""
- end
- end
- end
- return ""
- end)
- function GangWars.StartGangWar(ply, rival)
- local gang = ply:Gang()
- -- TODO: check not fighting ourself
- if gang == "" or rival == gang then return end --GangNotify(ply, NOTIFY_ERROR, 4, "") -- check not at war
- if GangWars.GangWars[rival] then
- GangNotify(ply, NOTIFY_ERROR, 4, rival .. " are already at war")
- return
- end
- -- check if we're already fighting?
- if GangWars.GangWars[gang] then
- GangNotify(ply, NOTIFY_ERROR, 4, "You are already fighting " .. GangWars.GangWars[gang].rival .. "")
- return
- end
- -- do we have access?
- --if ply:GetNW2Int("gang_rank") > 2 then
- -- GangNotify(ply, NOTIFY_ERROR, 4, "Gang War can only be started by leader or vice leader")
- -- return
- --end
- -- check we have bases
- if not GangWars.GangBases[rival] then
- GangNotify(ply, NOTIFY_ERROR, 4, rival .. " do not have a gang base")
- return
- end
- -- check their base is not on protection
- if GangWars.GangBases[rival].protected and GangWars.GangBases[rival].protected > CurTime() then
- local ptime = math.max(1, math.floor((GangWars.GangBases[rival].protected - CurTime()) / 60))
- GangNotify(ply, NOTIFY_ERROR, 4, rival .. " cannot be raided! Wait " .. ptime .. " minutes")
- return
- end
- --if GangWars.GangBases[gang] then
- -- GangNotify(ply, NOTIFY_ERROR, 4, "You don't have a Gang Base! Buy a building, press F2 on a door and click 'Set Gang Base'" )
- -- return
- --end
- -- check war time limit
- -- verify time is ok
- isql:QueryValue("SELECT TIME_TO_SEC(TIMEDIFF(NOW(), time)) FROM `gw_time` WHERE name = " .. isql:Escape("gang_war_" .. ply:Gang()) .. ";", function(time)
- local war_time = (60 * 60 * 6)
- if time and tonumber(time) < war_time then
- GangNotify(ply, NOTIFY_ERROR, 4, "You can only start a war once every 3 hours! Wait " .. math.floor((war_time - tonumber(time)) / 60) .. " minutes")
- return
- end
- isql:Query("UPDATE `gw_time` SET `time` = NOW() WHERE name = " .. isql:Escape("gang_war_" .. ply:Gang()) .. ";", function(d, s, err)
- -- decide tokens by gang member count
- local membercount = 0
- for k, v in pairs(player.GetAll()) do
- if v:Gang() == gang then
- membercount = membercount + 1
- end
- end
- -- add war entires for both gangs
- GangWars.GangWars[gang] = {
- gang = gang,
- rival = rival,
- role = "attacking",
- tokens = membercount * 3
- }
- GangWars.GangWars[rival] = {
- gang = rival,
- rival = gang,
- role = "defending",
- tokens = membercount * 3
- }
- -- update all clients in-game that war is active & starting now
- for k, v in pairs(player.GetAll()) do
- if v:Gang() == gang then
- GangWars.ClientUpdateGangWarData(v)
- TimerNotify(v, 60 * 30, "Attack " .. rival)
- net.Start("GangWarStartDialog")
- net.WriteString("attacking")
- net.WriteString(rival)
- net.Send(v)
- end
- if v:Gang() == rival then
- GangWars.ClientUpdateGangWarData(v)
- TimerNotify(v, 60 * 30, "Defend from " .. gang)
- net.Start("GangWarStartDialog")
- net.WriteString("defending")
- net.WriteString(gang)
- net.Send(v)
- end
- end
- -- create timer to end the war in favour of the defenders
- timer.Create("GangWar" .. gang, 60 * 30, 1, function()
- if GangWars.GangWars[rival] then
- GangWars.FinishGangWar(rival)
- end
- end)
- -- use traditional notify for console message log
- NotifyGang(gang, 4, 4, ply:Nick() .. " declared war on " .. rival, true)
- NotifyGang(rival, 4, 4, ply:Nick() .. " of " .. gang .. " declared war on our gang!", true)
- -- log
- GangWars.GangLog(gang, rival, "War", ply, ply:Nick() .. " declared war on " .. rival)
- end)
- end)
- end
- -- catch race conditions
- local FinishWarLimit = {}
- function GangWars.FinishGangWar(win)
- local winner = table.Copy(GangWars.GangWars[win])
- local loser = table.Copy(GangWars.GangWars[winner.rival])
- if not winner or not loser then
- MsgN("Error: couldn't find winner or loser in gang war? Winner was '" .. win .. "'")
- GangWars.Log("error", "", "Couldn't find winner or loser in gang war? Winner was '" .. win .. "'")
- return
- end
- local wl = winner.gang .. loser.gang
- if FinishWarLimit[wl] and FinishWarLimit[wl] > CurTime() then return end
- FinishWarLimit[wl] = CurTime() + 300
- -- determine points
- GangWars.DB.GetGangPoints(loser.gang, function(points)
- if not points then
- MsgN("Error with Gang Points?")
- GangWars.Log("error", "", "Finishing gang war couldn't find loser points for '" .. loser.gang .. "'")
- return
- end
- local defenders = GangWars.GangWars[winner.gang].role == "defending" and GangWars.GangWars[winner.gang] or GangWars.GangWars[loser.gang]
- -- DEFENSE LOSES - LOSE 20% of points
- if GangWars.GangWars[loser.gang].role == "defending" then
- points = math.floor((points / 100) * 20)
- local winpoints = math.floor(points / 2)
- -- change points
- GangWars.DB.AddGangPoints(winner.gang, winpoints, function() end)
- GangWars.DB.AddGangPoints(loser.gang, -points, function() end)
- -- member XP
- GiveGangMembersXPAuto(winner.gang, 60)
- -- notify
- NotifyGang(winner.gang, 4, 4, "You won the war vs " .. loser.gang .. ", winning " .. winpoints .. " points", true)
- NotifyGang(loser.gang, 4, 4, "You lost the war vs " .. winner.gang .. ", losing " .. points .. " points", true)
- -- defense should lose the base!
- if GangWars.GangBases[defenders.gang] then
- -- find base
- local bb = GangWars.FindBuildingByID(GangWars.GangBases[defenders.gang].base)
- -- update building doors
- for k, v in pairs(bb.doors) do
- v:SetNW2String("gang", "")
- end
- -- set data
- GangWars.GangBases[defenders.gang] = nil
- end
- -- DEFENSE WINS - GAIN BONUS POINTS
- else
- points = 25000
- -- member XP
- GiveGangMembersXPAuto(winner.gang, 40)
- -- change points
- GangWars.DB.AddGangPoints(winner.gang, points, function() end)
- -- notify
- NotifyGang(winner.gang, 4, 4, "You defended against " .. loser.gang .. ", and gained +" .. points .. " bonus points", true)
- NotifyGang(loser.gang, 4, 4, "You lost the war vs " .. winner.gang, true)
- -- set the base of the defense to protected status
- if GangWars.GangBases[defenders.gang] then
- GangWars.GangBases[defenders.gang].protected = CurTime() + 60 * 40
- end
- end
- -- update wars won status
- isql:Query("UPDATE `gw_gangs` SET `wars_won` = `wars_won` + 1 WHERE `name` = " .. isql:Escape(winner.gang))
- isql:Query("UPDATE `gw_gangs` SET `wars_lost` = `wars_lost` + 1 WHERE `name` = " .. isql:Escape(loser.gang))
- -- update wars won status - defending gang lost
- if GangWars.GangWars[loser.gang].role == "defending" then
- isql:Query("UPDATE `gw_gangs` SET `att_won` = `att_won` + 1 WHERE `name` = " .. isql:Escape(winner.gang))
- isql:Query("UPDATE `gw_gangs` SET `def_lost` = `def_lost` + 1 WHERE `name` = " .. isql:Escape(loser.gang))
- else
- isql:Query("UPDATE `gw_gangs` SET `def_won` = `def_won` + 1 WHERE `name` = " .. isql:Escape(winner.gang))
- isql:Query("UPDATE `gw_gangs` SET `att_lost` = `att_lost` + 1 WHERE `name` = " .. isql:Escape(loser.gang))
- end
- -- log
- GangWars.Log("gangwar", "", winner.gang .. " won a war vs " .. loser.gang)
- -- kill timers (only 1 will actually exist here)
- timer.Remove("GangWar" .. winner.gang)
- timer.Remove("GangWar" .. loser.gang)
- FinishWarLimit[wl] = CurTime() + 300
- -- clear data
- GangWars.GangWars[winner.gang] = nil
- GangWars.GangWars[loser.gang] = nil
- -- show dialogs
- for k, v in pairs(player.GetAll()) do
- if v:Gang() == winner.gang then
- net.Start("GangWarOverDialog")
- net.WriteString(loser.gang)
- net.WriteString("win")
- net.WriteInt(points, 32)
- net.Send(v)
- TimerNotifyClear(v)
- v:SendLua("GangWars.GangWar = nil")
- elseif v:Gang() == loser.gang then
- net.Start("GangWarOverDialog")
- net.WriteString(winner.gang)
- net.WriteString("lose")
- net.WriteInt(0, 32)
- net.Send(v)
- TimerNotifyClear(v)
- --GangWars.ClientUpdateGangWarData( v )
- v:SendLua("GangWars.GangWar = nil")
- end
- end
- -- log in ganglog
- GangWars.GangLog(winner.gang, "", "War Won", "none")
- GangWars.GangLog(loser.gang, "", "War Lost", "none")
- end)
- end
- -- On player spawn, check their gang war info is set
- hook.Add("PlayerSpawn", function(ply)
- GangWars.ClientUpdateGangWarData(ply)
- end)
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gang News
- net.Receive("set_gang_news", function(len, ply)
- local gang = ply:Gang()
- local news = net.ReadString()
- -- sanitize the news
- news = string.Replace(news, '<', '<')
- news = string.Replace(news, '>', '>')
- news = string.Replace(news, "\\n", '<br/>')
- GangWars.DB.GetPlayerGangRank(ply, function(rank)
- if rank and rank == 1 then
- -- Set news
- isql:Query("UPDATE `gw_gangs` SET `message` = " .. isql:Escape(news) .. " WHERE `name` = " .. isql:Escape(gang), function()
- -- ensure we're updated
- Gang.SendInfo(ply, ply:Gang())
- end)
- else
- GangNotify(ply, NOTIFY_ERROR, 4, "Only the leader can set the news")
- end
- end)
- end)
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gang Log
- function GangWars.GangLog(gang, log, event, member)
- event = event or "none"
- if type(member) == "Player" and IsValid(member) then
- member = member:SteamID()
- end
- member = member or "none"
- isql:Query("INSERT INTO `gw_gang_log` (`gang`, `date`, `log`, `event`, `member`) VALUES(" .. isql:Escape(gang) .. ", NOW(), " .. isql:Escape(log) .. ", " .. isql:Escape(event) .. ", " .. isql:Escape(member) .. ")")
- end
- function GangWars.GangLogNumber(gang, number, event, member, log)
- event = event or "none"
- if IsValid(member) and member:IsPlayer() then
- member = member:SteamID()
- end
- member = member or "none"
- if log then
- isql:Query("INSERT INTO `gw_gang_log` (`gang`, `date`, `number`, `event`, `member`, `log`) VALUES(" .. isql:Escape(gang) .. ", NOW(), " .. isql:Escape(number) .. ", " .. isql:Escape(event) .. ", " .. isql:Escape(member) .. ", " .. isql:Escape(log) .. ")")
- else
- isql:Query("INSERT INTO `gw_gang_log` (`gang`, `date`, `number`, `event`, `member`) VALUES(" .. isql:Escape(gang) .. ", NOW(), " .. isql:Escape(number) .. ", " .. isql:Escape(event) .. ", " .. isql:Escape(member) .. ")")
- end
- end
- function meta:GangLogRefresh()
- if not self.GangInfo or not self.GangInfo.name or self.GangInfo.name == "" then
- self:SetDataset("gang_log", {})
- else
- self:SetDatasetQuery("gang_log", "SELECT DATE_FORMAT(`date`, '%D @ %H:%i:%s') AS `date`, `log` FROM `gw_gang_log` WHERE `gang` = " .. isql:Escape(self.GangInfo.name) .. " ORDER BY `date` DESC LIMIT 1000;")
- end
- end
- -- remove older entries than 24*7 hours from the gang log
- timer.Create("PruneGangLogs", 60 * 120, 0, function()
- isql:Query("DELETE FROM `gw_gang_log` WHERE TIME_TO_SEC(TIMEDIFF(NOW(), `date`)) > 604800")
- end)
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gamemode Hooks
- local doonce = false
- local function GangInitTerritories()
- if doonce then return end
- doonce = true
- --[[
- for _, t in ipairs( GangTerritory ) do
- if t.map == string.lower(game.GetMap()) then
- local ter = ents.Create("gang_territory")
- ter:SetPos(t.pos)
- // Territory network ints
- ter:SetNW2String("territory_owned", "") // is it owned?
- ter:SetNW2Int("territory_id", _) // array index: GangTerritory[idx]
- ter.captime = t.captime
- ter.master = t.master
- ter.territory = t
- GangTerritory[_].ent = ter
- ter:Spawn()
- end
- end
- ]]
- -- Make the territory map in spawn
- local tel = ents.Create("gw_territory_map")
- tel:SetPos(GetNamedSpawn("spawn_map"))
- tel:Spawn()
- if GetMapName() == "gw_downtown" then
- tel:SetAngles(Angle(90, 90, 0))
- else
- tel:SetAngles(Angle(90, 90, 0))
- end
- -- Make the top gangs display in spawn
- local tel = ents.Create("gw_top_gangs")
- tel:SetPos(GetNamedSpawn("spawn_topgangs"))
- tel:Spawn()
- tel:SetAngles(Angle(0, 0, 90))
- -- HACK NPC's
- --local tel = ents.Create("npc_gunsmith")
- --tel:SetPos( GetNamedSpawn("gundealer") )
- --tel:Spawn()
- --local tel = ents.Create("npc_blacksmith")
- --tel:SetPos( GetNamedSpawn("blacksmith") )
- --tel:SetAngles(Angle(0,270,0))
- --tel:Spawn()
- --local tel = ents.Create("npc_armor")
- --tel:SetPos( GetNamedSpawn("armorsmith") )
- --tel:SetAngles(Angle(0,270,0))
- --tel:Spawn()
- local tel = ents.Create("npc_banker")
- tel:SetPos(GetNamedSpawn("banker"))
- tel:SetAngles(Angle(2.007624, -1.220685, 0.000000)) --UPTOWN
- --tel:SetAngles(Angle(0.160586, -90.368736, 0.000000)) --DOWNTOWN
- tel:Spawn()
- --[[
- local tel = ents.Create("npc_refiner")
- tel:SetPos( GetNamedSpawn("refiner") )
- tel:SetAngles(Angle(0,90,0))
- tel:Spawn()
- ]]
- local tel = ents.Create("npc_pothead")
- tel:SetPos(GetNamedSpawn("pothead"))
- tel:SetAngles(Angle(1.445511, -134.679184, 0.000000)) --UPTOWN
- --tel:SetAngles(Angle(-2.007504, 3.501955, 0.000000)) --DOWNTOWN
- tel:Spawn()
- local tel = ents.Create("npc_pothead")
- tel:SetPos(GetNamedSpawn("smackhead"))
- tel:SetAngles(Angle(0.240853, 178.907089, 0.000000)) --UPTOWN
- --tel:SetAngles(Angle(-2.007504, 3.501955, 0.000000)) --DOWNTOWN
- tel:Spawn()
- end
- hook.Add("InitPostEntity", "GangEntInit", GangInitTerritories)
- -- Handle gang rivalry kills
- local function GangRivalryDeath(victim, weapon, killer)
- -- if the killer is on a mass murder mission, cancel all affects & knock out
- if IsValid(victim) and IsValid(killer) and killer.MissionKills then return end
- if victim:IsPlayer() and killer:IsPlayer() and killer ~= victim and GangWars.GetLevel(victim) >= 10 then
- local vgang = victim:GetNW2String("gang_name")
- local kgang = killer:GetNW2String("gang_name")
- if vgang == "" or kgang == "" then return end
- -- see if there is a rivalry between killer and victim
- isql:QueryValue("SELECT COUNT(*) FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(kgang) .. " AND `rival` = " .. isql:Escape(vgang) .. ";", function(v)
- if not v or tonumber(v) < 1 then return end
- -- calculate the fine the other gang will pay
- local vlvl = GangWars.GetLevel(victim) / 2
- local fine = (vlvl * vlvl) * 10
- -- upgrade adds to the fine
- if killer.GangInfo then
- if killer.GangInfo.rivalboost and killer.GangInfo.rivalboost > 0 then
- local txt, aff = Gang.GetUpgradeEffect("rivalboost", killer.GangInfo.rivalboost - 1)
- fine = fine + math.floor((fine / 100) * aff)
- end
- end
- fine = math.floor(fine)
- --[[
- // add cash to gang stash
- // !!!!!!!!!!!!!!!
- local stashes = ents.FindByClass("gang_stash")
- if stashes then
- for k,v in pairs(stashes) do
- if v.gang == kgang then
- v:AddMoney() // adds % to stash
- end
- end
- end
- // !!!!!!!!!!!!!!!!!!!!
- ]]
- -- check they can afford it
- GangWars.DB.GangCanAfford(vgang, fine, function(canafford)
- if canafford then
- -- fine the gang
- GangWars.DB.AddGangMoney(vgang, -fine, function()
- NotifyGang(vgang, 4, 4, "Gang lost " .. CUR .. GangWars.FormatMoney(fine) .. " when " .. victim:Nick() .. " died")
- NotifyGang(kgang, 4, 4, "Gang gained " .. CUR .. GangWars.FormatMoney(fine) .. " for killing " .. victim:Nick())
- -- update rivalry stats
- isql:Query("UPDATE `gw_gang_rivals` SET `members_killed` = `members_killed` + 1 WHERE `gang` = " .. isql:Escape(kgang) .. " AND `rival` = " .. isql:Escape(vgang) .. ";")
- isql:Query("UPDATE `gw_gang_rivals` SET `members_lost` = `members_lost` + 1 WHERE `gang` = " .. isql:Escape(vgang) .. " AND `rival` = " .. isql:Escape(kgang) .. ";")
- -- log
- if IsValid(killer) then
- GangWars.GangLog(kgang, IsValid(victim) and victim:Nick() or "unknown", "KillRival", killer)
- end
- -- add XP and money
- GangWars.DB.AddGangMoney(kgang, fine)
- GangWars.DB.AddGangXP(kgang, fine / 10)
- end)
- else
- -- force the bankrupt gang to lose
- GangWars.DB.RivalryWin(kgang, vgang)
- end
- end)
- end)
- end
- end
- hook.Add("PlayerDeath", "GangRivalryDeath", GangRivalryDeath)
- --////////////////////////////////////////////////////////////////////////////
- -- Database
- local function ValidSteam(ply)
- if not IsValid(ply) then return end
- local steamID = ply:SteamID()
- --if steamID == "STEAM_ID_PENDING" or steamID == "STEAM_ID_UNKNOWN" or steamID == "UNKNOWN" or steamID == "PENDING" then
- -- return
- --end
- return steamID
- end
- -- gets the name of the gang a player is in
- function GangWars.DB.GetPlayerGang(ply, func, show_invites)
- local steamID = ""
- if type(ply) == "string" then
- steamID = ply
- else
- steamID = ValidSteam(ply)
- end
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- if show_invites then
- isql:QueryValue("SELECT `gang` FROM `gw_gang_members` WHERE `steam` = " .. isql:Escape(steamID) .. ";", function(r)
- if r then
- func(tostring(r))
- else
- func(nil)
- end
- end)
- else
- isql:QueryValue("SELECT `gang` FROM `gw_gang_members` WHERE `steam` = " .. isql:Escape(steamID) .. " AND `rank` < 6;", function(r)
- if r then
- func(tostring(r))
- else
- func(nil)
- end
- end)
- end
- end
- -- gets a players rank within their gang
- function GangWars.DB.GetPlayerGangRank(ply, func)
- local steamID = ""
- if type(ply) == "string" then
- steamID = ply
- else
- steamID = ValidSteam(ply)
- end
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- isql:QueryValue("SELECT `rank` FROM `gw_gang_members` WHERE `steam` = " .. isql:Escape(steamID) .. " AND `rank` < 6;", function(r)
- if r then
- func(tonumber(r))
- else
- func(nil)
- return
- end
- end)
- end
- -- returns gang name, xp, level, money
- function GangWars.DB.GetPlayerGangInfo(ply, func)
- local steamID = ""
- if type(ply) == "string" then
- steamID = ply
- else
- steamID = ValidSteam(ply)
- end
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- isql:QueryRow("SELECT * FROM `gw_gangs` INNER JOIN `gw_gang_members` ON gw_gang_members.gang = gw_gangs.name WHERE gw_gang_members.steam = " .. isql:Escape(steamID) .. ";", func)
- end
- -- returns gang name, xp, level, money
- function GangWars.DB.GetGangInfo(gang_name, func)
- isql:QueryRow("SELECT * FROM `gw_gangs` WHERE `name` = " .. isql:Escape(gang_name) .. ";", func)
- end
- -- gets the list of members of a gang
- function GangWars.DB.GetGangStorage(gang_name, func)
- isql:Query("SELECT * FROM `view_gang_storage` WHERE `gang` = " .. isql:Escape(gang_name) .. ";", func)
- end
- -- gets the list of members of a gang
- function GangWars.DB.GetPlayerGangMemberList(gang_name, func)
- isql:Query("SELECT `names`.`name` AS `name`, `members`.`rank` AS `rank`, `names`.`steam` AS `steam` FROM `gw_gang_members` `members` INNER JOIN `gw_characters` `names` ON `names`.`name` = `members`.`steam` WHERE `members`.`gang` = " .. isql:Escape(gang_name) .. ";", func)
- end
- -- gets the list of members + stats in a gang. takes a unique ID for temp table
- function GangWars.DB.GetGangMemberStats(gang_name, uid, func)
- local stats = {}
- local q = "SELECT * FROM `gw_gang_members` WHERE `gang` = " .. isql:Escape(gang_name) .. ";"
- isql:Query(q, function(da, su, er)
- if su then
- for k, v in pairs(da or {}) do
- if not stats[v["steam"]] then
- stats[v["steam"]] = {}
- stats[v["steam"]]["rank"] = v["rank"]
- stats[v["steam"]]["money_donated"] = 0
- stats[v["steam"]]["money_earned"] = 0
- stats[v["steam"]]["money_spent"] = 0
- stats[v["steam"]]["xp_earned"] = 0
- stats[v["steam"]]["captures"] = 0
- stats[v["steam"]]["wars"] = 0
- stats[v["steam"]]["invites"] = 0
- stats[v["steam"]]["rivals"] = 0
- end
- end
- end
- q = "SELECT * FROM `gw_gang_log` WHERE `date` >= NOW() - INTERVAL 7 DAY and `gang` = " .. isql:Escape(gang_name) .. ";"
- isql:Query(q, function(d, s, e)
- if s then
- for k, v in pairs(d or {}) do
- if not stats[v["member"]] then continue end
- if v["event"] == "Invite" then
- stats[v["member"]]["invites"] = stats[v["member"]]["invites"] + 1
- end
- if v["event"] == "Donate" then
- stats[v["member"]]["money_donated"] = stats[v["member"]]["money_donated"] + v["number"]
- end
- if v["event"] == "Buy" then
- stats[v["member"]]["money_spent"] = stats[v["member"]]["money_spent"] + v["number"]
- end
- if v["event"] == "Upgrade" then
- stats[v["member"]]["money_spent"] = stats[v["member"]]["money_spent"] + v["number"]
- end
- if v["event"] == "Print" then
- stats[v["member"]]["money_earned"] = stats[v["member"]]["money_earned"] + v["number"]
- end
- if v["event"] == "XP" then
- stats[v["member"]]["xp_earned"] = stats[v["member"]]["xp_earned"] + v["number"]
- end
- if v["event"] == "Capture" then
- stats[v["member"]]["captures"] = stats[v["member"]]["captures"] + 1
- end
- if v["event"] == "War" then
- stats[v["member"]]["wars"] = stats[v["member"]]["wars"] + 1
- end
- if v["event"] == "KillRival" then
- stats[v["member"]]["rivals"] = stats[v["member"]]["rivals"] + 1
- end
- end
- end
- func(stats)
- end)
- end)
- end
- -- gets the list of rivals of a gang
- function GangWars.DB.GetGangRivalList(gang_name, func)
- isql:Query("SELECT g.name, g.logo, g.color, g.color2, g.level, riv.*, TIME_TO_SEC(TIMEDIFF(NOW(), riv.`started`))/60/60 AS `hours_left` FROM `gw_gang_rivals` riv INNER JOIN `gw_gangs` g ON g.name = riv.rival WHERE riv.gang = " .. isql:Escape(gang_name) .. ";", func)
- end
- function GangWars.DB.CreateGang(ply, gang_name, gang_password, gang_logo, gang_color, gang_color2, func)
- local steamID = ValidSteam(ply)
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- -- reject bad gang names
- local allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:-1234567890 "
- for i = 1, string.len(gang_name) do
- local ok = false
- for j = 1, string.len(allowedCharacters) do
- if string.sub(gang_name, i, i) == string.sub(allowedCharacters, j, j) then
- ok = true
- break
- end
- end
- if not ok then
- func(nil, "Gang name contains illegal symbol at character #" .. i)
- return
- end
- end
- -- verify time is ok
- isql:QueryValue("SELECT TIME_TO_SEC(TIMEDIFF(NOW(), time)) FROM `gw_time` WHERE name = " .. isql:Escape("gang_create_" .. ply:RealSteamID()) .. ";", function(time)
- if time and tonumber(time) < 86400 then
- func(nil, "You can only make 1 gang per day")
- return
- end
- isql:Query("UPDATE `gw_time` SET `time` = NOW() WHERE name = " .. isql:Escape("gang_create_" .. ply:RealSteamID()) .. ";", function(d, s, err)
- -- check gang name doesn't exist
- isql:QueryValue("SELECT * FROM gw_gangs WHERE name = " .. isql:Escape(gang_name) .. ";", function(r)
- if r then
- func(nil, "Gang already exists!")
- return
- end
- -- check we're not already in a gang
- local g = GangWars.DB.GetPlayerGang(ply, function(g)
- if g and g ~= "" then
- func(nil, "You are already in a gang")
- return
- end
- -- delete any old invites we had
- isql:Query("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. ";", function()
- -- create gang
- isql:Query("INSERT INTO gw_gangs (`name`, `password`, `logo`, `color`, `color2`, `level`, `xp`, `money`, `creator`, `last_base`) VALUES(" .. isql:Escape(gang_name) .. ", " .. isql:Escape(gang_password) .. ", " .. isql:Escape(gang_logo) .. ", '" .. isql:Escape(gang_color.r) .. " " .. isql:Escape(gang_color.g) .. " " .. isql:Escape(gang_color.b) .. "', '" .. isql:Escape(gang_color2.r) .. " " .. isql:Escape(gang_color2.g) .. " " .. isql:Escape(gang_color2.b) .. "', 1, 0, 0, " .. isql:Escape(steamID) .. ", NOW());", function()
- -- and owner
- isql:Query("INSERT INTO gw_gang_members (`gang`, `steam`, `rank`) VALUES(" .. isql:Escape(gang_name) .. ", " .. isql:Escape(steamID) .. ", 1);", function()
- func(true)
- net.Start("sv_refresh_topgangs")
- net.Broadcast()
- end)
- end)
- end)
- end)
- end)
- end)
- end)
- end
- function GangWars.DB.SetPlayerGangRank(ply, gang, rank, func)
- local steamID = ""
- if type(ply) == "string" then
- steamID = ply
- else
- steamID = ValidSteam(ply)
- end
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- if rank < 1 or rank > 5 then
- func(nil, nil)
- return
- end
- isql:Query("UPDATE gw_gang_members SET `rank`= " .. isql:Escape(rank) .. " WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(gang) .. ";", function()
- func(true)
- end)
- end
- function GangWars.DB.GetGangMemberLimit(gang_name, func)
- func(10)
- --[[
- // check gang hasn't reached its member limit
- isql:Query("SELECT `level`, `boost_members` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) ..";",
- function(glevel)
- if not glevel then
- func()
- return
- end
- glevel = tonumber(glevel.level)
- local members = tonumber(glevel.boost_members)
- func( Gang.GetMemberLimit(glevel) + members )
- end)
- ]]
- end
- function GangWars.DB.InviteToGang(ply, gang_name, func)
- local steamID = ValidSteam(ply)
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- -- check they aren't in a gang
- isql:QueryValue("SELECT * FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. " AND `rank` < 6;", function(r)
- if r then
- func(nil, "Player is already in another gang")
- return
- end
- -- check gang hasn't reached its member limit
- isql:QueryRow("SELECT `level`, `memberboost` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(glevel)
- if not glevel then
- func(nil, "Couldn't find gang level or member boost")
- return
- end
- local members = 10 --(tonumber(glevel.memberboost)) + Gang.GetMemberLimit(tonumber(glevel.level))
- glevel = tonumber(glevel.level)
- isql:QueryValue("SELECT COUNT(*) FROM gw_gang_members WHERE `gang` = " .. isql:Escape(gang_name) .. ";", function(mcount)
- mcount = tonumber(mcount)
- if not glevel or not mcount or mcount >= members then
- func(nil, "Your gang has reached its member limit! (Max " .. members .. " members)")
- return
- end
- -- add them with invited rank
- isql:Query("INSERT INTO gw_gang_members (`gang`, `steam`, `rank`) VALUES(" .. isql:Escape(gang_name) .. ", " .. isql:Escape(steamID) .. ", 6);", function()
- func(true)
- end)
- end)
- end)
- end)
- end
- function GangWars.DB.AcceptInvite(ply, gang, func)
- local steamID = ValidSteam(ply)
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- -- check they are being invited to the gang
- isql:QueryValue("SELECT `rank` FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(gang) .. ";", function(r)
- if not r or tonumber(r) ~= 6 then
- func(nil, "Not invited to that gang!")
- return
- end
- -- add them with basic rank
- isql:Query("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` != " .. isql:Escape(gang) .. ";", function()
- -- add them with basic rank
- isql:Query("UPDATE gw_gang_members SET `rank` = '5' WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(gang) .. ";", function()
- func(true)
- end)
- end)
- end)
- end
- function GangWars.DB.RejectInvite(ply, gang, func)
- local steamID = ValidSteam(ply)
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- -- check they are being invited to the gang
- isql:QueryValue("SELECT `rank` FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(gang) .. ";", function(r)
- if not r or tonumber(r) ~= 6 then
- func(nil, "Not invited to that gang!")
- return
- end
- -- remove invite
- isql:Query("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(gang) .. ";", function()
- func(true)
- end)
- end)
- end
- function GangWars.DB.RemoveFromGangBySteam(steam, gang, func)
- isql:QueryRow("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steam) .. " AND `gang` = " .. isql:Escape(gang) .. ";", func)
- end
- function GangWars.DB.RemoveFromGangByName(ply_name, gang, func)
- -- find the player
- isql:QueryRow("SELECT n.name, n.steam, g.gang, g.rank FROM gw_characters n INNER JOIN gw_gang_members g ON g.steam = n.name WHERE n.name LIKE " .. isql:Escape(ply_name) .. ";", function(query)
- if not query or not query.name then
- func(nil, "Can't find target players name")
- return
- end
- local steamID = query.steam
- if not steamID or steamID == "" then
- func(nil, "Can't find target players steamID")
- return
- end
- local gang_name = query.gang
- if not gang_name or gang_name == "" or gang_name ~= gang then
- func(nil, "Can't find target players gang")
- return
- end
- -- remove them
- isql:Query("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. ";", function()
- func(true)
- end)
- end)
- -- if they were owner, delete gang & all members
- --if tonumber(rank) == 1 then
- -- isql:Query("DELETE FROM gw_gang_members WHERE `gang` = " .. isql:Escape(gang_name) .. ";")
- -- isql:Query("DELETE FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";")
- --end
- end
- function GangWars.DB.RemoveFromGang(ply, func)
- local steamID = ValidSteam(ply)
- if not steamID then
- func(nil, "Invalid STEAMID")
- return
- end
- local gang_name = GangWars.DB.GetPlayerGang(ply, function(gang_name)
- if not gang_name then
- func(nil, "Couldn't find gang")
- return
- end
- -- remove them
- isql:Query("DELETE FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. ";", function()
- func(true)
- net.Start("sv_refresh_topgangs")
- net.Broadcast()
- end)
- end)
- -- if they were owner, delete gang & all members
- --if tonumber(rank) == 1 then
- -- isql:Query("DELETE FROM gw_gang_members WHERE gang = " .. isql:Escape(gang_name) .. ";")
- -- isql:Query("DELETE FROM gw_gangs WHERE name = " .. isql:Escape(gang_name) .. ";")
- --end
- end
- function GangWars.DB.RivalryWin(winner, loser, ignoretruce, supressmessage)
- local args = loser
- local gang = winner
- -- see if other gang is our rival
- isql:QueryValue("SELECT COUNT(*) FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang) .. ";", function(truce)
- if (not truce or tonumber(truce) <= 0) and not ignoretruce then return end --GangNotify(ply, 3, 4, "You don't seem to have a rivalry with " .. args)
- -- remove the rivalry
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(gang) .. " AND `rival` = " .. isql:Escape(args) .. ";", function()
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang) .. ";", function()
- --MsgN("Deleted rivalry entires")
- isql:QueryValue("SELECT `points` FROM `gw_gangs` WHERE `name` = " .. isql:Escape(args) .. ";", function(loser_points)
- -- find level difference
- --isql:QueryValue("SELECT `level` FROM `gw_gangs` WHERE `name` = " .. isql:Escape(gang) .. ";",
- --function(my_level)
- --isql:QueryValue("SELECT `level` FROM `gw_gangs` WHERE `name` = " .. isql:Escape(args) .. ";",
- --function(their_level)
- --[[
- if not their_level then
- Msg("WARNING: " .. loser .. " requested mercy but can't find level for them\n")
- end
- if not my_level then
- Msg("WARNING: " .. loser .. " requested mercy but can't find level for " .. winner .. "\n")
- end
- if not their_level or not my_level then
- return
- end
- MsgN("Awarding points")
- // find level difference
- local lvl_diff = their_level - my_level
- // find points
- local points = math.max(10000 + (1000 * lvl_diff), 0)
- ]]
- local points = math.floor(loser_points / 5)
- -- award points
- GangWars.DB.AddGangPoints(gang, points, function() end)
- GangWars.DB.AddGangPoints(args, -points, function() end)
- -- refresh their view
- if not supressmessage then
- NotifyGang(gang, 4, 4, "You gained " .. points .. " points for defeating " .. args)
- NotifyGang(args, 4, 4, "You have lost the fight with " .. gang .. " and lost " .. points .. " points")
- end
- for k, v in pairs(player.GetAll()) do
- if IsValid(v) then
- if v:Gang() == gang or v:Gang() == args then
- InitPlayerGang(v)
- end
- end
- end
- -- add norivalry entry (5 days)
- -- Modified 03-10-2012 to block the spammy abuse of mercy to increase gang points
- isql:Query("INSERT INTO `gw_gang_norival` (`gang`, `rival`, `expires`) VALUES (" .. isql:Escape(gang) .. ", " .. isql:Escape(args) .. ", DATE_ADD(NOW(), INTERVAL 5 DAY));")
- -- add norivalry entry for the loser (1 day, to prevent spam)
- isql:Query("INSERT INTO `gw_gang_norival` (`gang`, `rival`, `expires`) VALUES (" .. isql:Escape(args) .. ", " .. isql:Escape(gang) .. ", DATE_ADD(NOW(), INTERVAL 1 DAY));")
- end)
- end)
- end)
- end)
- --end)
- end
- -- adds to a gangs XP
- function GangWars.DB.AddGangXP(gang_name, add_xp)
- if not gang_name or gang_name == "" then return end
- -- find the gangs xp & level
- isql:QueryRow("SELECT `xp`, `level`, `gangxpboost` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(r)
- if not r then return end
- local xp = tonumber(r['xp'])
- local level = tonumber(r['level'])
- local boost = tonumber(r['gangxpboost'])
- if level >= 20 then return end
- -- boost XP
- if boost and boost > 0 then
- local txt, aff = Gang.GetUpgradeEffect("missionboost", boost - 1)
- add_xp = add_xp + math.floor((add_xp / 100) * aff)
- end
- -- add the XP
- xp = xp + add_xp
- local levelup = false
- -- level up gang
- if xp > Gang.XP2Level(level) then
- levelup = true
- isql:Query("UPDATE gw_gangs SET `xp` = 0, `level` = " .. (level + 1) .. " WHERE `name` = " .. isql:Escape(gang_name) .. ";")
- else
- isql:Query("UPDATE gw_gangs SET `xp` = `xp` + '" .. add_xp .. "' WHERE `name` = " .. isql:Escape(gang_name) .. ";")
- end
- end)
- end
- -- adds to a gangs money
- function GangWars.DB.AddGangMoney(gang_name, money, func)
- if not gang_name or gang_name == "" then
- if func then
- func(nil)
- end
- return
- end
- isql:Query("UPDATE gw_gangs SET `money` = `money` + " .. isql:Escape(tonumber(money)) .. " WHERE `name` = " .. isql:Escape(gang_name) .. ";", func)
- end
- -- adds to a gangs points
- function GangWars.DB.AddGangPoints(gang_name, points, func)
- if not gang_name or gang_name == "" then
- if func then
- func(nil)
- end
- return
- end
- if GangWars.EventActive("doublexp") or GangWars.EventActive("doublepoints") then
- points = points * 2
- end
- isql:Query("UPDATE gw_gangs SET `points` = `points` + " .. isql:Escape(tonumber(points)) .. " WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(d)
- -- auto update clients in the gang of their points
- GangWars.DB.GetGangPoints(gang_name, function(gp)
- for k, v in pairs(player.GetAll()) do
- if gp and v:Gang() == gang_name then
- net.Start("ds_gang_points")
- net.WriteInt(gp, 32)
- net.Send(v)
- end
- end
- end)
- -- callback
- if func then
- func(d)
- end
- end)
- end
- -- fetch a gangs points
- function GangWars.DB.GetGangPoints(gang_name, func)
- if not gang_name or gang_name == "" then
- if func then
- func(nil)
- end
- return
- end
- isql:QueryValue("SELECT `points` FROM `gw_gangs` WHERE `name` = " .. isql:Escape(gang_name) .. ";", func)
- end
- -- checks gang money
- function GangWars.DB.GangCanAfford(gang_name, money, func)
- if not gang_name or gang_name == "" then return end
- isql:QueryValue("SELECT `money` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(mon)
- if mon and tonumber(mon) >= money then
- func(true)
- else
- func(false)
- end
- end)
- end
- -- upgrades a gang
- function GangWars.DB.GangUpgrade(gang_name, upgrade, func)
- if not gang_name or gang_name == "" then
- func(nil, nil)
- return
- end
- -- check the sql input!
- if upgrade ~= "hpboost" and upgrade ~= "regen" and upgrade ~= "extraitems" and upgrade ~= "xpboost" and upgrade ~= "speedboost" and upgrade ~= "printboost" and upgrade ~= "drugboost" and upgrade ~= "dropboost" and upgrade ~= "armorboost" and upgrade ~= "memberboost" and upgrade ~= "shopboost" and upgrade ~= "ppocketboost" and upgrade ~= "bankboost" and upgrade ~= "missionboost" and upgrade ~= "gangxpboost" then
- func(nil, "Invalid upgrade")
- return
- end
- -- find the upgrade level
- isql:QueryValue("SELECT `" .. upgrade .. "` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(ulevel)
- ulevel = tonumber(ulevel)
- if not ulevel or ulevel >= 10 then
- func(nil, "This is already fully upgraded")
- return
- end
- -- find the gangs money
- isql:QueryRow("SELECT `money`, `level` FROM gw_gangs WHERE `name` = " .. isql:Escape(gang_name) .. ";", function(r)
- if not r then
- func(nil, "Can't find your gang?")
- return
- end
- local amt = tonumber(r['money'])
- local glevel = tonumber(r['level'])
- -- check gang is high enough level
- if glevel <= ulevel then
- func(nil, "Gang isn't high enough level", amt)
- return
- end
- -- find upgrade cost
- local price = Gang.GetUpgradePrice(ulevel)
- -- check we can afford it
- if amt < price then
- func(nil, "Gang can't afford that", amt)
- return
- end
- -- charge the team
- amt = amt - price
- -- save and upgrade
- isql:Query("UPDATE gw_gangs SET `money` = " .. amt .. ", `" .. upgrade .. "` = " .. (ulevel + 1) .. " WHERE `name` = " .. isql:Escape(gang_name) .. ";", function()
- func(true, "", amt)
- end)
- end)
- end)
- end
- --////////////////////////////////////////////////////////////////////////////
- -- Commands
- -- change logo
- local function GC_ChangeLogo(len, ply)
- local decoded = net.ReadTable()
- if not decoded or not decoded.logo then return end
- if decoded.logo < 1 then return end
- if decoded.logo > 30 then return end
- local gname = "extra/" .. decoded.logo
- --[[
- if not string.find(gname, "extra") then
- GangNotify(ply, 3, 4, "Invalid logo... you've been logged, thank you!")
- GangWars.Log("cheat", ply, ply:Nick() .. " tried to set gang logo to '" .. gname .. "'")
- return
- end
- ]]
- local steamID = ValidSteam(ply)
- if not steamID then return end
- local gang = ply:GetNW2String("gang_name")
- -- find gang info
- GangWars.DB.GetGangInfo(gang, function(info)
- if not info then
- Msg("GC_ChangeLogo: GangWars.DB.GetGangInfo returned nil")
- return
- end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or tonumber(my_rank) > 1 then
- GangNotify(ply, 3, 4, "Only leaders can buy logos")
- return
- end
- local gangmoney = tonumber(info.money)
- local ganglevel = tonumber(info.level)
- local logoprice = 500000 + tonumber(decoded.logo) * 200000
- --local logolevel = 5 + math.floor(tonumber(decoded.logo) / 10) * 5
- local logolevel = math.min(10 + math.floor(tonumber(decoded.logo) / 10) * 5, 20)
- -- abandon if we're not high enough level
- if logolevel > ganglevel then
- GangNotify(ply, 3, 4, "Your gang must be level " .. logolevel .. " to buy that logo!")
- return
- end
- -- abandon if we can't afford it
- if gangmoney < logoprice then
- GangNotify(ply, 1, 4, "Your gang can not afford this logo!")
- return
- end
- -- charge gang
- GangWars.DB.AddGangMoney(gang, -logoprice, function()
- -- create gang
- isql:Query("UPDATE gw_gangs SET `logo` = " .. isql:Escape(gname) .. " WHERE `name` = " .. isql:Escape(info.name) .. ";", function()
- GangNotify(ply, 3, 4, "You successfully changed your gangs logo!")
- GangWars.GangLog(gang, ply:Nick() .. " changed the gang logo", "Logo Change", ply)
- InitPlayerGang(ply)
- end)
- end)
- end)
- end)
- end
- net.Receive("ChangeGangLogo", GC_ChangeLogo)
- -- change password
- local function GC_ChangePassword(len, ply)
- local decoded = net.ReadTable()
- local steamID = ValidSteam(ply)
- if not steamID then return end
- if decoded and not decoded.old or not decoded.new then return end
- -- find ranks
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or my_rank ~= 1 then
- GangNotify(ply, 3, 4, "You must be gang leader to change the password")
- return
- end
- -- find gang info
- GangWars.DB.GetGangInfo(ply:GetNW2String("gang_name"), function(info)
- if not info then
- Msg("GC_ChangePassword: GangWars.DB.GetGangInfo returned nil")
- return
- end
- local gangpass = info.password
- -- abandon if old password does not match
- if tostring(gangpass) ~= tostring(util.CRC(decoded.old)) then
- GangNotify(ply, 3, 4, "Old password was incorrect!")
- return
- end
- -- charge gang
- --GangWars.DB.AddGangMoney(gang, -500000,
- --function()
- -- create gang
- isql:Query("UPDATE gw_gangs SET `password` = " .. isql:Escape(util.CRC(decoded.new)) .. " WHERE `name` = " .. isql:Escape(info.name) .. ";", function()
- GangNotify(ply, 3, 4, "You successfully changed your gangs password!")
- GangWars.GangLog(gang, ply:Nick() .. " changed the gang password", "Password Change", ply)
- end)
- end)
- end)
- --end)
- end
- net.Receive("ChangeGangPassword", GC_ChangePassword)
- -- recover gang
- local function GC_Recover(len, ply)
- local decoded = net.ReadTable()
- local steamID = ValidSteam(ply)
- if not steamID then return end
- if decoded and not decoded.name or not decoded.password then return end
- -- find gang info
- GangWars.DB.GetGangInfo(decoded.name, function(info)
- if not info then
- Msg("GC_Recover: GangWars.DB.GetGangInfo returned nil")
- return
- end
- local gangpass = info.password
- -- abandon if old password does not match
- if tostring(gangpass) ~= tostring(util.CRC(decoded.password)) then
- GangNotify(ply, 3, 4, "The password was incorrect! This action has been logged and sent to the admin team.")
- GangWars.Log("gang_recover", ply, ply:Nick() .. " tried to recover the gang '" .. decoded.name .. "' with a bad password")
- return
- end
- -- Remove all members. Set us as the only member/leader.
- isql:QueryValue("SELECT COUNT(*) FROM `gw_gang_members` WHERE `gang` = " .. isql:Escape(info.name) .. " AND `rank` = 1;", function(row)
- if tonumber(row) and tonumber(row) > 0 then
- GangNotify(ply, 3, 4, "The gang already has a leader! This action has been logged and sent to the admin team.")
- GangWars.Log("gang_recover", ply, ply:Nick() .. " tried to recover the gang '" .. decoded.name .. "' but it already has a leader")
- return
- end
- -- create gang
- isql:Query("INSERT INTO `gw_gang_members` (`steam`, `gang`, `rank`) VALUES(" .. isql:Escape(steamID) .. ", " .. isql:Escape(info.name) .. ", 1);", function()
- InitPlayerGang(ply)
- ply:GangStorageRefresh()
- GangNotify(ply, 3, 4, "You successfully recovered the " .. info.name .. " gang!")
- GangWars.Log("gang_recover", ply, ply:Nick() .. " successfully recovered the gang '" .. decoded.name .. "'")
- end)
- end)
- end)
- end
- net.Receive("RecoverGang", GC_Recover)
- -- /create <gang name>
- local function GC_Create(len, ply)
- local decoded = net.ReadTable()
- local gname = decoded.name
- if string.len(gname) > 20 then
- GangNotify(ply, 3, 4, "Gang name must not exceed 20 characters!")
- return ""
- end
- local price = 100000
- if GangWars.GetLevel(ply) >= 10 and ply:CanAfford(price) and tonumber(decoded.logo) <= 25 then
- GangWars.DB.CreateGang(ply, gname, decoded.password, decoded.logo, decoded.color, decoded.color2, function(q, msg)
- if q then
- ply:AddMoney(-price, "Gang creation")
- InitPlayerGang(ply)
- ply:GangStorageRefresh()
- GangNotify(ply, 3, 4, "Congratulations! You have started the " .. gname .. " gang!")
- else
- if msg then
- GangNotify(ply, 3, 4, "Could not create gang. " .. msg)
- else
- GangNotify(ply, 3, 4, "Could not create gang.")
- end
- end
- end)
- else
- GangNotify(ply, 3, 4, "You must be level 10 and have " .. CUR .. "100,000 to create a gang")
- end
- end
- net.Receive("CreateGang", GC_Create)
- -- /leave gang
- local function GC_Leave(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return end
- GangWars.DB.RemoveFromGang(ply, function(q)
- --timer.Simple(1, function () InitPlayerGang(ply) end )
- --timer.Simple(1, function() UpdateClientGangInfo(gang) end)
- --timer.Simple(1, function() UpdateClientGangMembers(gang) end)
- if q then
- ply.GangInfo = nil
- GangNotify(ply, 3, 4, "You have left the " .. gang .. " gang")
- GangWars.GangLog(gang, ply:Nick() .. " left the gang", "Leave", ply)
- InitPlayerGang(ply)
- end
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_leave", GC_Leave)
- local function GC_Donate(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- local money = math.floor(tonumber(args))
- if money <= 0 then return "" end
- if not ply:CanAfford(money) then
- GangNotify(ply, 4, 4, "You don't have that much to donate!")
- return ""
- end
- -- swap money
- GangWars.DB.AddGangMoney(gang, money, function()
- InitPlayerGang(ply)
- ply:AddMoney(-money, "Gang donation")
- GangWars.GangLogNumber(gang, money, "Donate", ply)
- GangNotify(ply, 3, 4, "You have donated " .. CUR .. GangWars.FormatMoney(money) .. " to your gang")
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_donate", GC_Donate)
- -- upgrade gang
- local function GC_Upgrade(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return end
- -- find ranks
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or my_rank > 2 then
- GangNotify(ply, 3, 4, "You must be at least vice leader to purchase upgrades")
- return
- end
- -- do upgrade
- GangWars.DB.GangUpgrade(gang, args, function(q, msg, amnt)
- if q then
- --timer.Simple(2, function() UpdateClientGangInfo(gang) end)
- InitPlayerGang(ply)
- GangNotify(ply, 3, 4, "You have upgraded your gang")
- GangWars.GangLogNumber(gang, amnt, "Upgrade", ply, ply:Nick() .. " upgraded " .. args .. " for $" .. string.Comma(amnt))
- else
- if msg then
- GangNotify(ply, 3, 4, "You can't purchase that upgrade. " .. msg)
- else
- GangNotify(ply, 3, 4, "You can't purchase that upgrade")
- end
- end
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_upgrade", GC_Upgrade)
- -- /invite <player>
- local function GC_Invite(ply, args)
- -- find our gang
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then
- GangNotify(ply, 3, 4, "You aren't in a gang!")
- return
- end
- -- find our target
- local target = FindPlayer(args)
- if not target then
- GangNotify(ply, 3, 4, "Can't find that player to invite")
- return ""
- end
- -- find ranks
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or my_rank > 2 then
- GangNotify(ply, 3, 4, "You must be leader or vice leader to invite")
- return ""
- end
- -- invite them
- GangWars.DB.InviteToGang(target, gang, function(q, msg)
- if q then
- --timer.Simple(2, function () InitPlayerGang(target) end )
- --timer.Simple(2, function() UpdateClientGangInfo(gang) end)
- --timer.Simple(2, function() UpdateClientGangMembers(gang) end)
- unet.Start("gang_invite", target)
- net.WriteString(gang)
- unet.Send(target)
- InitPlayerGang(ply)
- InitPlayerGang(target)
- GangWars.GangLog(gang, ply:Nick() .. " invited " .. target:Nick(), "Invite", ply)
- GangNotify(ply, 3, 4, "You have invited " .. target:Nick() .. " to join " .. gang)
- GangNotify(target, 4, 4, "You have been invited to join " .. gang)
- else
- if msg then
- GangNotify(ply, 3, 4, "Invite failed. " .. msg)
- else
- GangNotify(ply, 3, 4, "Invite failed.")
- end
- end
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_invite", GC_Invite)
- -- /kick <player>
- local function GC_Kick(ply, cmd, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return end
- -- find our target
- local target = args[1]
- if target == "" then return end
- local reason = args[2] or ""
- if target == ply:SteamID() then
- GangNotify(ply, 3, 4, "You can't kick yourself")
- return ""
- end
- -- check they're in our gang
- GangWars.DB.GetPlayerGang(target, function(tgang)
- if not tgang or tgang ~= gang then return "" end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank then return end
- -- check other guys rank
- -- AND `rank` < 6
- isql:QueryValue("SELECT `rank` FROM gw_gang_members WHERE `steam` = " .. isql:Escape(target) .. ";", function(other_rank)
- if not other_rank then return end
- local rank_ok = false
- -- rank setting permissions. leaders can always kick
- if my_rank <= 1 then
- rank_ok = true
- -- vice leaders can kick captains
- elseif my_rank == 2 and other_rank > 2 then
- rank_ok = true
- elseif my_rank == 3 and other_rank > 3 then
- -- captains can kick vices
- rank_ok = true
- end
- -- check the target isn't higher ranking than us if we're not leader
- if my_rank > 1 and other_rank <= my_rank then
- rank_ok = false
- end
- -- invites can only be kicked by leaders and vice
- if other_rank >= 6 and my_rank > 2 then
- rank_ok = false
- end
- if not rank_ok then
- GangNotify(ply, 3, 4, "You don't have the rank to do that")
- return
- end
- -- remove them
- GangWars.DB.RemoveFromGangBySteam(target, gang, function()
- net.Start("sv_refresh_topgangs")
- net.Broadcast()
- InitPlayerGang(ply)
- GangNotify(ply, 3, 4, "You have kicked " .. target .. " from the gang")
- GangWars.GangLog(gang, ply:Nick() .. " kicked " .. target .. " (" .. reason .. ")", "Kick", ply)
- local tply = FindPlayer(target)
- if tply then
- InitPlayerGang(tply)
- GangNotify(tply, 4, 4, "You have been kicked from the " .. gang .. " gang")
- end
- end)
- end)
- end)
- end, true)
- end)
- return ""
- end
- --AddChatCommand("/gang_kick", GC_Kick)
- concommand.Add("gang_kick", GC_Kick)
- -- /promote <rank> <player>
- local function GC_Promote(ply, cmd, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- --local words = string.Explode(" ", args)
- --if #words ~= 2 then return "" end
- local ptype = args[1]
- -- find our target
- local target = args[2]
- if target == ply:SteamID() then
- GangNotify(ply, 3, 4, "You can't change your own rank")
- return ""
- end
- -- check they're in our gang
- GangWars.DB.GetPlayerGang(target, function(tgang)
- if not tgang or tgang ~= gang then return "" end
- -- check our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank then return "" end
- -- check other guys rank
- GangWars.DB.GetPlayerGangRank(target, function(other_rank)
- if not other_rank then return "" end
- local target_rank = tonumber(ptype)
- local rank_ok = false
- -- rank setting permissions. leaders can set all ranks
- if my_rank <= 1 then
- rank_ok = true
- -- vice leaders can set up to captain
- elseif my_rank == 2 and target_rank > 2 then
- rank_ok = true
- elseif my_rank == 3 and target_rank > 3 then
- -- captains can set vices
- rank_ok = true
- end
- -- check the target isn't higher ranking than us if we're not leader
- if my_rank > 1 and other_rank <= my_rank then
- rank_ok = false
- end
- -- abandon setting the same rank
- if target_rank == other_rank then
- rank_ok = false
- end
- if not rank_ok then
- GangNotify(ply, 3, 4, "You don't have the rank to do that")
- return ""
- end
- -- is it a promotion or demotion?
- local promotion = target_rank < other_rank
- -- set targets rank
- GangWars.DB.SetPlayerGangRank(target, gang, target_rank, function(q)
- if q then
- local text = "promoted"
- if not promotion then
- text = "demoted"
- end
- InitPlayerGang(ply)
- local target = FindPlayer(args[2])
- if IsValid(target) then
- InitPlayerGang(target)
- GangNotify(target, 4, 4, "You have been " .. text .. " within the gang")
- GangNotify(ply, 3, 4, "You " .. text .. " " .. target:Nick())
- GangWars.GangLog(gang, ply:Nick() .. " " .. text .. " " .. target:Nick(), "Promote", ply)
- else
- GangNotify(ply, 3, 4, "You " .. text .. " " .. args[2])
- GangWars.GangLog(gang, ply:Nick() .. " " .. text .. " " .. args[2], "Promote", ply)
- end
- end
- end)
- end)
- end)
- end)
- end)
- return ""
- end
- --AddChatCommand("/gang_promote", GC_Promote)
- concommand.Add("gang_promote", GC_Promote)
- -- /add rival
- local function GC_AddRival(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGangInfo(ply, function(gang)
- if not gang then return "" end
- -- check we're not naming ourself
- if gang.name == args then
- GangNotify(ply, 3, 4, "You can't name your own gang as a rival")
- return
- end
- if tonumber(gang.points) <= 0 then
- GangNotify(ply, 3, 4, args .. " can't be rivalled because your gang has no gang points")
- return
- end
- -- check rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or tonumber(my_rank) > 2 then
- GangNotify(ply, 3, 4, "Only leaders and vice leaders can add rivals")
- return
- end
- -- check the other gang exists
- isql:QueryRow("SELECT `level`, `points` FROM `gw_gangs` WHERE `name` = " .. isql:Escape(args) .. ";", function(q)
- if not q or tonumber(q.level) < (ply:GetNW2Int("gang_level") - 5) then
- GangNotify(ply, 3, 4, args .. " are too low level to be your rival")
- return
- end
- if tonumber(q.points) <= 0 then
- GangNotify(ply, 3, 4, args .. " can't be rivalled because they have no gang points")
- return
- end
- -- check they aren't already our rival
- isql:QueryValue("SELECT COUNT(*) FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(gang.name) .. " AND `rival` = " .. isql:Escape(args) .. ";", function(q)
- if q and tonumber(q) > 0 then
- GangNotify(ply, 3, 4, "You are already rivals with that gang")
- return
- end
- -- and the other way around
- isql:QueryValue("SELECT COUNT(*) FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang.name) .. ";", function(q)
- if q and tonumber(q) > 0 then
- GangNotify(ply, 3, 4, "You are already rivals with that gang")
- return
- end
- -- check the rivalry is allowed
- isql:QueryValue("SELECT DATEDIFF(`expires`, NOW()) AS diff FROM `gw_gang_norival` WHERE `gang` = " .. isql:Escape(gang.name) .. " AND `rival` = " .. isql:Escape(args) .. " AND DATEDIFF(`expires`, NOW()) > 0;", function(rcount)
- if rcount and tonumber(rcount) > 0 then
- GangNotify(ply, 3, 4, "You are not allowed to rival that gang for " .. rcount .. " more days")
- return
- end
- -- delete any norival entry for this gang
- isql:Query("DELETE FROM `gw_gang_norival` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang.name) .. ";", function()
- -- add rival relationship
- isql:Query("INSERT INTO `gw_gang_rivals` (`gang`, `rival`, `started`) VALUES(" .. isql:Escape(gang.name) .. ", " .. isql:Escape(args) .. ", NOW());", function()
- isql:Query("INSERT INTO `gw_gang_rivals` (`gang`, `rival`, `started`) VALUES(" .. isql:Escape(args) .. ", " .. isql:Escape(gang.name) .. ", NOW());", function()
- -- add them
- --InitPlayerGang(ply)
- --GangNotify(ply, 3, 4, "You have named " .. args .. " as a rival gang")
- NotifyGang(gang.name, 4, 4, "A gang rivalry has been started against " .. args .. "!")
- NotifyGang(args, 4, 4, gang.name .. " have declared a gang rivalry has against you!")
- for k, v in pairs(player.GetAll()) do
- if IsValid(v) then
- if v:Gang() == gang.name or v:Gang() == args then
- InitPlayerGang(v)
- end
- end
- end
- end)
- end)
- end)
- end)
- end)
- end)
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_addrival", GC_AddRival)
- -- request mercy
- local BlockMercySpam = {}
- local function GC_ReqMercy(ply, args)
- if not IsValid(ply) then return end
- if BlockMercySpam[ply:SteamID()] and BlockMercySpam[ply:SteamID()] > CurTime() then return end
- BlockMercySpam[ply:SteamID()] = CurTime() + 5
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or tonumber(my_rank) > 2 then
- GangNotify(ply, 3, 4, "Only leaders and co-leaders can request mercy")
- return
- end
- -- try and make them win
- GangWars.DB.RivalryWin(args, gang)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_mercy", GC_ReqMercy)
- -- request truce
- local function GC_ReqTruce(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or tonumber(my_rank) > 2 then
- GangNotify(ply, 3, 4, "Only leaders and co-leaders can request a truce")
- return
- end
- -- see if other gang is requesting a truce
- isql:QueryValue("SELECT `truce` FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang) .. ";", function(truce)
- if not truce then
- GangNotify(ply, 3, 4, "You don't seem to have a rivalry with " .. args)
- return
- end
- if tonumber(truce) == 1 then
- -- truce is agreed. remove the rivalry
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(gang) .. " AND `rival` = " .. isql:Escape(args) .. ";", function()
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang) .. ";", function()
- -- refresh their view
- InitPlayerGang(ply)
- NotifyGang(gang, 4, 4, "A truce with " .. args .. " has been agreed!")
- NotifyGang(args, 4, 4, "A truce with " .. gang .. " has been agreed!")
- end)
- end)
- else
- -- set truce request
- isql:Query("UPDATE `gw_gang_rivals` SET `truce` = '1' WHERE `gang` = " .. isql:Escape(gang) .. " AND `rival` = " .. isql:Escape(args) .. ";", function()
- -- refresh their view
- InitPlayerGang(ply)
- --GangNotify(ply, 3, 4, "You have requested a truce with " .. args)
- NotifyGang(gang, 4, 4, "You have requested a truce with " .. args)
- NotifyGang(args, 4, 4, gang .. " have requested a truce!")
- end)
- end
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_truce", GC_ReqTruce)
- -- cancel truce
- local function GC_CancelTruce(ply, args)
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank or tonumber(my_rank) > 2 then
- GangNotify(ply, 3, 4, "Only leaders and co-leaders can cancel a truce")
- return
- end
- -- just check they exist
- isql:Query("SELECT `truce` FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(args) .. " AND `rival` = " .. isql:Escape(gang) .. ";", function(truce)
- if truce then
- -- set truce request
- isql:Query("UPDATE `gw_gang_rivals` SET `truce` = '0' WHERE `gang` = " .. isql:Escape(gang) .. " AND `rival` = " .. isql:Escape(args) .. ";", function()
- -- refresh their view
- InitPlayerGang(ply)
- GangNotify(ply, 3, 4, "You have cancelled the truce request with " .. args)
- end)
- end
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_canceltruce", GC_CancelTruce)
- -- /accept
- local function GC_AcceptInvite(ply, args)
- GangWars.DB.AcceptInvite(ply, args, function(q)
- if q then
- InitPlayerGang(ply, function()
- ply:GangStorageRefresh()
- ply:GangLogRefresh()
- GangNotify(ply, 3, 4, "You accepted the gang invite from " .. args)
- GangWars.GangLog(args, ply:Nick() .. " joined the gang", "Join", ply)
- end)
- end
- end)
- return ""
- end
- AddChatCommand("/gang_accept", GC_AcceptInvite)
- -- /reject
- local function GC_RejectInvite(ply, args)
- GangWars.DB.RejectInvite(ply, args, function(q)
- if q then
- InitPlayerGang(ply)
- GangNotify(ply, 3, 4, "You rejected the gang invite from " .. args)
- GangWars.GangLog(args, ply:Nick() .. " rejected our gang invite", "Reject", ply)
- end
- end)
- return ""
- end
- AddChatCommand("/gang_reject", GC_RejectInvite)
- -- set item purchase permission
- local function GC_SetBuyPermission(ply, cmd, args)
- --Msg("GC_SetBuyPermission\n")
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then return "" end
- --Msg("GetPlayerGang\n")
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- --Msg("GetPlayerGangRank check\n")
- if not my_rank or tonumber(my_rank) > 2 then return end
- --Msg("GetPlayerGangRank ok\n")
- local itemid = args[1]
- local perms = args[2]
- -- delete any existing permissions
- isql:Query("DELETE FROM`gw_gang_permissions` WHERE `gang` = " .. isql:Escape(gang) .. " AND `item` = " .. isql:Escape(itemid) .. ";", function()
- --Msg("DELETE FROM`gw_gang_permissions`\n")
- -- insert new permissions
- isql:Query("INSERT INTO `gw_gang_permissions` (`gang`, `item`, `permission`) VALUES(" .. isql:Escape(gang) .. ", " .. isql:Escape(itemid) .. ", " .. isql:Escape(perms) .. ");", function() end) --Msg("done!\n")
- end)
- end)
- end)
- end
- concommand.Add("gang_permission", GC_SetBuyPermission)
- -- gang buy
- local function GC_BuyItem(ply, args)
- -- Can't buy gang stuff as a cop
- if ply:GetProfession().police then
- Notify(ply, 4, 4, "You cant buy gang stuff when working as AnkhTek Security!")
- return ""
- end
- -- find our gang name
- GangWars.DB.GetPlayerGang(ply, function(gang)
- if not gang then
- Msg("Warning: /gang_buy player had no gang?\n")
- return
- end
- -- find our rank
- GangWars.DB.GetPlayerGangRank(ply, function(my_rank)
- if not my_rank then
- Msg("Warning: /gang_buy player had no rank?\n")
- return
- end
- -- find permissions for the item
- isql:QueryValue("SELECT `permission` FROM `gw_gang_permissions` WHERE `gang` = " .. isql:Escape(gang) .. " AND `item` = " .. isql:Escape(args) .. ";", function(perm)
- if not perm then
- --Msg("Warning: /gang_buy " .. args .. ": couldn't find a permission entry for this item\n")
- --return
- -- no permissions = default. nobody can buy.
- perm = 0
- end
- -- check our rank has permission to buy
- local function HasItemPerm(perms, rank)
- local d = 16
- local pms = {0, 0, 0, 0, 0}
- local r = 5
- while perms > 0 do
- if perms - d >= 0 then
- perms = perms - d
- pms[r] = 1
- end
- d = d / 2
- r = r - 1
- end
- return pms[rank] == 1
- end
- -- check our rank has permission to buy
- if not HasItemPerm(perm, my_rank) then
- GangNotify(ply, 3, 4, "You don't have permission to buy that!")
- return
- end
- -- find gang info
- GangWars.DB.GetGangInfo(gang, function(info)
- if not info then
- Msg("GC_BuyItem: GangWars.DB.GetGangInfo returned nil")
- return
- end
- local gangmoney = tonumber(info.money)
- local ganglevel = tonumber(info.level)
- local found = false
- -- find item in gang shop
- for k, v in pairs(Gang.GangShop) do
- if v.id == args then
- found = v
- break
- end
- end
- -- abandon if item not found
- if not found then
- GangNotify(ply, 3, 4, "Item not found in gang shop")
- return
- end
- -- abandon if we're not high enough level
- if found.level > ganglevel then
- GangNotify(ply, 3, 4, "Your gang must be level " .. found.level .. " to spawn that!")
- return
- end
- -- fix the price
- local item_price = found.price
- -- discount upgrade
- if ply.GangInfo and found.entity ~= "gang_stash" then
- if ply.GangInfo.shopboost and ply.GangInfo.shopboost > 0 then
- local txt, aff = Gang.GetUpgradeEffect("shopboost", ply.GangInfo.shopboost - 1)
- item_price = item_price - math.floor((item_price / 100) * aff)
- end
- end
- -- abandon if we can't afford it
- if gangmoney < item_price then
- GangNotify(ply, 1, 4, "Your gang can not afford this!")
- return
- end
- -- abandon if gang is past its stash limit
- if found.entity == "gang_stash" then
- local stashes = {}
- for k, v in pairs(ents.FindByClass(found.entity)) do
- if v.gang == gang then
- table.insert(stashes, v)
- end
- end
- if #stashes >= math.min(ganglevel, 4) then
- GangNotify(ply, 1, 4, "You already have the max of " .. math.min(ganglevel, 4) .. " stashes")
- return
- end
- end
- -- abandon if gang is past its generator limit
- if found.entity == "base_generator" then
- local stashes = {}
- for k, v in pairs(ents.FindByClass(found.entity)) do
- if v.gang == gang then
- table.insert(stashes, v)
- end
- end
- if #stashes >= 2 then
- GangNotify(ply, 1, 4, "Your gang already have the max of 2 generators")
- return
- end
- end
- -- abandon if gang is past its printer limit
- if found.entity == "gang_printer" then
- local stashes = {}
- for k, v in pairs(ents.FindByClass(found.entity)) do
- if v.gang == gang then
- table.insert(stashes, v)
- end
- end
- --MsgN("checking gang printers. search is for '" .. found.entity .."', initial count is " .. #ents.FindByClass(found.entity) .. ", actual is " .. #stashes)
- if #stashes >= 4 then
- GangNotify(ply, 1, 4, "You already have the max of 4 gang printers")
- return
- end
- end
- -- abandon if gang is past its printer limit
- if found.entity == "orb_forge" then
- local stashes = {}
- for k, v in pairs(ents.FindByClass(found.entity)) do
- if v.gang == gang then
- table.insert(stashes, v)
- end
- end
- if #stashes >= 1 then
- GangNotify(ply, 1, 4, "You already have the max of 1 gang orb forge!")
- return
- end
- end
- -- abandon if gang is past its overclocker limit
- if found.entity == "overclocker_forge" then
- local stashes = {}
- for k, v in pairs(ents.FindByClass(found.entity)) do
- if v.gang == gang then
- table.insert(stashes, v)
- end
- end
- if #stashes >= 4 then
- GangNotify(ply, 1, 4, "You already have the max of 4 overclocker forges!")
- return
- end
- end
- -- charge gang
- GangWars.DB.AddGangMoney(gang, -item_price, function()
- GangNotify(ply, 1, 4, "You have bought a " .. found.name .. " for " .. CUR .. GangWars.FormatMoney(item_price))
- GangWars.GangLogNumber(gang, item_price, "Buy", ply, found.name)
- local trace = {}
- trace.start = ply:EyePos()
- trace.endpos = trace.start + ply:GetAimVector() * 85
- trace.filter = ply
- local tr = util.TraceLine(trace)
- -- create the item
- local spawnitem
- spawnitem = ents.Create(found.entity)
- spawnitem.SID = ply.SID
- spawnitem.gang = gang
- spawnitem:SetNWEntity("owning_ent", ply)
- spawnitem:SetNW2String("Owner", "Shared")
- spawnitem:SetNW2String("gang", gang)
- spawnitem.Owner = ply
- spawnitem.OwnerID = ply:SteamID()
- spawnitem:SetNW2Int("original_cost", item_price)
- spawnitem:SetPos(tr.HitPos)
- spawnitem.onlyremover = true
- spawnitem.nodupe = true
- if found.model then
- spawnitem.weaponclass = found.subclass
- spawnitem:SetModel(found.model)
- end
- -- create CII data
- local ciidata = {}
- ciidata.class = "item"
- ciidata.subclass = found.subclass
- ciidata.ent = found.entity
- ciidata.name = found.name or ""
- ciidata.model = found.model or ""
- ciidata.icon = found.icon
- ciidata.value = item_price or 0
- ciidata.level_lock = found.level or 0
- ciidata.quantity = 0
- ciidata.level = 1
- ciidata.entityex = found.subclass
- -- See if the item supports the CII interface
- if spawnitem.CIILoad then
- spawnitem:CIILoad(ciidata)
- end
- -- See if the item supports the newer LoadItem interface (it must have a ref)
- if spawnitem.LoadItem then
- local ref = GangWars.Item.FindByName(found.name)
- spawnitem:LoadItem(ref)
- end
- -- Else try the subclass interface
- if spawnitem.SetSubclass then
- spawnitem:SetSubclass(found.subclass)
- end
- spawnitem:Spawn()
- -- do it twice for the benefit of gang coolers etc (badly coded ents)
- if spawnitem.SetSubclass then
- spawnitem:SetSubclass(found.subclass)
- end
- spawnitem:CPPISetOwner(ply)
- end)
- end)
- end)
- end)
- end)
- return ""
- end
- AddChatCommand("/gang_buy", GC_BuyItem)
- --////////////////////////////////////////////////////////////////////////////
- -- Util
- function Gang.SendRivalList(rp, my_gang)
- if not my_gang then return end
- -- send full member list
- GangWars.DB.GetGangRivalList(my_gang, function(members)
- if not members then return end
- for k, v in pairs(members) do
- local col = string.Explode(" ", v.color)
- if #col == 3 then
- v.color = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
- end
- local col = string.Explode(" ", v.color2)
- if #col == 3 then
- v.color2 = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
- end
- end
- -- store the list - we use this to decide wether to drop evidence in sv_gamemode_functions.lua
- local tmp = {}
- for k, v in pairs(members) do
- table.insert(tmp, v.name)
- end
- rp.GangRivals = tmp
- net.Start("ds_gang_rivals")
- net.WriteTable(members)
- net.Send(rp)
- end)
- end
- function Gang.SendMemberList(rp, my_gang)
- if not my_gang then return end
- -- send full member list
- --[[
- GangWars.DB.GetPlayerGangMemberList(my_gang,
- function(members)
- if not members then return end
- net.Start("ds_gang_members")
- net.WriteTable(members)
- net.Send(rp)
- end)
- ]]
- GangWars.DB.GetGangMemberStats(my_gang, rp:SteamID(), function(members)
- if not members then return end
- net.Start("ds_gang_members")
- net.WriteTable(members)
- net.Send(rp)
- end)
- end
- function Gang.SendInfo(rp, my_gang)
- if not my_gang then return end
- -- set gang info
- GangWars.DB.GetGangInfo(my_gang, function(info)
- -- get members for count
- GangWars.DB.GetPlayerGangMemberList(my_gang, function(members)
- -- get shop permissions
- isql:Query("SELECT * FROM `gw_gang_permissions` WHERE `gang` = " .. isql:Escape(my_gang) .. ";", function(perms)
- local shop_permissions = {}
- if perms then
- for k, v in ipairs(perms) do
- shop_permissions[tostring(v.item)] = v.permission
- end
- end
- --[[
- local col = string.Explode(" ", info.color)
- if #col == 3 then
- col = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
- end
- local col2 = string.Explode(" ", info.color2)
- if #col2 == 3 then
- col2 = Color(tonumber(col2[1]), tonumber(col2[2]), tonumber(col2[3]), 255)
- end
- ]]
- -- this is pretty pointless, but could save copying over unwanted data (eg password)
- local MyGang = {}
- MyGang.name = info['name']
- MyGang.logo = info['logo']
- MyGang.message = info['message']
- MyGang.points = tonumber(info['points'])
- MyGang.color = Color(0, 0, 0)
- MyGang.color2 = Color(255, 0, 0)
- MyGang.xp = tonumber(info['xp'])
- MyGang.level = tonumber(info['level'])
- MyGang.money = tonumber(info['money'])
- MyGang.hpboost = tonumber(info['hpboost'])
- MyGang.regen = tonumber(info['regen'])
- MyGang.extraitems = tonumber(info['extraitems'])
- MyGang.xpboost = tonumber(info['xpboost'])
- MyGang.speedboost = tonumber(info['speedboost'])
- MyGang.printboost = tonumber(info['printboost'])
- MyGang.drugboost = tonumber(info['drugboost'])
- MyGang.dropboost = tonumber(info['dropboost'])
- -- gang patch
- MyGang.armorboost = tonumber(info['armorboost'])
- --MyGang.memberboost = tonumber(info['memberboost'])
- --MyGang.rivalboost = tonumber(info['rivalboost'])
- MyGang.shopboost = tonumber(info['shopboost'])
- --MyGang.ghostboost = tonumber(info['ghostboost'])
- MyGang.ppocketboost = tonumber(info['ppocketboost'])
- MyGang.bankboost = tonumber(info['bankboost'])
- MyGang.missionboost = tonumber(info['missionboost'])
- MyGang.gangxpboost = tonumber(info['gangxpboost'])
- --MyGang.capboost = tonumber(info['capboost'])
- MyGang.shop_permissions = shop_permissions
- MyGang.members = table.Count(members)
- net.Start("ds_gang_info")
- net.WriteTable(MyGang)
- net.Send(rp)
- end)
- end)
- end)
- end
- --[[
- // Refreshes a gangs info for all online players in the gang
- function UpdateClientGangInfo(gang_name)
- local RP = RecipientFilter()
- // Find all gangmembers
- GangWars.DB.GetPlayerGangMemberList(gang_name,
- function(mlist)
- if not mlist then return false end
- // Find gangmembers who are on the server
- for id, row in pairs(mlist) do
- local p = FindPlayer(row['name'])
- if p and IsValid(p) and p:IsPlayer() then
- RP:AddPlayer(p)
- end
- end
- // Send updates
- Gang.SendInfo(RP, gang_name)
- end)
- end
- // Refreshes a gangs player list for all online players in the gang
- function UpdateClientGangMembers(gang_name)
- local RP = RecipientFilter()
- // Find all gangmembers
- GangWars.DB.GetPlayerGangMemberList(gang_name,
- function(mlist)
- if not mlist then return false end
- // Find gangmembers who are on the server
- for id, row in pairs(mlist) do
- local p = FindPlayer(row['name'])
- if p and IsValid(p) and p:IsPlayer() then
- RP:AddPlayer(p)
- end
- end
- // Send updates
- Gang.SendMemberList(RP, gang_name)
- end)
- end
- ]]
- -- Updates a single players gang info
- function InitPlayerGang(ply, func)
- if not ply or not IsValid(ply) then return false end
- local steamID = ply:SteamID()
- -- find gang status
- isql:QueryRow("SELECT `gang`, `rank` FROM `gw_gang_members` WHERE `steam` = " .. isql:Escape(steamID) .. " ORDER BY `rank` ASC;", function(info)
- if not info or not info.gang or not info.rank then
- -- set no gang
- ply:SetNW2String("gang_name", "")
- ply:SetNW2Int("gang_rank", 0)
- ply:SetNW2Int("gang_level", 0)
- ply:SetNW2String("gang_logo", "")
- ply:SetNW2String("gang_color", "")
- ply:SetNW2String("gang_color2", "")
- ply.GangInfo = nil
- unet.Start("gang_clear", ply)
- unet.Send(ply)
- if func then
- func(false)
- end
- return
- elseif tonumber(info.rank) == 6 then
- -- set invited
- ply:SetNW2String("gang_name", "")
- ply:SetNW2Int("gang_rank", 0)
- ply:SetNW2Int("gang_level", 0)
- ply:SetNW2String("gang_logo", "")
- ply:SetNW2String("gang_color", "")
- ply:SetNW2String("gang_color2", "")
- ply.GangInfo = nil
- unet.Start("gang_clear", ply)
- unet.Send(ply)
- ply:SetNW2Int("gang_invited", true)
- isql:Query("SELECT `gang` FROM gw_gang_members WHERE `steam` = " .. isql:Escape(steamID) .. ";", function(members)
- if members then
- net.Start("ds_gang_invites")
- net.WriteTable(members)
- net.Send(ply)
- end
- end)
- if func then
- func(false)
- end
- return
- else
- -- set member
- GangWars.DB.GetGangInfo(info.gang, function(ginfo)
- if ginfo then
- -- convert ints
- ginfo.level = tonumber(ginfo.level)
- ginfo.rank = tonumber(ginfo.rank)
- for k, v in pairs(ginfo) do
- if tonumber(v) ~= nil then
- ginfo[k] = tonumber(v)
- end
- end
- -- set network vars
- ply:SetNW2String("gang_name", info.gang)
- ply:SetNW2Int("gang_rank", info.rank)
- ply:SetNW2Int("gang_level", ginfo.level)
- ply:SetNW2String("gang_logo", ginfo.logo)
- --ply:SetNW2String("gang_color", ginfo.color)
- --ply:SetNW2String("gang_color2", ginfo.color2)
- ply:SetNW2String("gang_color", "0 0 0")
- ply:SetNW2String("gang_color2", "255 0 0")
- -- attach gang info data to player
- ply.GangInfo = ginfo
- Gang.SendRivalList(ply, info.gang)
- Gang.SendMemberList(ply, info.gang)
- Gang.SendInfo(ply, info.gang)
- ply:GangLogRefresh()
- if func then
- func(true)
- end
- else
- if func then
- func(false)
- end
- end
- end)
- end
- end)
- return ""
- end
- local function DoRefresh(ply, args)
- InitPlayerGang(ply, function()
- ply:GangStorageRefresh()
- end)
- return ""
- end
- AddChatCommand("/gang_refresh", DoRefresh)
- hook.Add("PlayerPickedCharacter", "InitGang", function(ply)
- InitPlayerGang(ply, function()
- ply:GangStorageRefresh()
- ply:GangLogRefresh()
- --MsgN("SELECT `rival`, `members_killed` FROM gw_gang_rivals WHERE `gang` = " .. isql:Escape(ply:Gang()) .. " AND TIME_TO_SEC(TIMEDIFF(NOW(), started))/60/60 > 72;")
- isql:Query("SELECT `rival`, `members_killed` FROM gw_gang_rivals WHERE `gang` = " .. isql:Escape(ply:Gang()) .. " AND TIME_TO_SEC(TIMEDIFF(NOW(), started))/60/60 > 72;", function(q)
- if not q then return end
- --MsgN("looping")
- for k, v in pairs(q) do
- --MsgN("SELECT `members_killed` FROM gw_gang_rivals WHERE `gang` = " .. isql:Escape(v.rival) .. " AND `rival` = " .. isql:Escape(ply:Gang()) .. " AND TIME_TO_SEC(TIMEDIFF(NOW(), started))/60/60 > 72;")
- -- we've found a finished war. check who had more kills
- local our_kills = v.members_killed
- local our_gang = ply:Gang()
- isql:QueryValue("SELECT `members_killed` FROM gw_gang_rivals WHERE `gang` = " .. isql:Escape(v.rival) .. " AND `rival` = " .. isql:Escape(ply:Gang()) .. " AND TIME_TO_SEC(TIMEDIFF(NOW(), started))/60/60 > 72;", function(q2)
- --MsgN("result = " .. tostring(q2))
- if not q2 then return end
- local enemy_kills = q2
- local enemy_gang = v.rival
- --MsgN("fin")
- -- if either team has less than 10 kills?
- if our_kills == enemy_kills then
- NotifyGang(enemy_gang, 4, 4, "The rivalry with " .. our_gang .. " ended in a draw because you both had " .. our_kills .. " kills")
- NotifyGang(our_gang, 4, 4, "The rivalry with " .. enemy_gang .. " ended in a draw because you both had " .. our_kills .. " kills")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(our_gang) .. " AND `rival` = " .. isql:Escape(enemy_gang) .. ";")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(enemy_gang) .. " AND `rival` = " .. isql:Escape(our_gang) .. ";")
- elseif our_kills > enemy_kills then
- if our_kills < 30 then
- NotifyGang(enemy_gang, 4, 4, "The rivalry with " .. our_gang .. " ended with less than 30 kills. Nobody won!")
- NotifyGang(our_gang, 4, 4, "The rivalry with " .. enemy_gang .. " ended with less than 30 kills. Nobody won!")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(our_gang) .. " AND `rival` = " .. isql:Escape(enemy_gang) .. ";")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(enemy_gang) .. " AND `rival` = " .. isql:Escape(our_gang) .. ";")
- else
- NotifyGang(enemy_gang, 4, 4, "You lost the rivalry vs " .. our_gang .. " after 3 days, with " .. enemy_kills .. " kills")
- NotifyGang(our_gang, 4, 4, "You won the rivalry with " .. enemy_gang .. " after 3 days, with " .. our_kills .. " kills")
- GangWars.DB.RivalryWin(our_gang, enemy_gang, true)
- end
- else
- if enemy_kills < 30 then
- NotifyGang(enemy_gang, 4, 4, "The rivalry with " .. our_gang .. " ended with less than 30 kills. Nobody won!")
- NotifyGang(our_gang, 4, 4, "The rivalry with " .. enemy_gang .. " ended with less than 30 kills. Nobody won!")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(our_gang) .. " AND `rival` = " .. isql:Escape(enemy_gang) .. ";")
- isql:Query("DELETE FROM `gw_gang_rivals` WHERE `gang` = " .. isql:Escape(enemy_gang) .. " AND `rival` = " .. isql:Escape(our_gang) .. ";")
- else
- NotifyGang(enemy_gang, 4, 4, "You won the rivalry vs " .. our_gang .. " after 3 days, with " .. enemy_kills .. " kills")
- NotifyGang(our_gang, 4, 4, "You lost the rivalry with " .. enemy_gang .. " after 3 days, with " .. our_kills .. " kills")
- GangWars.DB.RivalryWin(enemy_gang, our_gang, true)
- end
- end
- end)
- end
- end)
- end)
- end)
- -- gang search
- local function GC_Search(ply, args)
- if string.len(args) < 2 or string.len(args) > 32 then
- GangNotify(ply, 3, 4, "Invalid Search")
- return ""
- end
- isql:Query("SELECT `name`, `logo`, `color`, `color2`, `level` FROM gw_gangs WHERE `name` LIKE " .. isql:Escape("%" .. args .. "%") .. " LIMIT 15;", function(q)
- if q then
- for k, v in pairs(q) do
- local col = string.Explode(" ", v.color)
- if #col == 3 then
- v.color = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
- end
- local col = string.Explode(" ", v.color2)
- if #col == 3 then
- v.color2 = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
- end
- end
- net.Start("ds_gang_search")
- net.WriteTable(q)
- net.Send(ply)
- end
- end)
- return ""
- end
- AddChatCommand("/gang_search", GC_Search)
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gang Storage
- local meta = FindMetaTable("Player")
- function meta:GangStorageRefresh()
- if not self.GangInfo or not self.GangInfo.name or self.GangInfo.name == "" then
- self:SetDataset("gang_storage", {})
- else
- self:SetDatasetQueryKeyed("gang_storage", "slot", "SELECT * FROM `view_gang_storage` WHERE `gang` = " .. isql:Escape(self.GangInfo.name) .. ";")
- end
- end
- function meta:AllGangStorageRefresh()
- if not self.GangInfo or not self.GangInfo.name or self.GangInfo.name == "" then return end
- for k, v in pairs(player.GetAll()) do
- if v.GangInfo and v.GangInfo.name and v.GangInfo.name == self.GangInfo.name then
- v:SetDatasetQueryKeyed("gang_storage", "slot", "SELECT * FROM `view_gang_storage` WHERE `gang` = " .. isql:Escape(self.GangInfo.name) .. ";")
- end
- end
- end
- --/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function GangWars.DBX.GangStorageAdd(ply, itemid, slotid, func)
- if not IsValid(ply) or not ply.GangInfo then
- func(nil)
- return
- end
- local steamID = ply:SteamID()
- -- stop exploit where you drop an item & drop from storage
- if ply.DroppingItem or ply.upgrading then
- GangWars.Log("cheat", ply, "JIMMYS Gang Storage Exploit (GSTADD)") -- log jimmy lol
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- if ply.trade then
- Notify(ply, 3, 4, "You can't do this while trading")
- ply:InvRefresh()
- ply:GangStorageRefresh()
- return
- end
- -- race condition exploits
- if ply.RCExploit and ply.RCExploit > CurTime() then
- GangWars.Log("cheat", ply, "Blocked RC (GSADD)")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- ply.RCExploit = CurTime() + 2
- -- no storage until level 5
- if ply.GangInfo.level < 5 then
- Notify(ply, 3, 4, "You can't use gang storage until your gang is level 5")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- -- Note the player is dropping something
- ply.DroppingItem = true
- ply.trade = true
- -- Find the item
- GangWars.DBX.GetInventoryItem(ply, itemid, function(checkitem)
- if not checkitem then
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- -- Can't drop credit items
- if checkitem.glon ~= "" and checkitem.glon ~= "nil" then
- local tmp = util.JSONToTable(checkitem.glon)
- if tmp.bound or tmp.superbound then
- Notify(ply, 4, 4, "You can't put bound items in storage!")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- end
- -- do we have permission?
- local gang = ply.GangInfo.name
- local rank = ply:GetNW2Int("gang_rank")
- -- find permissions for the item
- isql:QueryValue("SELECT `permission` FROM `gw_gang_permissions` WHERE `gang` = " .. isql:Escape(gang) .. " AND `item` = 'storage_add';", function(perm)
- if not perm then
- perm = 0
- end
- -- check our rank has permission to buy
- local function HasItemPerm(perms, rank)
- local d = 16
- local pms = {0, 0, 0, 0, 0}
- local r = 5
- while perms > 0 do
- if perms - d >= 0 then
- perms = perms - d
- pms[r] = 1
- end
- d = d / 2
- r = r - 1
- end
- return pms[rank] == 1
- end
- -- check our rank has permission to buy
- if not HasItemPerm(perm, rank) then
- GangNotify(ply, 3, 4, "You don't have permission to add items to storage!")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- -- add item
- isql:Query("SELECT * FROM `gw_items` WHERE `steam` = " .. isql:Escape(steamID) .. " AND `id` = " .. isql:Escape(itemid), function(d, s, err)
- d = d[1]
- if not d then
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- isql:Query("INSERT INTO `view_gang_storage` (`id`,`steam`, `class`,`ent`,`subclass`,`name`,`model`,`icon`,`value`,`quantity`,`level_lock`,`level`,`glon`,`slot`, `gang`) VALUES(" .. isql:Escape(d.id) .. "," .. isql:Escape(steamID) .. "," .. isql:Escape(d.class) .. "," .. isql:Escape(d.ent) .. "," .. isql:Escape(d.subclass) .. "," .. isql:Escape(d.name) .. "," .. isql:Escape(d.model) .. "," .. isql:Escape(d.icon) .. "," .. isql:Escape(d.value) .. "," .. isql:Escape(d.quantity) .. "," .. isql:Escape(d.level_lock) .. "," .. isql:Escape(d.level) .. "," .. isql:Escape(d.glon or {}) .. "," .. isql:Escape(slotid) .. "," .. isql:Escape(ply.GangInfo.name) .. ")", function(d, s, e)
- isql:Query("DELETE FROM `gw_items` WHERE `steam` = " .. isql:Escape(steamID) .. " AND `id` = " .. isql:Escape(itemid), function(d, s, e)
- GangWars.Log("gangstorage", ply, "{" .. ply:Gang() .. "} Added " .. checkitem.quantity .. "x" .. checkitem.name .. " [" .. checkitem.id .. "]")
- ply:InvRefresh()
- ply:AllGangStorageRefresh()
- ply.DroppingItem = false
- ply.trade = false
- if not s then
- Msg("GangWars.DBX.GangStorageAdd: Failed: " .. err .. "\n")
- end
- end)
- end)
- end)
- end)
- end)
- end
- function GangWars.DBX.GangStorageRemove(ply, itemid, slot)
- if not IsValid(ply) or not ply.GangInfo then
- func(nil)
- return
- end
- local steamID = ply:SteamID()
- -- stop exploit where you drop an item & drop from storage
- if ply.DroppingItem or ply.upgrading then
- GangWars.Log("cheat", ply, "JIMMYS Gang Storage Exploit (GSTREM)") -- log jimmy lol
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- if ply.trade then
- Notify(ply, 3, 4, "You can't do this while trading")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- -- race condition exploits
- if ply.RCExploit and ply.RCExploit > CurTime() then
- GangWars.Log("cheat", ply, "Blocked RC (GSREM)")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- ply.RCExploit = CurTime() + 2
- -- Note the player is dropping something
- ply.DroppingItem = true
- ply.trade = true
- -- do we have permission?
- local gang = ply.GangInfo.name
- local rank = ply:GetNW2Int("gang_rank")
- -- find permissions for the item
- isql:QueryValue("SELECT `permission` FROM `gw_gang_permissions` WHERE `gang` = " .. isql:Escape(gang) .. " AND `item` = 'storage_remove';", function(perm)
- if not perm then
- perm = 0
- end
- -- check our rank has permission to buy
- local function HasItemPerm(perms, rank)
- local d = 16
- local pms = {0, 0, 0, 0, 0}
- local r = 5
- while perms > 0 do
- if perms - d >= 0 then
- perms = perms - d
- pms[r] = 1
- end
- d = d / 2
- r = r - 1
- end
- return pms[rank] == 1
- end
- -- check our rank has permission to buy
- if not HasItemPerm(perm, rank) then
- GangNotify(ply, 3, 4, "You don't have permission to take items from storage!")
- ply:InvRefresh()
- ply:GangStorageRefresh()
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- -- Check for inventory space
- if not ply:HasInvSpace() then
- Notify(ply, 3, 4, "Your inventory is full!")
- ply:InvRefresh()
- ply:GangStorageRefresh()
- ply.DroppingItem = false
- ply.trade = false
- return
- end
- -- Switch item to inventory
- isql:Query("SELECT * FROM `view_gang_storage` WHERE `id` = " .. isql:Escape(itemid) .. " AND `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(ply.GangInfo.name), function(d, s, err)
- d = d[1]
- local ciidata = GangWars.Item.FindByName(d.name):CreateCII()
- ciidata.glon = util.JSONToTable(d.glon)
- ciidata.value = d.value
- ciidata.quantity = d.quantity
- ciidata.level_lock = d.level_lock
- ciidata.level = d.level
- GangWars.DBX.AddInventoryItemCII(ply, ciidata, function(s)
- if s then
- isql:Query("DELETE FROM `view_gang_storage` WHERE `steam` = " .. isql:Escape(steamID) .. " AND `id` = " .. isql:Escape(itemid) .. " AND `gang` = " .. isql:Escape(ply.GangInfo.name), function(d, s, e)
- ply:InvRefresh()
- ply:AllGangStorageRefresh()
- ply.DroppingItem = false
- ply.trade = false
- if not s then
- Msg("GangWars.DBX.GangStorageRemove: Failed: " .. err .. "\n")
- end
- end)
- end
- end, slot, false, true)
- end)
- end)
- end
- function GangWars.DBX.GangStorageMove(ply, itemid, slotid)
- if not IsValid(ply) or not ply.GangInfo then
- func(nil)
- return
- end
- local steamID = ply:SteamID()
- if ply.trade then
- Notify(ply, 3, 4, "You can't do this while trading")
- ply:GangStorageRefresh()
- ply:InvRefresh()
- -- timer.Simple(1, function()
- -- ply:InvRefresh()
- -- end)
- return
- end
- -- Switch item
- isql:Query("UPDATE `view_gang_storage` SET `slot` = " .. isql:Escape(slotid) .. " WHERE `id` = " .. isql:Escape(itemid) .. " AND `steam` = " .. isql:Escape(steamID) .. " AND `gang` = " .. isql:Escape(ply.GangInfo.name), function(d, s, err)
- ply:AllGangStorageRefresh()
- if not s then
- Msg("GangWars.DBX.GangStorageMove: Failed: " .. err .. "\n")
- end
- end)
- end
- --///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- -- Gang Storage Console Commands
- local function DoStorageRefresh(ply, cmd, args)
- ply:GangStorageRefresh()
- return ""
- end
- concommand.Add("gang_storage_refresh", DoStorageRefresh)
- local function DoStorageAdd(ply, cmd, args)
- local id = tonumber(args[1])
- local slot = tonumber(args[2])
- GangWars.DBX.GangStorageAdd(ply, id, slot)
- return ""
- end
- concommand.Add("gang_storage_add", DoStorageAdd)
- local function DoStorageRemove(ply, cmd, args)
- local id = tonumber(args[1])
- local slot = tonumber(args[2])
- GangWars.DBX.GangStorageRemove(ply, id, slot)
- return ""
- end
- concommand.Add("gang_storage_remove", DoStorageRemove)
- local function DoStorageMove(ply, cmd, args)
- local id = tonumber(args[1])
- local slot = tonumber(args[2])
- GangWars.DBX.GangStorageMove(ply, id, slot)
- return ""
- end
- concommand.Add("gang_storage_move", DoStorageMove)
- util.AddNetworkString("gw_tg_request")
- util.AddNetworkString("gw_tg_send")
- net.Receive("gw_tg_request", function(l, p)
- local gdata = {}
- local cache = {}
- if p.LastGangScoreboardRequest and p.LastGangScoreboardRequest < CurTime() then return end
- isql:Query("SELECT `color`, `color2`, `name`, `level`, `points`, `money` FROM `gw_gangs` ORDER BY `points` DESC LIMIT 10", function(d, s, e)
- if s then
- table.insert(gdata, d)
- isql:Query("SELECT * FROM `gw_gang_members`", function(d2, s2, e2)
- if s2 then
- table.insert(cache, d2)
- for index, gang in pairs(gdata) do
- for ind, data in pairs(cache) do
- if data.gang == gang.name then
- if not gdata[index]["players"] then
- gdata[index]["players"] = {}
- end
- table.insert(gdata[index]["players"], data)
- end
- end
- if not gdata[index]["players"] then
- gdata[index]["players"] = {}
- end
- end
- end
- net.Start("gw_tg_send")
- net.WriteTable(gdata)
- net.Send(p)
- end)
- end
- end)
- p.LastGangScoreboardRequest = CurTime() + 290
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement