· 6 years ago · Oct 17, 2019, 05:12 PM
1#include <sdktools>
2#include <sdkhooks>
3//#include <store>
4//#include <yPlugins/credits>
5//#include <smrpg/smrpg_clients>
6
7#pragma semicolon 1
8#pragma newdecls required
9
10char MOD_TAG[64];
11
12Database DB;
13bool g_bInitialized[MAXPLAYERS + 1];
14
15ConVar gc_MOD_TAG,gc_PumpkinsPercent;
16ConVar gc_NothingPercent, gc_VIPPercent/*gc_CreditsPercent,gc_RPGCreditsPercent, gc_RPGExpPercent*/;
17
18bool g_bShopZephyrus, g_bShopYamakashi;
19Handle g_hClientTimer[MAXPLAYERS + 1];
20int g_iClientPumpkins[MAXPLAYERS + 1];
21char ActualLogName[256];
22char letters[][] =
23{
24 "a", "A", "b", "B", "c", "C", "d", "D", "e", "E", "f", "F", "g", "G", "h", "H", "i", "I", "j", "J", "k", "K", "l", "L", "m", "M", "n", "N", "o", "O", "p", "P", "q", "Q", "r", "R", "s", "S", "t", "T", "u", "U", "v", "V", "w", "W", "y", "Y", "z", "Z"
25};
26
27char numerals[][] =
28{
29 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
30};
31
32public Plugin myinfo =
33{
34 name = "ADEPT --> Event Z Dyniami",
35 description = "Autorski Plugin StudioADEPT.net",
36 author = "Brum Brum",
37 version = "1.0",
38 url = "http://www.StudioADEPT.net/forum",
39};
40
41public void OnPluginStart()
42{
43 gc_PumpkinsPercent = CreateConVar("sm_event_pumpkins_drop_percent", "20", "% na zdobycie dyni");
44 gc_NothingPercent = CreateConVar("sm_event_nothing_percent", "65", "% na pusty drop");
45 gc_VIPPercent = CreateConVar("sm_event_vip_percent", "3", "% na vipa");
46 //gc_CreditsPercent = CreateConVar("sm_event_credits_percent", "12", "% na kredyty");
47 //gc_RPGCreditsPercent = CreateConVar("sm_event_rpg_credits_percent", "10", "% na kredyty rpg");
48 //gc_RPGExpPercent = CreateConVar("sm_event_rpg_credits_percent", "10", "% na kredyty rpg");
49 gc_MOD_TAG = CreateConVar("sm_event_mod_tag", "Katujemy", "TAG pokazywany na czacie i w menu");
50 gc_MOD_TAG.AddChangeHook(MOD_TAGNameChanged);
51 gc_MOD_TAG.GetString(MOD_TAG, sizeof(MOD_TAG));
52 AutoExecConfig(true, "ADEPT_PumpkinsEvent");
53
54 HookEvent("player_death", Event_PlayerDeath);
55 HookEvent("player_spawn", Event_PlayerSpawn);
56
57 RegConsoleCmd("sm_dynie", CMD_Dynie);
58 RegConsoleCmd("sm_dynia", CMD_Dynie);
59 RegAdminCmd("sm_dajdynie", CMD_DodajDynie, ADMFLAG_ROOT);
60
61 DB_Connect();
62 for (int i = 1; i <= MaxClients; i++) {
63 if (IsValidClient(i))SQL_LoadData(i);
64 }
65 g_bShopZephyrus = LibraryExists("store_zephyrus");
66 if (g_bShopZephyrus)return;
67
68 g_bShopYamakashi = LibraryExists("credits");
69 if (g_bShopYamakashi)return;
70}
71
72public void MOD_TAGNameChanged(ConVar convar, const char[] oldValue, const char[] newValue)
73{
74 Format(MOD_TAG, sizeof(MOD_TAG), newValue);
75}
76/***********************************************************************************
77***********************************DATABASE*****************************************
78************************************************************************************/
79public Action DB_Connect()
80{
81 if (SQL_CheckConfig("Custom_Events"))
82 {
83 char error[512];
84 DB = SQL_Connect("Custom_Events", true, error, sizeof(error));
85 DB.SetCharset("utf8");
86 if (DB == null) {
87 LogMessage("Could not connect to Custom_Events! Error: %s", error);
88 return;
89 } else {
90 char query[512];
91 Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS Custom_Events(id INT NOT NULL AUTO_INCREMENT,PlayerName VARCHAR(128) NOT NULL, SteamID VARCHAR(64) NOT NULL, waluta INT NOT NULL,nazwa_waluty VARCHAR(32) NOT NULL,PRIMARY KEY(`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;");
92 DB.Query(CreateTableHandler, query, _, DBPrio_Normal);
93 }
94 }
95 else {
96 SetFailState("Nie mozna odnalezc konfiguracji 'Custom_Events' w databases.cfg. ");
97 }
98}
99
100public void CreateTableHandler(Database db, DBResultSet results, const char[] error, any data) {
101 if (db == null) {
102 LogMessage("Could not create tables! Error: %s", error);
103 }
104}
105
106public void OnClientPutInServer(int client)
107{
108 if (IsClientInGame(client))
109 {
110 SQL_LoadData(client);
111 }
112}
113
114void SQL_LoadData(int client) {
115 char steamid[64];
116 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
117
118 char query[512];
119 Format(query, sizeof(query), "SELECT `waluta` FROM Custom_Events WHERE SteamID='%s'", steamid);
120 DB.Query(LoadDataHandler, query, GetClientUserId(client), DBPrio_High);
121}
122
123public void LoadDataHandler(Database db, DBResultSet results, const char[] error, int userid) {
124 if (db == null) {
125 LogMessage("Could not load user data! Error: %s", error);
126 return;
127 }
128
129 int client = GetClientOfUserId(userid);
130 if (!client)return;
131 if (IsClientInGame(client) && !IsFakeClient(client) && !g_bInitialized[client])
132 {
133 char name[256], auth[64];
134 GetClientName(client, name, sizeof(name));
135 GetClientAuthId(client, AuthId_Steam2, auth, sizeof(auth));
136
137 char sanitized_name[128];
138 DB.Escape(name, sanitized_name, sizeof(name));
139
140 if (results.RowCount == 0) {
141 char query[512];
142 Format(query, sizeof(query), "INSERT INTO Custom_Events(`PlayerName`,`SteamID`,`waluta`,`nazwa_waluty`) VALUES('%s','%s','%d','Dynie')", sanitized_name, auth, g_iClientPumpkins[client]);
143 DB.Query(InsertNewPlayer, query, _, DBPrio_Normal);
144 } else {
145 while (SQL_FetchRow(results)) {
146 g_iClientPumpkins[client] = SQL_FetchInt(results, 0);
147 }
148 }
149 g_bInitialized[client] = true;
150 }
151}
152
153public void InsertNewPlayer(Database db, DBResultSet results, const char[] error, any data) {
154 if (db == null) {
155 LogMessage("Could not insert player! Error: %s", error);
156 }
157}
158
159void SQL_SaveData(int client) {
160 if (CheckStatus(client))
161 {
162 char query[512], name[256], steamid[64];
163 GetClientName(client, name, sizeof(name));
164 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
165 char sanitized_name[128];
166 DB.Escape(name, sanitized_name, sizeof(name));
167 Format(query, sizeof(query), "UPDATE Custom_Events SET `waluta` = '%d',`PlayerName` = '%s' WHERE SteamID = '%s'", g_iClientPumpkins[client], sanitized_name, steamid);
168 DB.Query(UpdatePlayerData, query, _, DBPrio_High);
169 }
170}
171
172public void UpdatePlayerData(Database db, DBResultSet results, const char[] error, any data) {
173 if (db == null) {
174 LogMessage("Could not update player data! Error: %s", error);
175 }
176}
177
178public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
179{
180 MarkNativeAsOptional("Store_SetClientCredits");
181 MarkNativeAsOptional("Store_GetClientCredits");
182 MarkNativeAsOptional("shop_add_coins");
183 MarkNativeAsOptional("shop_get_coins");
184 MarkNativeAsOptional("shop_set_coins");
185 return APLRes_Success;
186}
187
188/*public void OnLibraryAdded(const char[] name)
189{
190 if (StrEqual(name, "store_zephyrus")) {
191 g_bShopZephyrus = true;
192 }
193 else if (StrEqual(name, "coins")) {
194 g_bShopYamakashi = true;
195 }
196}
197
198public void OnLibraryRemoved(const char[] name)
199{
200 if (StrEqual(name, "store_zephyrus"))g_bShopZephyrus = false;
201 else if (StrEqual(name, "coins"))g_bShopYamakashi = false;
202}*/
203
204public void OnMapStart()
205{
206 AddFileToDownloadsTable("materials/pumpkins.vmt");
207 AddFileToDownloadsTable("materials/pumpkins.vtf");
208 PrecacheDecal("pumpkins.vmt", true);
209 PrecacheDecal("pumpkins.vtf", true);
210 char time[32];
211 int TimeStamp = GetTime();
212 FormatTime(time, sizeof(time), "%F", TimeStamp);
213 BuildPath(Path_SM, ActualLogName, sizeof(ActualLogName), "logs/EventVips-%s.txt", time);
214}
215
216public void OnClientPostAdminCheck(int client)
217{
218 g_hClientTimer[client] = INVALID_HANDLE;
219}
220
221public void OnClientDisconnect(int client)
222{
223 SetClientOverlay(client, "none");
224 SQL_SaveData(client);
225 g_iClientPumpkins[client] = 0;
226 g_bInitialized[client] = false;
227 g_hClientTimer[client] = INVALID_HANDLE;
228}
229
230public Action Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
231{
232 int victim = GetClientOfUserId(event.GetInt("userid"));
233 int attacker = GetClientOfUserId(event.GetInt("attacker"));
234 if (victim == attacker)return Plugin_Continue;
235
236 if (GetRandomInt(0, 100) <= gc_PumpkinsPercent.IntValue) {
237 g_iClientPumpkins[attacker]++;
238 PrintToChat(attacker, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Zdobyłeś dynię! Masz już ich\x02 %d\x04!", MOD_TAG, g_iClientPumpkins[attacker]);
239 }
240 return Plugin_Continue;
241}
242
243public Action Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
244{
245 int client = GetClientOfUserId(event.GetInt("userid"));
246
247 if (g_hClientTimer[client] == INVALID_HANDLE) {
248 g_hClientTimer[client] = CreateTimer(1.0, RefreshHud, GetClientUserId(client), TIMER_REPEAT);
249 }
250 SetClientOverlay(client, "pumpkins");
251}
252
253public Action CMD_Dynie(int client, int args)
254{
255 char buffer[32];
256 OdmienSlowa(g_iClientPumpkins[client], "dynia", "dynie", "dyń", buffer, sizeof(buffer));
257
258 Menu menu = new Menu(Menu_Handler);
259 menu.SetTitle("%s -> Dynie\nAktualnie masz: %d %s!", MOD_TAG, g_iClientPumpkins[client], buffer);
260 menu.AddItem("", "Nagrody");
261 menu.AddItem("", "Otwórz dynię");
262 menu.ExitButton = true;
263 menu.Display(client, 60);
264}
265
266public Action CMD_DodajDynie(int client, int args)
267{
268 if (args < 2)
269 {
270 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Za mało argumentów. Użyj !dajdynie <Nick> <ilość>", MOD_TAG);
271 return Plugin_Handled;
272 }
273
274 char arg[64], arg2[16];
275 GetCmdArg(1, arg, sizeof(arg));
276 int target = FindTarget(client, arg);
277
278 GetCmdArg(2, arg2, sizeof(arg2));
279 int amount = StringToInt(arg2);
280
281 if (target == -1) {
282 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Nie można znaleźć tego gracza.", MOD_TAG);
283 return Plugin_Handled;
284 }
285
286 g_iClientPumpkins[target] += amount;
287 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Graczowi\x02 %N\x4 dodano\x02 %d\x04 dyń!", MOD_TAG, target, amount);
288 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Otrzymałeś\x02 %d dyń\x04 od ADMINA\x02 %N", MOD_TAG, amount, client);
289
290 return Plugin_Handled;
291}
292
293public int Menu_Handler(Menu menu, MenuAction action, int client, int item)
294{
295 if (!IsValidClient(client))return;
296
297 if (action == MenuAction_Select)
298 {
299 switch (item)
300 {
301 case 0:ShowRewards(client);
302 case 1:OpenPumpkins(client);
303 }
304 }
305 else if (action == MenuAction_End)delete menu;
306}
307
308void ShowRewards(int client)
309{
310 Menu menu = new Menu(Rewards_Handler);
311 menu.SetTitle("%s -> Nagrody", MOD_TAG);
312 menu.AddItem("", "VIP od 1 dnia do 30 dni", ITEMDRAW_DISABLED);
313 menu.AddItem("", "Nic", ITEMDRAW_DISABLED);
314 menu.AddItem("back", "Wróć");
315 menu.ExitButton = true;
316 menu.Display(client, 60);
317}
318
319public int Rewards_Handler(Menu menu, MenuAction action, int client, int item)
320{
321 if (action == MenuAction_Select)
322 {
323 char info[32];
324 menu.GetItem(item, info, sizeof(info));
325 if (StrEqual(info, "back", true))CMD_Dynie(client, 0);
326 }
327 else if (action == MenuAction_End)delete menu;
328}
329
330void OpenPumpkins(int client)
331{
332 if (!IsValidClient(client))return;
333
334 if (g_iClientPumpkins[client] <= 0) {
335 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Nie posiadasz żadnej dyni!", MOD_TAG);
336 return;
337 }
338 else {
339 bool succes = false;
340 int reward = GetRandomInt(0, 100);
341 if (reward <= gc_NothingPercent.IntValue) {
342 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 W tej dyni nic nie było... Przykro mi :(", MOD_TAG);
343 succes = true;
344 }
345 else if (reward >= gc_NothingPercent.IntValue + 1 && reward <= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue) {
346
347 int timevip = GetVIPTime();
348
349 while (timevip == 0) {
350 timevip = GetVIPTime();
351 }
352
353 char code[16];
354 Format(code, sizeof(code), "%s", GetCode());
355
356 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Wylosowałeś\x02 %d\x04 dni darmowego VIP'a!", MOD_TAG, timevip);
357 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Zgłoś się do Właściciela o VIP'a. Twój kod weryfikacyjny: \x02%s", MOD_TAG, code);
358
359 char steamid[64];
360 GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
361 LogToFile(ActualLogName, "Gracz: [ %N ][ %s ] Wylosował VIP'a na: [ %d ] dni. Jego Kod weryfikacyjny: [ %s ]", client, steamid, timevip, code);
362 succes = true;
363 }
364 /*else if (reward >= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + 1 && reward <= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + gc_CreditsPercent.IntValue)
365 {
366 int credits = GetRandomInt(0, 100), tempcredits;
367 if (credits <= 65)tempcredits = GetRandomInt(50, 500);
368 else if (credits >= 66 && credits <= 75)tempcredits = GetRandomInt(80, 100);
369 else if (credits >= 76 && credits <= 82)tempcredits = GetRandomInt(150, 200);
370 else if (credits >= 83 && credits <= 90)tempcredits = GetRandomInt(250, 300);
371 else if (credits >= 91 && credits <= 98)tempcredits = GetRandomInt(350, 400);
372 else if (credits >= 99 && credits <= 100)tempcredits = GetRandomInt(450, 500);
373
374 if (g_bShopZephyrus)Store_SetClientCredits(client, Store_GetClientCredits(client) + tempcredits);
375 else if (g_bShopYamakashi)shop_add_coins(client, tempcredits);
376
377 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Wylosowano\x02 %d\x04 monet!", MOD_TAG, tempcredits);
378 succes = true;
379 }
380 else if (reward >= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + gc_CreditsPercent.IntValue + 1 && reward <= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + gc_CreditsPercent.IntValue + gc_RPGCreditsPercent.IntValue)
381 {
382 int treward = GetRandomInt(0, 100), tempreward;
383 if (treward <= 70)tempreward = GetRandomInt(5, 50);
384 else if (treward >= 71 && treward <= 85)tempreward = GetRandomInt(51, 150);
385 else if (treward >= 86 && treward <= 95)tempreward = GetRandomInt(151, 200);
386 else if (treward >= 96 && treward <= 100)tempreward = GetRandomInt(201, 250);
387
388 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Wylosowano\x02 %d\x04 kredytów RPG!", MOD_TAG, tempreward);
389 succes = true;
390 }
391 else if (reward >= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + gc_CreditsPercent.IntValue + gc_RPGCreditsPercent.IntValue + 1 && reward <= gc_NothingPercent.IntValue + gc_VIPPercent.IntValue + gc_CreditsPercent.IntValue + gc_RPGCreditsPercent.IntValue + gc_RPGExpPercent.IntValue)
392 {
393 int treward = GetRandomInt(0, 100), tempreward;
394 if (treward <= 50)tempreward = GetRandomInt(25, 50);
395 else if (treward >= 51 && treward <= 61)tempreward = GetRandomInt(51, 150);
396 else if (treward >= 71 && treward <= 81)tempreward = GetRandomInt(151, 250);
397 else if (treward >= 82 && treward <= 95)tempreward = GetRandomInt(251, 500);
398 else if (treward >= 96 && treward <= 100)tempreward = GetRandomInt(501, 700);
399
400 PrintToChat(client, "\x01\x0B\x0E★ \x07[%s -> Event]\x04 Wylosowano\x02 %d\x04 punktów RPG!", MOD_TAG, tempreward);
401 succes = true;
402 }*/
403
404 if (!succes)OpenPumpkins(client);
405 else g_iClientPumpkins[client]--;
406 }
407}
408
409int GetVIPTime()
410{
411 int tvip = GetRandomInt(0, 100), timevip;
412 if (tvip <= 40)timevip = GetRandomInt(1, 2);
413 else if (tvip >= 41 && tvip <= 58)timevip = GetRandomInt(2, 5);
414 else if (tvip >= 59 && tvip <= 65)timevip = GetRandomInt(4, 7);
415 else if (tvip >= 66 && tvip <= 75)timevip = GetRandomInt(6, 9);
416 else if (tvip >= 76 && tvip <= 82)timevip = GetRandomInt(9, 12);
417 else if (tvip >= 83 && tvip <= 89)timevip = GetRandomInt(12, 17);
418 else if (tvip >= 90 && tvip <= 95)timevip = GetRandomInt(15, 20);
419 else if (tvip >= 99 && tvip <= 100)timevip = GetRandomInt(20, 30);
420
421 return timevip;
422}
423
424public Action RefreshHud(Handle timer, int userid)
425{
426 int client = GetClientOfUserId(userid);
427 if (!IsValidClient(client))return Plugin_Stop;
428 if (g_hClientTimer[client] == INVALID_HANDLE)return Plugin_Stop;
429
430 SetHudTextParams(0.06, 0.425, 0.9, 255, 255, 255, 255);
431 ShowHudText(client, -1, "%d", g_iClientPumpkins[client]);
432
433 CreateTimer(5.0, RefreshOverlay, GetClientUserId(client), TIMER_REPEAT);
434
435 return Plugin_Continue;
436}
437
438public Action RefreshOverlay(Handle timer, int userid)
439{
440 int client = GetClientOfUserId(userid);
441 if (!IsValidClient(client))return Plugin_Stop;
442 if (g_hClientTimer[client] == INVALID_HANDLE)return Plugin_Stop;
443
444 SetClientOverlay(client, "pumpkins");
445
446 return Plugin_Continue;
447}
448
449char GetCode()
450{
451 char code[16];
452
453 while (strlen(code) < 7)
454 {
455 int random = GetRandomInt(0, 100), rnum;
456 if (random <= 50) {
457 char buffer[16], tempcode[16];
458 rnum = GetRandomInt(0, sizeof(numerals) - 1);
459 Format(buffer, sizeof(buffer), "%s", numerals[rnum]);
460 Format(tempcode, sizeof(tempcode), "%s%s", code, buffer);
461 code = tempcode;
462 }
463 else {
464 char buffer[16], tempcode[16];
465 rnum = GetRandomInt(0, sizeof(letters) - 1);
466 Format(buffer, sizeof(buffer), "%s", letters[rnum]);
467 Format(tempcode, sizeof(tempcode), "%s%s", code, buffer);
468 code = tempcode;
469 }
470 }
471
472 return code;
473}
474
475stock void OdmienSlowa(int x, const char[] a, const char[] b, const char[] c, char[] result, int size_of)
476{
477 if (x == 1) {
478 Format(result, size_of, a);
479 return;
480 }
481 if (x % 10 > 1 && x % 10 < 5 && !(x % 100 >= 10 && x % 100 <= 21)) {
482 Format(result, size_of, b);
483 return;
484 }
485
486 Format(result, size_of, c);
487}
488
489bool CheckStatus(int client)
490{
491 if (client && IsClientInGame(client) && !IsFakeClient(client) && g_bInitialized[client])
492 {
493 return true;
494 }
495 else g_bInitialized[client] = false;
496 return false;
497}
498
499public void SetClientOverlay(int client, const char[] strOverlay)
500{
501 if (IsValidClient(client))
502 {
503 int iFlags = GetCommandFlags("r_screenoverlay") & (~FCVAR_CHEAT);
504 SetCommandFlags("r_screenoverlay", iFlags);
505 ClientCommand(client, "r_screenoverlay \"%s\"", strOverlay);
506 }
507}
508
509public Action DeleteOverlay(Handle timer, any iClient)
510{
511 if (IsValidClient(iClient)) {
512 SetClientOverlay(iClient, "");
513 }
514 return Plugin_Handled;
515}
516
517public bool IsValidClient(int client)
518{
519 if (!(1 <= client <= MaxClients) || !IsClientInGame(client) || !IsClientConnected(client) || IsFakeClient(client) || IsClientSourceTV(client))
520 return false;
521
522 return true;
523}