· 8 years ago · Jan 22, 2018, 03:02 PM
1#include <amxmodx>
2#include <engine>
3#include <sqlx>
4
5#define PLUGIN "KZ Achievements"
6#define VERSION "1.0"
7
8#define MAX_PLAYERS 32
9
10new const HOST[] = ""
11new const USER[] = ""
12new const PASS[] = ""
13new const DB[] = ""
14
15new g_FirstAch[MAX_PLAYERS + 1], g_SecondAch[MAX_PLAYERS + 1]
16
17new g_AuthID[MAX_PLAYERS + 1][35], g_LoadedInfo[MAX_PLAYERS + 1], g_ProRecords[MAX_PLAYERS + 1]
18
19new Handle:g_SqlTuple
20new Handle:SqlConnection
21new g_Error[512]
22
23public plugin_init()
24{
25 register_plugin(PLUGIN, VERSION, "GlaDiuS")
26
27 set_task(3.0, "plugin_sql")
28}
29
30public plugin_sql()
31{
32 g_SqlTuple = SQL_MakeDbTuple(HOST, USER, PASS, DB)
33
34 new ErrorCode
35 SqlConnection = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,511)
36
37 if(!SqlConnection)
38 {
39 server_print("[KZ] ACH SQL: Could not connect to SQL database.!")
40 log_amx("[KZ] ACH SQL: Could not connect to SQL database.")
41 return pause("a")
42 }
43
44 new createinto[1001]
45 formatex(createinto, 1000, "CREATE TABLE IF NOT EXISTS `kz_ach` (`authid` varchar(64) NOT NULL, `name` varchar(64) NOT NULL, `ach1` int(2) NOT NULL, `ach2` int(2) NOT NULL);")
46 SQL_ThreadQuery(g_SqlTuple,"Get_QueryHandle", createinto)
47
48 return PLUGIN_CONTINUE
49}
50
51public client_authorized(id)
52{
53 if(!is_user_hltv(id) && !is_user_bot(id))
54 {
55 get_user_authid(id, g_AuthID[id], charsmax(g_AuthID[]))
56 }
57}
58
59public client_putinserver(id)
60{
61 if(is_user_connected(id) && !is_user_bot(id))
62 {
63 LoadInfo(id)
64 }
65}
66
67public client_disconnect(id)
68{
69 if(is_user_connected(id) && !is_user_bot(id))
70 {
71 SaveInfo(id)
72 }
73}
74
75public LoadInfo(id)
76{
77 new createinto[1001], cData[2]
78 cData[0] = id
79
80 formatex(createinto, charsmax(createinto), "SELECT `ach1`, `ach2`, FROM `kz_ach` WHERE authid='%s'", g_AuthID[id])
81 SQL_ThreadQuery(g_SqlTuple, "Get_AchQueryHandler", createinto, cData, 2)
82
83 formatex(createinto, charsmax(createinto), "SELECT `mapname` FROM kz_pro15 WHERE authid='%s' ORDER BY mapname", g_AuthID[id])
84 SQL_ThreadQuery(g_SqlTuple, "GetProRecords_QueryHandler", createinto, cData, 2)
85}
86
87public SaveInfo(id)
88{
89 new createinto[1001], Name[32]
90 get_user_name(id, Name, charsmax(Name))
91
92 if(!g_LoadedInfo[id])
93 {
94 formatex( createinto, sizeof createinto - 1, "INSERT INTO `kz_ach` VALUES('%s', ^"%s^",'%d', '%d')", g_AuthID[id], Name, g_FirstAch[id])
95 }
96
97 else
98 {
99 formatex(createinto, sizeof createinto - 1, "UPDATE `kz_ach` SET ach1='%d' ach2='%d' WHERE authid='%s'", g_FirstAch[id], g_AuthID[id])
100 }
101
102 SQL_ThreadQuery(g_SqlTuple, "Get_QueryHandle", createinto )
103}
104
105public client_PreThink(id)
106{
107 if(is_user_connected(id))
108 {
109 if(!g_FirstAch[id] && g_ProRecords[id] == 50) // To obtain prorecs we need read kz_pro15 table :)
110 {
111 g_FirstAch[id] = 1
112 client_print(id, print_chat, "ACH Hero of World: Hey dude you have 50 prorecs, awesome")
113 }
114
115 if(!g_SecondAch[id] && what you want)
116 {
117 g_SecondAch[id] = 1
118 client_print(id, print_chat, "ACH Achievement 2: Hey dude you complete ach!")
119 }
120 }
121}
122
123public Get_AchQueryHandler(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
124{
125 if( iFailState != TQUERY_SUCCESS )
126 {
127 log_amx("[XJ] ACH SQL: SQL Error #%d - %s", iErrnum, szError)
128 client_print(0, print_chat, "!tACH SQL: !yWarning the SQL Tops can not be saved.")
129 }
130
131 new id = cData[0]
132
133 if(SQL_NumResults(hQuery) != 0 )
134 {
135 new x1 = SQL_FieldNameToNum(hQuery, "ach1")
136 new x2 = SQL_FieldNameToNum(hQuery, "ach2")
137 new a = SQL_ReadResult(hQuery, x1)
138 new b = SQL_ReadResult(hQuery, x2)
139
140 g_FirstAch[id] = a
141 g_SecondAch[id] = b
142 g_LoadedInfo[id] = true
143 }
144
145 else
146 {
147 g_FirstAch[id] = 0
148 g_SecondAch[id] = 0
149 g_LoadedInfo[id] = false
150 }
151
152 return PLUGIN_CONTINUE
153}
154
155public GetProRecords_QueryHandler(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
156{
157 if( iFailState != TQUERY_SUCCESS )
158 {
159 return log_amx("TOP15 SQL: SQL Error #%d - %s", iErrnum, szError)
160 }
161
162 new id = cData[0]
163 new mapname[64], Handle:QueryPrep
164 new count = 0
165
166 while( SQL_MoreResults(hQuery) )
167 {
168 SQL_ReadResult(hQuery, 0, mapname, 63)
169
170 QueryPrep = SQL_PrepareQuery(SqlConnection, "SELECT authid FROM `kz_pro15` WHERE mapname='%s' ORDER BY time LIMIT 15", mapname)
171
172 if( SQL_Execute(QueryPrep) )
173 {
174 while( SQL_MoreResults(QueryPrep) )
175 {
176 new authid[32]
177 SQL_ReadResult(QueryPrep, 0, authid, 31)
178
179 if( equal(authid, g_AuthID[id]) )
180 {
181 count++
182 break
183 }
184
185 SQL_NextRow(QueryPrep)
186 }
187
188 }
189 SQL_NextRow(hQuery)
190 }
191
192 g_ProRecords[id] = count
193
194 return PLUGIN_CONTINUE
195}
196
197public Get_QueryHandle(iFailState, Handle:hQuery, szError[], iErrnum, cData[], iSize, Float:fQueueTime)
198{
199 if( iFailState != TQUERY_SUCCESS )
200 {
201 log_amx("[KZ] ACH SQL: SQL Error #%d - %s", iErrnum, szError)
202 client_print(0, print_chat, "!tACH SQL: !yWarning the SQL Tops can not be saved.")
203 }
204
205 return PLUGIN_CONTINUE
206}