-
Notifications
You must be signed in to change notification settings - Fork 2
/
shavits-zonedmaps.sp
317 lines (255 loc) · 7.06 KB
/
shavits-zonedmaps.sp
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
#pragma semicolon 1
#include <sourcemod>
#pragma newdecls required
public Plugin myinfo =
{
name = "shavit - Zoned Maps",
author = "SlidyBat",
description = "Shows admins zoned/unzoned maps",
version = "1.1",
url = "",
};
Database g_hDatabase;
char g_cMySQLPrefix[32];
ArrayList g_aAllMapsList;
ArrayList g_aZonedMapsList;
int g_iMapFileSerial = -1;
bool g_bReadFromMapsFolder = true;
public void OnPluginStart()
{
g_aAllMapsList = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
g_aZonedMapsList = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
RegAdminCmd("sm_zonedmaps", Command_ZonedMaps, ADMFLAG_CHANGEMAP, "sm_zonedmaps - Shows admin zoned maps");
RegAdminCmd("sm_unzonedmaps", Command_UnzonedMaps, ADMFLAG_CHANGEMAP, "sm_unzonedmaps - Shows admin unzoned maps");
RegAdminCmd("sm_allmaps", Command_AllMaps, ADMFLAG_CHANGEMAP, "sm_allmaps - Shows admin all maps");
LoadAllMaps();
}
public void OnMapStart()
{
LoadAllMaps();
}
public void OnMapEnd()
{
g_iMapFileSerial = -1;
}
public Action Command_ZonedMaps(int client, int args)
{
OpenMapsMenu(client, true);
return Plugin_Handled;
}
public Action Command_UnzonedMaps(int client, int args)
{
OpenMapsMenu(client, false);
return Plugin_Handled;
}
public Action Command_AllMaps(int client, int args)
{
OpenAllMapsMenu(client);
return Plugin_Handled;
}
public void OpenMapsMenu(int client, bool zoned)
{
if (!g_aZonedMapsList.Length)
{
PrintToChat(client, "No zoned maps found, possible database connection error ...");
LogError("No zoned maps found, possible database connection error ...");
return;
}
else if (!zoned && !g_aAllMapsList.Length)
{
PrintToChat(client, "No map list found, possible file loading error ...");
LogError("No map list found, possible file loading error ...");
return;
}
char buffer[512];
Menu menu = new Menu(MapsMenuHandler);
Format(buffer, sizeof(buffer), "%s Maps:", zoned ? "Zoned" : "Unzoned");
Format(buffer, sizeof(buffer), "%s \n", buffer);
menu.SetTitle(buffer);
if (zoned)
{
for (int i = 0; i < g_aZonedMapsList.Length; i++)
{
g_aZonedMapsList.GetString(i, buffer, sizeof(buffer));
if (FindMap(buffer, buffer, sizeof(buffer)) != FindMap_NotFound)
{
menu.AddItem(buffer, buffer);
}
}
}
else
{
for (int i = 0; i < g_aAllMapsList.Length; i++)
{
g_aAllMapsList.GetString(i, buffer, sizeof(buffer));
if (FindMap(buffer, buffer, sizeof(buffer)) != FindMap_NotFound)
{
if (g_aZonedMapsList.FindString(buffer) < 0)
{
menu.AddItem(buffer, buffer);
}
}
}
}
menu.Display(client, MENU_TIME_FOREVER);
}
public void OpenAllMapsMenu(int client)
{
if (!g_aAllMapsList.Length)
{
LogError("Map List Failed");
return;
}
char buffer[512];
Menu menu = new Menu(MapsMenuHandler);
Format(buffer, sizeof(buffer), "All Maps:");
Format(buffer, sizeof(buffer), "%s \n", buffer);
menu.SetTitle(buffer);
for (int i = 0; i < g_aAllMapsList.Length; i++)
{
g_aAllMapsList.GetString(i, buffer, sizeof(buffer));
if (FindMap(buffer, buffer, sizeof(buffer)) != FindMap_NotFound)
{
menu.AddItem(buffer, buffer);
}
}
menu.Display(client, MENU_TIME_FOREVER);
}
public int MapsMenuHandler(Menu menu, MenuAction action, int param1, int param2)
{
if (action == MenuAction_Select)
{
char map[PLATFORM_MAX_PATH];
GetMenuItem(menu, param2, map, sizeof(map));
OpenChangeMapMenu(param1, map);
}
else if (action == MenuAction_End)
{
delete menu;
}
}
public void OpenChangeMapMenu(int client, char[] map)
{
char buffer[512];
Menu menu = new Menu(ChangeMapMenuHandler);
Format(buffer, sizeof(buffer), "Change map to %s?\n \n", map);
menu.SetTitle(buffer);
menu.AddItem(map, "Yes");
menu.AddItem("no", "No");
menu.Display(client, MENU_TIME_FOREVER);
}
public int ChangeMapMenuHandler(Menu menu, MenuAction action, int param1, int param2)
{
if (action == MenuAction_Select)
{
if (param2 == 0)
{
char map[PLATFORM_MAX_PATH];
GetMenuItem(menu, param2, map, sizeof(map));
PrintToChatAll("[SM] Changing map to %s ...", map);
DataPack data;
CreateDataTimer(2.0, Timer_ChangeMap, data);
data.WriteString(map);
}
}
else if (action == MenuAction_End)
{
delete menu;
}
}
public void LoadZonedMapsCallback(Database db, DBResultSet results, const char[] error, any data)
{
if (results == null)
{
LogError("[SQL Error] (LoadMapZonesCallback) - %s", error);
return;
}
while (results.FetchRow())
{
char map[PLATFORM_MAX_PATH];
SQL_FetchString(results, 0, map, sizeof(map));
g_aZonedMapsList.PushString(map);
}
}
public void LoadAllMaps()
{
SQL_SetPrefix();
g_aAllMapsList.Clear();
g_aZonedMapsList.Clear();
if (g_bReadFromMapsFolder)
{
LoadFromMapsFolder(g_aAllMapsList);
}
else if (ReadMapList(g_aAllMapsList, g_iMapFileSerial, "timer-zonedmaps", MAPLIST_FLAG_CLEARARRAY|MAPLIST_FLAG_MAPSFOLDER) != null)
{
if (g_iMapFileSerial == -1)
{
LogError("Unable to create a valid map list.");
}
}
g_aAllMapsList.Sort(Sort_Ascending, Sort_String);
char error[256];
g_hDatabase = SQL_Connect("shavit", true, error, sizeof(error));
if (g_hDatabase == INVALID_HANDLE)
{
delete g_hDatabase;
delete g_aAllMapsList;
delete g_aZonedMapsList;
SetFailState("[SQL Error] (Failed to connect to database) - %s", error);
}
char query[512];
Format(query, sizeof(query), "SELECT map FROM `%smapzones` WHERE type=1 ORDER BY `map`", g_cMySQLPrefix);
g_hDatabase.Query(LoadZonedMapsCallback, query, _, DBPrio_High);
}
bool LoadFromMapsFolder(ArrayList array)
{
//from yakmans maplister plugin
Handle mapdir = OpenDirectory("maps/");
char name[PLATFORM_MAX_PATH];
FileType filetype;
int namelen;
if (mapdir == INVALID_HANDLE)
return false;
if (mapdir != INVALID_HANDLE)
{
while (ReadDirEntry(mapdir, name, sizeof(name), filetype))
{
if (filetype != FileType_File)
continue;
namelen = strlen(name) - 4;
if (StrContains(name, ".bsp", false) != namelen)
continue;
name[namelen] = '\0';
array.PushString(name);
}
CloseHandle(mapdir);
mapdir = INVALID_HANDLE;
}
return true;
}
void SQL_SetPrefix()
{
char sFile[PLATFORM_MAX_PATH];
BuildPath(Path_SM, sFile, sizeof(sFile), "configs/shavit-prefix.txt");
File fFile = OpenFile(sFile, "r");
if (fFile == null)
{
SetFailState("Cannot open \"configs/shavit-prefix.txt\". Make sure this file exists and that the server has read permissions to it.");
}
char sLine[PLATFORM_MAX_PATH * 2];
while (fFile.ReadLine(sLine, sizeof(sLine)))
{
TrimString(sLine);
strcopy(g_cMySQLPrefix, sizeof(g_cMySQLPrefix), sLine);
break;
}
delete fFile;
}
public Action Timer_ChangeMap( Handle timer, DataPack data )
{
char map[PLATFORM_MAX_PATH];
data.Reset();
data.ReadString( map, sizeof(map) );
SetNextMap( map );
ForceChangeLevel( map, "RTV Mapvote" );
}