· 8 years ago · Jun 15, 2018, 10:58 PM
1#include <sourcemod>
2#include <cstrike>
3#include <sdkhooks>
4#include <sdktools>
5
6#define TAG "\x0B[GAMESITES]"
7#define VIP ADMFLAG_RESERVATION
8
9new Handle:db;
10float databasetime[MAXPLAYERS+1];
11
12const int sevendays = 611976;
13
14ConVar g_RemainingTime;
15int RemainingTime;
16
17public Plugin myinfo =
18{
19 name = "JailBreak - Free VIP",
20 author = "Darco",
21 description = "Give players FreeVip after 100 hours",
22 version = "1.0",
23 url = "https://www.twitch.tv/JsemBOT"
24};
25
26 /////////////////////////
27 // AUTHORIZATION //
28/////////////////////////
29
30public OnPluginStart()
31{
32 RegConsoleCmd("sm_freevip", command_freevip, "Show hours left to Free VIP")
33 RegAdminCmd("sm_setvip", command_setvip, ADMFLAG_RCON, "Give Player VIP")
34
35 HookEvent("player_disconnect", Event_PlayerDisconnect, EventHookMode_Pre);
36 HookEvent("player_spawn", Event_PlayerSpawn, EventHookMode_Pre);
37
38 g_RemainingTime = CreateConVar("RemainingTime", "100", "Set Time needed for VIP", FCVAR_PROTECTED, true, 0.0, true, 300.0);
39 HookConVarChange(g_RemainingTime, convar_RemainingTime);
40
41 new String:Error[255];
42 db = SQL_Connect("freevip", true, Error, sizeof(Error));
43
44 if(db == INVALID_HANDLE)
45 SetFailState(Error);
46
47 new String:Query[255];
48 Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS players (steamid VARCHAR(64) NOT NULL PRIMARY KEY, time float, start int, end int);");
49
50 SQL_LockDatabase(db);
51 SQL_FastQuery(db, Query);
52 SQL_UnlockDatabase(db);
53}
54
55public convar_RemainingTime(Handle:convar, const char[] oldVal, const char[] newVal)
56{
57 RemainingTime = StringToInt(newVal);
58}
59
60public OnClientAuthorized(client)
61{
62 char steamid[64];
63 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
64
65 new String:Query[255];
66 Format(Query, sizeof(Query), "INSERT OR IGNORE INTO players VALUES ('%s', 0, 0, 0)", steamid);
67 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
68}
69
70 ////////////////////
71 // COMMANDS //
72////////////////////
73
74public Action:command_freevip(client, args)
75{
76 char steamid[64];
77 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
78
79 new String:Query[255];
80 Format(Query, sizeof(Query), "SELECT steamid, time FROM players where steamid = '%s'", steamid);
81 SQL_TQuery(db, SQL_Read, Query);
82
83 return Plugin_Handled;
84}
85
86public Action:command_setvip(client, args)
87{
88 if(args == 2)
89 {
90 char arg[32];
91 GetCmdArg(2, arg, sizeof(arg));
92
93 char cmdid[32];
94 GetCmdArg(1, cmdid, sizeof(cmdid));
95
96 int length = StringToInt(arg);
97 int id = StringToInt(cmdid);
98
99 if(IsClientInGame(id) && !IsFakeClient(id))
100 {
101 char steamid[64];
102 GetClientAuthId(id, AuthId_Steam2, steamid, sizeof(steamid));
103
104 if(StrEqual(arg, "remove", false))
105 {
106 new String:Query[255];
107 Format(Query, sizeof(Query), "UPDATE players set start = 0, end = 0 WHERE steamid = '%s'", steamid);
108 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
109 PrintToChat(client, " %s\x09 HráÄi \x10%N \x09bylo odebráno VIP", TAG, id);
110 RemoveUserFlags(id, Admin_Reservation);
111 }
112
113 else if(length == -1 || length == 7)
114 {
115 int time = GetTime();
116
117 new String:Query[255];
118
119 if(length == -1)
120 {
121 Format(Query, sizeof(Query), "UPDATE players set start = '%i', end = -1 WHERE steamid = '%s'", time, steamid);
122 PrintToChat(client, " %s\x09 HráÄi \x10%N \x09bylo nastaveno permanentnà VIP", TAG, id);
123 SetUserFlagBits(id, GetUserFlagBits(id) | FlagToBit(Admin_Reservation));
124 }
125
126 else if (length == 7)
127 {
128 Format(Query, sizeof(Query), "UPDATE players set start = '%i', end = '%i' WHERE steamid = '%s'", time, time + sevendays, steamid);
129 PrintToChat(client, " %s\x09 HráÄi \x10%N \x09bylo nastaveno VIP na týden", TAG, id);
130 SetUserFlagBits(id, GetUserFlagBits(id) | FlagToBit(Admin_Reservation));
131 }
132
133
134
135 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
136 }
137
138 else
139 {
140 PrintToChat(client, " %s\x09 K nastavenà VIP použit \x10/setvip ID -1\x09 pro permanentnà VIP nebo \x10/setvip ID 7\x09 pro nastavenà VIP na týden. Pro odstranÄ›nà VIP sloužà pÅ™Ãkaz \x10/setvip ID Remove", TAG);
141 }
142 }
143
144 }
145
146 else
147 {
148 PrintToChat(client, " %s\x09 K nastavenà VIP použit \x10/setvip ID -1\x09 pro permanentnà VIP nebo \x10/setvip ID 7\x09 pro nastavenà VIP na týden. Pro odstranÄ›nà VIP sloužà pÅ™Ãkaz \x10/setvip ID Remove", TAG);
149 }
150
151 return Plugin_Handled;
152}
153
154 //////////////////
155 // EVENTS //
156//////////////////
157
158public Action Event_PlayerSpawn(Handle event, const char[] name, bool dontBroadcast)
159{
160 int client = GetClientOfUserId(GetEventInt(event, "userid"));
161
162 char steamid[64];
163 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
164
165 new String:Query[255];
166 Format(Query, sizeof(Query), "SELECT steamid, start, end FROM players WHERE steamid = '%s'", steamid);
167 SQL_TQuery(db, SQL_CheckVIP, Query);
168}
169
170public Action Event_PlayerDisconnect(Handle event, const char[] name, bool dontBroadcast)
171{
172 int client = GetClientOfUserId(GetEventInt(event, "userid"));
173
174 if(client != 0 && !IsFakeClient(client))
175 {
176 float time = GetClientTime(client);
177
178 char steamid[64];
179 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
180
181 new String:Query[255];
182 Format(Query, sizeof(Query), "SELECT steamid, time FROM players WHERE steamid = '%s'", steamid);
183 SQL_TQuery(db, SQL_UpdateTime, Query, client);
184
185 DataPack pack;
186 CreateDataTimer(0.5, GetConnectionTime, pack);
187 pack.WriteCell(client);
188 pack.WriteString(steamid)
189 pack.WriteFloat(time);
190 }
191}
192
193 /////////////////
194 // TIMER //
195/////////////////
196
197public Action GetConnectionTime (Handle timer, Handle pack)
198{
199 ResetPack(pack);
200
201 int client = ReadPackCell(pack);
202
203 char steamid[64];
204 ReadPackString(pack, steamid, sizeof(steamid));
205
206 float time = ReadPackFloat(pack);
207
208 new String:Query[255];
209 Format(Query, sizeof(Query), "UPDATE players set time = '%f' WHERE steamid = '%s'", time + databasetime[client], steamid);
210
211 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
212}
213
214 ////////////////////
215 // DATABASE //
216////////////////////
217
218public SQL_ErrorCheckCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
219{
220 if(hndl == INVALID_HANDLE)
221 {
222 SetFailState("Query failed! %s", error);
223 }
224}
225
226public SQL_UpdateTime(Handle:owner, Handle:hndl, const String:error[], any:data)
227{
228 if(hndl != INVALID_HANDLE)
229 {
230 if(SQL_FetchRow(hndl))
231 {
232 char sid[64];
233 SQL_FetchString(hndl, 0, sid, sizeof(sid));
234 databasetime[data] = SQL_FetchFloat(hndl, 1);
235 }
236 }
237}
238
239public SQL_Read(Handle:owner, Handle:hndl, const String:error[], any:data)
240{
241 if(hndl != INVALID_HANDLE)
242 {
243 if(SQL_FetchRow(hndl))
244 {
245 char sid[64];
246 SQL_FetchString(hndl, 0, sid, sizeof(sid));
247
248 float time = SQL_FetchFloat(hndl, 1);
249
250 int remaining = RoundFloat(time) / 3600;
251 int curtime = GetTime();
252
253 for(int cfa = 1; cfa <= MaxClients; cfa++)
254 {
255 if(IsClientInGame(cfa) && !IsFakeClient(cfa))
256 {
257 char steamid[64];
258 GetClientAuthId(cfa, AuthId_Steam2, steamid, sizeof(steamid));
259
260 if(StrEqual(sid, steamid, false))
261 {
262 if(!(GetUserFlagBits(cfa) & VIP))
263 {
264 RemainingTime = g_RemainingTime.IntValue;
265
266 if(remaining >= RemainingTime)
267 {
268 PrintToChat(cfa, " %s \x09Bylo ti darováno FreeVIP, které ti bude trvat 7 dnÃ", TAG);
269
270 new String:Query[255];
271 Format(Query, sizeof(Query), "UPDATE players set time = '%f', start = '%d', end = '%d' WHERE steamid = '%s'", time - (RemainingTime * 3600), curtime, curtime + sevendays, steamid);
272 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
273
274 SetUserFlagBits(cfa, GetUserFlagBits(cfa) | FlagToBit(Admin_Reservation));
275 }
276
277 else
278 {
279 PrintToChat(cfa, " %s \x09Aby jsi zÃskal FreeVIP, musÃÅ¡ mÃt nahráno %d hodin. AkutálnÄ› ti chybà \x10%d \x09.", TAG, RemainingTime, RemainingTime - remaining);
280 }
281 }
282
283 else
284 {
285 PrintToChat(cfa, " %s \x09Máš již aktivnà FreeVIP", TAG);
286 }
287 }
288 }
289 }
290 }
291 }
292}
293
294public SQL_CheckVIP(Handle:owner, Handle:hndl, const String:error[], any:data)
295{
296 if(hndl != INVALID_HANDLE)
297 {
298 if(SQL_FetchRow(hndl))
299 {
300 char sid[64];
301 SQL_FetchString(hndl, 0, sid, sizeof(sid));
302
303 int start = SQL_FetchInt(hndl, 1);
304 int end = SQL_FetchInt(hndl, 2);
305
306 int curtime = GetTime();
307
308 for(int cfa = 1; cfa <= MaxClients; cfa++)
309 {
310 if(IsClientInGame(cfa) && !IsFakeClient(cfa))
311 {
312 char steamid[64];
313 GetClientAuthId(cfa, AuthId_Steam2, steamid, sizeof(steamid));
314
315 if(StrEqual(sid, steamid, false))
316 {
317 if((curtime > start && curtime < end) || end == -1)
318 {
319 if(!(GetUserFlagBits(cfa) & VIP))
320 {
321 SetUserFlagBits(cfa, GetUserFlagBits(cfa) | FlagToBit(Admin_Reservation));
322 }
323 }
324
325 else if (start != 0 && end != 0)
326 {
327 new String:Query[255];
328 Format(Query, sizeof(Query), "UPDATE players set start = 0, end = 0 WHERE steamid = '%s'", steamid);
329 SQL_TQuery(db, SQL_ErrorCheckCallback, Query);
330 }
331 }
332 }
333 }
334 }
335 }
336}