· 7 years ago · Dec 18, 2018, 10:28 PM
1#include <cstrike>
2#include <sourcemod>
3#include <sdktools>
4#include <sdkhooks>
5#include <PlayerID>
6#include <colors>
7#pragma newdecls required
8#pragma semicolon 1
9#define NOT_ASSIGNED -1
10#define ALL -1
11#define TOP_LOAD 10
12#define TAG "\x03[ODZNAKI]\x01"
13int Score[MAXPLAYERS];
14int Gold[MAXPLAYERS];
15int Silver[MAXPLAYERS];
16int Bronze[MAXPLAYERS];
17int MedalScoreCounter[MAXPLAYERS];
18
19Database DB;
20bool tablesCreated = false;
21
22char TopName[TOP_LOAD][MAX_NAME_LENGTH];
23int TopScore[TOP_LOAD];
24int TopGold[TOP_LOAD];
25int TopSilver[TOP_LOAD];
26int TopBronze[TOP_LOAD];
27int AmountOfLeaders;
28
29char ColorsName[][] = { "{default}", "{darkred}", "{purple}", "{green}", "{lightgreen}", "{mediumgreen}", "{lightred}", "{lightpurple}", "{yellow}", "{greyblue}", "{blue}", "{violet}", "{firered}"};
30char ColorsTag[][] = { "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07", "\x08", "\x09", "\x0A", "\x0C", "\x0E", "\x0F"};
31
32public Plugin myinfo =
33{
34 name = "ODZNAKI",
35 author = "MAGNET / edit TIMOR",
36 description = "System odznak",
37 version = "1.0.0",
38 url = "http://cs-placzabaw.pl / http://go-code.pl"
39}
40public void OnPluginStart()
41{
42 SQL_InitConnection();
43 LoadTop();
44 HookEventEx("cs_win_panel_match", Event_WinPanelMatch);
45 RegConsoleCmd("sm_odznaki", ShowTop, "pokazuje topke");
46}
47public Action Event_WinPanelMatch(Handle event, const char[] name, bool dontBroadcast)
48{
49 GiveMedals();
50}
51public void SQL_InitConnection()
52{
53 SQL_ConnectToDB();
54}
55
56public void SQL_ConnectToDB()
57{
58 char sError[512];
59 DB = SQLite_UseDatabase("ODZNAKI",sError,sizeof(sError));
60
61 SQL_CheckIfConnected(sError);
62}
63public void SQL_CheckIfConnected(char[] sError)
64{
65 if (DB == null)
66 {
67 LogMessage("Could not connect to the DataBase! Error: %s", sError);
68 }
69 else SQL_Create_Players_Table();
70}
71public void SQL_Create_Players_Table()
72{
73 char query[512];
74 FormatPlayersQuery(query);
75 DB.Query(CheckIf_Players_QueryPassed, query, _, DBPrio_High);
76}
77public void FormatPlayersQuery(char[] query)
78{
79 Format(query, 511, "CREATE TABLE IF NOT EXISTS `Players` (`PlayerID` INTEGER NOT NULL UNIQUE, `Name` INTEGER NOT NULL, `GoldMedals` INTEGER NOT NULL, `SilverMedals` INTEGER NOT NULL, `BronzeMedals` INTEGER NOT NULL);");
80}
81public void CheckIf_Players_QueryPassed(Database db, DBResultSet results, const char[] error, any data)
82{
83
84 LogMessage("Could not create players table! Error: %s", error);
85 tablesCreated = true;
86}
87public void LoadPlayerData(int client)
88{
89 if (!tablesCreated)
90 {
91 LogMessage("Tables not yet created!");
92 CreateTimer(1.0, RepeatLoad, GetClientUserId(client));
93 return;
94 }
95 char query[512];
96 Format(query, sizeof(query), "SELECT * FROM `Players` WHERE `PlayerID`=%d", GetPlayerID(client));
97 DB.Query(LoadPlayerResults, query, GetClientUserId(client), DBPrio_High);
98}
99
100public Action RepeatLoad(Handle timer, int clientUserId)
101{
102 int client = GetClientOfUserId(clientUserId);
103 if (!client) return;
104 LoadPlayerData(client);
105}
106
107public void LoadPlayerResults(Database db, DBResultSet results, const char[] error, int clientUserId)
108{
109 if (db == null || results == null)
110 {
111 PrintToServer("Blad wczytywania danych gracza (ODZNAKI)!");
112 LogMessage("Could not retrieve player informations! Error: %s", error);
113 return;
114 }
115
116 int client = GetClientOfUserId(clientUserId);
117
118 if (!client) return;
119
120 // this is a new player, so we need to insert new row
121 if(results.RowCount == 0)
122 {
123 InsertPlayerToDatabase(client);
124 return;
125 }
126
127 while(SQL_FetchRow(results))
128 {
129 Gold[client] = SQL_FetchInt(results, 2);
130 Silver[client] = SQL_FetchInt(results, 3);
131 Bronze[client] = SQL_FetchInt(results, 4);
132 Score[client] = (Gold[client] * 3) + (Silver[client] * 2) + Bronze[client];
133 }
134}
135public void InsertPlayerToDatabase(int client)
136{
137 char NameBuffer[MAX_NAME_LENGTH];
138 GetClientName(client, NameBuffer, sizeof(NameBuffer));
139
140 char query[512];
141 Format(query, sizeof(query), "INSERT INTO `Players`(PlayerID,Name,GoldMedals,SilverMedals,BronzeMedals) VALUES(%d,'%s',0,0,0)", GetPlayerID(client), NameBuffer);
142 DB.Query(InsertPlayerResults, query, _, DBPrio_High);
143}
144
145public void InsertPlayerResults(Database db, DBResultSet results, const char[] error, any data)
146{
147 if (db == null || results == null)
148 {
149 PrintToServer("Blad wprowadzania danych gracza (ODZNAKI)!");
150 LogMessage("Could not insert player informations! Error: %s", error);
151 return;
152 }
153}
154public void LoadTop()
155{
156 if (!tablesCreated)
157 {
158 LogMessage("Tables not yet created!");
159 CreateTimer(1.0, RepeatLoadTop);
160 return;
161 }
162 char query[512];
163 Format(query, sizeof(query), "SELECT Name,GoldMedals,SilverMedals,BronzeMedals,(GoldMedals*3)+(SilverMedals*2)+BronzeMedals as Score FROM Players ORDER BY Score DESC LIMIT %d", TOP_LOAD);
164 DB.Query(GetTop10Results, query, _, DBPrio_High);
165}
166
167public Action RepeatLoadTop(Handle timer)
168{
169 LoadTop();
170}
171public void GetTop10Results(Database db, DBResultSet results, const char[] error, any data)
172{
173 if (db == null || results == null)
174 {
175 LogMessage("Could not get top! Error: %s", error);
176 return;
177 }
178
179 // there might be less than 10 players in the top
180 AmountOfLeaders = results.RowCount;
181 int i = 0;
182
183 while(SQL_FetchRow(results))
184 {
185 if (i == AmountOfLeaders) break;
186 SQL_FetchString(results, 0, TopName[i], MAX_NAME_LENGTH);
187 TopGold[i] = SQL_FetchInt(results, 1);
188 TopSilver[i] = SQL_FetchInt(results, 2);
189 TopBronze[i] = SQL_FetchInt(results, 3);
190 TopScore[i] = SQL_FetchInt(results, 4);
191 i++;
192 }
193}
194public void UpdatePlayerData(int client)
195{
196 if (!IsClientAuthorized(client) || GetPlayerID(client) == NOT_ASSIGNED)
197 {
198 ClearPlayerInfo(client);
199 return;
200 }
201
202 char NameBuffer[MAX_NAME_LENGTH];
203 GetClientName(client, NameBuffer, sizeof(NameBuffer));
204
205 char query[512];
206 Format(query, sizeof(query), "UPDATE Players SET Name='%s',GoldMedals=%d,SilverMedals=%d,BronzeMedals=%d WHERE PlayerID=%d", NameBuffer, Gold[client], Silver[client], Bronze[client], GetPlayerID(client));
207 DB.Query(CheckPlayerInfoUpdateResults, query, _, DBPrio_High);
208}
209
210public void CheckPlayerInfoUpdateResults(Database db, DBResultSet results, const char[] error, any data)
211{
212 if (db == null)
213 {
214 LogMessage("Could not update player informations! Error: %s", error);
215 return;
216 }
217}
218public void ClearPlayerInfo(int client)
219{
220 Score[client] = 0;
221 Gold[client] = 0;
222 Silver[client] = 0;
223 Bronze[client] = 0;
224 MedalScoreCounter[client] = 0;
225}
226public void OnClientDisconnect(int client)
227{
228 MedalScoreCounter[client] = 0;
229
230}
231public void GiveMedals()
232{
233 int GoldPlayerID = NOT_ASSIGNED;
234 int SilverPlayerID = NOT_ASSIGNED;
235 int BronzePlayerID = NOT_ASSIGNED;
236
237 int GoldScore = -9999;
238 int SilverScore = -9999;
239 int BronzeScore = -9999;
240
241 int playersCount = 0;
242
243 for (int i = 1; i < MAXPLAYERS;i++)
244 {
245 if (!IsClientConnected(i) || IsFakeClient(i) || IsClientSourceTV(i)) continue;
246
247 playersCount++;
248
249 int result = MedalScoreCounter[i] + CS_GetClientAssists(i);
250 if(result > GoldScore)
251 {
252 BronzeScore = SilverScore;
253 BronzePlayerID = SilverPlayerID;
254
255 SilverScore = GoldScore;
256 SilverPlayerID = GoldPlayerID;
257
258 GoldScore = result;
259 GoldPlayerID = i;
260 }
261 else if(result > SilverScore)
262 {
263 BronzeScore = SilverScore;
264 BronzePlayerID = SilverPlayerID;
265
266 SilverScore = result;
267 SilverPlayerID = i;
268 }
269 else if(result > BronzeScore)
270 {
271 BronzeScore = result;
272 BronzePlayerID = i;
273 }
274 }
275
276 GoPrint(ALL, "{green}Nagrody:");
277
278 if(GoldPlayerID != NOT_ASSIGNED && IsClientAuthorized(GoldPlayerID))
279 {
280 Gold[GoldPlayerID]++;
281 GoPrint(ALL, "{firered}Złoto:{green} %N{firered} (+3 punkty)", GoldPlayerID);
282 UpdatePlayerData(GoldPlayerID);
283 }
284 if(SilverPlayerID != NOT_ASSIGNED && IsClientAuthorized(SilverPlayerID))
285 {
286 Silver[SilverPlayerID]++;
287 Score[SilverPlayerID] += 2;
288 GoPrint(ALL, "{firered}Srebro:{green} %N{firered} (+2 punkty)", SilverPlayerID);
289 UpdatePlayerData(SilverPlayerID);
290 }
291 if(BronzePlayerID != NOT_ASSIGNED && IsClientAuthorized(BronzePlayerID))
292 {
293 Bronze[BronzePlayerID]++;
294 Score[BronzePlayerID]++;
295 GoPrint(ALL, "{firered}BrÄ…z:{green} %N{firered} (+1 punkt)", BronzePlayerID);
296 UpdatePlayerData(BronzePlayerID);
297 }
298}
299public void GoPrint(int client, char[] msg, any ...)
300{
301 int len = strlen(msg) + 255;
302 char[] formatted = new char[len];
303 VFormat(formatted, len, msg, 3);
304
305 for (int i = 0; i < 13;i++)
306 {
307 if (StrContains(formatted, ColorsName[i], false) != -1)
308 ReplaceString(formatted, len, ColorsName[i], ColorsTag[i], false);
309 }
310
311 if(client != ALL && IsClientInGame(client)) CPrintToChat(client, "%s %s", TAG, formatted);
312 else
313 {
314 for (int i = 1; i < MAXPLAYERS;i++)
315 {
316 if (!IsClientInGame(i)) continue;
317 CPrintToChat(i, "%s %s", TAG, formatted);
318 }
319 }
320}
321public void Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast)
322{
323 int victimId = event.GetInt("userid");
324 int attackerId = event.GetInt("attacker");
325 bool headshot = event.GetBool("headshot");
326
327 int victim = GetClientOfUserId(victimId);
328 int client = GetClientOfUserId(attackerId);
329
330 if (!IsValidClient(client)) return;
331
332 if (client == victim) return;
333
334
335 MedalScoreCounter[client] += headshot ? 3 : 2;
336 MedalScoreCounter[victim] -= 2;
337}
338bool IsValidClient(int client)
339{
340 if (client <= 0 || client > MaxClients) {
341 return false;
342 }
343
344 if (!IsClientInGame(client)) {
345 return false;
346 }
347
348 if (IsFakeClient(client)) {
349 return false;
350 }
351
352 return true;
353}
354public Action ShowTop(int client, int args)
355{
356 Menu menu = new Menu(ShowTop_Handler);
357
358 char FormatBuffer[MAX_NAME_LENGTH + 20];
359
360 menu.SetTitle("Aktualny ranking:");
361
362 for (int i = 0; i < AmountOfLeaders;i++)
363 {
364 Format(FormatBuffer, sizeof(FormatBuffer), "%s [%d]", TopName[i], TopScore[i]);
365 menu.AddItem("hold", FormatBuffer);
366 }
367
368 menu.ExitButton=true;
369 menu.Display(client, 30);
370}
371
372public int ShowTop_Handler(Menu menu, MenuAction action, int client, int item)
373{
374 if(action == MenuAction_Select)
375 {
376 char InfoBuffer[32];
377 menu.GetItem(item, InfoBuffer, sizeof(InfoBuffer));
378 if (StrEqual(InfoBuffer, "hold")) ShowTop(client, 1);
379 }
380}