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