· 7 years ago · Jan 22, 2019, 02:52 AM
1local meta = FindMetaTable( "Player" )
2
3TeamTable = {}
4RegTable = {}
5RegRanks = {}
6
7function CreateTeam( name, teamtbl )
8
9 if !istable( teamtbl ) then ErrorNoHalt( "Failed to create team, " .. ( name or "nil" ) ) return end
10
11 teamtbl.Description = teamtbl.Description or ""
12 teamtbl.Weapons = teamtbl.Weapons or {}
13 teamtbl.Colour = teamtbl.Colour or Color( 255, 255, 255 )
14 teamtbl.Side = teamtbl.Side or 0
15 teamtbl.Rank = teamtbl.Rank or 0
16 teamtbl.Model = teamtbl.Model or ""
17 teamtbl.Regiment = teamtbl.Regiment or ""
18 teamtbl.Bodygroups = teamtbl.Bodygroups or ""
19 teamtbl.Health = teamtbl.Health or 100
20 teamtbl.Name = name
21 teamtbl.Runspeed = teamtbl.Runspeed or 240
22 teamtbl.Walkspeed = teamtbl.Walkspeed or 160
23 teamtbl.Modelscale = teamtbl.Modelscale or 1
24 teamtbl.ShowRegiment = true
25
26 local count = table.Count( TeamTable )
27
28 teamtbl.Count = count
29
30 team.SetUp( count, name, teamtbl.Colour )
31 TeamTable[ count ] = teamtbl
32 local shouldteam = true
33 for k, v in pairs( RegTable ) do
34 if v.Regiment == teamtbl.Regiment then shouldteam = false break end
35 end
36 if shouldteam then
37 table.insert( RegTable, teamtbl )
38 end
39 RegRanks[ teamtbl.Regiment ] = RegRanks[ teamtbl.Regiment ] or {}
40 RegRanks[ teamtbl.Regiment ][ count ] = name
41
42 util.PrecacheModel( teamtbl.Model )
43
44 return count
45
46end
47
48local CustomPlayers = {
49 }
50
51if SERVER then
52
53 if ( !sql.TableExists( "jobdata" ) ) then
54
55 sql.Query( "CREATE TABLE IF NOT EXISTS jobdata ( steamid TEXT NOT NULL PRIMARY KEY, value TEXT );" )
56
57 end
58
59 meta.getJobTable = function(ply)
60 local tbl = TeamTable[ply:Team()]
61 return tbl
62end
63
64 function meta:GetJData( name, default )
65
66 name = string.format( "%s[%s]", self:SteamID64(), name )
67 local val = sql.QueryValue( "SELECT value FROM jobdata WHERE steamid = " .. sql.SQLStr( name ) .. " LIMIT 1" )
68 if ( val == nil ) then return default end
69
70 return val
71
72 end
73
74 function meta:SetJData( name, value )
75
76 name = string.format( "%s[%s]", self:SteamID64(), name )
77 sql.Query( "REPLACE INTO jobdata ( steamid, value ) VALUES ( " .. sql.SQLStr( name ) .. ", " .. sql.SQLStr( value ) .. " )" )
78
79 end
80
81 function GetJData( steamid, name, default )
82
83 name = string.format( "%s[%s]", steamid, name )
84 local val = sql.QueryValue( "SELECT value FROM jobdata WHERE steamid = " .. sql.SQLStr( name ) .. " LIMIT 1" )
85 if ( val == nil ) then return default end
86
87 return val
88
89 end
90
91 function SetJData( steamid, name, value )
92
93 name = string.format( "%s[%s]", steamid, name )
94 sql.Query( "REPLACE INTO jobdata ( steamid, value ) VALUES ( " .. sql.SQLStr( name ) .. ", " .. sql.SQLStr( value ) .. " )" )
95
96 end
97
98 function GM:PlayerInitialSpawn( ply )
99
100 if ply:GetJData( "job", nil ) ~= nil then
101
102 local job = ply:GetJData( "job", nil )
103 local tbl = TeamTable[ tonumber( job ) ]
104 ply:SetTeam( job )
105 ply:Spawn()
106 ply:SetModel( tbl.Model )
107
108 else
109
110 local tbl = TeamTable[ TEAM__ST_RECRUIT ]
111
112 ply:SetJData( "job", TEAM_ST_RECRUIT )
113 ply:SetTeam( TEAM_ST_RECRUIT )
114 ply:SetModel( tbl.Model )
115
116 end
117
118 end
119
120 if !file.Exists( "spawns" .. game.GetMap() .. ".txt", "DATA" ) then file.Write( "spawns" .. game.GetMap() .. ".txt", util.TableToJSON( {} ) ) end
121
122 local spawnTable = util.JSONToTable( file.Read( "spawns" .. game.GetMap() .. ".txt", "DATA" ) )
123
124 hook.Add( "PlayerSay", "SetTetSpawn", function( ply, text )
125
126 text = string.Explode( " ", text )
127
128 if string.lower( text[ 1 ] ) == "!setspawn" or string.lower( text[ 1 ] ) == "/setspawn" then
129
130 if text[ 2 ] == nil then return "" end
131
132 local t = string.lower( table.concat( text, " ", 2 ) )
133
134 spawnTable[ t ] = ply:GetPos()
135 file.Write( "spawns" .. game.GetMap() .. ".txt", util.TableToJSON( spawnTable ) )
136
137 ply:PrintMessage( HUD_PRINTTALK, "Spawn set." )
138
139 return ""
140
141 end
142
143 end )
144
145 function GM:PlayerSpawn( ply )
146
147 ply:SetRunSpeed( 240 )
148 ply:SetWalkSpeed( 160 )
149
150 if TeamTable[ ply:Team() ] then
151
152 local tbl = TeamTable[ ply:Team() ]
153
154 if spawnTable[ tbl.Side == 1 and "republic" or "cis" ] then
155
156 ply:SetPos( spawnTable[ tbl.Side == 1 and "republic" or "cis" ] )
157
158 elseif spawnTable[ string.lower( tbl.Regiment ) ] then
159
160 ply:SetPos( spawnTable[ string.lower( tbl.Regiment ) ] )
161
162 end
163
164 end
165
166 if ply:GetJData( "job", nil ) ~= nil then
167
168 local job = ply:GetJData( "job", nil )
169 local tbl = TeamTable[ tonumber( job ) ]
170 if CustomPlayers[ ply:SteamID() ] then
171 ply:SetModel( CustomPlayers[ ply:SteamID() ].model )
172 ply:SetBodyGroups( CustomPlayers[ ply:SteamID() ].bodygroups )
173 else
174 ply:SetModel( tbl.Model )
175 ply:SetBodyGroups( tbl.Bodygroups )
176 end
177 ply:SetHealth( tbl.Health )
178 ply:SetMaxHealth( tbl.Health )
179 ply:SetRunSpeed( tbl.Runspeed )
180 ply:SetWalkSpeed( tbl.Walkspeed )
181 ply:SetModelScale( tbl.Modelscale )
182
183 local weps = tbl.Weapons
184
185 for i = 1, #weps do
186
187 ply:Give( weps[ i ] )
188
189 end
190
191 ply:Give( "weapon_empty_hands" )
192 ply:Give( "climb_swep2" )
193
194 weps = ply:GetWeapons()
195
196 for i = 1, #weps do
197
198 if weps[ i ]:GetClass() == "weapon_frag" then continue end
199 if weps[ i ]:GetClass() == "tfa_swch_clonelauncher" then continue end
200
201 ply:GiveAmmo( 256, weps[ i ]:GetPrimaryAmmoType(), true )
202
203 end
204
205 if TeamTable[ ply:Team() ].Rank >= 1 then
206
207 ply:Give( "weapon_empty_hands" )
208 ply:Give( "climb_swep2" )
209
210 end
211
212 end
213
214 end
215
216end
217
218
219TEAM_ST_RECRUIT = CreateTeam( "ST Recruit", {
220 Description = "Recruit",
221 Weapons = {"tfa_e11_training"},
222 Colour = Color(21, 21, 21, 255 ),
223 Side = 1,
224 Rank = 0,
225 Model = "models/player/tiki/white.mdl",
226 Regiment = "Recruit",
227 Bodygroups = "0",
228} )