My favorites | Sign in
Project Home Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 142 attachment: ev_framework.lua (32.6 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/*-------------------------------------------------------------------------------------------------------------------------
Framework providing the main Evolve functions
-------------------------------------------------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------------------------------
Comfortable constants
-------------------------------------------------------------------------------------------------------------------------*/

evolve.constants = {}
evolve.colors = {}
evolve.ranks = {}
evolve.privileges = {}
evolve.bans = {}
evolve.constants.notallowed = "You are not allowed to do that."
evolve.constants.noplayers = "No matching players with an equal or lower immunity found."
evolve.constants.noplayers2 = "No matching players with a lower immunity found."
evolve.constants.noplayersnoimmunity = "No matching players found."
evolve.admins = 1
evolve.colors.blue = Color( 98, 176, 255, 255 )
evolve.colors.red = Color( 255, 62, 62, 255 )
evolve.colors.green = Color( 62, 255, 62, 255 )
evolve.colors.white = color_white
evolve.category = {}
evolve.category.administration = 1
evolve.category.actions = 2
evolve.category.punishment = 3
evolve.category.teleportation = 4

/*-------------------------------------------------------------------------------------------------------------------------
Messages and notifications
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:Message( msg )
print( "[EV] " .. msg )
end

if ( SERVER ) then
evolve.SilentNotify = false

function evolve:Notify( ... )
local ply
local arg = { ... }

if ( type( arg[1] ) == "Player" or arg[1] == NULL ) then ply = arg[1] end
if ( arg[1] == evolve.admins ) then
for _, pl in ipairs( player.GetAll() ) do
if ( pl:IsAdmin() ) then
table.remove( arg, 1 )
evolve:Notify( pl, unpack( arg ) )
end
end
return
end

if ( ply != NULL and !self.SilentNotify ) then
umsg.Start( "EV_Notification", ply )
umsg.Short( #arg )
for _, v in ipairs( arg ) do
if ( type( v ) == "string" ) then
umsg.String( v )
elseif ( type ( v ) == "table" ) then
umsg.Short( v.r )
umsg.Short( v.g )
umsg.Short( v.b )
umsg.Short( v.a )
end
end
umsg.End()
end

local str = ""
for _, v in ipairs( arg ) do
if ( type( v ) == "string" ) then str = str .. v end
end

if ( ply ) then
print( "[EV] " .. ply:Nick() .. " -> " .. str )
evolve:Log( evolve:PlayerLogStr( ply ) .. " -> " .. str )
else
print( "[EV] " .. str )
evolve:Log( str )
end
end
else
function evolve:Notify( ... )
local arg = { ... }

args = {}
for _, v in ipairs( arg ) do
if ( type( v ) == "string" or type( v ) == "table" ) then table.insert( args, v ) end
end

chat.AddText( unpack( args ) )
end

usermessage.Hook( "EV_Notification", function( um )
local argc = um:ReadShort()
local args = {}
for i = 1, argc / 2, 1 do
table.insert( args, Color( um:ReadShort(), um:ReadShort(), um:ReadShort(), um:ReadShort() ) )
table.insert( args, um:ReadString() )
end

chat.AddText( unpack( args ) )
end )
end

/*-------------------------------------------------------------------------------------------------------------------------
Utility functions
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:BoolToInt( bool )
if ( bool ) then return 1 else return 0 end
end

function evolve:KeyByValue( tbl, value, iterator )
iterator = iterator or pairs
for k, v in iterator( tbl ) do
if ( value == v ) then return k end
end
end

function evolve:FormatTime( t )
if ( t < 0 ) then
return "Forever"
elseif ( t < 60 ) then
if ( t == 1 ) then return "one second" else return t .. " seconds" end
elseif ( t < 3600 ) then
if ( math.ceil( t / 60 ) == 1 ) then return "one minute" else return math.ceil( t / 60 ) .. " minutes" end
elseif ( t < 24 * 3600 ) then
if ( math.ceil( t / 3600 ) == 1 ) then return "one hour" else return math.ceil( t / 3600 ) .. " hours" end
elseif ( t < 24 * 3600 * 7 ) then
if ( math.ceil( t / ( 24 * 3600 ) ) == 1 ) then return "one day" else return math.ceil( t / ( 24 * 3600 ) ) .. " days" end
elseif ( t < 24 * 3600 * 30 ) then
if ( math.ceil( t / ( 24 * 3600 * 7 ) ) == 1 ) then return "one week" else return math.ceil( t / ( 24 * 3600 * 7 ) ) .. " weeks" end
else
if ( math.ceil( t / ( 24 * 3600 * 30 ) ) == 1 ) then return "one month" else return math.ceil( t / ( 24 * 3600 * 30 ) ) .. " months" end
end
end

/*-------------------------------------------------------------------------------------------------------------------------
Plugin management
-------------------------------------------------------------------------------------------------------------------------*/

local pluginFile

function evolve:LoadPlugins()
evolve.plugins = {}

local plugins = file.FindInLua( "ev_plugins/*.lua" )
for _, plugin in ipairs( plugins ) do
local prefix = string.Left( plugin, string.find( plugin, "_" ) - 1 )
pluginFile = plugin

if ( CLIENT and ( prefix == "sh" or prefix == "cl" ) ) then
include( "ev_plugins/" .. plugin )
elseif ( SERVER ) then
include( "ev_plugins/" .. plugin )
if ( prefix == "sh" or prefix == "cl" ) then AddCSLuaFile( "ev_plugins/" .. plugin ) end
end
end
end

function evolve:RegisterPlugin( plugin )
if ( string.Left( pluginFile, string.find( pluginFile, "_" ) - 1 ) != "cl" or CLIENT ) then
table.insert( evolve.plugins, plugin )
plugin.File = pluginFile
if ( plugin.Privileges ) then table.Add( evolve.privileges, plugin.Privileges ) table.sort( evolve.privileges ) end
else
table.insert( evolve.plugins, { Title = plugin.Title, File = pluginFile } )
end
end

function evolve:FindPlugin( name )
for _, plugin in ipairs( evolve.plugins ) do
if ( plugin.Title == name ) then return plugin end
end
end

if ( !evolve.HookCall ) then evolve.HookCall = hook.Call end
hook.Call = function( name, gm, ... )
local arg = { ... }

for _, plugin in ipairs( evolve.plugins ) do
if ( plugin[ name ] ) then
local retValues = { pcall( plugin[name], plugin, ... ) }

if ( retValues[1] and retValues[2] != nil ) then
table.remove( retValues, 1 )
return unpack( retValues )
elseif ( !retValues[1] ) then
evolve:Notify( evolve.colors.red, "Hook '" .. name .. "' in plugin '" .. plugin.Title .. "' failed with error:" )
evolve:Notify( evolve.colors.red, retValues[2] )
end
end
end

if ( CLIENT ) then
for _, tab in ipairs( evolve.MENU.Tabs ) do
if ( tab[ name ] ) then
local retValues = { pcall( tab[name], tab, ... ) }

if ( retValues[1] and retValues[2] != nil ) then
table.remove( retValues, 1 )
return unpack( retValues )
elseif ( !retValues[1] ) then
evolve:Notify( evolve.colors.red, "Hook '" .. name .. "' in tab '" .. tab.Title .. "' failed with error:" )
evolve:Notify( evolve.colors.red, retValues[2] )
end
end
end
end

return evolve.HookCall( name, gm, ... )
end

if ( SERVER ) then
concommand.Add( "ev_reloadplugin", function( ply, com, args )
if ( !ply:IsValid() and args[1] ) then
local found

for k, plugin in ipairs( evolve.plugins ) do
if ( string.lower( plugin.Title ) == string.lower( args[1] ) ) then
found = k
break
end
end

if ( found ) then
print( "[EV] Reloading plugin " .. evolve.plugins[found].Title .. "..." )

local plugin = evolve.plugins[found].File
local title = evolve.plugins[found].Title
local prefix = string.Left( plugin, string.find( plugin, "_" ) - 1 )

if ( prefix != "cl" ) then table.remove( evolve.plugins, found ) pluginFile = plugin include( "ev_plugins/" .. plugin ) end

if ( prefix == "sh" or prefix == "cl" ) then
datastream.StreamToClients( player.GetAll(), "EV_PluginFile", { Title = title, Contents = file.Read( "../lua/ev_plugins/" .. plugin ) } )
end
else
print( "[EV] Plugin '" .. tostring( args[1] ) .. "' not found!" )
end
end
end )
else
datastream.Hook( "EV_PluginFile", function( handler, id, encoded, decoded )
for k, plugin in ipairs( evolve.plugins ) do
if ( string.lower( plugin.Title ) == string.lower( decoded.Title ) ) then
found = k
table.remove( evolve.plugins, k )
end
end

RunString( decoded.Contents )
end )
end

/*-------------------------------------------------------------------------------------------------------------------------
Player collections
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:IsNameMatch( ply, str )
if ( str == "*" ) then
return true
elseif ( str == "@" and ply:IsAdmin() ) then
return true
elseif ( str == "!@" and !ply:IsAdmin() ) then
return true
elseif ( string.match( str, "STEAM_[0-5]:[0-9]:[0-9]+" ) ) then
return ply:SteamID() == str
elseif ( string.Left( str, 1 ) == "\"" and string.Right( str, 1 ) == "\"" ) then
return ( ply:Nick() == string.sub( str, 2, #str - 1 ) )
else
return ( string.lower( ply:Nick() ) == string.lower( str ) or string.find( string.lower( ply:Nick() ), string.lower( str ) ) )
end
end

function evolve:FindPlayer( name, def, nonum, noimmunity )
local matches = {}

if ( !name or #name == 0 ) then
matches[1] = def
else
if ( type( name ) != "table" ) then name = { name } end
local name2 = table.Copy( name )
if ( nonum ) then
if ( #name2 > 1 and tonumber( name2[ #name2 ] ) ) then table.remove( name2, #name2 ) end
end

for _, ply in ipairs( player.GetAll() ) do
for _, pm in ipairs( name2 ) do
if ( evolve:IsNameMatch( ply, pm ) and !table.HasValue( matches, ply ) and ( noimmunity or !def or def:EV_BetterThanOrEqual( ply ) ) ) then table.insert( matches, ply ) end
end
end
end

return matches
end

function evolve:CreatePlayerList( tbl, notall )
local lst = ""
local lword = "and"
if ( notall ) then lword = "or" end

if ( #tbl == 1 ) then
lst = tbl[1]:Nick()
elseif ( #tbl == #player.GetAll() ) then
lst = "everyone"
else
for i = 1, #tbl do
if ( i == #tbl ) then lst = lst .. " " .. lword .. " " .. tbl[i]:Nick() elseif ( i == 1 ) then lst = tbl[i]:Nick() else lst = lst .. ", " .. tbl[i]:Nick() end
end
end

return lst
end

/*-------------------------------------------------------------------------------------------------------------------------
Ranks
-------------------------------------------------------------------------------------------------------------------------*/

function _R.Player:EV_IsRespected()
return self:GetNWString( "EV_UserGroup" ) == "respected" or self:EV_IsAdmin()
end

function _R.Player:EV_IsAdmin()
return self:GetNWString( "EV_UserGroup" ) == "admin" or self:IsAdmin() or self:EV_IsSuperAdmin()
end

function _R.Player:EV_IsSuperAdmin()
return self:GetNWString( "EV_UserGroup" ) == "superadmin" or self:IsSuperAdmin() or self:EV_IsOwner()
end

function _R.Player:EV_IsOwner()
if ( SERVER ) then
return self:GetNWString( "EV_UserGroup" ) == "owner" or self:IsListenServerHost()
else
return self:GetNWString( "EV_UserGroup" ) == "owner"
end
end

function _R.Player:EV_IsRank( rank )
return self:GetNWString( "EV_UserGroup" ) == rank
end

/*-------------------------------------------------------------------------------------------------------------------------
Console
-------------------------------------------------------------------------------------------------------------------------*/

function _R.Entity:Nick() if ( !self:IsValid() ) then return "Console" end end
function _R.Entity:EV_IsRespected() if ( !self:IsValid() ) then return true end end
function _R.Entity:EV_IsAdmin() if ( !self:IsValid() ) then return true end end
function _R.Entity:EV_IsSuperAdmin() if ( !self:IsValid() ) then return true end end
function _R.Entity:EV_IsOwner() if ( !self:IsValid() ) then return true end end
function _R.Entity:EV_GetRank() if ( !self:IsValid() ) then return "owner" end end
function _R.Entity:UniqueID() if ( !self:IsValid() ) then return 0 end end

/*-------------------------------------------------------------------------------------------------------------------------
Player information
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:LoadPlayerInfo()
if ( file.Exists( "ev_playerinfo.txt" ) ) then
debug.sethook()
self.PlayerInfo = glon.decode( file.Read( "ev_playerinfo.txt" ) )
else
self.PlayerInfo = {}
end
end

function evolve:SavePlayerInfo()
file.Write( "ev_playerinfo.txt", glon.encode( self.PlayerInfo ) )
end

function _R.Player:GetProperty( id, defaultvalue )
if ( !evolve.PlayerInfo ) then evolve:LoadPlayerInfo() end

if ( evolve.PlayerInfo[ self:UniqueID() ] ) then
return evolve.PlayerInfo[ self:UniqueID() ][ id ] or defaultvalue
else
return defaultvalue
end
end

function _R.Player:SetProperty( id, value )
if ( !evolve.PlayerInfo ) then evolve:LoadPlayerInfo() end
if ( !evolve.PlayerInfo[ self:UniqueID() ] ) then evolve.PlayerInfo[ self:UniqueID() ] = {} end

evolve.PlayerInfo[ self:UniqueID() ][ id ] = value
end

function evolve:UniqueIDByProperty( property, value, exact )
if ( !evolve.PlayerInfo ) then evolve:LoadPlayerInfo() end

for k, v in pairs( evolve.PlayerInfo ) do
if ( v[ property ] == value ) then
return k
elseif ( !exact and string.find( string.lower( v[ property ] or "" ), string.lower( value ) ) ) then
return k
end
end
end

function evolve:GetProperty( uniqueid, id, defaultvalue )
uniqueid = tostring( uniqueid )
if ( !evolve.PlayerInfo ) then evolve:LoadPlayerInfo() end

if ( evolve.PlayerInfo[ uniqueid ] ) then
return evolve.PlayerInfo[ uniqueid ][ id ] or defaultvalue
else
return defaultvalue
end
end

function evolve:SetProperty( uniqueid, id, value )
uniqueid = tostring( uniqueid )
if ( !evolve.PlayerInfo ) then evolve:LoadPlayerInfo() end
if ( !evolve.PlayerInfo[ uniqueid ] ) then evolve.PlayerInfo[ uniqueid ] = {} end

evolve.PlayerInfo[ uniqueid ][ id ] = value
end

function evolve:CommitProperties()
/*-------------------------------------------------------------------------------------------------------------------------
Check if a cleanup would be convenient
-------------------------------------------------------------------------------------------------------------------------*/

local count = table.Count( evolve.PlayerInfo )

if ( count > 800 ) then
local original = count
local info = {}
for uid, entry in pairs( evolve.PlayerInfo ) do
table.insert( info, { UID = uid, Rank = entry.Rank } )
end
table.SortByMember( info, "Rank", function(a, b) return a > b end )

for _, entry in pairs( info ) do
if ( ( !entry.BanEnd or entry.BanEnd < os.time() ) and ( !entry.Rank or entry.Rank == "guest" ) ) then
evolve.PlayerInfo[ entry.UID ] = nil
count = count - 1
if ( count < 800 ) then break end
end
end

evolve:Message( "Cleaned up " .. original - count .. " players." )
end

evolve:SavePlayerInfo()
end

/*-------------------------------------------------------------------------------------------------------------------------
Entity ownership
-------------------------------------------------------------------------------------------------------------------------*/

hook.Add( "PlayerSpawnedProp", "EV_SpawnHook", function( ply, model, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned prop '" .. model .. "'." ) end )
hook.Add( "PlayerSpawnedSENT", "EV_SpawnHook", function( ply, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned scripted entity '" .. ent:GetClass() .. "'." ) end )
hook.Add( "PlayerSpawnedNPC", "EV_SpawnHook", function( ply, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned npc '" .. ent:GetClass() .. "'." ) end )
hook.Add( "PlayerSpawnedVehicle", "EV_SpawnHook", function( ply, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned vehicle '" .. ent:GetClass() .. "'." ) end )
hook.Add( "PlayerSpawnedEffect", "EV_SpawnHook", function( ply, model, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned effect '" .. model .. "'." ) end )
hook.Add( "PlayerSpawnedRagdoll", "EV_SpawnHook", function( ply, model, ent ) ent.EV_Owner = ply:UniqueID() evolve:Log( evolve:PlayerLogStr( ply ) .. " spawned ragdoll '" .. model .. "'." ) end )

evolve.AddCount = _R.Player.AddCount
function _R.Player:AddCount( type, ent )
ent.EV_Owner = self:UniqueID()
return evolve.AddCount( self, type, ent )
end

evolve.CleanupAdd = cleanup.Add
function cleanup.Add( ply, type, ent )
if ( ent ) then ent.EV_Owner = ply:UniqueID() end
return evolve.CleanupAdd( ply, type, ent )
end

function _R.Entity:EV_GetOwner()
return self.EV_Owner
end

/*-------------------------------------------------------------------------------------------------------------------------
Ranks
-------------------------------------------------------------------------------------------------------------------------*/

// COMPATIBILITY
evolve.compatibilityRanks = glon.decode( file.Read( "ev_ranks.txt" ) )
// COMPATIBILITY

function _R.Player:EV_HasPrivilege( priv )
if ( evolve.ranks[ self:EV_GetRank() ] ) then
return self:GetNWString( "EV_UserGroup" ) == "owner" or table.HasValue( evolve.ranks[ self:GetNWString( "EV_UserGroup" ) ].Privileges, priv )
else
return false
end
end

function _R.Entity:EV_BetterThan( ply )
return true
end

function _R.Entity:EV_BetterThanOrEqual( ply )
return true
end

function _R.Player:EV_BetterThan( ply )
return tonumber( evolve.ranks[ self:GetNWString( "EV_UserGroup" ) ].Immunity ) > tonumber( evolve.ranks[ ply:GetNWString( "EV_UserGroup" ) ].Immunity ) or self == ply
end

function _R.Player:EV_BetterThanOrEqual( ply )
return tonumber( evolve.ranks[ self:GetNWString( "EV_UserGroup" ) ].Immunity ) >= tonumber( evolve.ranks[ ply:GetNWString( "EV_UserGroup" ) ].Immunity )
end

function _R.Entity:EV_HasPrivilege( priv )
if ( self == NULL ) then return true end
end

function _R.Entity:EV_BetterThan( ply )
if ( self == NULL ) then return true end
end

function _R.Player:EV_SetRank( rank )
self:SetProperty( "Rank", rank )
evolve:CommitProperties()

self:SetNWString( "EV_UserGroup", rank )

evolve:RankGroup( self, rank )

if ( self:EV_HasPrivilege( "Ban menu" ) ) then
evolve:SyncBans( self )
end
end

function _R.Player:EV_GetRank()
return self:GetNWString( "EV_UserGroup", "guest" )
end

function evolve:RankGroup( ply, rank )
ply:SetUserGroup( evolve.ranks[ rank ].UserGroup )
end

function evolve:Rank( ply )
if ( !ply:IsValid() ) then return end

self:TransferPrivileges( ply )
self:TransferRanks( ply )

if ( ply:IsListenServerHost() ) then ply:SetNWString( "EV_UserGroup", "owner" ) ply:SetNWString( "UserGroup", "superadmin" ) return end

local usergroup = ply:GetNWString( "UserGroup", "guest" )
if ( usergroup == "user" ) then usergroup = "guest" end
ply:SetNWString( "EV_UserGroup", usergroup )

local rank = ply:GetProperty( "Rank" )
if ( rank and evolve.ranks[ rank ] ) then
ply:SetNWString( "EV_UserGroup", rank )
usergroup = rank
else
// COMPATIBILITY
if ( evolve.compatibilityRanks ) then
for _, ranks in ipairs( evolve.compatibilityRanks ) do
if ( ranks.steamID == ply:SteamID() ) then
rank = ranks.rank

ply:SetNWString( "EV_UserGroup", rank )
usergroup = rank

ply:SetProperty( "Rank", rank )
evolve:CommitProperties()

break
end
end
end
// COMPATIBILITY
end

if ( ply:EV_HasPrivilege( "Ban menu" ) ) then
evolve:SyncBans( ply )
end

evolve:RankGroup( ply, usergroup )
end

hook.Add( "PlayerSpawn", "EV_RankHook", function( ply )
if ( !ply.EV_Ranked ) then
ply:SetNWString( "EV_UserGroup", ply:GetProperty( "Rank", "guest" ) )

timer.Simple( 1, function()
evolve:Rank( ply )
end )
ply.EV_Ranked = true
end
end )

/*-------------------------------------------------------------------------------------------------------------------------
Rank management
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:SaveRanks()
file.Write( "ev_userranks.txt", glon.encode( evolve.ranks ) )
end

function evolve:LoadRanks()
if ( file.Exists( "ev_userranks.txt" ) ) then
evolve.ranks = glon.decode( file.Read( "ev_userranks.txt" ) )
else
include( "ev_defaultranks.lua" )
evolve:SaveRanks()
end
end

if ( SERVER ) then evolve:LoadRanks() end

function evolve:SyncRanks()
for _, pl in ipairs( player.GetAll() ) do evolve:TransferRanks( pl ) end
end

function evolve:TransferPrivileges( ply )
if ( !ply:IsValid() ) then return end

for id, privilege in ipairs( evolve.privileges ) do
umsg.Start( "EV_Privilege", ply )
umsg.Short( id )
umsg.String( privilege )
umsg.End()
end
end

function evolve:TransferRank( ply, rank )
if ( !ply:IsValid() ) then return end

local data = evolve.ranks[ rank ]
local color = data.Color

umsg.Start( "EV_Rank", ply )
umsg.String( rank )
umsg.String( data.Title )
umsg.String( data.Icon )
umsg.String( data.UserGroup )
umsg.Short( data.Immunity )

if ( color ) then
umsg.Bool( true )
umsg.Short( color.r )
umsg.Short( color.g )
umsg.Short( color.b )
else
umsg.Bool( false )
end
umsg.End()

umsg.Start( "EV_RankPrivileges", ply )
umsg.String( rank )
umsg.Short( #( data.Privileges or {} ) )

for _, privilege in ipairs( data.Privileges or {} ) do
umsg.Short( evolve:KeyByValue( evolve.privileges, privilege, ipairs ) )
end
umsg.End()
end

function evolve:TransferRanks( ply )
for id, data in pairs( evolve.ranks ) do
evolve:TransferRank( ply, id )
end
end

usermessage.Hook( "EV_Rank", function( um )
local id = string.lower( um:ReadString() )
local title = um:ReadString()
local created = evolve.ranks[id] == nil

evolve.ranks[id] = {
Title = title,
Icon = um:ReadString(),
UserGroup = um:ReadString(),
Immunity = um:ReadShort(),
Privileges = {},
}

if ( um:ReadBool() ) then
evolve.ranks[id].Color = Color( um:ReadShort(), um:ReadShort(), um:ReadShort() )
end

evolve.ranks[id].IconTexture = surface.GetTextureID( "gui/silkicons/" .. evolve.ranks[id].Icon )

if ( created ) then
hook.Call( "EV_RankCreated", nil, id )
else
hook.Call( "EV_RankUpdated", nil, id )
end
end )

usermessage.Hook( "EV_Privilege", function( um )
local id = um:ReadShort()
local name = um:ReadString()

evolve.privileges[ id ] = name
end )

usermessage.Hook( "EV_RankPrivileges", function( um )
local rank = um:ReadString()
local privilegeCount = um:ReadShort()

for i = 1, privilegeCount do
table.insert( evolve.ranks[ rank ].Privileges, evolve.privileges[ um:ReadShort() ] )
end
end )

usermessage.Hook( "EV_RemoveRank", function( um )
local rank = um:ReadString()
hook.Call( "EV_RankRemoved", nil, rank )
evolve.ranks[ rank ] = nil
end )

usermessage.Hook( "EV_RenameRank", function( um )
local rank = um:ReadString():lower()
evolve.ranks[ rank ].Title = um:ReadString()

hook.Call( "EV_RankRenamed", nil, rank, evolve.ranks[ rank ].Title )
end )

usermessage.Hook( "EV_RankPrivilege", function( um )
local rank = um:ReadString()
local priv = evolve.privileges[ um:ReadShort() ]
local enabled = um:ReadBool()

if ( enabled ) then
table.insert( evolve.ranks[ rank ].Privileges, priv )
else
table.remove( evolve.ranks[ rank ].Privileges, evolve:KeyByValue( evolve.ranks[ rank ].Privileges, priv ) )
end

hook.Call( "EV_RankPrivilegeChange", nil, rank, priv, enabled )
end )

/*-------------------------------------------------------------------------------------------------------------------------
Rank modification
-------------------------------------------------------------------------------------------------------------------------*/

if ( SERVER ) then
concommand.Add( "ev_renamerank", function( ply, com, args )
if ( ply:EV_HasPrivilege( "Rank modification" ) ) then
if ( #args > 1 and evolve.ranks[ args[1] ] ) then
evolve:Notify( evolve.colors.red, ply:Nick(), evolve.colors.white, " has renamed ", evolve.colors.blue, evolve.ranks[ args[1] ].Title, evolve.colors.white, " to ", evolve.colors.blue, table.concat( args, " ", 2 ), evolve.colors.white, "." )

evolve.ranks[ args[1] ].Title = table.concat( args, " ", 2 )
evolve:SaveRanks()

umsg.Start( "EV_RenameRank" )
umsg.String( args[1] )
umsg.String( evolve.ranks[ args[1] ].Title )
umsg.End()
end
end
end )

concommand.Add( "ev_setrank", function( ply, com, args )
if ( ply:EV_HasPrivilege( "Rank modification" ) ) then
if ( #args == 3 and tonumber( args[3] ) and evolve.ranks[ args[1] ] and table.HasValue( evolve.privileges, args[2] ) and args[1] != "owner" ) then
local rank = args[1]
local privilege = args[2]

if ( tonumber( args[3] ) == 1 ) then
if ( !table.HasValue( evolve.ranks[ rank ].Privileges, privilege ) ) then
table.insert( evolve.ranks[ rank ].Privileges, privilege )
end
else
if ( table.HasValue( evolve.ranks[ rank ].Privileges, privilege ) ) then
table.remove( evolve.ranks[ rank ].Privileges, evolve:KeyByValue( evolve.ranks[ rank ].Privileges, privilege ) )
end
end

evolve:SaveRanks()

umsg.Start( "EV_RankPrivilege" )
umsg.String( rank )
umsg.Short( evolve:KeyByValue( evolve.privileges, privilege ) )
umsg.Bool( tonumber( args[3] ) == 1 )
umsg.End()
end
end
end )

concommand.Add( "ev_setrankp", function( ply, com, args )
if ( ply:EV_HasPrivilege( "Rank modification" ) ) then
if ( #args == 6 and tonumber( args[2] ) and evolve.ranks[ args[1] ] and ( args[3] == "guest" or args[3] == "admin" or args[3] == "superadmin" ) and tonumber( args[4] ) and tonumber( args[5] ) and tonumber( args[6] ) ) then
if ( args[1] != "owner" ) then
evolve.ranks[ args[1] ].Immunity = tonumber( args[2] )
evolve.ranks[ args[1] ].UserGroup = args[3]
end

evolve.ranks[ args[1] ].Color = Color( args[4], args[5], args[6] )
evolve:SaveRanks()

for _, pl in ipairs( player.GetAll() ) do
evolve:TransferRank( pl, args[1] )

if ( args[1] != "owner" and pl:GetNWString( "EV_UserGroup" ) == args[1] ) then
pl:SetNWString( "UserGroup", args[3] )
end
end
end
end
end )

concommand.Add( "ev_removerank", function( ply, com, args )
if ( ply:EV_HasPrivilege( "Rank modification" ) ) then
if ( args[1] != "guest" and args[1] != "owner" and evolve.ranks[ args[1] ] ) then
evolve:Notify( evolve.colors.red, ply:Nick(), evolve.colors.white, " has removed the rank ", evolve.colors.blue, evolve.ranks[ args[1] ].Title, evolve.colors.white, "." )

evolve.ranks[ args[1] ] = nil
evolve:SaveRanks()

for _, pl in ipairs( player.GetAll() ) do
if ( pl:EV_GetRank() == args[1] ) then
pl:EV_SetRank( "guest" )
end
end

umsg.Start( "EV_RemoveRank" )
umsg.String( args[1] )
umsg.End()
end
end
end )

concommand.Add( "ev_createrank", function( ply, com, args )
if ( ply:EV_HasPrivilege( "Rank modification" ) ) then
if ( ( #args == 2 or #args == 3 ) and !string.find( args[1], " " ) and string.lower( args[1] ) == args[1] and !evolve.ranks[ args[1] ] ) then
if ( #args == 2 ) then
evolve.ranks[ args[1] ] = {
Title = args[2],
Icon = "user",
UserGroup = "guest",
Immunity = 0,
Privileges = {},
}
elseif ( #args == 3 and evolve.ranks[ args[3] ] ) then
local parent = evolve.ranks[ args[3] ]

evolve.ranks[ args[1] ] = {
Title = args[2],
Icon = parent.Icon,
UserGroup = parent.UserGroup,
Immunity = tonumber( parent.Immunity ),
Privileges = table.Copy( parent.Privileges ),
}
end

evolve:SaveRanks()
evolve:SyncRanks()

evolve:Notify( evolve.colors.red, ply:Nick(), evolve.colors.white, " has created the rank ", evolve.colors.blue, args[2], evolve.colors.white, "." )
end
end
end )
end

/*-------------------------------------------------------------------------------------------------------------------------
Ban synchronization
-------------------------------------------------------------------------------------------------------------------------*/

if ( SERVER ) then
function evolve:SyncBans( ply )
for uniqueid, info in pairs( evolve.PlayerInfo ) do
if ( info.BanEnd and ( info.BanEnd > os.time() or info.BanEnd == 0 ) ) then
local time = info.BanEnd - os.time()
if ( info.BanEnd == 0 ) then time = 0 end
SendUserMessage( "EV_BanEntry", ply, tostring( uniqueid ), info.Nick, info.SteamID, info.BanReason, evolve:GetProperty( info.BanAdmin, "Nick" ), time )
end
end
end
else
usermessage.Hook( "EV_BanEntry", function( um )
if ( !evolve.bans ) then evolve.bans = {} end

local id = um:ReadString()
evolve.bans[id] = {
Nick = um:ReadString(),
SteamID = um:ReadString(),
Reason = um:ReadString(),
Admin = um:ReadString()
}

local time = um:ReadLong()
if ( time > 0 ) then
evolve.bans[id].End = time + os.time()
else
evolve.bans[id].End = 0
end

hook.Call( "EV_BanAdded", nil, id )
end )

usermessage.Hook( "EV_RemoveBanEntry", function( um )
if ( !evolve.bans ) then return end

local id = um:ReadString()
hook.Call( "EV_BanRemoved", nil, id )
evolve.bans[id] = nil
end )
end

/*-------------------------------------------------------------------------------------------------------------------------
Global data system
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:SaveGlobalVars()
file.Write( "ev_globalvars.txt", glon.encode( evolve.globalvars ) )
end

function evolve:LoadGlobalVars()
if ( file.Exists( "ev_globalvars.txt" ) ) then
evolve.globalvars = glon.decode( file.Read( "ev_globalvars.txt" ) )
else
evolve.globalvars = {}
evolve:SaveGlobalVars()
end
end
evolve:LoadGlobalVars()

function evolve:SetGlobalVar( name, value )
evolve.globalvars[name] = value
evolve:SaveGlobalVars()
end

function evolve:GetGlobalVar( name, default )
return evolve.globalvars[name] or default
end

/*-------------------------------------------------------------------------------------------------------------------------
Log system
-------------------------------------------------------------------------------------------------------------------------*/

function evolve:Log( str )
local cur = ""
cur = "[" .. os.date() .. "] " .. str .. "\n"

filex.Append( "ev_log" .. os.date("%m") .. "-" .. os.date("%d") .. ".txt", cur )
end

function evolve:PlayerLogStr( ply )
if ( ply:IsValid() ) then
if ( ply:IsPlayer() ) then
return ply:Nick() .. " [" .. ply:SteamID() .. "|" .. ply:IPAddress() .. "]"
else
return ply:GetClass()
end
else
return "Console"
end
end

hook.Add( "InitPostEntity", "EV_LogInit", function()
evolve:Log( "== Started in map '" .. game.GetMap() .. "' and gamemode '" .. GAMEMODE.Name .. "' ==" )
end )

hook.Add( "PlayerDisconnected", "EV_LogDisconnect", function( ply )
evolve:Log( evolve:PlayerLogStr( ply ) .. " disconnected from the server." )
evolve:Notify( evolve.colors.red, ply:Nick(), evolve.colors.white, " has disconnected! His STEAMID was ", evolve.colors.blue, .. ply:SteamID() .. "." )
end )


hook.Add( "PlayerConnect", "EV_LogConnect", function( name, address )
evolve:Log( name .. " [" .. address .. "] connected to the server." )
end )

hook.Add( "PlayerDeath", "EV_LogDeath", function( ply, inf, killer )
if ( ply != killer ) then
evolve:Log( evolve:PlayerLogStr( ply ) .. " was killed by " .. evolve:PlayerLogStr( killer ) .. "." )
end
end )

hook.Add( "PlayerSay", "EV_PlayerChat", function( ply, txt )
evolve:Log( evolve:PlayerLogStr( ply ) .. ": " .. txt )
end )
Powered by Google Project Hosting