· 6 years ago · Aug 07, 2019, 02:30 PM
1/* Sublime AMXX Editor v2.2 */
2
3#include <amxmodx>
4#include <colorchat>
5#include <sqlx>
6
7new userMedals[33][3]; // 0 zloty, 1 srebrny, 2 brazowy;
8
9new Handle:sql;
10
11public plugin_init()
12{
13 register_plugin("Medals", "v1", "K@MILOVVSKY")
14
15 register_clcmd("say s", "CheckFrags")
16 register_clcmd("say c", "CheckFrags2")
17}
18
19public plugin_cfg()
20{
21
22 new g_Error[512]
23 sql = SQL_MakeDbTuple("145.239.236.240", "srv54797", "7j833goz0g", "srv54797");
24
25 new ErrorCode, Handle:SqlConnection = SQL_Connect(sql,ErrorCode,g_Error,charsmax(g_Error))
26
27 new queryData[192];
28 formatex(queryData, charsmax(queryData), "CREATE TABLE IF NOT EXISTS `medals` (`id` int(5) NOT NULL auto_increment, `name` VARCHAR(35), `gold` INT NOT NULL, `silver` INT NOT NULL, `bronze` INT NOT NULL, PRIMARY KEY(`id`))");
29
30 SQL_PrepareQuery(sql, "ignore_handle", queryData);
31
32 SQL_FreeHandle(sql);
33}
34
35public plugin_end()
36{
37 SQL_FreeHandle(sql);
38}
39
40public client_authorized(id)
41{
42 Load(id);
43}
44
45public CheckFrags(indexs)
46{
47 new playersInfo[33][3], arrayLength;
48
49 for(new id = 1; id <= 32; id++)
50 {
51 if(!is_user_connected(id))
52 continue;
53
54 playersInfo[arrayLength][0] = get_user_frags(id);
55 playersInfo[arrayLength][1] = get_user_deaths(id);
56 playersInfo[arrayLength][2] = id;
57
58 arrayLength++;
59 }
60
61 if(!arrayLength)
62 return;
63
64 SortCustom2D(playersInfo, arrayLength, "compare2D");
65
66 new userName[33],
67 index;
68
69 new const stringMedals [][] = { "*** Zloty Medal ***", "** Srebrny Medal **", "* Brazowy medal *" }
70
71 for(new i = 0; i <= 2; i++)
72 {
73 index = playersInfo[i][2];
74
75 if(!is_user_connected(index))
76 continue;
77
78 get_user_name(index, userName, charsmax(userName));
79 ColorChat(0, RED, "%d miejsce:^x04 %s^x03 %d^x01 fragów |^x03 %d^x01 smierci^x03 %s", i+1, userName, playersInfo[i][0], playersInfo[i][1], stringMedals[i]);
80
81 userMedals[index][i]++;
82
83 Save(i);
84 }
85
86}
87
88public CheckFrags2(id)
89{
90 new const medales[][] = { "Zlote", "Srebrne", "Brazowe" }
91
92 for(new i = 0; i<=2; i++)
93 client_print(id, print_chat, "%s medale: %d sztuk", medales[i], userMedals[id][i]);
94}
95
96
97public compare2D(const elem1[], const elem2[]){
98 if(elem1[0] == elem2[0]){
99
100
101 if(elem1[1] == elem2[1])
102
103 return 0;
104 else if(elem1[1] < elem2[1])
105
106 return -1;
107
108 return 1;
109 }
110 else if(elem1[0] > elem2[0])
111 return -1;
112 return 1;
113}
114
115public Load(id)
116{
117
118 new Data[1]
119 Data[0] = id
120
121 new userName[33];
122 get_user_name(id, userName, charsmax(userName))
123
124 new qCommand[512]
125 formatex(qCommand, charsmax(qCommand), "SELECT * FROM `medals` WHERE `name` = ^"%s^";", userName)
126
127 SQL_ThreadQuery(sql, "LoadHandler", qCommand, Data, 1);
128
129 log_amx("# load_medals #");
130}
131
132public LoadHandler(FailState, Handle:Query, Error[], Errorcode, Data[], DataSize)
133{
134
135 new id = Data[0]
136 if(Errorcode)
137 log_amx("Blad w zapytaniu: %s [CheckData]", Error)
138
139 if(FailState == TQUERY_CONNECT_FAILED)
140 {
141 log_amx("Nie mozna podlaczyc sie do bazy danych.")
142 return PLUGIN_CONTINUE
143 }
144 else if(FailState == TQUERY_QUERY_FAILED)
145 {
146 log_amx("Zapytanie anulowane [CheckData]")
147 return PLUGIN_CONTINUE
148 }
149
150 if (SQL_MoreResults(Query)) {
151
152 userMedals[id][0] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "gold"))
153 userMedals[id][1] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "silver"))
154 userMedals[id][2] = SQL_ReadResult(Query, SQL_FieldNameToNum(Query, "bronze"))
155
156 log_amx("# load handler in if medals #")
157
158 } else {
159 new queryData[192];
160
161 new userName[33];
162 get_user_name(id, userName, charsmax(userName))
163
164 formatex(queryData, charsmax(queryData), "INSERT INTO `medals` VALUES (^"%s^", 0, 0, 0);", userName, userMedals[id][0], userMedals[id][1], userMedals[id][2])
165
166 SQL_ThreadQuery(sql, "ignore_handle", queryData);
167
168
169 log_amx("# load handler in else medals #")
170
171 }
172
173 return PLUGIN_CONTINUE
174}
175
176public Save(id)
177{
178 new userName[33];
179 get_user_name(id, userName, charsmax(userName))
180
181 new qCommand[512];
182 formatex(qCommand, charsmax(qCommand), "UPDATE `medals` SET `gold` = '%i', `silver` = '%i', `bronze` = '%i' WHERE name = ^"%s^";", userMedals[id][0], userMedals[id][1], userMedals[id][2], userName)
183
184
185 SQL_ThreadQuery(sql, "ignore_handle", qCommand);
186
187 log_amx("# save #")
188}
189
190public ignore_handle(failState, Handle:query, error[], errorNum, data[], dataSize)
191{
192 if (failState) {
193 if (failState == TQUERY_CONNECT_FAILED) log_to_file("csgo-error.log", "[CS:GO] Could not connect to SQL database. [%d] %s", errorNum, error);
194 else if (failState == TQUERY_QUERY_FAILED) log_to_file("csgo-error.log", "[CS:GO] Query failed. [%d] %s", errorNum, error);
195 }
196
197 return PLUGIN_CONTINUE;
198}