· 8 years ago · May 13, 2018, 02:44 PM
1#include <sourcemod>
2#include <sdktools>
3#include <sdkhooks>
4#include <kento_rankme/rankme>
5
6#define LoopClients(%1) for(int %1 = 1; %1 <= MaxClients; %1++)\
7if(IsClientInGame(%1))
8
9#define LoopRanks(%1) for(int %1 = 1; %1 < sizeof g_iRanks; %1++)
10
11#define PREFIX_NORMAL " \x02[RANK]"
12#define PREFIX_ERROR " \x02[\x04#\x02RANK]"
13
14Handle g_hSql;
15
16int g_iRank [MAXPLAYERS+1];
17int g_iPoints [MAXPLAYERS+1];
18
19int g_iRanks[][] = // [0] = points, [1] = rounds, [2] = mvps, [3] = match win [4] = kdr (value*100)
20{
21 {1100, 10, 10, 10, 100}, // aliczenie zaczyna się od 0 więc [0] == silver 1, punkty od tej pory to punkty do next lvl !
22 {1500, 50, 20, 20, 105}, // liczba 1200 to liczba do osiagniecia silver'a 2 itd
23 {2000, 100, 30, 30, 110},
24 {2500, 200, 40, 40, 115},
25 {3000, 400, 50, 50, 120},
26 {3500, 600, 75, 60, 125},
27 {4000, 800, 100, 70, 130},
28 {4750, 1000, 200, 80, 140},
29 {5500, 2000, 300, 90, 145},
30 {6250, 3000, 400, 100, 150},
31 {7000, 4000, 500, 110, 155},
32 {8000, 5000, 750, 120, 160},
33 {9000, 6000, 1000, 130, 170},
34 {10000, 7000, 1200, 140, 180},
35 {12000, 8000, 1400, 150, 190},
36 {14000, 9000, 1600, 160, 200},
37 {16000, 10000, 1800, 170, 220},
38 {18000, 11000, 2000, 180, 240}
39};
40
41char g_sRanksNames[][] =
42{
43 "Silver 1",
44 "Silver 2",
45 "Silver 3",
46 "Silver 4",
47 "Silver Elite",
48 "Silver Elite Master",
49 "Gold Nova 1",
50 "Gold Nova 2",
51 "Gold Nova 3",
52 "Gold Nova Master",
53 "Master Guardian 1",
54 "Master Guardian 2",
55 "Master Guardian Elite",
56 "Distinguished Master Guardian",
57 "Legendary Eagle",
58 "Legendary Eagle Master",
59 "Supreme Master First Class",
60 "The Global Elite"
61};
62
63public Plugin myinfo =
64{
65 name = "Rang system - kento rankme",
66 description = "Simple rank system based on Kento_Rankme",
67 version = "0.2b",
68};
69
70public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
71{
72 CreateNative("RankSys_getClientRank", native_RankSys_getClientRank);
73 return APLRes_Success;
74}
75
76public OnPluginStart()
77{
78 // Cmds
79 RegConsoleCmd("sm_ranga", CMD_Grade, "Wyświetla główne menu rang.");
80
81 // Hooks
82 HookEvent( "round_end", Event_RoundEnd);
83 HookEvent("announce_phase_end", Event_AnnouncePhaseEnd);
84 // MySql shit
85 DB_Connect();
86}
87
88public void OnMapStart()
89{
90 SDKHook(FindEntityByClassname(MaxClients + 1, "cs_player_manager"), SDKHook_ThinkPost, onThinkPost);
91}
92
93public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float[3] vel, float[3] angles, int &weapon)
94{
95 if(buttons & IN_SCORE)
96 {
97 Handle hBuffer = StartMessageOne("ServerRankRevealAll", client);
98 if(hBuffer)
99 EndMessage();
100 }
101}
102
103public Action Event_AnnouncePhaseEnd(Handle event, char[] name, bool dontBroadcast)
104{
105 Handle hBuffer = StartMessageAll("ServerRankRevealAll");
106 if(hBuffer)
107 EndMessage();
108}
109public Action __________________________________________________________(){}
110public OnClientAuthorized(client, const String:auth[])
111{
112 CheckRank(client, false);
113}
114
115public void OnClientDisconnect(int client)
116{
117 CheckRank(client, false);
118 DB_SaveInfo(client);
119}
120
121public Action ________________________________________________________(){}
122public Action DB_Connect()
123{
124 if(SQL_CheckConfig("rankme"))
125 {
126 char DBBuffer[512];
127 g_hSql = SQL_Connect("rankme", true, DBBuffer, sizeof(DBBuffer));
128 if (g_hSql == INVALID_HANDLE)
129 LogError("[RANGI] Could not connect: %s", DBBuffer);
130 else
131 {
132 SQL_LockDatabase(g_hSql);
133 SQL_FastQuery(g_hSql, "CREATE TABLE IF NOT EXISTS rankme_5vs5_overpass_rangi (auth_data VARCHAR(48) NOT NULL PRIMARY KEY default '',\
134 name VARCHAR(64) NOT NULL default '',\
135 points INT NOT NULL default 0,\
136 rank INT NOT NULL default 0)\
137 ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci;");
138 SQL_UnlockDatabase(g_hSql);
139 }
140 }
141 else
142 SetFailState("Nie mozna odnalezc konfiguracji 'rankme' w databases.cfg.");
143}
144
145public DB_SaveInfo(int client)
146{
147 if(!IsValidClient(client))
148 return;
149
150 char authid[64];
151 if(!GetClientAuthId(client, AuthId_Steam2, authid, sizeof(authid)))
152 return;
153
154 char nick[64];
155 char EscapedName[64];
156
157 GetClientName(client, nick, sizeof(nick));
158 SQL_EscapeString(g_hSql, nick, EscapedName, sizeof(nick));
159
160 char query[2048];
161 Format(query, sizeof(query), "INSERT INTO `rankme_5vs5_overpass_rangi` (auth_data, name, points, rank) VALUES ('%s', '%s', %d, %d) ON DUPLICATE KEY UPDATE name=VALUES(name), points=VALUES(points), rank=VALUES(rank)", authid, EscapedName, g_iPoints[client], g_iRank[client]);
162 SQL_TQuery(g_hSql, DB_SaveInfoCallback, query);
163}
164
165public void DB_TopCallback(Handle owner, Handle hndl, const char[] error, DataPack info_pack)
166{
167 if(hndl == INVALID_HANDLE)
168 {
169 LogError("[TOP] Query failed! %s", error);
170 return;
171 }
172
173 info_pack.Reset(); // Get some info !
174 int client = info_pack.ReadCell();
175
176 if(client == 0 || !IsClientInGame(client))
177 return;
178
179 delete info_pack;
180
181 int i;
182
183 Menu menu = new Menu(TOP_Handler, MENU_ACTIONS_ALL);
184 menu.SetTitle("[#TOP] Nick - Ranga - Punkty");
185
186 char menu_item[256];
187 char sAuth[64];
188
189 while(SQL_FetchRow(hndl))
190 {
191 i++;
192 SQL_FetchString(hndl, 0, menu_item, sizeof(menu_item)); // name
193
194 Format(menu_item, sizeof(menu_item), "#%d - %s - %s - [%d]", i, menu_item, g_sRanksNames[SQL_FetchInt(hndl, 2)], SQL_FetchInt(hndl, 1));
195 menu.AddItem(sAuth, menu_item);
196 }
197
198 if(i == 0)
199 menu.AddItem("no_records", "Wygląda na to, że ranking jest czysty.");
200
201 menu.ExitButton = true;
202 menu.Display(client, 0);
203}
204
205public DB_SaveInfoCallback(Handle owner, Handle query, const char[] error, any data)
206{
207 if(query == INVALID_HANDLE)
208 {
209 LogError("[RANGI] Failed to save client info (error: %s)", error);
210 return;
211 }
212}
213
214public Action _______________________________________________________(){}
215public void onThinkPost(int entity)
216{
217 int iRank[MAXPLAYERS+1];
218 static m_iCompetitiveRanking = -1;
219
220 if(m_iCompetitiveRanking == -1)
221 m_iCompetitiveRanking = FindSendPropInfo("CCSPlayerResource", "m_iCompetitiveRanking");
222
223 GetEntDataArray(entity, m_iCompetitiveRanking, iRank, MaxClients+1);
224 LoopClients(i)
225 {
226 iRank[i] = g_iRank[i]+1;
227
228 SetEntDataArray(entity, m_iCompetitiveRanking, iRank, MaxClients+1);
229 }
230}
231public Action Event_RoundEnd(Handle event, const char[] name, bool dontBroadcast)
232{
233 LoopClients(i)
234 {
235 CheckRank(i, true);
236 DB_SaveInfo(i);
237 }
238}
239
240public Action ______________________________________________________(){}
241public Action CMD_Grade(int client, int args)
242{
243 CheckRank(client, true);
244 char title[256];
245 int iStats[128], iRounds, iMvps, iMatch;
246 float fKDR;
247
248 RankMe_GetStats(client, iStats);
249 iRounds = iStats[TR_WIN] + iStats[CT_WIN];
250 iMatch = iStats[MATCH_WIN];
251 iMvps = iStats[MVP];
252 fKDR = float(iStats[KILLS] + iStats[ASSISTS])/float(iStats[DEATHS]);
253
254 if(g_iRank[client] == sizeof(g_iRanks)-1)
255 Format(title, sizeof(title), "Rank by OneFrag.pl\n \n* Osiagnales maksymalny stopień.\n* Ranga: [%s]\n \n", g_sRanksNames[g_iRank[client]]);
256 else
257 Format(title, sizeof(title), "Rank by OneFrag.pl\n \n* Ranga: [%s] \n* Punkty [%d/%d]\n* MVP: [%d/%d]\n* Wygrane rundy: [%d/%d]\n* Wygrane mecze: [%d/%d]\n* KDR: [%.2f/%.2f]\n \n", g_sRanksNames[g_iRank[client]], g_iPoints[client], g_iRanks[g_iRank[client]][0], iMvps, g_iRanks[g_iRank[client]][2], iRounds, g_iRanks[g_iRank[client]][1], iMatch, g_iRanks[g_iRank[client]][3] , fKDR, float(g_iRanks[g_iRank[client]][4])/100.0);
258
259 Menu menu = new Menu(MainMenu_Handler, MENU_ACTIONS_ALL);
260 menu.SetTitle(title);
261 menu.AddItem("Item_1", "➫ Wyświetl wymagania na poszczególne rangi");
262 //menu.AddItem("Item_2", "âž« Ranking serwera");
263 menu.ExitButton = true;
264 menu.Display(client, 300);
265 return Plugin_Handled;
266}
267
268public Show_TopServer(int client)
269{
270 char query[256];
271
272 DataPack info_pack = new DataPack();
273 info_pack.WriteCell(client); // client
274
275 Format(query,sizeof(query), "SELECT name, points, rank FROM rankme_5vs5_overpass_rangi WHERE points > 0 ORDER BY points DESC");
276 SQL_TQuery(g_hSql, DB_TopCallback, query, info_pack);
277}
278
279public Action _____________________________________________________(){}
280public void CreateMenu_Help(int client)
281{
282 Menu menu = new Menu(HelpMenu_Handler, MENU_ACTIONS_ALL);
283 menu.SetTitle("Wymagania poszczególnych rang:\n \n");
284
285 LoopRanks(i)
286 {
287 menu.AddItem(g_sRanksNames[i], g_sRanksNames[i]);
288 }
289
290 menu.ExitButton = true;
291 menu.Display(client, 0);
292}
293
294public Action ____________________________________________________(){}
295public int MainMenu_Handler(Menu menu, MenuAction action, int param1, int param2)
296{
297 switch(action)
298 {
299 case MenuAction_Select:
300 {
301 char Item[32];
302 menu.GetItem(param2, Item, sizeof(Item));
303
304 if(StrEqual(Item, "Item_1"))
305 CreateMenu_Help(param1);
306 else if(StrEqual(Item, "Item_2"))
307 Show_TopServer(param1);
308 }
309 case MenuAction_End:
310 delete menu;
311 case MenuAction_DrawItem:
312 {
313 char info[32];
314 menu.GetItem(param2, info, sizeof(info));
315 if(StrEqual(info, "ITEMDRAW_DISABLED"))
316 return ITEMDRAW_DISABLED;
317 }
318 }
319 return 0;
320}
321
322public int HelpMenu_Handler(Menu menu, MenuAction action, int param1, param2)
323{
324 switch(action)
325 {
326 case MenuAction_Select:
327 {
328 char Item[32];
329 menu.GetItem(param2, Item, sizeof(Item));
330
331 LoopRanks(i)
332 {
333 if(StrEqual(g_sRanksNames[i], Item))
334 {
335 int iStats[128], iRounds, iMvps, iMatch;
336 float fKDR;
337
338 RankMe_GetStats(param1, iStats);
339 iRounds = iStats[TR_WIN] + iStats[CT_WIN];
340 iMvps = iStats[MVP];
341 iMatch = iStats[MATCH_WIN];
342
343 fKDR = float(iStats[KILLS] + iStats[ASSISTS])/float(iStats[DEATHS]);
344
345 PrintToChat(param1, "Wymagane punkty %d/%d", g_iPoints[param1], g_iRanks[param2+1][0]);
346 PrintToChat(param1, "Wymagane mvp %d/%d", iMvps, g_iRanks[param2+1][2]);
347 PrintToChat(param1, "Wymagane wygrane rundy %d/%d", iRounds, g_iRanks[param2+1][1]);
348 PrintToChat(param1, "Wymagane wygrane mecze %d/%d", iMatch, g_iRanks[param2+1][3]);
349 PrintToChat(param1, "Wymagane kdr %.2f/%.2f", fKDR, float(g_iRanks[param2+1][4])/100);
350 }
351 }
352 }
353 case MenuAction_End:
354 delete menu;
355 case MenuAction_DrawItem:
356 {
357 char info[32];
358 menu.GetItem(param2, info, sizeof(info));
359 if(StrEqual(info, "ITEMDRAW_DISABLED"))
360 return ITEMDRAW_DISABLED;
361 }
362 }
363 return 0;
364}
365
366public int TOP_Handler(Menu menu, MenuAction action, int param1, int param2)
367{
368 if(action == MenuAction_End)
369 delete menu;
370 return 0;
371}
372
373public Action ________________________________________________(){}
374void CheckRank(int client, bool bMsg)
375{
376 if(!IsValidClient(client))
377 return;
378
379 bool bLvlDown = false, bLvlUP = false;
380 int iStats[128], iRounds, iMvps, iMatch;
381 float fKDR;
382
383 RankMe_GetStats(client, iStats);
384 g_iPoints[client] = iStats[SCORE];
385 iRounds = iStats[TR_WIN] + iStats[CT_WIN];
386 iMvps = iStats[MVP];
387 iMatch = iStats[MATCH_WIN];
388 fKDR = float(iStats[KILLS] + iStats[ASSISTS])/float(iStats[DEATHS]);
389
390 while(g_iPoints[client] >= g_iRanks[g_iRank[client]][0] && g_iRank[client] < sizeof(g_iRanks)-1
391 && iRounds >= g_iRanks[g_iRank[client]][1]
392 && iMvps >= g_iRanks[g_iRank[client]][2]
393 && iMatch >= g_iRanks[g_iRank[client]][3]
394 && fKDR >= float(g_iRanks[g_iRank[client]][4])/100.0) // Promotion
395 {
396 g_iRank[client]++;
397 bLvlUP = true;
398 }
399
400 if(g_iRank[client] > 0)
401 {
402 while(g_iPoints[client] < g_iRanks[g_iRank[client]-1][0] // Spadek
403 && iRounds >= g_iRanks[g_iRank[client]-1][1]
404 && iMvps >= g_iRanks[g_iRank[client]-1][2]
405 && iMatch >= g_iRanks[g_iRank[client]][3]
406 && fKDR >= float(g_iRanks[g_iRank[client]-1][4])/100.0)
407 {
408 g_iRank[client]--;
409 bLvlDown = true;
410 }
411 }
412
413 if(bMsg)
414 {
415 if(bLvlUP)
416 {
417 PrintToChat(client, " ==============================================");
418 if(g_iRank[client] == sizeof(g_iRanks)-1)
419 PrintToChat(client, " %s \x07Osiagnales maksymalny stopień. Twoja ranga to : %s.", PREFIX_NORMAL, g_sRanksNames[g_iRank[client]]);
420 else
421 PrintToChat(client, " %s \x06Awanasowales do rangi \x03%s\x06 !", PREFIX_NORMAL, g_sRanksNames[g_iRank[client]]);
422 PrintToChat(client, " ==============================================");
423 }
424
425 if(bLvlDown)
426 {
427 PrintToChat(client, " ==============================================");
428 PrintToChat(client, " %s \x06Spadłeś do rangi \x03%s", PREFIX_NORMAL, g_sRanksNames[g_iRank[client]]);
429 PrintToChat(client, " ==============================================");
430 }
431 }
432}
433
434public int native_RankSys_getClientRank(Handle plugin, int numParams) {
435 return g_iRank[GetNativeCell(1)];
436}
437
438stock bool IsValidClient(int client)
439{
440 if (!(1 <= client <= MaxClients) || !IsClientInGame(client) || IsFakeClient(client) || IsClientSourceTV(client) || IsClientReplay(client))
441 {
442 return false;
443 }
444 return true;
445}