· 6 years ago · Apr 13, 2019, 09:36 PM
1/* =============================================
2 Load & Save
3 Rank & Top
4============================================= */
5
6new Handle:g_SqlTuple
7new g_Error[512]
8new const SqlDB[] = "RankSystem"
9
10public plugin_init()
11{
12 register_clcmd("say /ranktop", "Show_Top")
13 register_clcmd("say_team /ranktop", "Show_Top")
14
15 register_clcmd("say /myrank", "Show_Rank")
16 register_clcmd("say_team /myrank", "Show_Rank")
17
18 set_task(1.0, "MySql_Init")
19}
20
21// Mojesh da izpolvash globalna za imeto
22public client_putinserver(id)
23{
24 new szName[32]
25 get_user_name(id, szName, charsmax(szName))
26 set_task(0.1, "Load_Data", id, szName, sizeof(szName))
27}
28
29public client_disconnected(id)
30{
31 new szName[32]
32 get_user_name(id, szName, charsmax(szName))
33 Save_Data(id, szName)
34}
35
36public MySql_Init()
37{
38 new ErrorCode
39 new Host[32], User[32], Pass[32], Db[32]
40 get_cvar_string("amx_sql_host", Host, charsmax(Host))
41 get_cvar_string("amx_sql_user", User, charsmax(User))
42 get_cvar_string("amx_sql_pass", Pass, charsmax(Pass))
43 get_cvar_string("amx_sql_db", Db, charsmax(Db))
44
45 g_SqlTuple = SQL_MakeDbTuple(Host, User, Pass, Db)
46
47 new Handle:SqlConnection = SQL_Connect(g_SqlTuple, ErrorCode, g_Error, 511)
48
49 if(SqlConnection == Empty_Handle)
50 set_fail_state(g_Error)
51
52 new Handle:Queries = SQL_PrepareQuery(SqlConnection, "CREATE TABLE IF NOT EXISTS `%s`\
53 (`Name` VARCHAR(32) NOT NULL,\
54 `Exp` INT(10) NOT NULL,\
55 `Level` INT(10) NOT NULL,\
56 PRIMARY KEY (Name));", SqlDB)
57
58 // Tuk mojesh da dobavqsh kolkoto neshta ti trqbva da zapisva :D:D
59 // No posle trqbva da promenqsh pri Update v Save_Data
60 // I v parse_loaded_data
61
62 if(!SQL_Execute(Queries))
63 {
64 SQL_QueryError(Queries, g_Error, charsmax(g_Error))
65 set_fail_state(g_Error)
66 }
67
68 SQL_FreeHandle(Queries)
69 SQL_FreeHandle(SqlConnection)
70}
71
72public QueryHandler(FailState, Handle:Query, Error[], Errcode, Data[], DataSize)
73{
74 switch(FailState)
75 {
76 case -2: log_amx("[SQL Error] Failed to connect (%d):%s", Errcode, Error)
77 case -1: log_amx("[SQL Error] (%d):%s", Errcode, Error)
78 }
79 return PLUGIN_HANDLED
80}
81
82public Save_Data(id, szName[])
83{
84 new szTemp[512]
85 format(szTemp, charsmax(szTemp), "UPDATE `%s` SET `Exp`='%i',`Level`='%i' WHERE `Name`='%s';" , SqlDB, _YOUR_XP_VAR_HERE_, _YOUR_LEVEL_VAR_HERE_, szName)
86 SQL_ThreadQuery(g_SqlTuple, "QueryHandler", szTemp)
87}
88
89public Load_Data(szName[], id)
90{
91 if (!is_user_connected(id))
92 return
93
94 new ErrorCode
95 new Handle:SqlConnection = SQL_Connect(g_SqlTuple, ErrorCode, g_Error, 511)
96
97 SQL_QuoteString(SqlConnection, szName, 32, szName)
98
99 if (SqlConnection == Empty_Handle)
100 {
101 log_amx(g_Error)
102 return
103 }
104 new Handle:Query = SQL_PrepareQuery(SqlConnection, "SELECT * FROM %s WHERE Name = '%s';", SqlDB, szName)
105
106 if (!SQL_Execute(Query))
107 {
108 SQL_QueryError(Query, g_Error, 511)
109 log_amx(g_Error)
110 return
111 }
112 if (SQL_NumResults(Query) > 0)
113 {
114 parse_loaded_data(id, szName)
115 }
116 else
117 {
118 register_new_player(id, szName)
119 }
120 SQL_FreeHandle(Query)
121 SQL_FreeHandle(SqlConnection)
122}
123public parse_loaded_data(id, szName[])
124{
125 new ErrorCode
126 new Handle:SqlConnection = SQL_Connect(g_SqlTuple, ErrorCode, g_Error, 511)
127
128 SQL_QuoteString(SqlConnection, szName, 31, szName)
129
130 if (SqlConnection == Empty_Handle)
131 {
132 log_amx(g_Error)
133 return
134 }
135
136 new Handle:Query = SQL_PrepareQuery(SqlConnection, "SELECT Exp,Level FROM %s WHERE Name = '%s';", SqlDB, szName)
137
138 if (!SQL_Execute(Query))
139 {
140 SQL_QueryError(Query, g_Error, 511)
141 log_amx(g_Error)
142 }
143
144 if (SQL_NumResults(Query) > 0)
145 {
146 _YOUR_XP_VAR_HERE_ = SQL_ReadResult(Query, 0)
147 _YOUR_LEVEL_VAR_HERE_ = SQL_ReadResult(Query, 1)
148 _YOUR_NEXT_LEVEL_VAR_HERE_ = _YOUR_LEVEL_VAR_HERE_
149 _YOUR_NEXT_LEVEL_VAR_HERE_++ // Pribavqsh 1 level otgore nad segashniq..
150 }
151 SQL_FreeHandle(Query)
152 SQL_FreeHandle(SqlConnection)
153}
154public register_new_player(id, szName[])
155{
156
157 new ErrorCode
158 new Handle:SqlConnection = SQL_Connect(g_SqlTuple, ErrorCode, g_Error, 511)
159
160 SQL_QuoteString(SqlConnection, szName, 31, szName)
161
162 if(SqlConnection == Empty_Handle)
163 {
164 log_amx(g_Error)
165 return
166 }
167
168 new Handle:Query = SQL_PrepareQuery(SqlConnection, "INSERT INTO %s (`Name`,`Exp`,`Level`) VALUES ('%s','0','0');", SqlDB, szName)
169
170 if(!SQL_Execute(Query))
171 {
172 SQL_QueryError(Query, g_Error, 511)
173 log_amx(g_Error)
174 }
175
176 SQL_FreeHandle(Query)
177 SQL_FreeHandle(SqlConnection)
178
179 _YOUR_XP_VAR_HERE_ = 0
180 _YOUR_LEVEL_VAR_HERE_ = 0
181 _YOUR_NEXT_LEVEL_VAR_HERE_ = 1
182}
183
184// Top 10 Style
185new g_sBuffer[4096]
186new Style[] = "<meta charset=UTF-8><style>body{font-family:Arial;}img{margin-bottom:10px;}th{background:#57b9ff;color:#FFF;padding:5px;border-bottom:2px #24a4ff solid;text-align:left}td{padding:3px;border-bottom:1px #8aceff dashed}table{color:#2c75ff;background:#FFF;font-size:12px}h2,h3{color:#333;font-family:Verdana}#c{background:#F0F7E2}#r{height:10px;background:#717171}#clr{background:none;color:#575757;font-size:20px}</style>"
187
188public Show_Rank(id)
189{
190 new pPlayer[1]
191 pPlayer[0] = id
192
193 new szTemp[512]
194 format(szTemp,charsmax(szTemp),"SELECT COUNT(*) FROM `%s` WHERE `Exp` >= %i", SqlDB, _YOUR_XP_VAR_HERE_)
195 SQL_ThreadQuery(g_SqlTuple, "Sql_Rank", szTemp, pPlayer, sizeof(pPlayer))
196
197 return PLUGIN_CONTINUE
198}
199public Sql_Rank(FailState, Handle:Query, Error[], Errcode, pPlayer[], DataSize)
200{
201 if(FailState == TQUERY_CONNECT_FAILED)
202 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
203 else if(FailState == TQUERY_QUERY_FAILED)
204 log_amx("Load Query failed. [%d] %s", Errcode, Error)
205
206 new iCount = 0
207 iCount = SQL_ReadResult(Query, 0)
208
209 if (iCount == 0)
210 iCount = 1
211
212 new id
213 id = pPlayer[0]
214
215 client_print(id, print_chat, "* Your rank is %i with %i Experience", iCount, _YOUR_XP_VAR_HERE_)
216
217 return PLUGIN_HANDLED
218}
219
220public Show_Top(id)
221{
222 new pPlayer[1]
223 pPlayer[0] = id
224 new szTemp[512]
225 format(szTemp,charsmax(szTemp),"SELECT * FROM `%s` ORDER BY Exp DESC LIMIT 0,10", SqlDB)
226 SQL_ThreadQuery(g_SqlTuple, "Sql_Top", szTemp, pPlayer, 1)
227
228 return PLUGIN_HANDLED
229}
230
231public Sql_Top(FailState, Handle:Query, Error[], Errcode, pPlayer[], DataSize)
232{
233 if(FailState == TQUERY_CONNECT_FAILED)
234 log_amx("Load - Could not connect to SQL database. [%d] %s", Errcode, Error)
235 else if(FailState == TQUERY_QUERY_FAILED)
236 log_amx("Load Query failed. [%d] %s", Errcode, Error)
237
238 new id
239 id = pPlayer[0]
240
241 new iRow = SQL_NumResults(Query)
242
243 new szNames[12][64], szExp[12], szLevel[12]
244
245 if(SQL_MoreResults(Query))
246 {
247 for(new i = 0; i < iRow; i++)
248 {
249 SQL_ReadResult(Query, 0, szNames[i], 63)
250 szExp[i] = SQL_ReadResult(Query, 1)
251 szLevel[i] = SQL_ReadResult(Query, 2)
252 SQL_NextRow(Query)
253 }
254 }
255 if(iRow > 0)
256 {
257 new iLen=0
258 iLen = format(g_sBuffer[iLen], 4095, Style)
259 iLen += format(g_sBuffer[iLen], 4095 - iLen, "<body><table width=100%% border=0 align=center cellpadding=0 cellspacing=1>")
260 iLen += format(g_sBuffer[iLen], 4095 - iLen, "<tr><th>%s<th>%s<th>%s<th>%s</tr>","#","Name","Exp","Level")
261
262
263 for(new i = 0; i < iRow; i++)
264 {
265 replace_all(szNames[i], 63, "&", "&")
266 replace_all(szNames[i], 63, "<", "<")
267 replace_all(szNames[i], 63, ">", ">")
268
269 iLen += format(g_sBuffer[iLen], 4095 - iLen, "<tr><td>%i<td><b>%s</b><td>%i<td>%i", i + 1, szNames[i], szExp[i], szLevel[i])
270 }
271 show_motd(id, g_sBuffer, "Top 10 Players")
272 }
273
274 return PLUGIN_HANDLED
275}