· 7 years ago · Dec 23, 2018, 03:18 PM
1util.AddNetworkString("request_refunddata")
2util.AddNetworkString("request_notifyrefund")
3
4util.AddNetworkString("request_executerefund")
5
6-- Defining Enums
7
8local TYPE_ENT = 0
9local TYPE_PRINT = 1
10local TYPE_WEP = 2
11
12
13// Tables
14
15print("[REFUND] Initizialing REFUND-System...")
16
17local currentrefundnum = {}
18
19
20RFSYS = RFSYS or {}
21RFSYS.Config = RFSYS.Config or {}
22RFSYS.refund = RFSYS.refund or {}
23
24RFSYS.Config.ShouldRefundonRestart = false
25
26
27RFSYS.Config.UseWhitelistedEntities = true
28
29RFSYS.Config.WhitelistedEntities = {
30
31}
32
33
34RFSYS.Config.UseWhitelistedWeapons = true
35RFSYS.Config.IsWhitelistBlacklist = true
36
37RFSYS.Config.WhitelistedWeapons = {
38 ["gmod_tool"] = true,
39 ["keys"] = true,
40 ["pocket"] = true,
41 ["driving_license"] = true,
42 ["firearms_license"] = true,
43 ["weapon_physcannon"] = true,
44 ["gmod_camera"] = true,
45 ["weapon_physgun"] = true,
46 ["weapon_r_restrained"] = true,
47 ["tbfy_surrendered"] = true,
48 ["weapon_r_cuffed"] = true,
49 ["collections_bag"] = true,
50 ["csgo_switchknive_bayonet"] = true,
51 ["csgo_switchknive_karambit"] = true,
52 ["csgo_switchknive_bowie"] = true,
53 ["csgo_switchknive_gut"] = true,
54 ["csgo_switchknive_flip"] = true,
55 ["csgo_switchknive_falchion"] = true,
56 ["csgo_switchknive_butterfly"] = true,
57 ["csgo_switchknive_shadow"] = true,
58 ["weapon_angryhobo"] = true,
59 ["weapon_arc_atmcard"] = true,
60 ["stunstick"] = true,
61 ["unarrest_stick"] = true,
62 ["weapon_medkit"] = true,
63 ["itemstore_pickup"] = true,
64 ["guitar"] = true,
65 ["begging_swep"] = true,
66 ["weapon_fists"] = true,
67 ["guitar_stalker"] = true,
68 ["gardening_hoe"] = true,
69 ["vc_jerrycan"] = true,
70 ["vc_wrench"] = true,
71 ["weapon_vape_american"] = true,
72 ["weapon_vape_custom"] = true,
73 ["weapon_vape_hallucinogenic"] = true,
74 ["weapon_vape_juicy"] = true,
75 ["weapon_vape_german"] = true,
76 ["weapon_vape"] = true,
77 ["weapon_superman"] = true,
78 ["weapon_superschurke"] = true,
79 ["z_rammbock"] = true,
80 ["fbi_rammbock"] = true,
81 ["weapon_cloaking_device"] = true,
82 ["weapon_searadar"] = true,
83 ["weaponchecker"] = true,
84 ["lockpick"] = true,
85 ["arrest_stick"] = true,
86 ["sg_adrenaline"] = true,
87 ["weapon_pass_rp"] = true,
88 ["weapon_keypadchecker"] = true,
89 ["weapon_jihadbomb"] = true,
90 ["pass_fbi"] = true,
91 ["climb_swep2"] = true,
92 ["swep_disguise_briefcase"] = true,
93 ["vc_spikestrip_wep"] = true,
94 ["weapon_stungun"] = true,
95 ["weapon_r_baton"] = true,
96 ["fine_list"] = true,
97 ["weapon_r_handcuffs"] = true,
98 ["swep_radiodevice"] = true,
99 ["med_kit"] = true,
100 ["manhack_welder"] = true,
101 ["weapon_hooker"] = true,
102 ["door_ram"] = true,
103 ["cw_kk_hk416"] = true,
104 ["cw_hkusp"] = true,
105 ["weapon_cloaking_ghost"] = true,
106 ["tow_attach"] = true,
107 ["cw_nen_glock17"] = true,
108 ["cw_g36c_gsg9"] = true,
109 ["mps_police_line"] = true,
110 ["mps_police_line"] = true,
111 ["blink_swep"] = true,
112 ["weapon_angryhobo1"] = true,
113 ["snowball_thrower"] = true,
114 ["weapon_prophide"] = true,
115 ["weapon_bugbait"] = true,
116
117 ["weapon_vape_winter"] = true,
118
119 ["weapon_ar2"] = true,
120 ["weapon_rpg"] = true,
121 ["weapon_357"] = true,
122 ["weapon_shotgun"] = true,
123 ["weapon_crossbow"] = true,
124 ["weapon_frag"] = true,
125 ["weapon_smg1"] = true,
126 ["weapon_pistol"] = true,
127 ["weapon_crowbar"] = true,
128
129}
130
131RFSYS.Config.WhenShouldRefundsExpire = 86400 * 3 -- 3 Days
132
133
134
135
136
137RFSYS.SeasonID = RFSYS.SeasonID or nil
138
139function RFSYS:CreateNewSeasonID()
140
141 if !file.Exists( "refund_system", "DATA" ) then
142 file.CreateDir( "refund_system" )
143 end
144
145 if !file.Exists( "refund_system/season.txt", "DATA" ) then
146 file.Write( "refund_system/season.txt", 1 )
147 self.SeasonID = 1
148 else
149 local oldseasonid = file.Read("refund_system/season.txt", "DATA")
150 file.Write( "refund_system/season.txt", tonumber(oldseasonid) + 1 )
151 self.SeasonID = tonumber(oldseasonid) + 1
152 end
153 print("[REFUND] Created new seasonid ("..tostring(self.SeasonID)..")")
154
155end
156
157
158function RFSYS:LoadPlayerRefunds( ply )
159
160 local steamid = ply:SteamID()
161 local data = sql.Query("SELECT * FROM refundsplayer WHERE playerid = '"..steamid.."' AND NOT seasonid = "..self.SeasonID..";")
162 print("[REFUND] Loaded "..ply:Nick().."'s refunds...")
163 RFSYS.refund[ply] = {}
164 if not data then return end
165 print("[REFUND] Loaded "..ply:Nick().."'s refunds from sql...")
166 for k,v in pairs( data ) do
167 if tonumber(v.seasonid) != self.SeasonID then
168 RFSYS.refund[ply][v.seasonid] = RFSYS.refund[ply][v.seasonid] or {}
169 --print(v.playerid,v.seasonid,v.identifier,v.type,v.data,v.timestamp)
170 RFSYS.refund[ply][v.seasonid][v.identifier] = { data = v.data, type = v.type, timestamp = v.timestamp }
171 end
172 end
173
174 self:SendFirstJoinPopup( ply )
175
176end
177
178function RFSYS:RefreshPlayer( ply )
179 local steamid = ply:SteamID()
180 local data = sql.Query("SELECT * FROM refundsplayer WHERE playerid = '"..steamid.."';")
181 print("[REFUND] Loaded "..ply:Nick().."'s refunds...")
182 RFSYS.refund[ply] = {}
183 if not data then return end
184 print("[REFUND] Loaded "..ply:Nick().."'s refunds from sql...")
185 for k,v in pairs( data ) do
186 if tonumber(v.seasonid) != self.SeasonID then
187 RFSYS.refund[ply][v.seasonid] = RFSYS.refund[ply][v.seasonid] or {}
188 RFSYS.refund[ply][v.seasonid][v.identifier] = { data = v.data, type = v.type, timestamp = v.timestamp }
189 end
190 end
191end
192
193function RFSYS:SendFirstJoinPopup( ply )
194 net.Start("request_notifyrefund")
195 net.Send( ply)
196end
197
198function RFSYS:ClearPlayerRefund( ply, seasonid )
199
200 local steamid = ply:SteamID()
201 if not seasonid then return end
202 print("[REFUND] Clearing "..ply:Nick().." refunds...")
203 sql.Query("DELETE FROM refundsplayer WHERE playerid = '"..steamid.."' AND seasonid = "..seasonid..";")
204
205end
206
207function RFSYS:ClearCurrentSeason( ply )
208
209 local steamid = ply:SteamID()
210 if not self.SeasonID then return end
211 print("[REFUND] Clearing "..ply:Nick().." current season...")
212 sql.Query("DELETE FROM refundsplayer WHERE playerid = '"..steamid.."' AND< seasonid = "..self.SeasonID..";")
213
214end
215
216function RFSYS:RefundPlayerSeason( ply, seasonid, bank )
217 local steamid = ply:SteamID()
218 local data = sql.Query("SELECT * FROM refundsplayer WHERE playerid = '"..steamid.."' AND seasonid = "..seasonid..";")
219 sql.Query("DELETE FROM refundsplayer WHERE playerid = '"..steamid.."' AND seasonid = "..seasonid..";")
220 --print("Refunding players season: ", seasonid)
221 for k,v in pairs( data ) do
222 print(k,v)
223 PrintTable(v)
224 if tonumber(v.type) == TYPE_WEP then
225 -- print("Detected weapons!")
226 ply:Give(v.data)
227 elseif tonumber(v.type) == TYPE_ENT then
228 -- print("Detected Entity!")
229 local enttable = util.JSONToTable( v.data )
230 -- print("enttable")
231 -- PrintTable(enttable)
232 if bank then
233 ARCBank.GetOwnedAccounts(ply,function(errorcode, account_list)
234 for i=1, #account_list do
235 local v = account_list[i]
236 if string.StartWith( v, "_" ) then
237 ARCBank.AddMoney(ply, v, tonumber(enttable[2]), 2, "REFUND-System",function(errorcode) end)
238 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ auf dein Konto erhalten!" )
239 return
240 end
241 end
242 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wenn er kein Konto besitzt dann bekommt er es auch auf die Hand
243 ply:addMoney(tonumber(enttable[2]))
244 end)
245
246 else
247 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wird nur ausgeführt wenn kein ARCBank auf dem Server geladen ist
248 ply:addMoney(tonumber(enttable[2]))
249 end
250 end
251 end
252end
253
254function RFSYS:RefundPlayerAll( ply, bank )
255 local steamid = ply:SteamID()
256
257 local data = sql.Query("SELECT * FROM refundsplayer WHERE playerid = '"..steamid.."' AND NOT seasonid = "..self.SeasonID..";")
258 sql.Query("DELETE FROM refundsplayer WHERE playerid = '"..steamid.."' AND NOT seasonid = "..self.SeasonID..";")
259 for k,v in pairs( data ) do
260 -- print(k,v)
261 -- PrintTable(v)
262 if tonumber(v.type) == TYPE_WEP then
263 -- print("Detected weapons!")
264 ply:Give(v.data)
265 elseif tonumber(v.type) == TYPE_ENT then
266 local enttable = util.JSONToTable( v.data )
267 if bank then
268 ARCBank.GetOwnedAccounts(ply,function(errorcode, account_list)
269 for i=1, #account_list do
270 local v = account_list[i]
271 if string.StartWith( v, "_" ) then
272 ARCBank.AddMoney(ply, v, tonumber(enttable[2]), 2, "REFUND-System",function(errorcode) end)
273 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ auf dein Konto erhalten!" )
274 return
275 end
276 end
277 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wenn er kein Konto besitzt dann bekommt er es auch auf die Hand
278 ply:addMoney(tonumber(enttable[2]))
279 end)
280
281 else
282 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wird nur ausgeführt wenn kein ARCBank auf dem Server geladen ist
283 ply:addMoney(tonumber(enttable[2]))
284 end
285 end
286 end
287
288end
289
290function RFSYS:RefundPlayerSingleItem( ply, seasonid, id, bank )
291 --print("Refunding single item from", ply, seasonid, id, bank)
292 local steamid = ply:SteamID()
293 local data = sql.Query("SELECT * FROM refundsplayer WHERE playerid = '"..steamid.."' AND seasonid = "..seasonid.." AND identifier = "..id..";")
294 if not data then GChat:NotifyPlayer( ply, "Ungültige Anfrage! (NO MATCH TO SQL)" ) return end
295 sql.Query("DELETE FROM refundsplayer WHERE playerid = '"..steamid.."' AND seasonid = "..seasonid.." AND identifier = "..id..";")
296 --print("Data Single Item:")
297 data = data[1]
298 --PrintTable(data)
299 --print(tonumber(data.type))
300 if tonumber(data.type) == TYPE_WEP then
301 ply:Give(data.data)
302 elseif tonumber(data.type) == TYPE_ENT then
303 local enttable = util.JSONToTable( data.data )
304 if bank then
305 ARCBank.GetOwnedAccounts(ply,function(errorcode, account_list)
306 for i=1, #account_list do
307 local v = account_list[i]
308 if string.StartWith( v, "_" ) then
309 ARCBank.AddMoney(ply, v, tonumber(enttable[2]), 2, "REFUND-System",function(errorcode) end)
310 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ auf dein Konto erhalten!" )
311 return
312 end
313 end
314 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wenn er kein Konto besitzt dann bekommt er es auch auf die Hand
315 ply:addMoney(tonumber(enttable[2]))
316 end)
317
318 else
319 GChat:NotifyPlayer( ply, "Du hast "..enttable[2].."$ erhalten!" ) -- Wird nur ausgeführt wenn kein ARCBank auf dem Server geladen ist
320 ply:addMoney(tonumber(enttable[2]))
321 end
322 else
323 GChat:NotifyPlayer( ply, "Ungültige Anfrage! (UNKNOWN REQUEST TYPE)" )
324 return
325 end
326end
327
328function RFSYS:RegisterRemoval( ent )
329
330 --print("[REFUND] Checking Removing an entity...",ent)
331 if not IsValid( ent ) or not ent.refundable then return end
332 sql.Query("DELETE FROM refundsplayer WHERE seasonid = '"..self.SeasonID.."' AND identifier = "..ent:EntIndex()..";")
333 --print("[REFUND] Removing an entity...",ent)
334
335end
336
337function RFSYS:RegisterEntity( ply, ent, type, price )
338 if not IsValid( ply ) or not IsValid( ent ) then return end
339 if not self.SeasonID then return end
340
341 ent:CallOnRemove( "refund_remove", function(ent) self.RegisterRemoval(ent) end)
342 --print("[REFUND] Trying Registering a new entity....", ply, ent ,type,price)
343 if type == TYPE_WEP then
344 if RFSYS.Config.UseWhitelistedWeapons and ( ( not self.Config.WhitelistedWeapons[ent:GetClass()] and not RFSYS.Config.IsWhitelistBlacklist ) or ( RFSYS.Config.IsWhitelistBlacklist and self.Config.WhitelistedWeapons[ent:GetClass()] ) ) then return end
345 ent.refundable = true
346 --print("[REFUND] Registering Weapon...",ent)
347 sql.Query("INSERT INTO refundsplayer (playerid,seasonid,identifier,type,data,timestamp) VALUES ('"..ply:SteamID().."',"..self.SeasonID..","..ent:EntIndex()..","..type..",'"..ent:GetClass().."',"..os.time()..")")
348
349 ent.OnDrop = function( ent )
350 self.RegisterRemoval( ent )
351 end
352
353 ent.OwnerChanged = function( ent )
354 self.RegisterRemoval( ent )
355 end
356
357 elseif type == TYPE_ENT then
358 ent.refundable = true
359 --print("[REFUND] Registering Entity...",ent)
360 sql.Query("INSERT INTO refundsplayer (playerid,seasonid,identifier,type,data,timestamp) VALUES ('"..ply:SteamID().."',"..self.SeasonID..","..ent:EntIndex()..","..type..",'".. util.TableToJSON( { ent:GetClass(), price } ) .."',"..os.time()..")")
361
362 else
363 --print("Not Saving!")
364 return
365 end
366
367end
368
369
370function RFSYS:CheckForToOldRefunds()
371 sql.Query("DELETE FROM refundsplayer WHERE timestamp <= '"..math.Round( os.time() - RFSYS.Config.WhenShouldRefundsExpire ).."';")
372end
373
374
375local REFUND_SERVER_RESTART = false
376
377hook.Add( "ShutDown", "Refund_ShutDown", function()
378 if RFSYS.Config.ShouldRefundonRestart then
379 REFUND_SERVER_RESTART = true
380 end
381end )
382
383hook.Add( "Initialize", "refund_Initialize", function()
384 print("[REFUND] Initialize")
385 timer.Simple( 3, function()
386
387 sql.Query([[
388 CREATE TABLE IF NOT EXISTS refundsplayer(
389 playerid VARCHAR(20) NOT NULL,
390 seasonid INTEGER NOT NULL,
391 identifier INTEGER NOT NULL,
392 type INTEGER NOT NULL,
393 data TEXT NOT NULL,
394 timestamp INTEGER NOT NULL
395 );
396 ]])
397
398 RFSYS:CheckForToOldRefunds()
399 RFSYS:CreateNewSeasonID()
400
401 end )
402end )
403
404hook.Add( "PlayerInitialSpawn", "refund_PlayerInitialSpawn", function( ply )
405 timer.Simple( 1, function()
406 if not IsValid( ply ) then return end
407 RFSYS:LoadPlayerRefunds( ply )
408 end)
409end)
410
411
412hook.Add( "PlayerDisconnected", "refund_PlayerDisconnected", function( ply )
413
414 RFSYS:ClearCurrentSeason( ply )
415
416end )
417
418
419--PrintTable( sql.Query("SELECT * FROM refundsplayer;") or {} )
420--print( "Total Entrys: "..#(sql.Query("SELECT * FROM refundsplayer;") or {}))
421
422hook.Add( "playerBoughtCustomEntity", "refund_playerBoughtCustomEntity", function( ply, entTable, ent, price )
423
424 RFSYS:RegisterEntity( ply, ent, TYPE_ENT, price )
425
426end )
427
428hook.Add( "WeaponEquip", "refund_WeaponEquip", function( wep, ply )
429 RFSYS:RegisterEntity( ply, wep, TYPE_WEP, 0 )
430end)
431
432hook.Add( "EntityRemoved", "refund_EntityRemoved", function(ent) RFSYS:RegisterRemoval( ent ) end)
433
434--hook.Remove("EntityRemoved", "refund_EntityRemoved")
435
436hook.Add( "PlayerSay", "refund_PlayerSay", function( ply, txt )
437 if ( string.lower( txt ) == "/refund" ) then
438 RFSYS:SendRefundsToPlayer( ply )
439 return ""
440 end
441end )
442
443/*
444
445print("Printing Refund Table...")
446for k,v in pairs( player.GetAll() ) do
447 print("Printing Refund Table of ", v)
448 PrintTable( RFSYS.refund[v] )
449
450end
451
452*/
453
454net.Receive("request_refunddata", function( len, ply )
455
456 RFSYS:SendRefundsToPlayer( ply )
457
458end)
459
460function RFSYS:SendRefundsToPlayer( ply )
461
462 if (ply.NextRefundRequest and ply.NextRefundRequest > CurTime() ) then
463 GChat:NotifyPlayer( ply , "Warte einen Moment, bevor du wieder etwas anfragen darfst!")
464 return ""
465 end
466 if not RFSYS.refund[ply] then return end
467 ply.NextRefundRequest = CurTime()
468 --if #RFSYS.refund[ply] < 1 then GChat:NotifyPlayer( ply , "Es wurden keine Items gefunden!") return end
469 net.Start("request_refunddata")
470 net.WriteTable( RFSYS.refund[ply] )
471 net.Send( ply )
472
473end
474
475
476net.Receive("request_executerefund", function( len, ply )
477
478 local refundtype = net.ReadString()
479
480 --print("Received Refund request!")
481
482 if refundtype == "selected" then
483
484 local season = net.ReadInt(32)
485 local data = net.ReadTable()
486 --print("Season: ", season)
487 --PrintTable(data)
488
489 for k,v in pairs( data ) do
490 --print(v)
491 --PrintTable(v)
492 RFSYS:RefundPlayerSingleItem( ply, season, v.identifier, (ARCBank) or false )
493
494 end
495 RFSYS:RefreshPlayer( ply )
496 elseif refundtype == "season" then
497
498 local season = net.ReadInt(32)
499 --print( season )
500 RFSYS:RefundPlayerSeason( ply, season, (ARCBank) or false )
501 RFSYS:RefreshPlayer( ply )
502
503 elseif refundtype == "all" then
504
505 RFSYS:RefundPlayerAll( ply, (ARCBank) or false )
506 RFSYS:RefreshPlayer( ply )
507
508 else
509 return
510 end
511
512end)