· 6 years ago · Sep 30, 2019, 12:08 PM
1local meta = FindMetaTable("Player")
2
3TeamTable = {}
4RegTable = {}
5RegRanks = {}
6
7function meta:IsAdmin()
8 if admintable[ string.lower( self:GetUserGroup() ) ] then return true end
9 return false
10end
11
12function meta:CanSetSpawn()
13 if canSetSpawn[ string.lower( self:GetUserGroup() ) ] then return true end
14 return false
15end
16
17function CreateTeam( name, teamtbl )
18 if !istable( teamtbl ) then ErrorNoHalt( "Failed to create team, " .. ( name or "nil" ) ) return end
19
20 teamtbl.Weapons = teamtbl.Weapons or {}
21 teamtbl.Health = teamtbl.Health or 100
22 teamtbl.Colour = teamtbl.Colour or Color( 255, 255, 255 )
23 teamtbl.Clearance = teamtbl.Clearance or "Unassigned"
24 teamtbl.Rank = teamtbl.Rank or 0
25 teamtbl.Model = teamtbl.Model or ""
26 teamtbl.Regiment = teamtbl.Regiment or ""
27 teamtbl.Name = name
28
29 local count = table.Count( TeamTable )
30
31 teamtbl.Count = count
32
33 team.SetUp( count, name, teamtbl.Colour )
34 TeamTable[ count ] = teamtbl
35
36 local shouldteam = true
37 for k, v in pairs( RegTable ) do
38 if v.Regiment == teamtbl.Regiment then shouldteam = false break end
39 end
40 if shouldteam then
41 table.insert( RegTable, teamtbl )
42 end
43 RegRanks[ teamtbl.Regiment ] = RegRanks[ teamtbl.Regiment ] or {}
44 RegRanks[ teamtbl.Regiment ][ count ] = name
45 util.PrecacheModel( teamtbl.Model )
46 return count
47end
48
49if SERVER then
50 if ( !sql.TableExists( "jobdata" ) ) then
51 sql.Query( "CREATE TABLE IF NOT EXISTS jobdata ( steamid TEXT NOT NULL, jobslot INTEGER NOT NULL, jobvalue TEXT, PRIMARY KEY (steamid, jobslot));" )
52 end
53
54 function meta:GetJData( name, jobslot, default )
55 name = string.format( "%s[%s]", self:SteamID64(), name )
56 local val = sql.QueryValue( "SELECT jobvalue FROM jobdata WHERE steamid = " .. sql.SQLStr( name ) .. " AND jobslot = " .. jobslot .. " LIMIT 1" )
57 if ( val == nil ) then return default end
58 return val
59 end
60
61 function meta:SetJData( name, jobslot, jobvalue )
62 name = string.format( "%s[%s]", self:SteamID64(), name )
63 sql.Query( "REPLACE INTO jobdata ( steamid, jobslot, jobvalue ) VALUES ( " .. sql.SQLStr( name ) .. ", " .. jobslot.. ", " .. sql.SQLStr( jobvalue ) .. " )" )
64 end
65
66--[[
67 function meta:RemoveJData( name, jobslot, default )
68 name = string.format( "%s[%s]", self:SteamID64(), name )
69 sql.Query( "DELETE FROM jobdata WHERE steamid = " .. sql.SQLStr( name ) .. " AND jobslot = " .. jobslot )
70 if ( val == nil ) then return default end
71 return val
72 end
73]]
74
75 if !file.Exists( "spawns.txt", "DATA" ) then file.Write( "spawns.txt", util.TableToJSON( {} ) ) end
76
77 local spawnTable = util.JSONToTable( file.Read( "spawns.txt", "DATA" ) )
78
79 hook.Add( "PlayerSay", "SetSpawn", function( ply, text )
80 if !ply:CanSetSpawn() then return end
81
82 text = string.Explode( " ", text )
83
84 if string.lower( text[ 1 ] ) == "!setspawn" or string.lower( text[ 1 ] ) == "/setspawn" then
85 if text[ 2 ] == nil then return "" end
86 local t = string.lower( table.concat( text, " ", 2 ) )
87 spawnTable[ game.GetMap() ] = spawnTable[ game.GetMap() ] or {}
88 spawnTable[ game.GetMap() ][ t ] = ply:GetPos()
89 file.Write( "spawns.txt", util.TableToJSON( spawnTable ) )
90 ply:PrintMessage( HUD_PRINTTALK, "[SET SPAWN] Spawn set for " .. text[ 2 ] .. "" )
91 return ""
92 end
93 end )
94
95 hook.Add( "PlayerSay", "DelSpawn", function( ply, text )
96 if !ply:CanSetSpawn() then return end
97
98 text = string.Explode( " ", text )
99
100 if string.lower( text[ 1 ] ) == "!delspawn" or string.lower( text[ 1 ] ) == "/delspawn" then
101 if text[ 2 ] == nil then return "" end
102 local t = string.lower( table.concat( text, " ", 2 ) )
103 spawnTable[ game.GetMap() ] = spawnTable[ game.GetMap() ] or {}
104 spawnTable[ game.GetMap() ][ t ] = nil
105 file.Write( "spawns.txt", util.TableToJSON( spawnTable ) )
106 ply:PrintMessage( HUD_PRINTTALK, "[DEL SPAWN] Spawn deleted for " .. text[ 2 ] .. "" )
107 return ""
108 end
109 end )
110
111 function GM:PlayerInitialSpawn( ply )
112 if tonumber(ply:GetJData( "job", 0, nil )) ~= nil then
113 local job = tonumber(ply:GetJData( "job", 0, nil ))
114 local tbl = TeamTable[ tonumber(job) ]
115 ply:SetTeam( job )
116 ply:Spawn()
117 // New Code
118 local permamodelled = false
119 for k,v in pairs(CustomPlayerModels) do
120 if (v.SteamID == ply:SteamID() ) and (v.Regiment == TeamTable[ply:Team()].Regiment) then
121 permamodelled = true
122 ply:SetModel( v.Model )
123 end
124 end
125 if permamodelled == false then
126 ply:SetModel( tbl.Model )
127 end
128 // End of New Code
129 /* Martibo Code
130 if CustomPlayerModels[ ply:SteamID() ] then
131 if CustomPlayerModels[ ply:SteamID() ].Regiment == TeamTable[ ply:Team() ].Regiment then
132 ply:SetModel( CustomPlayerModels[ ply:SteamID() ].Model )
133 end
134 else
135 ply:SetModel( tbl.Model )
136 end
137 */
138 else
139 local tbl = TeamTable[ TEAM_UNASSIGNED_CADET ]
140 ply:SetJData( "job", 0, TEAM_UNASSIGNED_CADET )
141 ply:SetTeam( TEAM_UNASSIGNED_CADET )
142 ply:SetModel( tbl.Model )
143 end
144 end
145
146 function GM:PlayerSpawn( ply )
147 ply:SetRunSpeed(Settings.runspeed)
148 ply:SetWalkSpeed(Settings.walkspeed)
149 if ply:IsAdmin() then
150 ply:Give("weapon_physgun")
151 ply:Give("gmod_tool")
152 end
153
154 local jobslot = 0
155 while tonumber(ply:GetJData( "job", jobslot, nil)) ~= ply:Team() or jobslot > 3 do
156 jobslot = jobslot + 1
157 end
158
159 if TeamTable[ ply:Team() ] then
160
161 local tbl = TeamTable[ ply:Team() ]
162 if spawnTable[ game.GetMap() ] and spawnTable[ game.GetMap() ][ string.lower( tbl.Regiment ) ] then
163 ply:SetPos( spawnTable[ game.GetMap() ][ string.lower( tbl.Regiment ) ] )
164 elseif spawnTable[ game.GetMap() ] and spawnTable[ game.GetMap() ][ "all" ] then
165 ply:SetPos( spawnTable[ game.GetMap() ][ "all" ] )
166 end
167
168 end
169
170 if tonumber(ply:GetJData( "job", jobslot, nil)) ~= nil then
171 local job = tonumber(ply:GetJData( "job", jobslot, nil ))
172 local tbl = TeamTable[ tonumber( job ) ]
173 // New Code
174 local permamodelled = false
175 for k,v in pairs(CustomPlayerModels) do
176 if (v.SteamID == ply:SteamID() ) and (v.Regiment == TeamTable[ply:Team()].Regiment) then
177 permamodelled = true
178 ply:SetModel( v.Model )
179 end
180 end
181 if permamodelled == false then
182 ply:SetModel( tbl.Model )
183 end
184 // End of New Code
185 /* Martibo Code
186 if CustomPlayerModels[ ply:SteamID() ] and CustomPlayerModels[ ply:SteamID() ].Regiment == tbl.Regiment then
187 print(ply:Nick() .. " has a custom job!")
188 ply:SetModel( CustomPlayerModels[ ply:SteamID() ].Model )
189 print("Has been allocated a custom Model as they are in the required regiment!")
190 print(CustomPlayerModels[ ply:SteamID() ].Regiment)
191 print(tbl.Regiment)
192 else
193 ply:SetModel( tbl.Model )
194 end
195 */
196 ply:SetHealth(tbl.Health)
197 ply:SetMaxHealth(tbl.Health)
198
199 local weps = tbl.Weapons
200
201 for i = 1, #weps do
202 ply:Give( weps[ i ] )
203 end
204 ply:Give("bkeycard")
205
206 weps = ply:GetWeapons()
207
208 for i = 1, #weps do
209 ply:GiveAmmo( Settings.ammunition, weps[ i ]:GetPrimaryAmmoType(), true )
210 end
211 end
212 print("Player Team: " .. TeamTable[ply:Team()].Regiment .. " " .. TeamTable[ply:Team()].Count)
213 end
214end