forked from DouglasSherk/AMXModX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jailmod.sma
428 lines (326 loc) · 10.6 KB
/
jailmod.sma
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
#include <amxmodx>
#include <amxmisc>
#include <sqlx>
#include <engine>
#define MAX_JAILS 20
#define ITEMS_PER_MENU 6
#define ON_CHECK(%1) if(!get_pcvar_num(p_Mode)) return %1
new Handle:g_Sql
new bool:g_Access[33]
new p_PdJobs
new p_Table
new p_Reconnect
new p_Death
new p_Mode
new p_Distance
new g_Table[64]
new g_JailNames[MAX_JAILS][33]
new Float:g_JailOrigins[MAX_JAILS][3]
new g_JailCommands[MAX_JAILS][32]
new g_JailNum
new g_Menu[] = "JailModMenu"
new g_Keys
new g_MenuPage[33]
new g_Jailed[33] = {-1,...}
// rather than making 3 static variables, let's make 1 global
new g_Query[256]
new g_Died[33]
public plugin_init()
{
register_plugin("Ultimate Jail Mod","1.0","Hawk552")
register_clcmd("jailmodmenu","CmdJailMod")
register_event("ResetHUD","EventResetHUD","be")
register_event("DeathMsg","EventDeathMsg","a")
p_PdJobs = get_cvar_pointer("rp_jobid_mcpd")
p_Table = register_cvar("amx_jail_table","jail_users")
p_Reconnect = register_cvar("amx_jail_reconnect","1")
p_Death = register_cvar("amx_jail_death","1")
p_Mode = register_cvar("amx_jail_mode","1")
p_Distance = register_cvar("amx_jail_distance","50.0")
for(new Count;Count < 10;Count++)
g_Keys += (1<<Count)
new ConfigsDir[128],MapName[33],FileName[128]
get_configsdir(ConfigsDir,127)
get_mapname(MapName,32)
format(FileName,127,"%s/jailmod/%s.ini",ConfigsDir,MapName)
new File = fopen(FileName,"r")
if(!File)
{
format(g_Query,255,"No file found for this map, tried to open ^"%s^".",FileName)
return set_fail_state(g_Query)
}
new Buffer[128],Left[33],Right[33],Origins[3][11]
while(!feof(File) && g_JailNum < MAX_JAILS)
{
fgets(File,Buffer,127)
if(Buffer[0] == ';')
continue
if(containi(Buffer,"[") != -1 && containi(Buffer,"]") != -1)
{
replace(Buffer,127,"[","")
replace(Buffer,127,"]","")
if(g_JailCommands[g_JailNum][0])
register_clcmd(g_JailCommands[g_JailNum],"JailCommand")
copy(g_JailNames[g_JailNum++],32,Buffer)
}
else if(containi(Buffer,"origin") != -1)
{
parse(Buffer,Left,32,Right,32)
remove_quotes(Right)
trim(Right)
parse(Right,Origins[0],10,Origins[1],10,Origins[2],10)
for(new Count;Count < 3;Count++)
g_JailOrigins[g_JailNum][Count] = str_to_float(Origins[Count])
}
else if(containi(Buffer,"command") != -1)
{
parse(Buffer,Left,32,Right,32)
remove_quotes(Right)
trim(Right)
copy(g_JailCommands[g_JailNum],32,Right)
}
}
g_JailNum++
register_menucmd(register_menuid(g_Menu),g_Keys,"MenuHandle")
new Host[64],User[64],Pass[64],Db[64]
// no pcvars because we're only fetching them once
get_cvar_string("economy_mysql_host",Host,32)
get_cvar_string("economy_mysql_user",User,32)
get_cvar_string("economy_mysql_pass",Pass,32)
get_cvar_string("economy_mysql_db",Db,32)
g_Sql = SQL_MakeDbTuple(Host,User,Pass,Db)
new Error[256],Errcode,Handle:SqlConnect = SQL_Connect(g_Sql,Errcode,Error,255)
if(Errcode || !SqlConnect)
{
log_amx("Error connecting to SQL database: %s",Error)
return set_fail_state("Could not connect to SQL database.")
}
get_pcvar_string(p_Table,g_Table,63)
new Handle:Query = SQL_PrepareQuery(SqlConnect,"CREATE TABLE IF NOT EXISTS %s (authid VARCHAR(36),cell INT,map VARCHAR(33))",g_Table)
if(!Query || !SQL_Execute(Query))
log_amx("Failed to create jail table (%s).",g_Table)
else
{
SQL_FreeHandle(Query)
SQL_FreeHandle(SqlConnect)
}
return PLUGIN_CONTINUE
}
public EventDeathMsg()
{
ON_CHECK()
new id = read_data(2)
g_Died[id] = 0
if(get_pcvar_num(p_Death))
FreePlayer(0,id)
if(get_pcvar_num(p_Mode) == 1)
{
new Float:Origin[3]
entity_get_vector(id,EV_VEC_origin,Origin)
if(vector_distance(Origin,g_JailOrigins[g_Jailed[id]]) > get_pcvar_float(p_Distance))
FreePlayer(0,id)
}
}
public JailCommand(id)
{
if(!g_Access[id] || !get_pcvar_num(p_Mode))
return PLUGIN_HANDLED
new Arg[33],Num,List[MAX_JAILS]
read_argv(0,Arg,32)
for(new Count = 1;Count <= g_JailNum;Count++)
if(equali(Arg,g_JailCommands[Count]))
{
for(new Count2;Count2 < g_JailNum;Count2++)
if(equali(g_JailCommands[Count],g_JailCommands[Count2]))
List[Num++] = Count2
Count = List[random(Num - 1)]
g_MenuPage[id] = (Count - 3) / 7
MenuHandle(id,Count - g_MenuPage[id] * 7)
break
}
return PLUGIN_HANDLED
}
public client_disconnect(id)
{
g_Jailed[id] = -1
g_MenuPage[id] = 0
g_Access[id] = false
g_Died[id] = 0
}
public client_putinserver(id)
{
ON_CHECK()
new Authid[36]
get_user_authid(id,Authid,35)
new Data[1]
format(g_Query,255,"SELECT JobID FROM money WHERE steamid='%s'",Authid)
Data[0] = id
SQL_ThreadQuery(g_Sql,"JailAccessHandle",g_Query,Data,1)
}
public JailAccessHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
return set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
return set_fail_state("Internal error: consult developer.")
if(Errcode)
return log_amx("Error on query: %s",Error)
new PdJobs[32],Start[16],End[16],Min,Max
get_pcvar_string(p_PdJobs,PdJobs,31)
parse(PdJobs,Start,15,End,15)
Min = str_to_num(Start)
Max = str_to_num(End)
new JobId = SQL_ReadResult(Query,0)
g_Access[Data[0]] = JobId && JobId <= Max && JobId >= Min ? true : false
// not necessary, but we have to since we returned values at the top
return PLUGIN_CONTINUE
}
public EventResetHUD(id)
{
// can't possibly be in jail, so just stop now if user has access
if(g_Access[id] || !get_pcvar_num(p_Reconnect))
return
ON_CHECK()
new Data[1],Authid[36],MapName[33]
get_user_authid(id,Authid,35)
get_mapname(MapName,32)
format(g_Query,255,"SELECT cell FROM %s WHERE authid='%s' AND map='%s'",g_Table,Authid,MapName)
Data[0] = id
SQL_ThreadQuery(g_Sql,"CheckJail",g_Query,Data,1)
}
public CheckJail(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
return set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
return set_fail_state("Internal error: consult developer.")
if(Errcode)
return log_amx("Error on query: %s",Error)
new NumResults = SQL_NumResults(Query)
// they're not jailed, at least in this map -- let them go
if(!NumResults)
return PLUGIN_CONTINUE
new id = Data[0]
g_Jailed[id] = SQL_ReadResult(Query,0)
// get back in jail
entity_set_origin(id,g_JailOrigins[g_Jailed[id] + 1])
client_print(id,print_chat,"[JM] You have been put back in jail because that is where you were when you last disconnected.")
return PLUGIN_CONTINUE
}
public CmdJailMod(id)
{
if(!g_Access[id])
return client_print(id,print_chat,"[JM] You don't have access to this command.")
ON_CHECK(PLUGIN_CONTINUE)
static Menu[512]
new Pos,Num,Keys = (1<<7|1<<8|1<<9),Mode = get_pcvar_num(p_Mode)
Pos += format(Menu[Pos],sizeof Menu - Pos - 1,"Jail Mod^n^n")
for(new Count = g_MenuPage[id] * ITEMS_PER_MENU;Count < g_MenuPage[id] * ITEMS_PER_MENU + ITEMS_PER_MENU;Count++)
if(g_JailNames[Count][0] && Count < g_JailNum)
{
Keys |= (1<<Num)
Pos += format(Menu[Pos],sizeof Menu - Pos - 1,"%d. %s^n",++Num,g_JailNames[Count])
}
if(Mode == 2)
Keys |= (1<<6)
Pos += format(Menu[Pos],sizeof Menu - Pos - 1,"%s^n^n8. Last Page^n9. Next Page^n^n0. Exit",Mode == 2 ? "^n7. Free Target Player" : "")
show_menu(id,g_Keys,Menu,-1,g_Menu)
return PLUGIN_CONTINUE
}
public MenuHandle(id,Key)
switch(Key)
{
case 6 :
{
new Index,Body
get_user_aiming(id,Index,Body,500)
if(!Index || !is_user_alive(Index))
return
FreePlayer(id,Index)
}
case 7 :
{
if(g_MenuPage[id])
g_MenuPage[id]--
CmdJailMod(id)
}
case 8 :
{
if((g_MenuPage[id] + 1) * ITEMS_PER_MENU + ITEMS_PER_MENU < g_JailNum)
g_MenuPage[id]++
CmdJailMod(id)
}
case 9 :
return
default :
{
new RealKey = g_MenuPage[id] * ITEMS_PER_MENU + Key
if(RealKey > MAX_JAILS)
{
// I hate adding brackets, but we have to here.
CmdJailMod(id)
return
}
new Index,Body
get_user_aiming(id,Index,Body,500)
if(!Index || !is_user_alive(Index))
return
// I dunno why, but if I do this for some reason it randomly works
entity_set_origin(Index,g_JailOrigins[RealKey + 1])
new Name[33]
get_user_name(Index,Name,32)
client_print(id,print_chat,"[JM] You have sent %s to %s.",Name,g_JailNames[RealKey])
get_user_name(id,Name,32)
client_print(Index,print_chat,"[JM] You have been sent to %s by %s.",g_JailNames[RealKey],Name)
g_Jailed[Index] = RealKey
if(get_pcvar_num(p_Reconnect))
{
static Authid[36],MapName[33]
get_user_authid(Index,Authid,35)
get_mapname(MapName,32)
format(g_Query,255,"INSERT INTO %s VALUES ('%s','%d','%s')",g_Table,Authid,g_Jailed[Index],MapName)
SQL_ThreadQuery(g_Sql,"JailMenuHandle",g_Query)
}
}
}
public JailMenuHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
if(FailState == TQUERY_CONNECT_FAILED)
set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
set_fail_state("Internal error: consult developer.")
else if(Errcode)
log_amx("Error on query: %s",Error)
FreePlayer(id,Index)
{
if(g_Jailed[Index] < 0 || !get_pcvar_num(p_Reconnect))
return PLUGIN_CONTINUE
new Authid[36],MapName[33],Data[2]
get_user_authid(Index,Authid,35)
get_mapname(MapName,32)
Data[0] = id
Data[1] = Index
format(g_Query,255,"DELETE FROM %s WHERE authid='%s' AND map='%s'",g_Table,Authid,MapName)
SQL_ThreadQuery(g_Sql,"FreePlayerHandle",g_Query,Data,2)
return PLUGIN_HANDLED
}
public FreePlayerHandle(FailState,Handle:Query,Error[],Errcode,Data[],DataSize)
{
if(FailState == TQUERY_CONNECT_FAILED)
set_fail_state("Could not connect to SQL database.")
else if(FailState == TQUERY_QUERY_FAILED)
set_fail_state("Internal error: consult developer.")
else if(Errcode)
log_amx("Error on query: %s",Error)
if(SQL_AffectedRows(Query))
{
new Name[33],id = Data[0],Index = Data[1]
get_user_name(Index,Name,32)
if(id)
{
client_print(id,print_chat,"[JM] You have freed %s from jail.",Name)
get_user_name(id,Name,32)
client_print(Index,print_chat,"[JM] You have been freed from jail by %s.",Name)
}
g_Jailed[Index] = -1
}
}