· 7 years ago · Oct 15, 2018, 06:28 PM
1bool g_bInitialized[MAXPLAYERS+1];
2char g_sSQL_CreateTable_SQLITE[] = "CREATE TABLE IF NOT EXISTS lvl_base (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, value INTEGER NOT NULL default 0, steam varchar(32) NOT NULL default '', name varchar(128) NOT NULL default '', rank INTEGER NOT NULL default 0, kills INTEGER NOT NULL default 0, deaths INTEGER NOT NULL default 0, shoots INTEGER NOT NULL default 0, hits INTEGER NOT NULL default 0, headshots INTEGER NOT NULL default 0, assists INTEGER NOT NULL default 0, vip INTEGER NOT NULL default 0, lastconnect INTEGER NOT NULL default 0);",
3 g_sSQL_CreateTable_MYSQL[] = "CREATE TABLE IF NOT EXISTS lvl_base (id int(12) NOT NULL AUTO_INCREMENT, value int(12) NOT NULL default 0, steam varchar(32) NOT NULL default '', name varchar(128) NOT NULL default '', rank int(12) NOT NULL default 0, kills int(12) NOT NULL default 0, deaths int(12) NOT NULL default 0, shoots int(12) NOT NULL default 0, hits int(12) NOT NULL default 0, headshots int(12) NOT NULL default 0, assists int(12) NOT NULL default 0, vip int(12) NOT NULL default 0, lastconnect int(12) NOT NULL default 0, PRIMARY KEY (id)) CHARSET=utf8 COLLATE utf8_general_ci",
4 g_sSQL_CreatePlayer[] = "INSERT INTO lvl_base (value, steam, name, lastconnect) VALUES (%d, '%s', '%s', %d);",
5 g_sSQL_LoadPlayer[] = "SELECT value, rank, kills, deaths, shoots, hits, headshots, assists, vip FROM lvl_base WHERE steam = '%s';",
6 g_sSQL_SavePlayer[] = "UPDATE lvl_base SET value = %d, name = '%s', rank = %d, kills = %d, deaths = %d, shoots = %d, hits = %d, headshots = %d, assists = %d, vip = %d, lastconnect = %d WHERE steam = '%s';",
7 g_sSQL_CountPlayers[] = "SELECT steam FROM lvl_base;",
8 g_sSQL_PlacePlayer[] = "SELECT steam FROM lvl_base WHERE value >= %d;",
9 g_sSQL_PurgeDB[] = "DELETE FROM lvl_base WHERE lastconnect < %d;",
10 g_sSQL_CallTOP[] = "SELECT name, value FROM lvl_base ORDER BY value DESC LIMIT %i, 10;",
11 g_sSteamID[MAXPLAYERS+1][32];
12Database g_hDatabase = null;
13
14void ConnectDB()
15{
16 char sIdent[16], sError[256];
17 g_hDatabase = SQL_Connect("levels_ranks", false, sError, 256);
18 if(!g_hDatabase)
19 {
20 g_hDatabase = SQLite_UseDatabase("lr_base", sError, 256);
21 if(!g_hDatabase)
22 {
23 CrashLR("Could not connect to the database (%s)", sError);
24 }
25 }
26
27 DBDriver hDatabaseDriver = g_hDatabase.Driver;
28 hDatabaseDriver.GetIdentifier(sIdent, sizeof(sIdent));
29
30 SQL_LockDatabase(g_hDatabase);
31 switch(sIdent[0])
32 {
33 case 's': if(!SQL_FastQuery(g_hDatabase, g_sSQL_CreateTable_SQLITE)) CrashLR("ConnectDB - could not create table in SQLite");
34 case 'm': if(!SQL_FastQuery(g_hDatabase, g_sSQL_CreateTable_MYSQL)) CrashLR("ConnectDB - could not create table in MySQL");
35 default: CrashLR("ConnectDB - type database is invalid");
36 }
37 SQL_UnlockDatabase(g_hDatabase);
38
39 g_hDatabase.SetCharset("utf8");
40}
41
42void GetCountPlayers()
43{
44 if(!g_hDatabase)
45 {
46 LogLR("GetCountPlayers - database is invalid");
47 return;
48 }
49
50 g_hDatabase.Query(SQL_GetCountPlayers, g_sSQL_CountPlayers);
51}
52
53DBCallbackLR(SQL_GetCountPlayers)
54{
55 if(dbRs == null)
56 {
57 LogLR("SQL_GetCountPlayers - error while working with data (%s)", sError);
58 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
59 {
60 TryReconnectDB();
61 }
62 return;
63 }
64
65 g_iDBCountPlayers = dbRs.RowCount;
66}
67
68void GetPlacePlayer(int iClient)
69{
70 if(!g_hDatabase)
71 {
72 LogLR("GetPlacePlayer - database is invalid");
73 return;
74 }
75
76 char sQuery[256];
77 FormatEx(sQuery, 256, g_sSQL_PlacePlayer, EXP(iClient));
78 g_hDatabase.Query(SQL_GetPlacePlayer, sQuery, iClient);
79}
80
81DBCallbackLR(SQL_GetPlacePlayer)
82{
83 if(dbRs == null)
84 {
85 LogLR("SQL_GetPlacePlayer - error while working with data (%s)", sError);
86 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
87 {
88 TryReconnectDB();
89 }
90 return;
91 }
92
93 g_iDBRankPlayer[iClient] = dbRs.RowCount;
94}
95
96void CreateDataPlayer(int iClient)
97{
98 if(!g_hDatabase)
99 {
100 LogLR("CreateDataPlayer - database is invalid");
101 return;
102 }
103
104 if(IsClientConnected(iClient) && IsClientInGame(iClient) && !IsFakeClient(iClient))
105 {
106 char sQuery[512], sSaveName[MAX_NAME_LENGTH * 2 + 1];
107 g_hDatabase.Escape(GetFixNamePlayer(iClient), sSaveName, sizeof(sSaveName));
108
109 switch(g_iTypeStatistics)
110 {
111 case 1: EXP(iClient) = 1000;
112 default: EXP(iClient) = 0;
113 }
114
115 FormatEx(sQuery, sizeof(sQuery), g_sSQL_CreatePlayer, EXP(iClient), g_sSteamID[iClient], sSaveName, GetTime());
116 g_hDatabase.Query(SQL_CreateDataPlayer, sQuery, iClient);
117 }
118}
119
120DBCallbackLR(SQL_CreateDataPlayer)
121{
122 if(dbRs == null)
123 {
124 LogLR("SQL_CreateDataPlayer - error while working with data (%s)", sError);
125 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
126 {
127 TryReconnectDB();
128 }
129 return;
130 }
131
132 g_bInitialized[iClient] = true;
133 RANK(iClient) = 0;
134 KILLS(iClient) = 0;
135 DEATHS(iClient) = 0;
136 SHOOTS(iClient) = 0;
137 HITS(iClient) = 0;
138 HEADSHOTS(iClient) = 0;
139 ASSISTS(iClient) = 0;
140 VIP(iClient) = 0;
141 CheckRank(iClient);
142}
143
144void LoadDataPlayer(int iClient)
145{
146 if(!g_hDatabase)
147 {
148 LogLR("LoadDataPlayer - database is invalid");
149 return;
150 }
151
152 if(!IsFakeClient(iClient))
153 {
154 char sQuery[256];
155 GetClientAuthId(iClient, AuthId_Steam2, g_sSteamID[iClient], 32);
156 FormatEx(sQuery, sizeof(sQuery), g_sSQL_LoadPlayer, g_sSteamID[iClient]);
157 g_hDatabase.Query(SQL_LoadDataPlayer, sQuery, iClient);
158 }
159}
160
161DBCallbackLR(SQL_LoadDataPlayer)
162{
163 if(dbRs == null)
164 {
165 LogLR("SQL_LoadDataPlayer - error while working with data (%s)", sError);
166 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
167 {
168 TryReconnectDB();
169 }
170 return;
171 }
172
173 if(dbRs.HasResults && dbRs.FetchRow())
174 {
175 for(int i = 0; i < 9; i++)
176 {
177 g_iClientData[iClient][i] = dbRs.FetchInt(i);
178 }
179 g_bInitialized[iClient] = true;
180 CheckRank(iClient);
181 }
182 else CreateDataPlayer(iClient);
183}
184
185void SaveDataPlayer(int iClient)
186{
187 if(!g_hDatabase)
188 {
189 LogLR("SaveDataPlayer - database is invalid");
190 return;
191 }
192
193 if(g_bInitialized[iClient])
194 {
195 char sQuery[512], sSaveName[MAX_NAME_LENGTH * 2 + 1];
196 g_hDatabase.Escape(GetFixNamePlayer(iClient), sSaveName, sizeof(sSaveName));
197
198 FormatEx(sQuery, 512, g_sSQL_SavePlayer, EXP(iClient), sSaveName, RANK(iClient), KILLS(iClient), DEATHS(iClient), SHOOTS(iClient), HITS(iClient), HEADSHOTS(iClient), ASSISTS(iClient), VIP(iClient), GetTime(), g_sSteamID[iClient]);
199 g_hDatabase.Query(SQL_SaveDataPlayer, sQuery, iClient, DBPrio_High);
200 }
201}
202
203DBCallbackLR(SQL_SaveDataPlayer)
204{
205 if(dbRs == null)
206 {
207 LogLR("SQL_SaveDataPlayer - error while working with data (%s)", sError);
208 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
209 {
210 TryReconnectDB();
211 }
212 }
213}
214
215void PurgeDatabase()
216{
217 if(!g_hDatabase)
218 {
219 LogLR("PurgeDatabase - database is invalid");
220 return;
221 }
222
223 char sQuery[256];
224 FormatEx(sQuery, 256, g_sSQL_PurgeDB, GetTime() - (g_iDaysDeleteFromBase * 86400));
225 g_hDatabase.Query(SQL_PurgeDatabase, sQuery);
226}
227
228DBCallbackLR(SQL_PurgeDatabase)
229{
230 if(dbRs == null)
231 {
232 LogLR("SQL_PurgeDatabase - error while working with data (%s)", sError);
233 if(StrContains(sError, "Lost connection to MySQL", false) != -1)
234 {
235 TryReconnectDB();
236 }
237 }
238}
239
240void ResetStats()
241{
242 if(!g_hDatabase)
243 {
244 LogLR("ResetStats - database is invalid");
245 return;
246 }
247
248 SQL_LockDatabase(g_hDatabase);
249 SQL_FastQuery(g_hDatabase, "DELETE FROM lvl_base;");
250 SQL_UnlockDatabase(g_hDatabase);
251
252 for(int i = 1; i <= MaxClients; i++)
253 {
254 if(g_bInitialized[i])
255 {
256 g_bInitialized[i] = false;
257 CreateDataPlayer(i);
258 }
259 }
260}
261
262void TryReconnectDB()
263{
264 delete g_hDatabase;
265 g_hDatabase = null;
266 g_iCountRetryConnect = 0;
267 CreateTimer(g_fDBReconnectTime, TryReconnectDBTimer, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
268}
269
270public Action TryReconnectDBTimer(Handle hTimer)
271{
272 char sError[256];
273 g_hDatabase = SQL_Connect("levels_ranks", false, sError, 256);
274
275 if(!g_hDatabase)
276 {
277 g_iCountRetryConnect++;
278 if(g_iCountRetryConnect == g_iDBReconnectCount)
279 {
280 CrashLR("The attempt to restore the connection was failed, plugin disabled (%s)", sError);
281 }
282 else LogLR("The attempt to restore the connection was failed #%i", g_iCountRetryConnect);
283 }
284 else
285 {
286 g_hDatabase.SetCharset("utf8");
287 return Plugin_Stop;
288 }
289
290 return Plugin_Continue;
291}
292
293/*
294* Fix name by ФеникÑ
295*/
296char[] GetFixNamePlayer(int iClient)
297{
298 char sName[MAX_NAME_LENGTH * 2 + 1];
299 GetClientName(iClient, sName, sizeof(sName));
300
301 for(int i = 0, len = strlen(sName), CharBytes; i < len;)
302 {
303 if((CharBytes = GetCharBytes(sName[i])) == 4)
304 {
305 len -= 4;
306 for(int u = i; u <= len; u++)
307 {
308 sName[u] = sName[u+4];
309 }
310 }
311 else i += CharBytes;
312 }
313 return sName;
314}