· 5 years ago · Feb 08, 2020, 06:32 PM
1-- include("../shared/sh_gangs.lua")
2
3/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4// Gamemode Hooks
5
6local doonce = false
7function GangInitTerritories()
8
9 if doonce then return end
10 doonce = true
11
12 for _, t in ipairs( GangTerritory ) do
13 if t.map == string.lower(game.GetMap()) then
14 local ter = ents.Create("gang_territory")
15 ter:SetPos(t.pos)
16
17 ter:SetNWInt("territory_id", _) // array index: GangTerritory[idx]
18 ter.captime = t.captime
19 ter.master = t.master
20 ter.territory = t
21 GangTerritory[_].ent = ter
22
23 ter:Spawn()
24 end
25 end
26
27 // HACK NPC's
28
29 -- local tel = ents.Create("npc_gunsmith")
30 -- tel:SetPos( GetNamedSpawn("gundealer") )
31 -- tel:Spawn()
32
33 local tel = ents.Create("npc_blacksmith")
34 tel:SetPos( GetNamedSpawn("blacksmith") )
35 tel:SetAngles(Angle(0,270,0))
36 tel:Spawn()
37
38 //local tel = ents.Create("npc_armor")
39 -- tel:SetPos( GetNamedSpawn("armorsmith") )
40 -- tel:SetAngles(Angle(0,270,0))
41 -- tel:Spawn()
42
43 local tel = ents.Create("npc_banker")
44 tel:SetPos( GetNamedSpawn("banker") )
45 tel:SetAngles(Angle(0,90,0))
46 tel:Spawn()
47
48 local tel = ents.Create("npc_refiner")
49 tel:SetPos( GetNamedSpawn("refiner") )
50 tel:SetAngles(Angle(0,90,0))
51 tel:Spawn()
52
53 local tel = ents.Create("npc_pothead")
54 tel:SetPos( GetNamedSpawn("pothead") )
55 tel:SetAngles(Angle(0,180,0))
56 tel:Spawn()
57
58 local tel = ents.Create("npc_pothead")
59 tel:SetPos( GetNamedSpawn("smackhead") )
60 tel:SetAngles(Angle(0,90,0))
61 tel:Spawn()
62
63end
64--hook.Add( "InitPostEntity", "GangEntInit", GangInitTerritories )
65timer.Simple(10,function() GangInitTerritories() end)
66
67
68// Handle gang rivalry kills
69local function GangRivalryDeath( victim, weapon, killer )
70
71 // if the killer is on a mass murder mission, cancel all affects & knock out
72 if IsValid(victim) and IsValid(killer) and killer.MissionKills then
73 return
74 end
75
76 if victim:IsPlayer() and killer:IsPlayer() and killer ~= victim and Streetwars.GetLevel(victim) >= 10 then
77
78 local vgang = victim:GetNWString("gang_name")
79 local kgang = killer:GetNWString("gang_name")
80
81 // see if there is a rivalry between killer and victim
82 MySQL.QueryValueEx("SELECT COUNT(*) FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(kgang) .. " AND `rival` = " .. MySQL.SQLStr(vgang) .. ";",
83 function(v)
84 if not v or tonumber(v) < 1 then return end
85
86 // calculate the fine the other gang will pay
87 local vlvl = Streetwars.GetLevel(victim) / 2
88 local fine = (vlvl * vlvl) * 10
89
90 // upgrade adds to the fine
91 if killer.GangInfo then
92 if killer.GangInfo.rivalboost and killer.GangInfo.rivalboost > 0 then
93 local txt,aff = Gang.GetUpgradeEffect("rivalboost", killer.GangInfo.rivalboost-1)
94
95 fine = fine + math.floor((fine/100) * aff)
96 end
97 end
98
99 // add cash to gang stash
100 // !!!!!!!!!!!!!!!
101 local stashes = ents.FindByClass("gang_stash")
102 if stashes then
103 for k,v in pairs(stashes) do
104 if v.gang == kgang then
105 v:AddMoney() // adds % to stash
106 end
107 end
108 end
109 // !!!!!!!!!!!!!!!!!!!!
110
111
112
113 // check they can afford it
114 Streetwars.DB.GangCanAfford(vgang, fine,
115 function(canafford)
116
117 if canafford then
118
119 // fine the gang
120 Streetwars.DB.AddGangMoney(vgang, -fine,
121 function()
122
123 NotifyGang(vgang, 4, 4, "You lost " .. CUR .. Streetwars.FormatMoney(fine) .. " when " .. victim:Nick() .. " died")
124 NotifyGang(kgang, 4, 4, "You gained " .. CUR .. Streetwars.FormatMoney(fine) .. " for killing " .. victim:Nick())
125
126 // update rivalry stats
127 MySQL.QueryEx("UPDATE `sw_gang_rivals` SET `members_killed` = `members_killed` + 1 WHERE `gang` = " .. MySQL.SQLStr(kgang) .. " AND `rival` = " .. MySQL.SQLStr(vgang) .. ";")
128
129 // add XP and money
130 Streetwars.DB.AddGangMoney(kgang, fine)
131 Streetwars.DB.AddGangXP(kgang, fine / 10)
132
133 end)
134 else
135
136 // force the bankrupt gang to lose
137 Streetwars.DB.RivalryWin(kgang, vgang)
138
139 end
140
141 end)
142
143 end)
144
145 end
146
147end
148hook.Add( "PlayerDeath", "GangRivalryDeath", GangRivalryDeath )
149
150//////////////////////////////////////////////////////////////////////////////
151// Database
152
153local function ValidSteam(ply)
154 if not IsValid(ply) then
155 return
156 end
157 local steamID = ply:SteamID()
158 if steamID == "STEAM_ID_PENDING" or steamID == "STEAM_ID_UNKNOWN" or steamID == "UNKNOWN" or steamID == "PENDING" then
159 return
160 end
161 return steamID
162end
163
164// gets the name of the gang a player is in
165function Streetwars.DB.GetPlayerGang(ply, func)
166 local steamID = ValidSteam(ply)
167 if not steamID then
168 func(nil, "Invalid STEAMID")
169 return
170 end
171
172 MySQL.QueryValueEx("SELECT `gang` FROM `sw_gang_members` WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `rank` < 6;",
173 function(r)
174 if r then
175 func( tostring(r) )
176 else
177 func( nil )
178 end
179 end)
180
181end
182
183// gets a players rank within their gang
184function Streetwars.DB.GetPlayerGangRank(ply, func)
185 local steamID = ValidSteam(ply)
186 if not steamID then
187 func(nil, "Invalid STEAMID")
188 return
189 end
190
191 MySQL.QueryValueEx("SELECT `rank` FROM `sw_gang_members` WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `rank` < 6;",
192 function(r)
193 if r then
194 func( tonumber(r) )
195 else
196 func( nil )
197 return
198 end
199 end)
200end
201
202// returns gang name, xp, level, money
203function Streetwars.DB.GetGangInfo(gang_name, func)
204 MySQL.QueryRowEx("SELECT * FROM `sw_gangs` WHERE `name` = ".. MySQL.SQLStr(gang_name) ..";", func)
205end
206
207// gets the list of members of a gang
208function Streetwars.DB.GetPlayerGangMemberList(gang_name, func)
209 MySQL.QueryEx("SELECT `names`.`name` AS `name`, `members`.`rank` AS `rank`, `names`.`steam` AS `steam` FROM `sw_gang_members` `members` INNER JOIN `sw_playerdata` `names` ON `names`.`steam` = `members`.`steam` WHERE `members`.`gang` = ".. MySQL.SQLStr(gang_name) ..";", func)
210end
211
212// gets the list of rivals of a gang
213function Streetwars.DB.GetGangRivalList(gang_name, func)
214 MySQL.QueryEx("SELECT g.name, g.logo, g.color, g.color2, g.level, riv.* FROM `sw_gang_rivals` riv INNER JOIN `sw_gangs` g ON g.name = riv.rival WHERE riv.gang = ".. MySQL.SQLStr(gang_name) ..";", func)
215end
216
217function Streetwars.DB.CreateGang(ply, gang_name, gang_password, func)
218 local steamID = ValidSteam(ply)
219 if not steamID then
220 func(nil, "Invalid STEAMID")
221 return
222 end
223
224 // reject bad gang names
225 local allowedCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:-1234567890 "
226 for i=1, string.len(gang_name) do
227
228 local ok = false
229
230 for j=1, string.len(allowedCharacters) do
231 if string.sub(gang_name, i, i) == string.sub(allowedCharacters, j, j) then
232 ok = true
233 break
234 end
235 end
236
237 if not ok then
238 func(nil, "Gang name contains illegal symbol at character #" .. i )
239 return
240 end
241 end
242
243 // check gang name doesn't exist
244 MySQL.QueryValueEx("SELECT * FROM sw_gangs WHERE name = ".. MySQL.SQLStr(gang_name) ..";",
245 function(r)
246 if r then
247 func(nil, "Gang already exists!")
248 return
249 end
250
251 // check we're not already in a gang
252 local g = Streetwars.DB.GetPlayerGang(ply,
253 function(g)
254 if g and g ~= "" then
255 func(nil, "You are already in a gang")
256 return
257 end
258
259 // delete any old invites we had
260 MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steamID) .. ";",
261 function()
262
263 // create gang
264 MySQL.QueryEx("INSERT INTO sw_gangs (`name`, `password`, `logo`, `color`, `color2`, `level`, `xp`, `money`) VALUES(" .. MySQL.SQLStr(gang_name) .. ", " .. MySQL.SQLStr(gang_password) .. ", " .. MySQL.SQLStr(gang_logo) .. ", '" .. 255 .. " " .. 255 .. " " .. 255 .. "', '" .. 255 .. " " .. 255 .. " " .. 255 .. "', 1, 0, 0);",
265 function()
266
267 // and owner
268 MySQL.QueryEx("INSERT INTO sw_gang_members (`gang`, `steam`, `rank`) VALUES(" .. MySQL.SQLStr(gang_name) .. ", " .. MySQL.SQLStr(steamID) .. ", 1);",
269 function()
270 func(true)
271 end)
272
273 end)
274 end)
275
276 end)
277
278 end)
279
280end
281
282// sets a players rank within their gang
283function Streetwars.DB.SetPlayerGangRank(ply, gang, rank, func)
284 local steamID = ValidSteam(ply)
285 if not steamID then
286 func(nil, "Invalid STEAMID")
287 return
288 end
289
290 if rank < 1 or rank > 5 then
291 func(nil, nil)
292 return
293 end
294
295 MySQL.QueryEx("UPDATE sw_gang_members SET `rank`= "..MySQL.SQLStr(rank).." WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `gang` = ".. MySQL.SQLStr(gang) ..";",
296 function()
297 func( true )
298 end)
299
300end
301
302function Streetwars.DB.GetGangMemberLimit(gang_name, func)
303 // check gang hasn't reached its member limit
304 MySQL.QueryEx("SELECT `level`, `boost_members` FROM sw_gangs WHERE `name` = " .. MySQL.SQLStr(gang_name) ..";",
305 function(glevel)
306 if not glevel then
307 func()
308 return
309 end
310 glevel = tonumber(glevel.level)
311 local members = tonumber(glevel.boost_members)
312
313 func( Gang.GetMemberLimit(glevel) + members )
314 end)
315end
316
317function Streetwars.DB.InviteToGang(ply, gang_name, func)
318 local steamID = ValidSteam(ply)
319 if not steamID then
320 func(nil, "Invalid STEAMID")
321 return
322 end
323
324 // check they aren't in a gang
325 MySQL.QueryValueEx("SELECT * FROM sw_gang_members WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `rank` < 6;",
326 function(r)
327 if r then
328 func(nil, "Player is already in another gang")
329 return
330 end
331
332 // check gang hasn't reached its member limit
333 MySQL.QueryRowEx("SELECT `level`, `memberboost` FROM sw_gangs WHERE `name` = " .. MySQL.SQLStr(gang_name) ..";",
334 function(glevel)
335 if not glevel then
336 func(nil, "Couldn't find gang level or member boost")
337 return
338 end
339
340 local members = (tonumber(glevel.memberboost) * 2) + Gang.GetMemberLimit(tonumber(glevel.level))
341
342 glevel = tonumber(glevel.level)
343
344 MySQL.QueryValueEx("SELECT COUNT(*) FROM sw_gang_members WHERE `gang` = " .. MySQL.SQLStr(gang_name) ..";",
345 function(mcount)
346 mcount = tonumber(mcount)
347
348 if not glevel or not mcount or mcount >= members then
349 func(nil, "Your gang has reached its member limit! (Max ".. members .." members)")
350 return
351 end
352
353 // add them with invited rank
354 MySQL.QueryEx("INSERT INTO sw_gang_members (`gang`, `steam`, `rank`) VALUES(" .. MySQL.SQLStr(gang_name) .. ", " .. MySQL.SQLStr(steamID) .. ", 6);",
355 function()
356 func(true)
357 end)
358
359 end)
360
361 end)
362
363 end)
364
365end
366
367function Streetwars.DB.AcceptInvite(ply, gang, func)
368 local steamID = ValidSteam(ply)
369 if not steamID then
370 func(nil, "Invalid STEAMID")
371 return
372 end
373
374 // check they are being invited to the gang
375 MySQL.QueryValueEx("SELECT `rank` FROM sw_gang_members WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `gang` = ".. MySQL.SQLStr(gang) ..";",
376 function(r)
377 if not r or tonumber(r) ~= 6 then
378 func(nil, "Not invited to that gang!")
379 return
380 end
381
382 // add them with basic rank
383 MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steamID) .. " AND `gang` != ".. MySQL.SQLStr(gang) ..";",
384 function()
385
386 // add them with basic rank
387 MySQL.QueryEx("UPDATE sw_gang_members SET `rank` = '5' WHERE `steam` = " .. MySQL.SQLStr(steamID) .. " AND `gang` = ".. MySQL.SQLStr(gang) ..";",
388 function()
389 func(true)
390 end)
391
392 end)
393
394 end)
395end
396
397// gets the list of members of a gang
398function Streetwars.DB.GetPlayerGangMemberListSimple(gang_name)
399
400 return MySQL.Query("SELECT `names`.`name` AS `name`, `members`.`rank` AS `rank`, `names`.`steam` AS `steam` FROM `sw_gang_members` `members` INNER JOIN `sw_playerdata` `names` ON `names`.`steam` = `members`.`steam` WHERE `members`.`gang` = ".. MySQL.SQLStr(gang_name) ..";", func)
401
402end
403
404function Streetwars.DB.RejectInvite(ply, gang, func)
405 local steamID = ValidSteam(ply)
406 if not steamID then
407 func(nil, "Invalid STEAMID")
408 return
409 end
410
411 // check they are being invited to the gang
412 MySQL.QueryValueEx("SELECT `rank` FROM sw_gang_members WHERE `steam` = ".. MySQL.SQLStr(steamID) .." AND `gang` = ".. MySQL.SQLStr(gang) ..";",
413 function(r)
414 if not r or tonumber(r) ~= 6 then
415 func(nil, "Not invited to that gang!")
416 return
417 end
418
419 // remove invite
420 MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steamID) .. " AND `gang` = ".. MySQL.SQLStr(gang) ..";",
421 function()
422 func(true)
423 end)
424
425 end)
426end
427
428
429function Streetwars.DB.RemoveFromGangBySteam(steam, gang, func)
430 MySQL.QueryRowEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steam) .. " AND `gang` = " .. MySQL.SQLStr(gang) .. ";", func)
431end
432function Streetwars.DB.RemoveFromGangByName(ply_name, gang, func)
433 // find the player
434 MySQL.QueryRowEx("SELECT n.name, n.steam, g.gang, g.rank FROM sw_playerdata n INNER JOIN sw_gang_members g ON g.steam = n.steam WHERE n.name LIKE " .. MySQL.SQLStr(ply_name) .. ";",
435 function(query)
436
437 if not query or not query.name then
438 func(nil, "Can't find target players name")
439 return
440 end
441
442 local steamID = query.steam
443 if not steamID or steamID == "" then
444 func(nil, "Can't find target players steamID")
445 return
446 end
447
448 local gang_name = query.gang
449 if not gang_name or gang_name == "" or gang_name != gang then
450 func(nil, "Can't find target players gang")
451 return
452 end
453
454 // remove them
455 MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steamID) .. ";",
456 function()
457 func(true)
458 end)
459
460 // if they were owner, delete gang & all members
461 //if tonumber(rank) == 1 then
462 // MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `gang` = " .. MySQL.SQLStr(gang_name) .. ";")
463 // MySQL.QueryEx("DELETE FROM sw_gangs WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";")
464 //end
465
466 end)
467
468end
469function Streetwars.DB.RemoveFromGang(ply, func)
470 local steamID = ValidSteam(ply)
471 if not steamID then
472 func(nil, "Invalid STEAMID")
473 return
474 end
475
476 local gang_name = Streetwars.DB.GetPlayerGang(ply,
477 function(gang_name)
478 if not gang_name then
479 func(nil, "Couldn't find gang")
480 return
481 end
482
483 // remove them
484 MySQL.QueryEx("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(steamID) .. ";",
485 function()
486 func(true)
487 end)
488
489 // if they were owner, delete gang & all members
490 //if tonumber(rank) == 1 then
491 // MySQL.Query("DELETE FROM sw_gang_members WHERE gang = " .. MySQL.SQLStr(gang_name) .. ";")
492 // MySQL.Query("DELETE FROM sw_gangs WHERE name = " .. MySQL.SQLStr(gang_name) .. ";")
493 //end
494
495 end)
496
497end
498
499/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
500// Get SteamID from RPName
501function Streetwars.DB.RetrieveSteamID(name)
502 if name == nil then return end
503 return MySQL.QueryValue("SELECT `steam` FROM sw_playerdata WHERE `name` = " .. MySQL.SQLStr(name) .. ";")
504end
505
506
507
508function Streetwars.DB.RivalryWin(winner, loser)
509
510 local args = loser
511 local gang = winner
512
513 // see if other gang is our rival
514 MySQL.QueryValueEx("SELECT COUNT(*) FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
515 function(truce)
516 if not truce or tonumber(truce) <= 0 then
517 //GangNotify(ply, 4, 4, "You don't seem to have a rivalry with " .. args)
518 return
519 end
520
521 MsgN("RivalryWin()")
522
523 // remove the rivalry
524 MySQL.QueryEx("DELETE FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. ";",
525 function()
526
527 MySQL.QueryEx("DELETE FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
528 function()
529
530 MsgN("Deleted rivalry entires")
531
532 // find level difference
533 MySQL.QueryValueEx("SELECT `level` FROM `sw_gangs` WHERE `name` = " .. MySQL.SQLStr(gang) .. ";",
534 function(my_level)
535
536 MySQL.QueryValueEx("SELECT `level` FROM `sw_gangs` WHERE `name` = " .. MySQL.SQLStr(args) .. ";",
537 function(their_level)
538
539 if not their_level then
540 Msg("WARNING: " .. loser .. " requested mercy but can't find level for them\n")
541 end
542 if not my_level then
543 Msg("WARNING: " .. loser .. " requested mercy but can't find level for " .. winner .. "\n")
544 end
545 if not their_level or not my_level then
546 return
547 end
548
549 MsgN("Awarding points")
550
551 // find level difference
552 local lvl_diff = their_level - my_level
553
554 // find points
555 local points = math.max(10000 + (1000 * lvl_diff), 0)
556
557 // award points
558 Streetwars.DB.AddGangPoints(gang, points, function() end)
559 Streetwars.DB.AddGangPoints(args, -points, function() end)
560
561 // refresh their view
562 NotifyGang(gang, 4, 4, "You gained " .. points .. " points for defeating " .. args)
563 NotifyGang(args, 4, 4, "You have lost the fight with " .. gang .. " and lost " .. points .. " points")
564
565 // add norivalry entry (5 days)
566 MySQL.QueryEx("INSERT INTO `sw_gang_norival` (`gang`, `rival`, `expires`) VALUES (" .. MySQL.SQLStr(gang) .. ", " .. MySQL.SQLStr(args) .. ", DATE_ADD(NOW(), INTERVAL 5 DAY));")
567
568 end)
569
570 end)
571
572 end)
573
574 end)
575
576 end)
577end
578
579// adds to a gangs XP
580function Streetwars.DB.AddGangXP(gang_name, add_xp)
581 if not gang_name or gang_name == "" then return end
582
583 // find the gangs xp & level
584
585 MySQL.QueryRowEx("SELECT `xp`, `level`, `gangxpboost` FROM sw_gangs WHERE `name` = ".. MySQL.SQLStr(gang_name) ..";",
586 function(r)
587 if not r then return end
588
589 local xp = tonumber(r['xp'])
590 local level = tonumber(r['level'])
591 local boost = tonumber(r['gangxpboost'])
592
593 if level >= 20 then return end
594
595 // boost XP
596 if boost and boost > 0 then
597 local txt,aff = Gang.GetUpgradeEffect("missionboost", boost-1)
598 add_xp = add_xp + math.floor((add_xp/100) * aff)
599 end
600
601 // add the XP
602 xp = xp + add_xp
603
604 // level up gang
605 if xp > Gang.XP2Level(level) then
606 MySQL.QueryEx("UPDATE sw_gangs SET `xp` = 0, `level` = ".. (level+1) .." WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";")
607
608 MySQL.QueryRowEx("SELECT `xp` FROM sw_gangs WHERE `name` = ".. MySQL.SQLStr(gang_name) ..";", -- log to ganglog to make sure xp was reset.
609 function(t)
610 if not t then return end
611
612 local xp2 = tonumber(t['xp'])
613 Streetwars.GangLog("system", "info", gang_name .. " has levelled up to level " .. (level+1) .. " resetting their xp to " .. xp2 .. ".")
614 end)
615 else
616
617 MySQL.QueryEx("UPDATE sw_gangs SET `xp` = ".. xp .." WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";")
618 end
619
620 end)
621end
622
623// adds to a gangs money
624function Streetwars.DB.AddGangMoney(gang_name, money, func)
625 if not gang_name or gang_name == "" then
626 if func then func(nil) end
627 return
628 end
629
630 MySQL.QueryEx("UPDATE sw_gangs SET `money` = `money` + ".. MySQL.SQLStr(tonumber(money)) .." WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";", func)
631end
632
633// adds to a gangs points
634function Streetwars.DB.AddGangPoints(gang_name, points, func)
635 if not gang_name or gang_name == "" then
636 if func then func(nil) end
637 return
638 end
639
640 MySQL.QueryEx("UPDATE sw_gangs SET `points` = `points` + ".. MySQL.SQLStr(tonumber(points)) .." WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";", func)
641end
642
643function Streetwars.DB.GetGangPoints(gang_name)
644 if not gang_name or gang_name == "" then return end
645
646 local xp = MySQL.QueryValue("SELECT `points` FROM sw_gangs WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";")
647 if xp then
648 return tonumber(xp)
649 else
650 return -1000
651 end
652end
653
654// checks gang money
655function Streetwars.DB.GangCanAfford(gang_name, money, func)
656 if not gang_name or gang_name == "" then return end
657
658 MySQL.QueryValueEx("SELECT `money` FROM sw_gangs WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";",
659 function(mon)
660 if mon and tonumber(mon) >= money then
661 func(true)
662 else
663 func(false)
664 end
665 end)
666end
667
668// upgrades a gang
669function Streetwars.DB.GangUpgrade(gang_name, upgrade, func)
670 if not gang_name or gang_name == "" then
671 func(nil, nil)
672 return
673 end
674
675 // check the sql input!
676 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 ~= "rivalboost" and upgrade ~= "shopboost" and upgrade ~= "ghostboost" and upgrade ~= "ppocketboost" and upgrade ~= "bankboost" and upgrade ~= "missionboost" and upgrade ~= "gangxpboost" and upgrade ~= "capboost" then
677 func(nil, "Invalid upgrade")
678 return
679 end
680
681 // find the upgrade level
682 MySQL.QueryValueEx("SELECT `".. upgrade .."` FROM sw_gangs WHERE `name` = ".. MySQL.SQLStr(gang_name) ..";",
683 function(ulevel)
684 ulevel = tonumber(ulevel)
685 if not ulevel or ulevel >= 10 then
686 func(nil, "This is already fully upgraded")
687 return
688 end
689
690 // find the gangs money
691 MySQL.QueryRowEx("SELECT `money`, `level` FROM sw_gangs WHERE `name` = ".. MySQL.SQLStr(gang_name) ..";",
692 function(r)
693 if not r then
694 func(nil, "Can't find your gang?")
695 return
696 end
697 local amt = tonumber(r['money'])
698 local glevel = tonumber(r['level'])
699
700 // check gang is high enough level
701 if glevel <= ulevel then
702 func(nil, "Gang isn't high enough level")
703 return
704 end
705
706 // find upgrade cost
707 local price = Gang.GetUpgradePrice(ulevel)
708
709 // check we can afford it
710 if amt < price then
711 func(nil, "Gang can't afford that")
712 return
713 end
714
715 // charge the team
716 amt = amt - price
717
718 // save and upgrade
719 MySQL.QueryEx("UPDATE sw_gangs SET `money` = ".. amt ..", `"..upgrade.."` = "..(ulevel+1).." WHERE `name` = " .. MySQL.SQLStr(gang_name) .. ";",
720 function()
721 func( true )
722 end)
723
724 end)
725
726 end)
727
728end
729
730
731//////////////////////////////////////////////////////////////////////////////
732// Commands
733
734// change logo
735local function GC_ChangeLogo( ply, handler, id, encoded, decoded )
736
737 if not decoded or not decoded.logo then return end
738
739 local gname = "extra/" .. decoded.logo
740
741 /*
742 if not string.find(gname, "extra") then
743 GangNotify(ply, 4, 4, "Invalid logo... you've been logged, thank you!")
744 Streetwars.Log("cheat", ply, ply:Nick() .. " tried to set gang logo to '" .. gname .. "'")
745 return
746 end
747 */
748
749 local steamID = ValidSteam(ply)
750 if not steamID then
751 return
752 end
753
754 local gang = ply:GetNWString("gang_name")
755
756 // find gang info
757 Streetwars.DB.GetGangInfo(gang,
758 function(info)
759
760 if not info then
761 Msg("GC_ChangeLogo: Streetwars.DB.GetGangInfo returned nil")
762 return
763 end
764
765 local gangmoney = tonumber(info.money)
766 local ganglevel = tonumber(info.level)
767
768 local logoprice = 500000 + tonumber(decoded.logo) * 200000
769 //local logolevel = 5 + math.floor(tonumber(decoded.logo) / 10) * 5
770 local logolevel = math.min(10 + math.floor(tonumber(decoded.logo) / 10) * 5, 20)
771
772 // abandon if we're not high enough level
773 if logolevel > ganglevel then
774 GangNotify(ply, 4, 4, "Your gang must be level " .. logolevel .. " to buy that logo!")
775 return
776 end
777
778 // abandon if we can't afford it
779 if gangmoney < logoprice then
780 GangNotify(ply, 1, 4, "Your gang can not afford this logo!")
781 return
782 end
783
784 // charge gang
785 Streetwars.DB.AddGangMoney(gang, -logoprice,
786 function()
787
788 // create gang
789 MySQL.QueryEx("UPDATE sw_gangs SET `logo` = " .. MySQL.SQLStr(gname) .. " WHERE `name` = " .. MySQL.SQLStr(info.name) .. ";",
790 function()
791
792 GangNotify(ply, 4, 4, "You successfully changed your gangs logo!")
793 InitPlayerGang( ply )
794 end)
795
796 end)
797
798 end)
799
800end
801net.Receive("ChangeGangLogo", GC_ChangeLogo);
802
803// change password
804local function GC_ChangePassword( length, ply)
805
806 decoded = net.ReadTable()
807 local steamID = ValidSteam(ply)
808 if not steamID then
809 return
810 end
811
812 if decoded and not decoded.old or not decoded.new then return end
813
814 // find ranks
815 Streetwars.DB.GetPlayerGangRank(ply,
816 function(my_rank)
817 if not my_rank or my_rank ~= 1 then
818 GangNotify(ply, 4, 4, "You must be gang leader to change the password")
819 return
820 end
821
822 // find gang info
823 Streetwars.DB.GetGangInfo(ply:GetNWString("gang_name"),
824 function(info)
825
826 if not info then
827 Msg("GC_ChangePassword: Streetwars.DB.GetGangInfo returned nil")
828 return
829 end
830
831 local gangpass = info.password
832
833 // abandon if old password does not match
834 if tostring(gangpass) != tostring(util.CRC(decoded.old)) then
835 GangNotify(ply, 4, 4, "Old password was incorrect!")
836 return
837 end
838
839 // charge gang
840 //Streetwars.DB.AddGangMoney(gang, -500000,
841 //function()
842
843 // create gang
844 MySQL.QueryEx("UPDATE sw_gangs SET `password` = " .. MySQL.SQLStr(util.CRC(decoded.new)) .. " WHERE `name` = " .. MySQL.SQLStr(info.name) .. ";",
845 function()
846
847 Streetwars.GangLog(ply, "info", ply:Nick() .. " has changed the password to " .. info.name)
848 GangNotify(ply, 4, 4, "You successfully changed your gangs password!")
849
850 end)
851
852 //end)
853
854 end)
855 end)
856
857end
858net.Receive("ChangeGangPassword", GC_ChangePassword);
859
860// recover gang
861local function GC_Recover( length, ply )
862 decoded = net.ReadTable()
863
864 local steamID = ValidSteam(ply)
865 if not steamID then
866 return
867 end
868
869 if decoded and not decoded.name or not decoded.password then return end
870
871 // find gang info
872 Streetwars.DB.GetGangInfo(decoded.name,
873 function(info)
874
875 if not info then
876 Msg("GC_Recover: Streetwars.DB.GetGangInfo returned nil")
877 return
878 end
879
880 local gangpass = info.password
881
882 // abandon if old password does not match
883 if tostring(gangpass) != tostring(util.CRC(decoded.password)) then
884 GangNotify(ply, 4, 4, "The password was incorrect! This action has been logged and sent to the admin team.")
885 Streetwars.Log("gang_recover", ply, ply:Nick() .. " tried to recover the gang '" .. decoded.name .. "' with a bad password")
886 Streetwars.GangLog(ply, "info", ply:Nick() .. " attempted to recover gang '" .. decoded.name .. "' with a bad password")
887 return
888 end
889
890
891 // Remove all members. Set us as the only member/leader.
892 MySQL.QueryValueEx("SELECT COUNT(*) FROM `sw_gang_members` WHERE `gang` = " .. MySQL.SQLStr(info.name) .. " AND `rank` = 1;",
893 function(row)
894
895 if tonumber( row ) and tonumber( row ) > 0 then
896 GangNotify(ply, 4, 4, "The gang already has a leader! This action has been logged and sent to the admin team.")
897 Streetwars.Log("gang_recover", ply, ply:Nick() .. " tried to recover the gang '" .. decoded.name .. "' but it already has a leader")
898 Streetwars.GangLog(ply, "info", ply:Nick() .. " attempted to recover gang '" .. decoded.name .. "' but it already has a leader")
899 return
900 end
901
902 // create gang
903 MySQL.QueryEx("INSERT INTO `sw_gang_members` (`steam`, `gang`, `rank`) VALUES(" .. MySQL.SQLStr(steamID) .. ", " .. MySQL.SQLStr(info.name) .. ", 1);",
904 function()
905
906 InitPlayerGang(ply)
907 GangNotify(ply, 4, 4, "You successfully recovered the " .. info.name .. " gang!")
908 Streetwars.Log("gang_recover", ply, ply:Nick() .. " successfully recovered the gang '" .. decoded.name .. "'")
909 Streetwars.GangLog(ply, "info", ply:Nick() .. " has successfully recovered the gang '" .. decoded.name .. "'")
910
911 end)
912
913 end)
914
915 end)
916
917end
918net.Receive("RecoverGang", GC_Recover);
919
920// /create <gang name>
921
922local function GC_Create( Length, ply )
923
924 decoded = net.ReadTable()
925
926 local gname = decoded.name
927
928 if string.len(gname) > 20 then
929 GangNotify(ply, 4, 4, "Gang name must not exceed 20 characters!")
930 return ""
931 end
932
933 local price = 100000
934 if Streetwars.GetLevel(ply) >= 10 and ply:CanAfford(price) then
935 Streetwars.DB.CreateGang(ply, gname, decoded.password,
936 function(q, msg)
937 if q then
938 ply:AddMoney(-price)
939 InitPlayerGang(ply)
940 Streetwars.GangLog(ply, "info", ply:Nick() .. " has created " .. gname)
941 GangNotify(ply, 4, 4, "Congratulations! You have started the " .. gname .. " gang!")
942 else
943 if msg then
944 GangNotify(ply, 4, 4, "Could not create gang. " .. msg)
945 else
946 GangNotify(ply, 4, 4, "Could not create gang.")
947 end
948 end
949 end)
950 else
951 GangNotify(ply, 4, 4, "You must be level 10 and have " .. CUR .. "100,000 to create a gang")
952 end
953
954end
955net.Receive("CreateGang", GC_Create);
956
957// /leave gang
958local function GC_Leave(ply, args)
959
960 // find our gang name
961 Streetwars.DB.GetPlayerGang(ply,
962 function(gang)
963 if not gang then return end
964
965 Streetwars.DB.RemoveFromGang(ply,
966 function(q)
967 //timer.Simple(1, function () InitPlayerGang(ply) end )
968 //timer.Simple(1, function() UpdateClientGangInfo(gang) end)
969 //timer.Simple(1, function() UpdateClientGangMembers(gang) end)
970
971 if q then
972 GangNotify(ply, 4, 4, "You have left the " .. gang .. " gang")
973 Streetwars.GangLog(ply, "info", ply:Nick() .. " has left " .. gang)
974 InitPlayerGang(ply)
975 end
976 end)
977 end)
978
979
980 return ""
981end
982AddChatCommand("/gang_leave", GC_Leave)
983
984// /donate to gang
985local function GC_Donate(ply, args)
986 // find our gang name
987 Streetwars.DB.GetPlayerGang(ply,
988 function(gang)
989 if not gang then return "" end
990
991 local money = tonumber(args)
992 if money < 0 then return "" end
993
994 if not ply:CanAfford(money) then
995 GangNotify(ply, 4,4, "You don't have that much to donate!")
996 return ""
997 end
998
999 // swap money
1000 Streetwars.DB.AddGangMoney(gang, money,
1001 function()
1002
1003 InitPlayerGang( ply )
1004
1005 ply:AddMoney( -money )
1006
1007 Streetwars.GangLog(ply, "money", ply:Nick() .. " has donated " .. CUR..Streetwars.FormatMoney(money) .. " to " .. gang)
1008 GangNotify(ply, 4, 4, "You have donated " .. CUR..Streetwars.FormatMoney(money) .. " to your gang")
1009 end)
1010 end)
1011
1012 return ""
1013end
1014AddChatCommand("/gang_donate", GC_Donate)
1015
1016// upgrade gang
1017local function GC_Upgrade(ply, args)
1018 // find our gang name
1019 Streetwars.DB.GetPlayerGang(ply,
1020 function(gang)
1021 if not gang then return end
1022
1023 // find ranks
1024 Streetwars.DB.GetPlayerGangRank(ply,
1025 function(my_rank)
1026 if not my_rank or my_rank > 2 then
1027 GangNotify(ply, 4, 4, "You must be at least vice leader to purchase upgrades")
1028 return
1029 end
1030
1031 // do upgrade
1032 Streetwars.DB.GangUpgrade(gang, args,
1033 function (q, msg)
1034 if q then
1035 //timer.Simple(2, function() UpdateClientGangInfo(gang) end)
1036 InitPlayerGang( ply )
1037 Streetwars.GangLog(ply, "info", ply:Nick() .. " has upgraded " .. gang .. "'s " .. args)
1038 GangNotify(ply, 4, 4, "You have upgraded your gang")
1039 else
1040 if msg then
1041 GangNotify(ply, 4, 4, "You can't purchase that upgrade. " .. msg)
1042 else
1043 GangNotify(ply, 4, 4, "You can't purchase that upgrade")
1044 end
1045 end
1046 end)
1047 end)
1048 end)
1049 return ""
1050end
1051AddChatCommand("/gang_upgrade", GC_Upgrade)
1052
1053// /invite <player>
1054local function GC_Invite(ply, args)
1055 // find our gang
1056 Streetwars.DB.GetPlayerGang(ply,
1057 function(gang)
1058 if not gang then
1059 GangNotify(ply, 4, 4, "You aren't in a gang!")
1060 return
1061 end
1062
1063 // find our target
1064 local target = FindPlayer(args)
1065 if not target then
1066 GangNotify(ply, 4, 4, "Can't find that player to invite")
1067 return ""
1068 end
1069
1070 // find ranks
1071 Streetwars.DB.GetPlayerGangRank(ply,
1072 function(my_rank)
1073 if not my_rank or my_rank > 2 then
1074 GangNotify(ply, 4, 4, "You must be leader or vice leader to invite")
1075 return ""
1076 end
1077
1078 // invite them
1079 Streetwars.DB.InviteToGang(target, gang,
1080 function(q, msg)
1081 if q then
1082 //timer.Simple(2, function () InitPlayerGang(target) end )
1083 //timer.Simple(2, function() UpdateClientGangInfo(gang) end)
1084 //timer.Simple(2, function() UpdateClientGangMembers(gang) end)
1085
1086
1087 net.Start("gang_invite")
1088 net.WriteString(gang)
1089 net.Send(target)
1090
1091 InitPlayerGang( ply )
1092 InitPlayerGang( target )
1093
1094 Streetwars.GangLog(ply, "info", ply:Nick() .. " has invited " .. target:Nick() .. " to join " .. gang)
1095 GangNotify(ply, 4, 4, "You have invited " .. target:Nick() .. " to join " .. gang)
1096 GangNotify(target, 4, 4, "You have been invited to join " .. gang)
1097 else
1098 if msg then
1099 GangNotify(ply, 4, 4, "Invite failed. " .. msg)
1100 else
1101 GangNotify(ply, 4, 4, "Invite failed.")
1102 end
1103 end
1104 end)
1105 end)
1106 end)
1107 return ""
1108end
1109AddChatCommand("/gang_invite", GC_Invite)
1110
1111// /kick <player>
1112local function GC_Kick(ply, args)
1113 // find our gang name
1114
1115 gang = MySQL.QueryValue("SELECT `gang` FROM `sw_gang_members` WHERE `steam` = ".. MySQL.SQLStr(ply:SteamID()) .." AND `rank` < 6;")
1116 target = Streetwars.DB.RetrieveSteamID(args)
1117
1118 // find our target
1119 if IsValid( FindPlayer(target) ) then
1120 targetonline = true
1121 else
1122 targetonline = false
1123 end
1124
1125
1126
1127Streetwars.DB.GetPlayerGangMemberList(gang,
1128 function(MyGangMembers)
1129
1130
1131
1132
1133
1134 /// Getting our rank from the same table as we got his from
1135 for k,v in pairs(MyGangMembers) do
1136 if v.steam == ply:SteamID() then
1137
1138 my_rank = v.rank
1139 break
1140
1141 end
1142 end
1143
1144
1145
1146 --local other_rank = 5
1147 --local rank_ok = true
1148
1149 // rank setting permissions. leaders can always kick
1150 if my_rank == 1 then
1151
1152
1153 --// vice leaders can kick captains
1154 --elseif my_rank == 2 and other_rank > 2 then
1155 -- rank_ok = true
1156
1157 --// captains can kick vices
1158 --elseif my_rank == 3 and other_rank > 3 then
1159 -- rank_ok = true
1160
1161 --end
1162
1163 --// check the target isn't higher ranking than us if we're not leader
1164 --if my_rank > 1 and other_rank >= my_rank then
1165 -- rank_ok = false
1166 --
1167 --end
1168
1169 --// invites can be kicked by leaders
1170 --if other_rank >= 6 and my_rank > 1 then
1171 -- rank_ok = false
1172 --end
1173
1174 --if not rank_ok then
1175 -- GangNotify(ply, 4, 4, "You don't have the rank to do that")
1176 -- return
1177 --end
1178
1179
1180 // remove them
1181 MySQL.Query("DELETE FROM sw_gang_members WHERE `steam` = " .. MySQL.SQLStr(target) .. " AND `gang` = " .. MySQL.SQLStr(gang) .. ";")
1182
1183 InitPlayerGang( ply )
1184 Streetwars.GangLog(ply, "info", ply:Nick() .. " has kicked " .. args .. " from " .. gang)
1185 GangNotify(ply, 4, 4, "You have kicked " .. args .. " from the gang")
1186
1187
1188
1189 if targetonline then
1190
1191 for k,v in pairs(player.GetAll()) do
1192
1193 if v:SteamID() == target then
1194 InitPlayerGang(v)
1195 v:SetNWInt("gang_rank", 0)
1196 GangNotify(v, 4, 4, "You have been kicked from the " .. gang .. " gang")
1197
1198 end
1199 end
1200 end
1201 end
1202 end)
1203 return ""
1204end
1205AddChatCommand("/gang_kick", GC_Kick)
1206
1207// /promote <rank> <player>
1208function GC_Promote(len, ply)
1209 // find our gang name
1210
1211 --if #args ~= 2 then return "" end
1212
1213 local ptype = net.ReadFloat()
1214 local targname = net.ReadString()
1215 --print(args[1])
1216
1217 _G.MyGangsMembers = {}
1218
1219
1220 gang = MySQL.QueryValue("SELECT `gang` FROM `sw_gang_members` WHERE `steam` = ".. MySQL.SQLStr(ply:SteamID()) .." AND `rank` < 6;")
1221 --print(gang)
1222
1223 target = Streetwars.DB.RetrieveSteamID(targname)
1224 --print(args[1])
1225 --print(args[2])
1226 --print(target)
1227
1228 // find our target
1229 if not FindPlayer(targname) then
1230 targetonline = false
1231 --print("offline")
1232 else
1233 targetonline = true
1234 --print("online")
1235 onlinetarg = FindPlayer(targname)
1236 end
1237
1238
1239 if target == ply:SteamID() then
1240 GangNotify(ply, 4, 4, "You can't change your own rank")
1241 return ""
1242 end
1243
1244
1245 // check they're in our gang
1246 MyGangMembers = Streetwars.DB.GetPlayerGangMemberListSimple(gang)
1247 for k,v in pairs(MyGangMembers) do
1248 if v.steam == target then
1249
1250 other_rank = v.rank
1251 isgangmember = true
1252
1253 if v.rank == 6 then
1254 isinvited = true
1255 else
1256 isinvited = false
1257 end
1258
1259 break
1260
1261 end
1262 end
1263
1264 if !isgangmember then
1265 GangNotify(ply, 4, 4, "That player is not in your gang")
1266 return ""
1267 end
1268
1269 if isinvited then
1270 GangNotify(ply, 4,4, " You cannot set the ranks of invited players")
1271 return ""
1272 end
1273
1274
1275 /// Getting our rank from the same table as we got his from
1276 for k,v in pairs(MyGangMembers) do
1277 if v.steam == ply:SteamID() then
1278
1279 my_rank = v.rank
1280 break
1281
1282 end
1283 end
1284
1285
1286
1287 target_rank = tonumber(ptype)
1288 local rank_ok = false
1289
1290 // rank setting permissions. leaders can set all ranks
1291 if my_rank <= 1 then
1292 rank_ok = true
1293
1294 // vice leaders can set up to captain
1295 elseif my_rank == 2 and target_rank > 2 then
1296 rank_ok = true
1297
1298 // captains can set vices
1299 elseif my_rank == 3 and target_rank > 3 then
1300 rank_ok = true
1301
1302 end
1303
1304 // check the target isn't higher ranking than us if we're not leader
1305 if my_rank > 1 and other_rank >= my_rank then
1306 rank_ok = false
1307 end
1308
1309 // abandon setting the same rank
1310 if target_rank == other_rank then
1311 GangNotify(ply, 4, 4, "Player is already assigned that rank")
1312 return ""
1313 end
1314
1315 if not rank_ok then
1316 GangNotify(ply, 4, 4, "You don't have the rank to do that")
1317 return ""
1318 end
1319
1320 // is it a promotion or demotion?
1321 local promotion = target_rank < other_rank
1322
1323 // set targets rank
1324 MySQL.Query("UPDATE sw_gang_members SET `rank`= "..MySQL.SQLStr(target_rank).." WHERE `steam` = ".. MySQL.SQLStr(target) .." AND `gang` = ".. MySQL.SQLStr(gang) ..";" )
1325
1326 //timer.Simple(1, function() UpdateClientGangMembers(gang) end)
1327 InitPlayerGang( ply )
1328 if targetonline then
1329
1330 for k,v in pairs(player.GetAll()) do
1331
1332 if v:SteamID() == target then
1333 v:ConCommand("say /gang_refresh")
1334
1335 end
1336 end
1337
1338 end
1339
1340 local text = "promoted"
1341 if not promotion then text = "demoted" end
1342 Streetwars.GangLog(ply, "info", ply:Nick() .. " has " .. text .. " " .. targname .. " to " .. target_rank) -- todo add function that takes rank id and returns text name
1343 GangNotify(ply, 4, 4, "You " .. text .. " " .. targname )
1344 if targetonline then
1345 GangNotify(onlinetarg, 4, 4, "You have been " .. text .. " within the gang")
1346 end
1347 return ""
1348end
1349net.Receive( "Promote", GC_Promote)
1350
1351
1352-- // /add rival
1353-- local function GC_AddRival(ply, args)
1354
1355 -- // find our gang name
1356 -- Streetwars.DB.GetPlayerGang(ply,
1357 -- function(gang)
1358 -- if not gang then return "" end
1359
1360 -- // check we're not naming ourself
1361 -- if gang == args then
1362 -- GangNotify(ply, 4, 4, "You can't name your own gang as a rival")
1363 -- return
1364 -- end
1365
1366 -- // check rank
1367 -- Streetwars.DB.GetPlayerGangRank(ply,
1368 -- function(my_rank)
1369 -- if not my_rank or tonumber(my_rank) > 2 then
1370 -- GangNotify(ply, 4, 4, "Only leaders and co-leaders can add rivals")
1371 -- return
1372 -- end
1373
1374 -- // check the other gang exists
1375 -- MySQL.QueryValueEx("SELECT `level` FROM `sw_gangs` WHERE `name` = " .. MySQL.SQLStr(args) .. ";",
1376 -- function(q)
1377 -- if not q or tonumber(q) < (ply:GetNWInt("gang_level") - 5) then
1378 -- GangNotify(ply, 4, 4, args .. " are too low level to be your rival")
1379 -- return
1380 -- end
1381
1382 -- // check they aren't already our rival
1383 -- MySQL.QueryValueEx("SELECT COUNT(*) FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. ";",
1384 -- function(q)
1385 -- if q and tonumber(q) > 0 then
1386 -- GangNotify(ply, 4, 4, "You are already rivals with that gang")
1387 -- return
1388 -- end
1389
1390 -- // and the other way around
1391 -- MySQL.QueryValueEx("SELECT COUNT(*) FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
1392 -- function(q)
1393 -- if q and tonumber(q) > 0 then
1394 -- GangNotify(ply, 4, 4, "You are already rivals with that gang")
1395 -- return
1396 -- end
1397
1398 -- // check the rivalry is allowed
1399 -- MySQL.QueryValueEx("SELECT DATEDIFF(`expires`,NOW()) FROM `sw_gang_norival` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. " AND DATEDIFF(`expires`,NOW()) > 0;",
1400 -- function(rcount)
1401
1402 -- if rcount and tonumber(rcount) > 0 then
1403 -- GangNotify(ply, 4, 4, "You are not allowed to rival that gang for " .. rcount .. " more days")
1404 -- return
1405 -- end
1406
1407 -- // delete any norival entry for this gang
1408 -- MySQL.QueryEx("DELETE FROM `sw_gang_norival` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
1409 -- function()
1410
1411 -- // add rival relationship
1412 -- MySQL.QueryEx("INSERT INTO `sw_gang_rivals` (`gang`, `rival`) VALUES(" .. MySQL.SQLStr(gang) .. ", " .. MySQL.SQLStr(args) .. ");",
1413 -- function()
1414 -- MySQL.QueryEx("INSERT INTO `sw_gang_rivals` (`gang`, `rival`) VALUES(" .. MySQL.SQLStr(args) .. ", " .. MySQL.SQLStr(gang) .. ");",
1415 -- function()
1416
1417 -- // add them
1418 -- InitPlayerGang(ply)
1419 -- //GangNotify(ply, 4, 4, "You have named " .. args .. " as a rival gang")
1420 -- NotifyGang(gang, 4, 4, "A gang rivalry has been started against " .. args .. "!")
1421 -- NotifyGang(args, 4, 4, gang .. " have declared a gang rivalry has against you!")
1422
1423 -- end)
1424 -- end)
1425
1426 -- end)
1427
1428 -- end)
1429
1430 -- end)
1431
1432 -- end)
1433
1434 -- end)
1435
1436 -- end)
1437
1438 -- end)
1439
1440 -- return ""
1441-- end
1442-- AddChatCommand("/gang_addrival", GC_AddRival)
1443
1444
1445-- // request mercy
1446-- local BlockMercySpam = {}
1447-- local function GC_ReqMercy(ply, args)
1448
1449 -- if not IsValid(ply) then return end
1450 -- if BlockMercySpam[ply:SteamID()] and BlockMercySpam[ply:SteamID()] > CurTime() then
1451 -- return
1452 -- end
1453 -- BlockMercySpam[ply:SteamID()] = CurTime() + 5
1454
1455 -- // find our gang name
1456 -- Streetwars.DB.GetPlayerGang(ply,
1457 -- function(gang)
1458 -- if not gang then return "" end
1459
1460 -- // find our rank
1461 -- Streetwars.DB.GetPlayerGangRank(ply,
1462 -- function (my_rank)
1463 -- if not my_rank or tonumber(my_rank) > 2 then
1464 -- GangNotify(ply, 4, 4, "Only leaders and co-leaders can request mercy")
1465 -- return
1466 -- end
1467
1468 -- // try and make them win
1469 -- Streetwars.DB.RivalryWin(args, gang)
1470
1471 -- end)
1472
1473 -- end)
1474
1475 -- return ""
1476-- end
1477-- AddChatCommand("/gang_mercy", GC_ReqMercy)
1478
1479-- // request truce
1480-- local function GC_ReqTruce(ply, args)
1481 -- // find our gang name
1482 -- Streetwars.DB.GetPlayerGang(ply,
1483 -- function(gang)
1484 -- if not gang then return "" end
1485
1486 -- // find our rank
1487 -- Streetwars.DB.GetPlayerGangRank(ply,
1488 -- function (my_rank)
1489 -- if not my_rank or tonumber(my_rank) > 2 then
1490 -- GangNotify(ply, 4, 4, "Only leaders and co-leaders can request a truce")
1491 -- return
1492 -- end
1493
1494 -- // see if other gang is requesting a truce
1495 -- MySQL.QueryValueEx("SELECT `truce` FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
1496 -- function(truce)
1497 -- if not truce then
1498 -- GangNotify(ply, 4, 4, "You don't seem to have a rivalry with " .. args)
1499 -- return
1500 -- end
1501
1502 -- if tonumber(truce) == 1 then
1503
1504 -- // truce is agreed. remove the rivalry
1505 -- MySQL.QueryEx("DELETE FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. ";",
1506 -- function()
1507
1508 -- MySQL.QueryEx("DELETE FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
1509 -- function()
1510
1511 -- // refresh their view
1512 -- InitPlayerGang( ply )
1513 -- NotifyGang(gang, 4, 4, "A truce with " .. args .. " has been agreed!")
1514 -- NotifyGang(args, 4, 4, "A truce with " .. gang .. " has been agreed!")
1515
1516 -- end)
1517
1518 -- end)
1519
1520 -- else
1521
1522 -- // set truce request
1523 -- MySQL.QueryEx("UPDATE `sw_gang_rivals` SET `truce` = '1' WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. ";",
1524 -- function()
1525
1526 -- // refresh their view
1527 -- InitPlayerGang( ply )
1528 -- //GangNotify(ply, 4, 4, "You have requested a truce with " .. args)
1529 -- NotifyGang(gang, 4, 4, "You have requested a truce with " .. args)
1530 -- NotifyGang(args, 4, 4, gang .. " have requested a truce!")
1531 -- end)
1532
1533 -- end
1534
1535 -- end)
1536
1537 -- end)
1538
1539 -- end)
1540
1541 -- return ""
1542-- end
1543-- AddChatCommand("/gang_truce", GC_ReqTruce)
1544
1545-- // cancel truce
1546-- local function GC_CancelTruce(ply, args)
1547 -- // find our gang name
1548 -- Streetwars.DB.GetPlayerGang(ply,
1549 -- function(gang)
1550 -- if not gang then return "" end
1551
1552 -- // find our rank
1553 -- Streetwars.DB.GetPlayerGangRank(ply,
1554 -- function (my_rank)
1555 -- if not my_rank or tonumber(my_rank) > 2 then
1556 -- GangNotify(ply, 4, 4, "Only leaders and co-leaders can cancel a truce")
1557 -- return
1558 -- end
1559
1560 -- // just check they exist
1561 -- MySQL.QueryEx("SELECT `truce` FROM `sw_gang_rivals` WHERE `gang` = " .. MySQL.SQLStr(args) .. " AND `rival` = " .. MySQL.SQLStr(gang) .. ";",
1562 -- function(truce)
1563
1564 -- if truce then
1565
1566 -- // set truce request
1567 -- MySQL.QueryEx("UPDATE `sw_gang_rivals` SET `truce` = '0' WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `rival` = " .. MySQL.SQLStr(args) .. ";",
1568 -- function()
1569
1570 -- // refresh their view
1571 -- InitPlayerGang( ply )
1572 -- GangNotify(ply, 4, 4, "You have cancelled the truce request with " .. args)
1573
1574 -- end)
1575
1576 -- end
1577
1578 -- end)
1579
1580 -- end)
1581
1582 -- end)
1583
1584 -- return ""
1585-- end
1586-- AddChatCommand("/gang_canceltruce", GC_CancelTruce)
1587
1588
1589// /accept
1590local function GC_AcceptInvite(ply, args)
1591
1592 Streetwars.DB.AcceptInvite(ply, args,
1593 function(q)
1594 if q then
1595 InitPlayerGang(ply)
1596 Streetwars.GangLog(ply, "info", ply:Nick() .. " has joined " .. args)
1597 GangNotify(ply, 4, 4, "You accepted the gang invite from " .. args)
1598 end
1599 end)
1600 return ""
1601end
1602AddChatCommand("/gang_accept", GC_AcceptInvite)
1603
1604// /reject
1605local function GC_RejectInvite(ply, args)
1606
1607 Streetwars.DB.RejectInvite(ply, args,
1608 function(q)
1609 if q then
1610 InitPlayerGang(ply)
1611 GangNotify(ply, 4, 4, "You rejected the gang invite from " .. args)
1612 end
1613 end)
1614 return ""
1615end
1616AddChatCommand("/gang_reject", GC_RejectInvite)
1617
1618// set item purchase permission
1619local function GC_SetBuyPermission(ply, cmd, args)
1620
1621 //Msg("GC_SetBuyPermission\n")
1622
1623 // find our gang name
1624 Streetwars.DB.GetPlayerGang(ply,
1625 function(gang)
1626 if not gang then return "" end
1627
1628 //Msg("GetPlayerGang\n")
1629
1630 // find our rank
1631 Streetwars.DB.GetPlayerGangRank(ply,
1632 function (my_rank)
1633
1634 //Msg("GetPlayerGangRank check\n")
1635
1636 if not my_rank or tonumber(my_rank) > 2 then
1637 return
1638 end
1639
1640 //Msg("GetPlayerGangRank ok\n")
1641
1642 local itemid = args[1]
1643 local perms = args[2]
1644
1645 // delete any existing permissions
1646 MySQL.QueryEx("DELETE FROM`sw_gang_permissions` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `item` = " .. MySQL.SQLStr(itemid) .. ";",
1647 function()
1648
1649 //Msg("DELETE FROM`sw_gang_permissions`\n")
1650
1651 // insert new permissions
1652 MySQL.QueryEx( "INSERT INTO `sw_gang_permissions` (`gang`, `item`, `permission`) VALUES(" .. MySQL.SQLStr(gang) .. ", " .. MySQL.SQLStr(itemid) .. ", " .. MySQL.SQLStr(perms) .. ");",
1653 function()
1654
1655 //Msg("done!\n")
1656
1657 end)
1658
1659 end)
1660
1661 end)
1662
1663 end)
1664
1665end
1666concommand.Add("gang_permission",GC_SetBuyPermission )
1667
1668// gang buy
1669local function GC_BuyItem(ply, args)
1670
1671 // find our gang name
1672 Streetwars.DB.GetPlayerGang(ply,
1673 function(gang)
1674 if not gang then
1675 Msg("Warning: /gang_buy player had no gang?\n")
1676 return
1677 end
1678
1679 // find our rank
1680 Streetwars.DB.GetPlayerGangRank(ply,
1681 function (my_rank)
1682 if not my_rank then
1683 Msg("Warning: /gang_buy player had no rank?\n")
1684 return
1685 end
1686
1687 // find permissions for the item
1688 MySQL.QueryValueEx("SELECT `permission` FROM `sw_gang_permissions` WHERE `gang` = " .. MySQL.SQLStr(gang) .. " AND `item` = " .. MySQL.SQLStr(args) .. ";",
1689 function (perm)
1690 if not perm then
1691 //Msg("Warning: /gang_buy " .. args .. ": couldn't find a permission entry for this item\n")
1692 //return
1693
1694 // no permissions = default. nobody can buy.
1695 perm = 0
1696 end
1697
1698 // check our rank has permission to buy
1699 local function HasItemPerm(perms, rank)
1700 local d = 16
1701 local pms = {0, 0, 0, 0, 0}
1702 local r = 5
1703 while perms > 0 do
1704 if perms - d >= 0 then
1705 perms = perms - d
1706 pms[r] = 1
1707 end
1708
1709 d = d / 2
1710 r = r - 1
1711 end
1712
1713 return pms[rank] == 1
1714 end
1715
1716 // check our rank has permission to buy
1717 if my_rank == 5 then
1718 GangNotify(ply, 4, 4, "You don't have permission to buy gang items!")
1719 return
1720 end
1721
1722 // find gang info
1723 Streetwars.DB.GetGangInfo(gang,
1724 function(info)
1725
1726 if not info then
1727 Msg("GC_BuyItem: Streetwars.DB.GetGangInfo returned nil")
1728 return
1729 end
1730
1731 local gangmoney = tonumber(info.money)
1732 local ganglevel = tonumber(info.level)
1733 local gangmiltia = tonumber(info.extraitems)
1734
1735
1736 local found = false
1737
1738 // find item in gang shop
1739 for k,v in pairs(Gang.GangShop) do
1740 if v.id == args then
1741 found = v
1742 break
1743 end
1744 end
1745
1746
1747 // abandon if item not found
1748 if not found then
1749 GangNotify(ply, 4, 4, "Item not found in gang shop")
1750 return
1751 end
1752
1753 // abandon if we're not high enough level
1754 if found.level > gangmiltia then
1755 GangNotify(ply, 4, 4, "Your gang must be level " .. found.level .. " to spawn that!")
1756 return
1757 end
1758
1759 // fix the price
1760 local item_price = found.price
1761
1762 if found.entity == "printer_booster" then
1763 if ply.printerboostercount and ply.printerboostercount >= Streetwars.MaxPrinterBoosters() then
1764 GangNotify(ply, 1, 4, "You can't spawn more than " .. Streetwars.MaxPrinterBoosters() .. " printer boosters!")
1765 return ""
1766 end
1767 end
1768
1769 // abandon if we can't afford it
1770 if gangmoney < item_price then
1771 GangNotify(ply, 1, 4, "Your gang can not afford this!")
1772 return
1773 end
1774
1775
1776 // abandon if gang is past its stash limit
1777 -- if found.entity == "gang_stash" then
1778 -- local stashes = {}
1779 -- for k,v in pairs(ents.FindByClass(found.entity)) do
1780 -- if v.gang == gang then
1781 -- table.insert(stashes, v)
1782 -- end
1783 -- end
1784
1785 -- if #stashes >= math.min(ganglevel, 5) then
1786 -- GangNotify(ply, 1, 4, "You already have the max of 5 stashes")
1787 -- return
1788 -- end
1789 -- end
1790
1791 -- // abandon if gang is past its printer limit
1792 -- if found.entity == "gang_printer" then
1793 -- local stashes = {}
1794 -- for k,v in pairs(ents.FindByClass(found.entity)) do
1795 -- if v.gang == gang then
1796 -- table.insert(stashes, v)
1797 -- end
1798 -- end
1799
1800 -- if #stashes >= 4 then
1801 -- GangNotify(ply, 1, 4, "You already have the max of 4 gang printers")
1802 -- return
1803 -- end
1804 -- end
1805
1806 // charge gang
1807 Streetwars.DB.AddGangMoney(gang, -item_price,
1808 function()
1809 Streetwars.GangLog(ply, "info", ply:Nick() .. " has purchased a " .. found.name .. " for " .. CUR .. Streetwars.FormatMoney(item_price))
1810 GangNotify(ply, 1, 4, "You have bought a " .. found.name .. " for " .. CUR .. Streetwars.FormatMoney(item_price))
1811
1812 local trace = {}
1813 trace.start = ply:EyePos()
1814 trace.endpos = trace.start + ply:GetAimVector() * 85
1815 trace.filter = ply
1816 local tr = util.TraceLine(trace)
1817
1818 // create the item
1819 local spawnitem
1820 spawnitem = ents.Create(found.entity)
1821 spawnitem.SID = ply.SID
1822 spawnitem.gang = gang
1823 spawnitem:SetNWEntity("owning_ent", ply)
1824 spawnitem:SetNWString("Owner", "Shared")
1825 spawnitem:SetNWString("gang", gang)
1826 spawnitem.Owner = ply
1827 spawnitem.OwnerID = ply:SteamID()
1828 spawnitem:SetNWInt("original_cost", item_price)
1829 spawnitem:SetPos(tr.HitPos)
1830 spawnitem.onlyremover = true
1831 spawnitem.nodupe = true
1832
1833 if found.model then
1834 spawnitem.weaponclass = found.subclass
1835 spawnitem:SetModel(found.model)
1836 end
1837
1838 // create CII data
1839 local ciidata = {}
1840 ciidata.class = "item"
1841 ciidata.subclass = found.subclass
1842 ciidata.ent = "spawned_weapon"
1843 ciidata.name = found.name or ""
1844 ciidata.model = found.model or ""
1845 ciidata.value = item_price or 0
1846 ciidata.level_lock = found.level or 0
1847 ciidata.quantity = 0
1848 ciidata.level = 1
1849
1850 // See if the item supports the CII interface
1851 if spawnitem.CIILoad then spawnitem:CIILoad(ciidata) end
1852
1853 // Else try the subclass interface
1854 if spawnitem.SetSubclass then spawnitem:SetSubclass(found.subclass) end
1855
1856 spawnitem:Spawn()
1857
1858 // do it twice for the benefit of gang coolers etc (badly coded ents)
1859 if spawnitem.SetSubclass then spawnitem:SetSubclass(found.subclass) end
1860
1861 end)
1862
1863 end)
1864
1865 end)
1866
1867 end)
1868
1869 end)
1870
1871 return ""
1872end
1873AddChatCommand("/gang_buy", GC_BuyItem)
1874
1875
1876//////////////////////////////////////////////////////////////////////////////
1877// Util
1878
1879
1880-- function Gang.SendRivalList(rp, my_gang)
1881 -- if not my_gang then return end
1882
1883 -- // send full member list
1884 -- Streetwars.DB.GetGangRivalList(my_gang,
1885 -- function(members)
1886 -- if not members then return end
1887
1888 -- for k,v in pairs(members) do
1889 -- local col = string.Explode(" ", v.color)
1890 -- if #col == 3 then
1891 -- v.color = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
1892 -- end
1893 -- local col = string.Explode(" ", v.color2)
1894 -- if #col == 3 then
1895 -- v.color2 = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
1896 -- end
1897 -- end
1898
1899 -- datastream.StreamToClients(rp, "ds_gang_rivals", members)
1900 -- end)
1901-- end
1902
1903
1904function SendMemberList(rp, my_gang)
1905 if not my_gang then return end
1906
1907 // send full member list
1908 Streetwars.DB.GetPlayerGangMemberList(my_gang,
1909 function(members)
1910 -- PrintTable(members)
1911
1912 if not members then return end
1913
1914 net.Start("ds_gang_members")
1915 net.WriteTable(members)
1916 net.Send(rp)
1917
1918 -- PrintTable(members)
1919
1920 end)
1921end
1922
1923
1924function SendInfo(rp, my_gang)
1925 if not my_gang then return end
1926
1927 // set gang info
1928 Streetwars.DB.GetGangInfo(my_gang,
1929 function(info)
1930
1931 // get members for count
1932 Streetwars.DB.GetPlayerGangMemberList(my_gang,
1933 function(members)
1934
1935 // get shop permissions
1936 MySQL.QueryEx("SELECT * FROM `sw_gang_permissions` WHERE `gang` = " .. MySQL.SQLStr(my_gang) .. ";",
1937 function(perms)
1938
1939 local shop_permissions = {}
1940 if perms then
1941 for k,v in ipairs(perms) do
1942 shop_permissions[tostring(v.item)] = v.permission
1943 end
1944 end
1945
1946 local col = string.Explode(" ", info.color)
1947 if #col == 3 then
1948 col = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
1949 end
1950 local col2 = string.Explode(" ", info.color2)
1951 if #col2 == 3 then
1952 col2 = Color(tonumber(col2[1]), tonumber(col2[2]), tonumber(col2[3]), 255)
1953 end
1954
1955 // this is pretty pointless, but could save copying over unwanted data (eg password)
1956 local MyGang = {}
1957 MyGang.name = info['name']
1958 -- MyGang.logo = info['logo']
1959 MyGang.points = tonumber(info['points'])
1960 -- MyGang.color = col
1961 -- MyGang.color2 = col2
1962 MyGang.xp = tonumber(info['xp'])
1963 MyGang.level = tonumber(info['level'])
1964 MyGang.money = tonumber(info['money'])
1965 MyGang.hpboost = tonumber(info['hpboost'])
1966 MyGang.regen = tonumber(info['regen'])
1967 MyGang.extraitems = tonumber(info['extraitems'])
1968 MyGang.xpboost = tonumber(info['xpboost'])
1969 MyGang.speedboost = tonumber(info['speedboost'])
1970 MyGang.printboost = tonumber(info['printboost'])
1971 MyGang.drugboost = tonumber(info['drugboost'])
1972 MyGang.dropboost = tonumber(info['dropboost'])
1973 // gang patch
1974 MyGang.armorboost = tonumber(info['armorboost'])
1975 MyGang.memberboost = tonumber(info['memberboost'])
1976 MyGang.rivalboost = tonumber(info['rivalboost'])
1977 MyGang.shopboost = tonumber(info['shopboost'])
1978 MyGang.ghostboost = tonumber(info['ghostboost'])
1979 MyGang.ppocketboost = tonumber(info['ppocketboost'])
1980 MyGang.bankboost = tonumber(info['bankboost'])
1981 MyGang.missionboost = tonumber(info['missionboost'])
1982 MyGang.gangxpboost = tonumber(info['gangxpboost'])
1983 MyGang.capboost = tonumber(info['capboost'])
1984
1985 MyGang.shop_permissions = shop_permissions
1986 MyGang.members = #members
1987
1988 net.Start("ds_gang_info")
1989 net.WriteTable(MyGang)
1990 net.Send(rp)
1991 end)
1992 end)
1993 end)
1994end
1995
1996
1997// Refreshes a gangs info for all online players in the gang
1998function UpdateClientGangInfo(gang_name)
1999 local RP = RecipientFilter()
2000
2001 // Find all gangmembers
2002 Streetwars.DB.GetPlayerGangMemberList(gang_name,
2003 function(mlist)
2004 if not mlist then return false end
2005
2006 // Find gangmembers who are on the server
2007 for id, row in pairs(mlist) do
2008 local p = FindPlayer(row['name'])
2009 if p and IsValid(p) and p:IsPlayer() then
2010 RP:AddPlayer(p)
2011 end
2012 end
2013
2014 // Send updates
2015 SendInfo(RP, gang_name)
2016 end)
2017end
2018/*
2019// Refreshes a gangs player list for all online players in the gang
2020function UpdateClientGangMembers(gang_name)
2021 local RP = RecipientFilter()
2022
2023 // Find all gangmembers
2024 Streetwars.DB.GetPlayerGangMemberList(gang_name,
2025 function(mlist)
2026 if not mlist then return false end
2027
2028 // Find gangmembers who are on the server
2029 for id, row in pairs(mlist) do
2030 local p = FindPlayer(row['name'])
2031 if p and IsValid(p) and p:IsPlayer() then
2032 RP:AddPlayer(p)
2033 end
2034 end
2035
2036 // Send updates
2037 SendMemberList(RP, gang_name)
2038 end)
2039end
2040*/
2041
2042// Updates a single players gang info
2043function InitPlayerGang(ply)
2044 if not ply or not IsValid(ply) then return false end
2045
2046 local steamID = ply:SteamID()
2047
2048 // find gang status
2049 local info = MySQL.QueryRow("SELECT `gang`, `rank` FROM sw_gang_members WHERE `steam` = ".. MySQL.SQLStr(steamID) .." ORDER BY `rank` ASC;")
2050
2051 if not info or not info.gang or not info.rank then
2052
2053 // set no gang
2054 ply:SetNWString("gang_name", "")
2055 ply:SetNWInt("gang_rank", 0)
2056 ply:SetNWInt("gang_level", 0)
2057 --ply:SetNWString("gang_logo", "")
2058 --ply:SetNWString("gang_color", "")
2059 umsg.Start("gang_clear", ply)
2060 umsg.End()
2061 return
2062
2063 elseif tonumber(info.rank) == 6 then
2064
2065 // set invited
2066 ply:SetNWString("gang_name", "")
2067 ply:SetNWInt("gang_rank", 0)
2068 ply:SetNWInt("gang_level", 0)
2069 --ply:SetNWString("gang_logo", "")
2070 --ply:SetNWString("gang_color", "")
2071 umsg.Start("gang_clear", ply)
2072 umsg.End()
2073 ply:SetNWInt("gang_invited", true)
2074
2075 MySQL.QueryEx("SELECT `gang` FROM sw_gang_members WHERE `steam` = ".. MySQL.SQLStr(steamID) ..";", function (members)
2076 if members then
2077 net.Start("ds_gang_invites")
2078 net.WriteTable(members)
2079 net.Send(ply)
2080 end
2081 end)
2082 else
2083
2084 // set member
2085 Streetwars.DB.GetGangInfo(info.gang, function(ginfo)
2086
2087 if ginfo then
2088
2089 // convert ints
2090 ginfo.level = tonumber(ginfo.level)
2091 ginfo.rank = tonumber(ginfo.rank)
2092 for k, v in pairs(ginfo) do
2093 if tonumber(v) != nil then
2094 ginfo[k] = tonumber(v)
2095 end
2096 end
2097
2098 // set network vars
2099 ply:SetNWString("gang_name", info.gang)
2100 ply:SetNWInt("gang_rank", info.rank)
2101 ply:SetNWInt("gang_level", ginfo.level)
2102 --ply:SetNWString("gang_logo", ginfo.logo)
2103 --ply:SetNWString("gang_color", ginfo.color)
2104 ply:SetNWString("gang_color2", ginfo.color2)
2105
2106 // attach gang info data to player
2107 ply.GangInfo = ginfo
2108
2109 -- Gang.SendRivalList(ply, info.gang)
2110 SendMemberList(ply, info.gang)
2111 SendInfo(ply, info.gang)
2112 //timer.Simple(1, function() UpdateClientGangInfo(gang) end)
2113
2114 end
2115
2116 end)
2117
2118 end
2119
2120 return ""
2121end
2122AddChatCommand("/gang_refresh", InitPlayerGang)
2123
2124// gang search
2125local function GC_Search(ply, args)
2126
2127 if string.len(args) < 2 or string.len(args) > 32 then
2128 GangNotify(ply, 4, 4, "Invalid Search")
2129 return ""
2130 end
2131
2132 MySQL.QueryEx("SELECT `name`, `logo`, `color`, `color2`, `level` FROM sw_gangs WHERE `name` LIKE ".. MySQL.SQLStr("%" .. args .. "%") .." LIMIT 15;",
2133 function(q)
2134 if q then
2135
2136 for k, v in pairs(q) do
2137 local col = string.Explode(" ", v.color)
2138 if #col == 3 then
2139 v.color = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
2140 end
2141 local col = string.Explode(" ", v.color2)
2142 if #col == 3 then
2143 v.color2 = Color(tonumber(col[1]), tonumber(col[2]), tonumber(col[3]), 255)
2144 end
2145 end
2146
2147 -- datastream.StreamToClients(ply, "ds_gang_search", q)
2148 end
2149 end)
2150 return ""
2151end
2152AddChatCommand("/gang_search", GC_Search)
2153
2154// color gang members
2155local function GC_Color(ply, args)
2156 // find our gang name
2157 Streetwars.DB.GetPlayerGang(ply,
2158 function(gang)
2159 if not gang then return end
2160
2161 // find ranks
2162 Streetwars.DB.GetPlayerGangRank(ply,
2163 function(my_rank)
2164 if not my_rank or my_rank > 2 then
2165 GangNotify(ply, 4, 4, "You must be at least vice leader to activate gang colors!")
2166 return
2167 end
2168
2169 local r = math.random(0, 256)
2170 local g = math.random(0, 256)
2171 local b = math.random(0, 256)
2172
2173 local colors = {}
2174 for w in args:gmatch("%S+") do
2175 table.insert(colors, w)
2176 end
2177
2178 if #colors == 3 then
2179 if tonumber(colors[1]) ~= nil then
2180 r = math.Clamp(tonumber(colors[1]), 0, 255)
2181 end
2182
2183 if tonumber(colors[2]) ~= nil then
2184 g = math.Clamp(tonumber(colors[2]), 0, 255)
2185 end
2186
2187 if tonumber(colors[3]) ~= nil then
2188 b = math.Clamp(tonumber(colors[3]), 0, 255)
2189 end
2190 end
2191
2192 for k, v in pairs (player.GetAll()) do
2193 if v.GangInfo and v.GangInfo.name == ply.GangInfo.name then
2194 v:SetColor(Color(r, g, b, 255))
2195 end
2196 end
2197 end)
2198 end)
2199
2200 return ""
2201end
2202AddChatCommand("/gang_color", GC_Color)
2203
2204// color gang members
2205local function GC_Uncolor(ply, args)
2206 // find our gang name
2207 Streetwars.DB.GetPlayerGang(ply,
2208 function(gang)
2209 if not gang then return end
2210
2211 // find ranks
2212 Streetwars.DB.GetPlayerGangRank(ply,
2213 function(my_rank)
2214 if not my_rank or my_rank > 2 then
2215 GangNotify(ply, 4, 4, "You must be at least vice leader to deactivate gang colors!")
2216 return
2217 end
2218
2219 for k, v in pairs (player.GetAll()) do
2220 if v.GangInfo and v.GangInfo.name == ply.GangInfo.name then
2221 v:SetColor(Color(255, 255, 255, 255))
2222 end
2223 end
2224 end)
2225 end)
2226
2227 return ""
2228end
2229AddChatCommand("/gang_uncolor", GC_Uncolor)
2230
2231// color gang members
2232local function GC_Uncolorme(ply, args)
2233 // find our gang name
2234 Streetwars.DB.GetPlayerGang(ply,
2235 function(gang)
2236 if not gang then return end
2237
2238 ply:SetColor(Color(255, 255, 255, 255))
2239 end)
2240
2241 return ""
2242end
2243AddChatCommand("/gang_uncolorme", GC_Uncolorme)
2244
2245GangRewardQueue = util.Stack()
2246
2247local function GrantGangReward()
2248 -- print("gangxp timer triggered")
2249 -- local gang,xp = nil, nil
2250 -- while GangRewardQueue:Size() > 0 do
2251 -- gang,xp = Streetwars.DequeueGangXP()
2252 -- if gang ~= nil and xp ~= nil then
2253 -- print("gang: " .. gang .. ", xp: " .. xp)
2254 -- Streetwars.DB.AddGangXP(gang, xp)
2255 -- else
2256 -- print("SWERROR: GrantGangReward size was above 0 but gang or xp were nil!")
2257 -- end
2258 -- end
2259
2260 // OR
2261
2262 local e = Streetwars.DequeueGangRewards()
2263 if e then
2264 -- Add gang xp
2265 Streetwars.DB.AddGangXP(e.gang, e.xp)
2266 -- Add gang money
2267 Streetwars.DB.AddGangMoney(e.gang, e.money)
2268 -- Add gang points
2269 Streetwars.DB.AddGangPoints(e.gang, e.points)
2270
2271 -- Notification
2272 NotifyGangSilent(e.gang, 2, 2, e.territory .. " Territory: " .. CUR .. Streetwars.FormatMoney(e.money) .. ", +" .. e.xp .. "XP")
2273 end
2274end
2275
2276timer.Create("gangxp", 10, 0, GrantGangReward)
2277
2278function Streetwars.EnqueueGangRewards(gang, xp, money, points, territory)
2279 GangRewardQueue:Push({ gang = gang, xp = xp, money = money, points = points, territory = territory })
2280end
2281
2282function Streetwars.DequeueGangRewards()
2283 local top = GangRewardQueue:Top()
2284 if top then
2285 GangRewardQueue:Pop(1)
2286 return top
2287 end
2288
2289 return nil
2290end