· 9 years ago · Jan 29, 2017, 01:20 PM
1#include <a_samp>
2#include <a_mysql>
3#include <foreach>
4
5//Configuring MySQL Stuff to set up the connection
6
7#define MYSQL_HOST "Your DB Host" // Change this to your MySQL Remote IP or "localhost".
8#define MYSQL_USER "Your DB User" // Change this to your MySQL Database username.
9#define MYSQL_PASS "Your DB Pass" // Change this to your MySQL Database password.
10#define MYSQL_DATABASE "Your DB Name" // Change this to your MySQL Database name.
11
12// Well, don't just enter random information and expect it to work, information
13// Should be valid and working fine.
14
15#define DIALOG_REGISTER (0)
16#define DIALOG_LOGIN (1)
17
18// Make sure the dialog IDs above do not match any dialog ID you're using in your
19// gamemode otherwise they won't do their job properly.
20
21new MySQL: Database, Corrupt_Check[MAX_PLAYERS];
22
23//Creating an enumerator to store player's data for further use (below).
24
25//==============================================================================
26
27enum ENUM_PLAYER_DATA
28{
29 ID,
30 Name[25],
31
32 Password[65],
33 Salt[11],
34
35 PasswordFails,
36
37 Kills,
38 Deaths,
39
40 Score,
41 Cash,
42
43 Cache: Player_Cache,
44 bool:LoggedIn
45}
46
47new pInfo[MAX_PLAYERS][ENUM_PLAYER_DATA];
48
49//==============================================================================
50
51public OnGameModeInit()
52{
53 new MySQLOpt: option_id = mysql_init_options();
54 mysql_set_option(option_id, AUTO_RECONNECT, true); // We will set that option to automatically reconnect on timeouts.
55
56// Database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE, option_id); // Setting up the "Database" handle on the given MySQL details above.
57 Database = mysql_connect("43.225.188.185", "ahmed", "kosomk_1", "ahmed", option_id);
58
59 if(Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0) // Checking if the database connection is invalid to shutdown.
60 {
61 print("I couldn't connect to the MySQL server, closing."); // Printing a message to the log.
62
63 SendRconCommand("exit"); // Sending console command to shut down server.
64 return 1;
65 }
66
67 print("I have connected to the MySQL server."); // If the given MySQL details were all okay, this message prints to the log.
68
69 // Now, we will set up the information table of the player's information.
70
71 mysql_tquery(Database, "CREATE TABLE IF NOT EXISTS `PLAYERS` (`ID` int(11) NOT NULL AUTO_INCREMENT,`USERNAME` varchar(24) NOT NULL,`PASSWORD` char(65) NOT NULL,`SALT` char(11) NOT NULL,`SCORE` mediumint(7), `KILLS` mediumint(7), `CASH` mediumint(7) NOT NULL DEFAULT '0',`DEATHS` mediumint(7) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`), UNIQUE KEY `USERNAME` (`USERNAME`))");
72
73 // So, this code is probably the only one which you haven't understood.
74 // Well, we firstly create a table only if not existing in the database which is "USERS".
75 // We create "ID" and set it as a primary key with auto increment to use it in retrieving information and many more uses.
76 // We create "USERNAME" and set it as a unique key, the USERNAME stores every player's name in the database so you can
77 // Control the players in offline mode and when a player leaves everything storted like kills, deaths, password and Saltion key
78 // Wouldn't be lost upon server's close or player's disconnection.
79 // We store kills, deaths, score and cash as written above so they might be useful for further use.
80
81 return 1;
82}
83
84public OnGameModeExit()
85{
86 foreach(new i: Player)
87 {
88 if(IsPlayerConnected(i)) // Checking if the players stored in "i" are connected.
89 {
90 OnPlayerDisconnect(i, 1); // We do that so players wouldn't lose their data upon server's close.
91 }
92 }
93
94 mysql_close(Database); // Closing the database.
95 return 1;
96}
97
98public OnPlayerConnect(playerid)
99{
100 new DB_Query[115];
101
102 //Resetting player information.
103 pInfo[playerid][Kills] = 0;
104 pInfo[playerid][Deaths] = 0;
105 pInfo[playerid][PasswordFails] = 0;
106
107 GetPlayerName(playerid, pInfo[playerid][Name], MAX_PLAYER_NAME); // Getting the player's name.
108 Corrupt_Check[playerid]++;
109
110 mysql_format(Database, DB_Query, sizeof(DB_Query), "SELECT * FROM `PLAYERS` WHERE `USERNAME` = '%e' LIMIT 1", pInfo[playerid][Name]);
111 mysql_tquery(Database, DB_Query, "OnPlayerDataCheck", "ii", playerid, Corrupt_Check[playerid]);
112 return 1;
113}
114
115public OnPlayerDisconnect(playerid, reason)
116{
117 Corrupt_Check[playerid]++;
118
119 new DB_Query[256];
120 //Running a query to save the player's data using the stored stuff.
121 mysql_format(Database, DB_Query, sizeof(DB_Query), "UPDATE `PLAYERS` SET `SCORE` = %d, `CASH` = %d, `KILLS` = %d, `DEATHS` = %d WHERE `ID` = %d LIMIT 1",
122 pInfo[playerid][Score], pInfo[playerid][Cash], pInfo[playerid][Kills], pInfo[playerid][Deaths], pInfo[playerid][ID]);
123
124 mysql_tquery(Database, DB_Query);
125
126 if(cache_is_valid(pInfo[playerid][Player_Cache])) //Checking if the player's cache ID is valid.
127 {
128 cache_delete(pInfo[playerid][Player_Cache]); // Deleting the cache.
129 pInfo[playerid][Player_Cache] = MYSQL_INVALID_CACHE; // Setting the stored player Cache as invalid.
130 }
131
132 pInfo[playerid][LoggedIn] = false;
133 print("OnPlayerDisconnect has been called."); // Sending message once OnPlayerDisconnect is called.
134 return 1;
135}
136
137public OnPlayerDeath(playerid, killerid, reason)
138{
139 if(killerid != INVALID_PLAYER_ID) // Checking if the killer of the player is valid.
140 {
141 //Increasing the kills of the killer and the deaths of the player.
142 pInfo[killerid][Kills]++;
143 pInfo[playerid][Deaths]++;
144 }
145 return 1;
146}
147
148public OnPlayerRequestSpawn(playerid)
149{
150 if(pInfo[playerid][LoggedIn] == false) return 0; // Ignoring the request incase player isn't logged in.
151 return 1;
152}
153
154public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
155{
156 switch (dialogid)
157 {
158 case DIALOG_LOGIN:
159 {
160 if(!response) return Kick(playerid);
161
162 new Salted_Key[65];
163 SHA256_PassHash(inputtext, pInfo[playerid][Salt], Salted_Key, 65);
164
165 if(strcmp(Salted_Key, pInfo[playerid][Password]) == 0)
166 {
167 // Now, password should be correct as well as the strings
168 // Matched with each other, so nothing is wrong until now.
169
170 // We will activate the cache of player to make use of it e.g.
171 // Retrieve their data.
172
173 cache_set_active(pInfo[playerid][Player_Cache]);
174
175 // Okay, we are retrieving the information now..
176 cache_get_value_int(0, "ID", pInfo[playerid][ID]);
177
178 cache_get_value_int(0, "KILLS", pInfo[playerid][Kills]);
179 cache_get_value_int(0, "DEATHS", pInfo[playerid][Deaths]);
180
181 cache_get_value_int(0, "SCORE", pInfo[playerid][Score]);
182 cache_get_value_int(0, "CASH", pInfo[playerid][Cash]);
183
184 SetPlayerScore(playerid, pInfo[playerid][Score]);
185
186 ResetPlayerMoney(playerid);
187 GivePlayerMoney(playerid, pInfo[playerid][Cash]);
188
189 // So, we have successfully retrieved data? Now deactivating the cache.
190
191 cache_delete(pInfo[playerid][Player_Cache]);
192 pInfo[playerid][Player_Cache] = MYSQL_INVALID_CACHE;
193
194 pInfo[playerid][LoggedIn] = true;
195 SendClientMessage(playerid, 0x00FF00FF, "Logged in to the account.");
196 }
197 else
198 {
199 new String[150];
200
201 pInfo[playerid][PasswordFails] += 1;
202 printf("%s has been failed to login. (%d)", pInfo[playerid][Name], pInfo[playerid][PasswordFails]);
203 // Printing the message that someone has failed to login to his account.
204
205 if (pInfo[playerid][PasswordFails] >= 3) // If the fails exceeded the limit we kick the player.
206 {
207 format(String, sizeof(String), "%s has been kicked Reason: {FF0000}(%d/3) Login fails.", pInfo[playerid][Name], pInfo[playerid][PasswordFails]);
208 SendClientMessageToAll(0x969696FF, String);
209 Kick(playerid);
210 }
211 else
212 {
213 // If the player didn't exceed the limits we send him a message that the password is wrong.
214 format(String, sizeof(String), "Wrong password, you have %d out of 3 tries.", pInfo[playerid][PasswordFails]);
215 SendClientMessage(playerid, 0xFF0000FF, String);
216
217 format(String, sizeof(String), "{FFFFFF}Welcome back, %s.\n\n{0099FF}This account is already registered.\n\
218 {0099FF}Please, input your password below to proceed to the game.\n\n", pInfo[playerid][Name]);
219 ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login System", String, "Login", "Leave");
220 }
221 }
222 }
223 case DIALOG_REGISTER:
224 {
225 if(!response) return Kick(playerid);
226
227 if(strlen(inputtext) <= 5 || strlen(inputtext) > 60)
228 {
229 // If the password length is less than or equal to 5 and more than 60
230 // It repeats the process and shows error message as seen below.
231
232 SendClientMessage(playerid, 0x969696FF, "Invalid password length, should be 5 - 60.");
233
234 new String[150];
235
236 format(String, sizeof(String), "{FFFFFF}Welcome %s.\n\n{0099FF}This account is not registered.\n\
237 {0099FF}Please, input your password below to proceed.\n\n", pInfo[playerid][Name]);
238 ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration System", String, "Register", "Leave");
239 }
240 else
241 {
242
243 // Salting the player's password using SHA256 for a better security.
244
245 for (new i = 0; i < 10; i++)
246 {
247 pInfo[playerid][Salt][i] = random(79) + 47;
248 }
249
250 pInfo[playerid][Salt][10] = 0;
251 SHA256_PassHash(inputtext, pInfo[playerid][Salt], pInfo[playerid][Password], 65);
252
253 new DB_Query[225];
254
255 // Storing player's information if everything goes right.
256 mysql_format(Database, DB_Query, sizeof(DB_Query), "INSERT INTO `PLAYERS` (`USERNAME`, `PASSWORD`, `SALT`, `SCORE`, `KILLS`, `CASH`, `DEATHS`)\
257 VALUES ('%e', '%s', '%e', '20', '0', '0', '0')", pInfo[playerid][Name], pInfo[playerid][Password], pInfo[playerid][Salt]);
258 mysql_tquery(Database, DB_Query, "OnPlayerRegister", "d", playerid);
259 }
260 }
261 }
262 return 1;
263}
264
265forward public OnPlayerDataCheck(playerid, corrupt_check);
266public OnPlayerDataCheck(playerid, corrupt_check)
267{
268 if (corrupt_check != Corrupt_Check[playerid]) return Kick(playerid);
269 // You'd have asked already what's corrput_check and how it'd benefit me?
270 // Well basically MySQL query takes long, incase a player leaves while its not proceeded
271 // With ID 1 for example, then another player comes as ID 1 it'll basically corrupt the data
272 // So, once the query is done, the player will have the wrong data assigned for himself.
273
274 new String[150];
275
276 if(cache_num_rows() > 0)
277 {
278 // If the player exists, everything is okay and nothing is wrongly detected
279 // The player's password and Saltion key gets stored as seen below
280 // So we won't have to get a headache just to match player's password.
281
282 cache_get_value(0, "PASSWORD", pInfo[playerid][Password], 65);
283 cache_get_value(0, "SALT", pInfo[playerid][Salt], 11);
284
285 pInfo[playerid][Player_Cache] = cache_save();
286 // ^ Storing the cache ID of the player for further use later.
287
288 format(String, sizeof(String), "{FFFFFF}Welcome back, %s.\n\n{0099FF}This account is already registered.\n\
289 {0099FF}Please, input your password below to proceed to the game.\n\n", pInfo[playerid][Name]);
290 ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login System", String, "Login", "Leave");
291 }
292 else
293 {
294 format(String, sizeof(String), "{FFFFFF}Welcome %s.\n\n{0099FF}This account is not registered.\n\
295 {0099FF}Please, input your password below to proceed to the game.\n\n", pInfo[playerid][Name]);
296 ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "Registration System", String, "Register", "Leave");
297 }
298 return 1;
299}
300
301forward public OnPlayerRegister(playerid);
302public OnPlayerRegister(playerid)
303{
304 // This gets called only when the player registers a new account.
305 SendClientMessage(playerid, 0x00FF00FF, "You are now registered and has been logged in.");
306 pInfo[playerid][LoggedIn] = true;
307 return 1;
308}
309
310// End of script //