· 8 years ago · Nov 18, 2017, 02:02 PM
1#include <sourcemod>
2#include <sdktools>
3#include <multicolors>
4#include <emitsoundany>
5
6Handle g_hTimes, g_hUsers, g_hNames;
7int g_iCount;
8
9ConVar cv_iGiveAways, cv_iRequired, cv_sText, cv_sSound;
10
11int iColor[4] = { 0, 0, 0, 100 };
12
13char g_sSQLBuffer[3096];
14
15bool g_bIsMySQl;
16
17// DB handle
18Handle g_hDB = INVALID_HANDLE;
19
20public Plugin:myinfo =
21{
22 name = "SM Giveaways by time",
23 description = "",
24 author = "Franc1sco franug",
25 version = "1.0",
26 url = "http://steamcommunity.com/id/franug"
27};
28
29
30public OnPluginStart()
31{
32 cv_iGiveAways = CreateConVar("sm_giveaways_time", "240", "Time in minutes for each giveaway");
33 cv_iRequired = CreateConVar("sm_giveaways_required", "30", "Time in minutes required for participate on giveaways");
34 cv_sText = CreateConVar("sm_giveaways_text", "1 key", "Text about the prizes");
35
36 cv_sSound = CreateConVar("sm_giveaways_sound", "hg/roundstart.mp3", "Sound when someone won the giveaway");
37
38 CreateTimer(1.0, Timer_Add, _, TIMER_REPEAT);
39
40 g_hTimes = CreateTrie();
41 g_hUsers = CreateArray(64);
42 g_hNames = CreateArray(64);
43
44 RegConsoleCmd("sm_winners", Command_Winners);
45 RegConsoleCmd("sm_giveaways", Command_Winners);
46
47 RegConsoleCmd("sm_info", Command_Info);
48
49 SQL_TConnect(OnSQLConnect, "storage-local");
50}
51
52public OnMapStart()
53{
54 char sSound[256];
55
56 GetConVarString(cv_sSound, sSound, sizeof(sSound));
57
58 PrecacheSoundAny(sSound);
59
60 Format(sSound, sizeof(sSound), "sound/%s", sSound);
61
62 AddFileToDownloadsTable(sSound);
63}
64
65public int OnSQLConnect(Handle owner, Handle hndl, char [] error, any data)
66{
67 if(hndl == INVALID_HANDLE)
68 {
69 LogError("Database failure: %s", error);
70
71 SetFailState("Databases dont work");
72 }
73 else
74 {
75 g_hDB = hndl;
76
77 SQL_GetDriverIdent(SQL_ReadDriver(g_hDB), g_sSQLBuffer, sizeof(g_sSQLBuffer));
78 g_bIsMySQl = StrEqual(g_sSQLBuffer,"mysql", false) ? true : false;
79
80 if(g_bIsMySQl)
81 {
82 Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS `winners` (`playername` varchar(128) NOT NULL, `steamid` varchar(64),`prize` varchar(128),`last_accountuse` int(64) NOT NULL)");
83
84 SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
85 }
86 else
87 {
88 Format(g_sSQLBuffer, sizeof(g_sSQLBuffer), "CREATE TABLE IF NOT EXISTS winners (playername varchar(128) NOT NULL, steamid varchar(64),prize varchar(128),last_accountuse int(64) NOT NULL)");
89
90 SQL_TQuery(g_hDB, OnSQLConnectCallback, g_sSQLBuffer);
91 }
92 }
93}
94
95public int OnSQLConnectCallback(Handle owner, Handle hndl, char [] error, any data)
96{
97 if(hndl == INVALID_HANDLE)
98 {
99 LogError("Query failure: %s", error);
100 return;
101 }
102}
103
104public Action Command_Winners(client, args)
105{
106 /*
107 if(GetArraySize(g_hUsers) == 0)
108 {
109 PrintToChat(client, "Not possible winners yet!");
110 return Plugin_Handled;
111 }
112
113 char temp[64], temp2[64];
114 Menu menu_winners = new Menu(Menu_Handler);
115 SetMenuTitle(menu_winners, "Possible winners");
116 for(new i=0;i<GetArraySize(g_hNames);++i)
117 {
118 GetArrayString(g_hNames, i, temp, 64);
119 GetArrayString(g_hUsers, i, temp2, 64);
120
121 AddMenuItem(menu_winners, temp2, temp);
122 }
123
124 DisplayMenu(menu_winners, client, 0);
125 */
126
127 char buffer[200];
128 Format(buffer, sizeof(buffer), "SELECT playername, steamid, last_accountuse, prize FROM winners ORDER BY last_accountuse");
129 SQL_TQuery(g_hDB, ShowTotalCallback, buffer, client);
130
131 return Plugin_Handled;
132}
133
134public int ShowTotalCallback(Handle owner, Handle hndl, char [] error, any client)
135{
136 if(hndl == INVALID_HANDLE)
137 {
138 LogError(error);
139 PrintToServer("Last Connect SQL Error: %s", error);
140 return;
141 }
142
143 Menu menu2 = CreateMenu(DIDMenuHandler2);
144 menu2.SetTitle("Possible winners");
145
146 char name[64];
147 char steamid[128];
148 char sTime[128];
149 char prize[128];
150 char itemtext[256];
151
152 int itime;
153
154 if(SQL_HasResultSet(hndl))
155 {
156 while (SQL_FetchRow(hndl))
157 {
158 SQL_FetchString(hndl, 0, name, sizeof(name));
159 SQL_FetchString(hndl, 1, steamid, sizeof(steamid));
160 itime = SQL_FetchInt(hndl, 2);
161 SQL_FetchString(hndl, 3, prize, sizeof(prize));
162
163 FormatTime(sTime, sizeof(sTime), "%d/%m/%Y", itime);
164
165 Format(itemtext, 256, "%s - %s - %s", sTime, name, prize);
166
167 menu2.AddItem(steamid, itemtext);
168 }
169 }
170
171 menu2.ExitButton = true;
172 menu2.Display(client,MENU_TIME_FOREVER);
173}
174
175public int DIDMenuHandler2(Menu menu2, MenuAction action, int client, int itemNum)
176{
177 if( action == MenuAction_Select )
178 {
179 char info[128];
180
181 GetMenuItem(menu2, itemNum, info, sizeof(info));
182
183 Format(info, sizeof(info), "http://steamcommunity.com/profiles/%s", info);
184 PrintToChat(client, info);
185 PrintToConsole(client, info);
186 }
187 if(action == MenuAction_End)
188 {
189 CloseHandle(menu2);
190 }
191}
192
193public OnClientPostAdminCheck(client)
194{
195 CreateTimer(5.0, Timer_Info, GetClientUserId(client));
196}
197
198public Action Timer_Info(Handle hTimer, any userid)
199{
200 int client = GetClientOfUserId(userid);
201 if (client == 0 || !IsClientInGame(client))return;
202
203
204 CPrintToChat(client, "[CSGO-GIVEAWAYS.COM] {orchid}Welcome, this servers is the right place if you are looking for awesome giveaways! Type !info learn the rules. GOOD LUCK!");
205}
206
207public Action Command_Info(client, args)
208{
209 CPrintToChat(client, "[CSGO-GIVEAWAYS.COM] {orchid}To participate in the giveaway you need to be on the server for the minimum required time displayed on your screen, after that you can even leave the server or stay till a winner is chosen!");
210 CPrintToChat(client, "{orchid}Our admins will contact the winners within 48 hours! GOOD LUCK!");
211 return Plugin_Handled;
212}
213
214public int Menu_Handler(Menu menu, MenuAction action, int client, int param)
215{
216 switch (action)
217 {
218 case MenuAction_Select:
219 {
220
221 char info[128], community[128];
222
223 GetMenuItem(menu, param, info, sizeof(info));
224 GetCommunityID(info, community, sizeof(community));
225
226 Format(community, sizeof(community), "http://steamcommunity.com/profiles/%s", community);
227 PrintToChat(client, community);
228 PrintToConsole(client, community);
229 }
230 case MenuAction_End:
231 {
232 CloseHandle(menu);
233 }
234
235 }
236}
237
238public Action Timer_Add(Handle hTimer)
239{
240 char steamid[64];
241 int value;
242
243 char sBuffer[128], thetime[128], prizes[128];
244
245 for (int i = 1; i <= MaxClients; i++)
246 if (IsClientInGame(i) && GetClientTeam(i) > 1 && !IsFakeClient(i))
247 {
248
249 GetClientAuthId(i, AuthId_Engine, steamid, sizeof(steamid));
250
251 if (!GetTrieValue(g_hTimes, steamid, value))
252 {
253 value = 0;
254 }
255 else value += 1;
256 //PrintToConsole(i, "value is %i", value);
257 if(value >= GetConVarInt(cv_iRequired)*60)
258 {
259 //PrintToConsole(i, "pasadooo");
260 if(FindStringInArray(g_hUsers, steamid) == -1)
261 {
262 CPrintToChat(i, "[CSGO-GIVEAWAYS.COM] {orchid}Awesome! You now have enough playtime for a chance to win the giveaway! Feel free to even leave the server now, you can still win :)");
263 //PrintToConsole(i, "siiii");
264 GetClientName(i, sBuffer, sizeof(sBuffer));
265 PushArrayString(g_hUsers, steamid);
266 PushArrayString(g_hNames, sBuffer);
267 }
268
269 }
270 SetTrieValue(g_hTimes, steamid, value);
271
272 //
273 ShowTimer(GetConVarInt(cv_iRequired)*60, thetime, sizeof(thetime));
274
275 Format(sBuffer, sizeof(sBuffer), "Required time for a chance to win: %s.", thetime);
276
277 SetHudTextParamsEx(0.0, 0.6, 1.1, iColor, {0, 0, 0, 100}, 0, 0.0, 0.0, 0.0);
278 ShowHudText(i, 1, sBuffer);
279 //
280
281 GetConVarString(cv_sText, prizes, sizeof(prizes));
282
283 Format(sBuffer, sizeof(sBuffer), "Prize: %s.", prizes);
284
285 SetHudTextParamsEx(0.0, 0.63, 1.1, iColor, {0, 0, 0, 100}, 0, 0.0, 0.0, 0.0);
286 ShowHudText(i, 2, sBuffer);
287 //
288
289 ShowTimer(value, thetime, sizeof(thetime));
290
291 SetHudTextParamsEx(0.0, 0.66, 1.1, iColor, {0, 0, 0, 100}, 0, 0.0, 0.0, 0.0);
292 Format(sBuffer, sizeof(sBuffer), "Your time on the server: %s", thetime);
293 ShowHudText(i, 3, sBuffer);
294
295 //
296
297 ShowTimer(GetConVarInt(cv_iGiveAways)*60-g_iCount, thetime, sizeof(thetime));
298
299 SetHudTextParamsEx(0.0, 0.69, 1.1, iColor, {0, 0, 0, 100}, 0, 0.0, 0.0, 0.0);
300 Format(sBuffer, sizeof(sBuffer), "Time left till a winner is chosen: %s", thetime);
301 ShowHudText(i, 4, sBuffer);
302
303 }
304
305
306 g_iCount++;
307
308 if(g_iCount >= GetConVarInt(cv_iGiveAways)*60)
309 Giveaway();
310}
311
312public void Giveaway()
313{
314 if(GetArraySize(g_hUsers) == 0)
315 {
316 CPrintToChatAll("[CSGO-GIVEAWAYS.COM] {orchid}Nobody won this giveaway!");
317 ResetAll();
318 return;
319 }
320
321 int winner = GetRandomInt(0, GetArraySize(g_hUsers) - 1);
322
323 char sDirPath[PLATFORM_MAX_PATH];
324 Format(sDirPath, sizeof(sDirPath), "addons/sourcemod/logs/giveaways");
325
326 if (!DirExists(sDirPath))
327 {
328 CreateDirectory(sDirPath, 511);
329 }
330
331 char sTime[PLATFORM_MAX_PATH], steamid[64], thetime[128];
332
333 FormatTime(sTime, sizeof(sTime), "%d-%m-%Y");
334
335 Format(sDirPath, sizeof(sDirPath), "%s/winners-%s.log",sDirPath,sTime)
336
337 char name[128], steamurl[256], prize[128];
338
339 GetArrayString(g_hNames, winner, name, sizeof(name));
340 GetArrayString(g_hUsers, winner, steamid, sizeof(steamid));
341
342 GetCommunityID(steamid, steamurl, sizeof(steamurl));
343
344 GetConVarString(cv_sText, prize, sizeof(prize));
345
346 decl String:szTime[20];
347 FormatTime(szTime, sizeof(szTime), "%H:%M");
348
349 int value;
350 GetTrieValue(g_hTimes, steamid, value);
351
352 ShowTimer(value, thetime,128)
353
354 new Handle:fileHandle=OpenFile(sDirPath,"a");
355 WriteFileLine(fileHandle,"%s - %s - %s - %s -> http://steamcommunity.com/profiles/%s - Played %s", sTime, szTime, prize, name, steamurl, thetime);
356 CloseHandle(fileHandle);
357
358 //LogToFile(sDirPath, "Winner is %s with url http://steamcommunity.com/profiles/%s and prize %s", name, steamurl, prize);
359
360 InsertSQLNewPlayer(steamurl, name, prize);
361
362 char sSound[256];
363 GetConVarString(cv_sSound, sSound, sizeof(sSound));
364
365 EmitSoundToAllAny(sSound);
366
367 CPrintToChatAll("[CSGO-GIVEAWAYS.COM] {orchid}Congratulations! The winner is %s", name);
368
369 PrintCenterTextAll("Congratulations!\nThe winner is %s", name);
370
371 ResetAll();
372}
373
374public void InsertSQLNewPlayer(char[] steamid, char[] Name, char[] prize)
375{
376 char query[255], SafeName[MAX_NAME_LENGTH+1];
377
378 TrimString(Name);
379 SQL_EscapeString(g_hDB, Name, SafeName, sizeof(SafeName));
380
381 Format(query, sizeof(query), "INSERT INTO winners(playername, steamid, prize, last_accountuse) VALUES('%s', '%s', '%s', '%d');", SafeName, steamid, prize, GetTime());
382 SQL_TQuery(g_hDB, SaveSQLPlayerCallback, query);
383
384 //PrintToChatAll(query);
385}
386
387public int SaveSQLPlayerCallback(Handle owner, Handle hndl, char [] error, any data)
388{
389 if(hndl == INVALID_HANDLE)
390 {
391 LogError("Query failure: %s", error);
392 }
393}
394
395void ResetAll()
396{
397 ClearArray(g_hUsers);
398 ClearArray(g_hNames);
399 ClearTrie(g_hTimes);
400
401 g_iCount = 0;
402}
403
404bool GetCommunityID(char [] AuthID, char [] FriendID, int size)
405{
406 if(strlen(AuthID) < 11 || AuthID[0]!='S' || AuthID[6]=='I')
407 {
408 FriendID[0] = 0;
409 return false;
410 }
411 int iUpper = 765611979;
412 int iFriendID = StringToInt(AuthID[10])*2 + 60265728 + AuthID[8]-48;
413 int iDiv = iFriendID/100000000;
414 int iIdx = 9-(iDiv?iDiv/10+1:0);
415 iUpper += iDiv;
416 IntToString(iFriendID, FriendID[iIdx], size-iIdx);
417 iIdx = FriendID[9];
418 IntToString(iUpper, FriendID, size);
419 FriendID[9] = iIdx;
420 return true;
421}
422
423int ShowTimer(int Time, char[] buffer,int sizef)
424{
425 int g_iHours = 0;
426 int g_iMinutes = 0;
427 int g_iSeconds = Time;
428
429 while(g_iSeconds > 3600)
430 {
431 g_iHours++;
432 g_iSeconds -= 3600;
433 }
434 while(g_iSeconds >= 60)
435 {
436 g_iMinutes++;
437 g_iSeconds -= 60;
438 }
439 if(g_iHours >= 1)
440 {
441 if(g_iMinutes == 0 && g_iSeconds == 0) Format(buffer, sizef, "%d hour%s", g_iHours,g_iHours!=1?"s":"");
442 else if(g_iMinutes > 0 && g_iSeconds == 0) Format(buffer, sizef, "%d hour%s and %d minute%s", g_iHours,g_iHours!=1?"s":"", g_iMinutes,g_iMinutes!=1?"s":"");
443 else Format(buffer, sizef, "%d hour%s, %d minute%s and %d second%s", g_iHours,g_iHours!=1?"s":"", g_iMinutes,g_iMinutes!=1?"s":"", g_iSeconds,g_iSeconds!=1?"s":"");
444 }
445 else if(g_iMinutes >= 1)
446 {
447 if(g_iSeconds == 0)
448 Format(buffer, sizef, "%d minute%s", g_iMinutes,g_iMinutes!=1?"s":"");
449 else Format(buffer, sizef, "%d minute%s and %d second%s", g_iMinutes,g_iMinutes!=1?"s":"", g_iSeconds,g_iSeconds!=1?"s":"");
450 }
451 else
452 {
453 Format(buffer, sizef, "%d second%s", g_iSeconds,g_iSeconds!=1?"s":"");
454 }
455}