Advertisement
Guest User

rreeee

a guest
Dec 19th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.24 KB | None | 0 0
  1. local easynet = SH_WHITELIST.easynet
  2.  
  3. if (SH_WHITELIST.UseWorkshop) then
  4. resource.AddWorkshop("1251198810")
  5. -- resource.AddWorkshop("76561198087662037")
  6. else
  7. resource.AddSingleFile("materials/shenesis/general/back.png")
  8. resource.AddSingleFile("materials/shenesis/general/close.png")
  9. resource.AddSingleFile("materials/shenesis/general/list.png")
  10. resource.AddSingleFile("materials/shenesis/general/checked.png")
  11. resource.AddSingleFile("resource/fonts/circular.ttf")
  12. resource.AddSingleFile("resource/fonts/circular_bold.ttf")
  13. end
  14.  
  15. function SH_WHITELIST:DatabaseConnected()
  16. local dm = self.DatabaseMode
  17. if (dm == "mysqloo") then
  18. local to_create = {
  19. sh_whitelist = [[`type` int(11) NOT NULL, `name` varchar(128) COLLATE utf8_unicode_ci NOT NULL, `whitelisted` text COLLATE utf8_unicode_ci NOT NULL]],
  20. }
  21.  
  22. local function Advance()
  23. if (table.Count(to_create) > 0) then
  24. local k = table.GetFirstKey(to_create)
  25. local d = to_create[k]
  26. to_create[k] = nil
  27.  
  28. self:TryCreateTable(k, d, function()
  29. Advance()
  30. end)
  31. else
  32. self:BetterQuery([[ALTER TABLE sh_whitelist ADD faction_chosen int(11) NOT NULL DEFAULT 0]], {})
  33.  
  34. timer.Simple(1, function()
  35. self:PostDatabaseConnected()
  36. end)
  37. end
  38. end
  39.  
  40. Advance()
  41. else
  42. self:TryCreateTable("sh_whitelist", [[`type` INTEGER NOT NULL, `name` TEXT NOT NULL UNIQUE, `whitelisted` TEXT NOT NULL]])
  43.  
  44. _SH_QUERY_SILENT = true
  45. self:BetterQuery([[ALTER TABLE sh_whitelist ADD COLUMN faction_chosen INTEGER NOT NULL DEFAULT 0]], {})
  46. _SH_QUERY_SILENT = false
  47.  
  48. self:PostDatabaseConnected()
  49. end
  50. end
  51.  
  52. function SH_WHITELIST:PostDatabaseConnected(cb)
  53. self:DBPrint("Post database actions..")
  54.  
  55. self.Whitelisted = {
  56. players = {},
  57. usergroups = {},
  58. }
  59. self.FactionsChosen = {}
  60.  
  61. self:BetterQuery("SELECT * FROM sh_whitelist", {}, function(q, ok, data)
  62. if (!ok) then
  63. return end
  64.  
  65. for _, wl in pairs (data) do
  66. local dest = tonumber(wl.type) == self.TYPE_USERGROUP and self.Whitelisted.usergroups or self.Whitelisted.players
  67. local tbl = self:ParseCSVLine(wl.whitelisted)
  68. local jobs = {}
  69. for _, v in pairs (tbl) do
  70. jobs[v] = true
  71. end
  72.  
  73. dest[wl.name] = jobs
  74.  
  75. if (tonumber(wl.type) == self.TYPE_STEAMID) then
  76. self.FactionsChosen[wl.name] = tonumber(wl.faction_chosen)
  77. end
  78. end
  79.  
  80. if (cb) then
  81. cb()
  82. end
  83. end)
  84. end
  85.  
  86. function SH_WHITELIST:PlayerInitialSpawn(ply)
  87. ply.SH_WHITELIST = {}
  88. ply.SH_WHITELIST_USERGROUP = ply:GetUserGroup()
  89.  
  90. self.FactionsChosen[ply:SteamID()] = self.FactionsChosen[ply:SteamID()] or 0
  91. self:ApplyWhitelist(ply)
  92.  
  93. timer.Simple(1, function()
  94. if (!IsValid(ply)) then
  95. return end
  96.  
  97. for _, name in pairs (self.AutoAssignJobs) do
  98. local job = self:GetJobByName(name)
  99. if (!job) then
  100. continue end
  101.  
  102. if (self:IsPlayerWhitelisted(ply, job)) then
  103. ply:changeTeam(job.team, true)
  104. ply:Spawn()
  105. break
  106. end
  107. end
  108. end)
  109. end
  110.  
  111. function SH_WHITELIST:PlayerSpawn(ply)
  112. self:FactionPlayerSpawn(ply)
  113. ply.SH_WHITELIST = {}
  114. ply.SH_WHITELIST_USERGROUP = ply:GetUserGroup()
  115.  
  116. self.FactionsChosen[ply:SteamID()] = self.FactionsChosen[ply:SteamID()] or 0
  117. self:ApplyWhitelist(ply)
  118.  
  119. timer.Simple(1, function()
  120. if (!IsValid(ply)) then
  121. return end
  122.  
  123. for _, name in pairs (self.AutoAssignJobs) do
  124. local job = self:GetJobByName(name)
  125. if (!job) then
  126. continue end
  127.  
  128. if (self:IsPlayerWhitelisted(ply, job)) then
  129. ply:changeTeam(job.team, true)
  130. break
  131. end
  132. end
  133. end)
  134. end
  135.  
  136. function SH_WHITELIST:SendWhitelist(ply, steamid, usergroup)
  137. local wl
  138. if (usergroup) then
  139. wl = self.Whitelisted.usergroups[usergroup]
  140. else
  141. wl = self.Whitelisted.players[steamid]
  142. end
  143.  
  144. local jobs = {}
  145. if (wl) then
  146. for k in pairs (wl) do
  147. table.insert(jobs, k)
  148. end
  149. end
  150.  
  151. local n, d = "", {jobs = jobs}
  152. if (usergroup) then
  153. n = "SH_WHITELIST.SendWhitelistUsergroup"
  154. d.usergroup = usergroup
  155. else
  156. n = "SH_WHITELIST.SendWhitelistSteamID"
  157. d.steamid = steamid
  158. end
  159.  
  160. easynet.Send(ply, n, d)
  161. end
  162.  
  163. function SH_WHITELIST:SendWhitelistSaved(ply)
  164. local steamids, usergroups = {}, {}
  165. for steamid in pairs (self.Whitelisted.players) do
  166. table.insert(steamids, steamid:Replace("STEAM_")) -- let's save some bits
  167. end
  168. for usg in pairs (self.Whitelisted.usergroups) do
  169. table.insert(usergroups, usg)
  170. end
  171.  
  172. -- TODO? Compress76561198087662019
  173. easynet.Send(ply, "SH_WHITELIST.SendWhitelistSaved", {steamids = steamids, usergroups = usergroups})
  174. end
  175.  
  176. function SH_WHITELIST:SendPlayerWhitelist(ply, target)
  177. local jobs = {}
  178. for k in pairs (ply.SH_WHITELIST or {}) do
  179. table.insert(jobs, k)
  180. end
  181.  
  182. easynet.Send(ply, "SH_WHITELIST.SendWhitelistPlayer", {jobs = jobs})
  183. end
  184.  
  185. function SH_WHITELIST:WhitelistInternal(type, name, jobs, whitelisted, admin)
  186. local index = {}
  187. for _, job in pairs (RPExtraTeams) do
  188. index[job.name] = job
  189. end
  190.  
  191. local valid, valid2 = {}, {}
  192. for i, jobname in pairs (jobs) do
  193. if (!index[jobname] or !self:IsWhitelistJob(index[jobname])) or (admin and !self:CanWhitelist(admin, jobname)) then
  194. continue end
  195.  
  196. table.insert(valid, jobname)
  197. table.insert(valid2, whitelisted[i])
  198. end
  199.  
  200. if (#valid == 0) then
  201. return end
  202.  
  203. local is_usergroup = type == self.TYPE_USERGROUP
  204. local tbl
  205. if (is_usergroup) then
  206. tbl = self.Whitelisted.usergroups[name]
  207. else
  208. tbl = self.Whitelisted.players[name]
  209. end
  210.  
  211. local exists = tbl ~= nil
  212. tbl = tbl or {}
  213.  
  214. for i, job in pairs (valid) do
  215. if (whitelisted[i]) then
  216. tbl[job] = true
  217. else
  218. tbl[job] = nil
  219. end
  220. end
  221.  
  222. if (is_usergroup) then
  223. self.Whitelisted.usergroups[name] = tbl
  224. else
  225. self.Whitelisted.players[name] = tbl
  226. end
  227.  
  228. local csv = self:TableToCSV(tbl, true)
  229. if (exists) then
  230. self:BetterQuery([[
  231. UPDATE sh_whitelist
  232. SET whitelisted = {whitelisted}
  233. WHERE type = {type} AND name = {name}
  234. ]], {type = type, name = name, whitelisted = csv})
  235. else
  236. self:TryInsert([[
  237. INSERT INTO sh_whitelist (type, name, whitelisted)
  238. VALUES ({type}, {name}, {whitelisted})
  239. ]], {type = type, name = name, whitelisted = csv})
  240. end
  241.  
  242. local ccat = {}
  243. local first = valid2[1]
  244. for i, b in pairs (valid2) do
  245. if (first == b) then
  246. table.insert(ccat, valid[i])
  247. end
  248. end
  249.  
  250. local msg = string.format(self.Language[first and "now_whitelisted_for_x" or "no_longer_whitelisted_for_x"], table.concat(ccat, ", ")) -- 76561198087662028
  251. for _, v in ipairs (player.GetAll()) do
  252. if (v:SteamID() == name or v:GetUserGroup() == name) then
  253. self:ApplyWhitelist(v)
  254.  
  255. if (self.NotifyWhitelist) then
  256. self:Notify(v, msg, first)
  257. end
  258.  
  259. local job = v:Team()
  260. local jobname = team.GetName(job)
  261. if (table.HasValue(valid, jobname) and !self:CanBecomeJob(v, job)) then
  262. self:Notify(v, self.Language.booted_from_job_unwhitelisted, false, jobname)
  263. v:changeTeam(GAMEMODE.DefaultTeam, true)
  264. end
  265. end
  266. end
  267.  
  268. self:SendWhitelist(self:GetInMenu(admin), !is_usergroup and name or nil, is_usergroup and name or nil)
  269.  
  270. if (self.UseServerLog) then
  271. local cc = {}
  272. for i, jobname in pairs (valid) do
  273. table.insert(cc, jobname .. " [" .. tostring(valid2[i]):upper() .. "]")
  274. end
  275.  
  276. if (is_usergroup) then
  277. name = name .. " (" .. self.Language.usergroup .. ")"
  278. end
  279. ServerLog(string.format(self.Language.x_changed_whitelist_for_y, admin or "SERVER", name, table.concat(cc, ", ")) .. "\n")
  280. end
  281. end
  282.  
  283. function SH_WHITELIST:WhitelistSteamID(admin, steamid, jobs, whitelisted)
  284. if (admin and !self:Assert(self:CanWhitelist(admin), admin, "no_permission")) then
  285. return end
  286.  
  287. local ply
  288.  
  289. if (type(steamid) == "Player") then
  290. ply = steamid
  291. steamid = steamid:SteamID()
  292. elseif (steamid:StartWith("76")) then -- SID64
  293. steamid = util.SteamIDFrom64(steamid)
  294. end
  295. ply = player.GetBySteamID(steamid)
  296.  
  297. if (admin and !self:IsAdmin(admin)) then
  298. if (!self:Assert(self.NonAdminsCanWhitelistAll or IsValid(ply), admin, "no_permission")) then
  299. return end
  300. end
  301.  
  302. self:WhitelistInternal(self.TYPE_STEAMID, steamid, jobs, whitelisted, admin)
  303. end
  304.  
  305. function SH_WHITELIST:WhitelistUsergroup(admin, usergroup, jobs, whitelisted)
  306. if (admin and !self:Assert(self:CanWhitelist(admin), admin, "no_permission")) then
  307. return end
  308.  
  309. if (admin and !self:IsAdmin(admin)) then
  310. if (!self:Assert(self.NonAdminsCanWhitelistAll and self.NonAdminsCanWhitelistUsergroups, admin, "no_permission")) then
  311. return end
  312. end
  313.  
  314. if (!self:Assert(self.Usergroups[usergroup] == nil, admin, "this_usergroup_cant_be_whitelisted")) then
  315. return end
  316.  
  317. self:WhitelistInternal(self.TYPE_USERGROUP, usergroup, jobs, whitelisted, admin)
  318. end
  319.  
  320. function SH_WHITELIST:GetInMenu(except)
  321. local t = {}
  322. for _, v in ipairs (player.GetAll()) do
  323. if (v.SH_WHITELIST_MENU) and (v ~= except) then
  324. table.insert(t, v)
  325. end
  326. end
  327.  
  328. return t
  329. end
  330.  
  331. function SH_WHITELIST:Notify(ply, msg, good, ...)
  332. local a = {...}
  333. if (#a > 0) then
  334. easynet.Send(ply, "SH_WHITELIST.Notify", {message = string.format(msg, unpack(a)), good = good})
  335. else
  336. easynet.Send(ply, "SH_WHITELIST.Notify", {message = msg, good = good})
  337. end
  338. end
  339.  
  340. -- Erases the tables and creates them again.
  341. -- Map restart needed for client data to reset.
  342. function SH_WHITELIST:WipeData()
  343. local dm = self.DatabaseMode
  344. if (dm == "mysqloo") then
  345. self:BetterQuery([[
  346. DROP TABLE IF EXISTS sh_whitelist
  347. ]], {})
  348. else
  349. self:BetterQuery([[
  350. DROP TABLE sh_whitelist
  351. ]], {})
  352. end
  353.  
  354. self:DBPrint("Data wiped. Restart the map for changes to fully apply.")
  355. end
  356.  
  357. function SH_WHITELIST:OptimizeDatabase(ply)
  358. if (ply and !self:Assert(self:IsAdmin(ply), ply, "no_permission")) then
  359. return end
  360.  
  361. self:DBPrint("Optimizing database..")
  362. if (ply) then
  363. ServerLog(ply:Nick() .. " has optimized the SH Whitelist database\n")
  364. end
  365.  
  366. self:BetterQuery([[
  367. DELETE FROM sh_whitelist
  368. WHERE whitelisted = ''
  369. ]], {}, function()
  370. self:PostDatabaseConnected(function()
  371. if (IsValid(ply)) then
  372. self:SendWhitelistSaved(ply)
  373. self:Notify(ply, "database_optimized", true)
  374. end
  375. end)
  376. end)
  377. end
  378.  
  379. hook.Add("PlayerInitialSpawn", "SH_WHITELIST.PlayerInitialSpawn", function(ply)
  380. SH_WHITELIST:PlayerInitialSpawn(ply)
  381. end)
  382.  
  383. hook.Add("PlayerSpawn", "SH_WHITELIST.PlayerSpawn", function(ply)
  384. SH_WHITELIST:PlayerSpawn(ply)
  385. end)
  386.  
  387. hook.Add("PlayerSay", "SH_WHITELIST.MenuCommand", function(ply, say)
  388. say = say:Replace("!", "/"):lower():Trim()
  389.  
  390. if (SH_WHITELIST.MenuCommands[say]) then
  391. if (SH_WHITELIST:Assert(SH_WHITELIST:CanWhitelist(ply), ply)) then
  392. easynet.Send(ply, "SH_WHITELIST.OpenMenu")
  393. end
  394.  
  395. return ""
  396. end
  397.  
  398. if (SH_WHITELIST.FactionsEnable and SH_WHITELIST.FactionsCommands[say]) then
  399. local chosen = SH_WHITELIST.FactionsChosen[ply:SteamID()] > 0
  400. if (!SH_WHITELIST:Assert(!chosen or SH_WHITELIST.FactionsCanChange(ply), ply, "already_chosen_faction")) then
  401. return ""
  402. end
  403.  
  404. easynet.Send(ply, "SH_WHITELIST.FactionMenu", {changing = chosen})
  405. return ""
  406. end
  407. end)
  408.  
  409. hook.Add("playerCanChangeTeam", "SH_WHITELIST.playerCanChangeTeam", function(ply, to, force)
  410. if (force) then
  411. return end
  412.  
  413. if (!SH_WHITELIST:CanBecomeJob(ply, to)) then
  414. return false, SH_WHITELIST.Language.not_whitelisted_for_job or "not_whitelisted_for_job"
  415. end
  416.  
  417. ply.SH_WHITELIST = {}
  418. ply.SH_WHITELIST_USERGROUP = ply:GetUserGroup()
  419.  
  420. self.FactionsChosen[ply:SteamID()] = self.FactionsChosen[ply:SteamID()] or 0
  421. self:ApplyWhitelist(ply)
  422.  
  423. timer.Simple(1, function()
  424. if (!IsValid(ply)) then
  425. return end
  426. for _, name in pairs (self.AutoAssignJobs) do
  427. local job = self:GetJobByName(name)
  428. if (!job) then
  429. continue end
  430.  
  431. if (self:IsPlayerWhitelisted(ply, job)) then
  432. ply:changeTeam(job.team, true)
  433. break
  434. end
  435. end
  436. end)
  437. end)
  438.  
  439.  
  440. timer.Create("SH_WHITELIST.CheckUsergroup", 1, 0, function()
  441. for _, v in ipairs (player.GetAll()) do
  442. if (!v.SH_WHITELIST_USERGROUP) then -- nani kore www 76561198087662037
  443. continue end
  444.  
  445. local usg = v:GetUserGroup()
  446. if (usg ~= v.SH_WHITELIST_USERGROUP) then
  447. SH_WHITELIST:ApplyWhitelist(v)
  448. v.SH_WHITELIST_USERGROUP = usg
  449.  
  450. end
  451. end
  452. end)
  453.  
  454. -- Messy part: console commands for donation systems
  455. local function DoPrint(ply, msg)
  456. msg = "[SH WHITELIST] " .. msg
  457.  
  458. if (IsValid(ply)) then
  459. ply:PrintMessage(HUD_PRINTCONSOLE, msg)
  460. end
  461.  
  462. print(msg)
  463. end
  464.  
  465. concommand.Add("sh_whitelist_add_steamid", function(ply, cmd, args)
  466. if (!args[1]) then
  467. DoPrint(ply, "SteamID is missing.")
  468. return
  469. end
  470.  
  471. if (!args[2]) then
  472. DoPrint(ply, "Job name is missing.")
  473. return
  474. end
  475.  
  476. if (!SH_WHITELIST:IsValidSteamID(args[1])) then
  477. DoPrint(ply, "Invalid SteamID.")
  478. return
  479. end
  480.  
  481. local job = SH_WHITELIST:GetJobByName(args[2])
  482. if (!job) then
  483. DoPrint(ply, "This job does not exist. " .. args[2])
  484. return
  485. end
  486.  
  487. if (!SH_WHITELIST:IsWhitelistJob(job)) then
  488. DoPrint(ply, "This job cannot be whitelisted. Check your config file!")
  489. return
  490. end
  491.  
  492. SH_WHITELIST:WhitelistSteamID(ply, args[1], {args[2]}, {true})
  493. end)
  494.  
  495. concommand.Add("sh_whitelist_remove_steamid", function(ply, cmd, args)
  496. if (!args[1]) then
  497. DoPrint(ply, "SteamID is missing.")
  498. return
  499. end
  500.  
  501. if (!args[2]) then
  502. DoPrint(ply, "Job name is missing.")
  503. return
  504. end
  505.  
  506. if (!SH_WHITELIST:IsValidSteamID(args[1])) then
  507. DoPrint(ply, "Invalid SteamID.")
  508. return
  509. end
  510.  
  511. local job = SH_WHITELIST:GetJobByName(args[2])
  512. if (!job) then
  513. DoPrint(ply, "This job does not exist. " .. args[2])
  514. return
  515. end
  516.  
  517. if (!SH_WHITELIST:IsWhitelistJob(job)) then
  518. DoPrint(ply, "This job cannot be unwhitelisted. Check your config file!")
  519. return
  520. end
  521.  
  522. SH_WHITELIST:WhitelistSteamID(ply, args[1], {args[2]}, {false})
  523. end)
  524.  
  525. concommand.Add("sh_whitelist_add_usergroup", function(ply, cmd, args)
  526. if (!args[1]) then
  527. DoPrint(ply, "Usergroup is missing.")
  528. return
  529. end
  530.  
  531. if (!args[2]) then
  532. DoPrint(ply, "Job name is missing.")
  533. return
  534. end
  535.  
  536. local job = SH_WHITELIST:GetJobByName(args[2])
  537. if (!job) then
  538. DoPrint(ply, "This job does not exist. " .. args[2])
  539. return
  540. end
  541.  
  542. if (!SH_WHITELIST:IsWhitelistJob(job)) then
  543. DoPrint(ply, "This job cannot be whitelisted. Check your config file!")
  544. return
  545. end
  546.  
  547. SH_WHITELIST:WhitelistUsergroup(ply, args[1], {args[2]}, {true})
  548. end)
  549.  
  550. concommand.Add("sh_whitelist_remove_usergroup", function(ply, cmd, args)
  551. if (!args[1]) then
  552. DoPrint(ply, "Usergroup is missing.")
  553. return
  554. end
  555.  
  556. if (!args[2]) then
  557. DoPrint(ply, "Job name is missing.")
  558. return
  559. end
  560.  
  561. local job = SH_WHITELIST:GetJobByName(args[2])
  562. if (!job) then
  563. DoPrint(ply, "This job does not exist. " .. args[2])
  564. return
  565. end
  566.  
  567. if (!SH_WHITELIST:IsWhitelistJob(job)) then
  568. DoPrint(ply, "This job cannot be whitelisted. Check your config file!")
  569. return
  570. end
  571.  
  572. SH_WHITELIST:WhitelistUsergroup(ply, args[1], {args[2]}, {false})
  573. end)
  574.  
  575. concommand.Add("sh_whitelist_faction_reset", function(ply, cmd, args)
  576. local steamid = args[1]
  577. if (!steamid) then
  578. DoPrint(ply, "SteamID is missing.")
  579. return
  580. end
  581.  
  582. if (!SH_WHITELIST:IsValidSteamID(steamid)) then
  583. DoPrint(ply, "Invalid SteamID.")
  584. return
  585. end
  586.  
  587. if (steamid:StartWith("76")) then -- SID64
  588. steamid = util.SteamIDFrom64(steamid)
  589. end
  590.  
  591. if (!SH_WHITELIST.FactionsChosen[steamid] or SH_WHITELIST.FactionsChosen[steamid] == 0) then
  592. DoPrint(ply, "This player has not chosen a faction yet!")
  593. return
  594. end
  595.  
  596. local before = SH_WHITELIST.FactionsChosen[steamid]
  597. local beforefct = SH_WHITELIST.FactionsList[before]
  598. local unwl, unwl2 = {}, {}
  599. if (beforefct) then
  600. unwl = table.Copy(beforefct.Jobs)
  601.  
  602. for i = 1, #unwl do
  603. table.insert(unwl2, false)
  604. end
  605. end
  606.  
  607. SH_WHITELIST.FactionsChosen[steamid] = 0
  608.  
  609. SH_WHITELIST:WhitelistSteamID(nil, steamid, unwl, unwl2)
  610. SH_WHITELIST:BetterQuery([[
  611. UPDATE sh_whitelist
  612. SET faction_chosen = 0
  613. WHERE type = 1 AND name = {steamid}
  614. ]], {steamid = steamid})
  615.  
  616. local target = player.GetBySteamID(steamid)
  617. if (IsValid(target)) then
  618. easynet.Send(target, "SH_WHITELIST.FactionMenu", {changing = false})
  619. end
  620.  
  621. DoPrint(ply, "Faction reset for " .. steamid .. "!")
  622. end)
  623.  
  624. concommand.Add("sh_whitelist_optimize_database", function(ply, cmd, args)
  625. SH_WHITELIST:OptimizeDatabase(ply)
  626. end)
  627.  
  628. concommand.Add("sh_whitelist_wipe_database", function(ply, cmd, args)
  629. if (IsValid(ply) and !ply:IsSuperAdmin()) then
  630. return end
  631.  
  632. SH_WHITELIST:WipeData()
  633. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement