· 8 years ago · Feb 28, 2018, 07:04 PM
1#include <a_samp>
2#include <a_mysql>
3//XXXXXXXXXXX|| MySql Configs || XXXXXXXXXX
4#define mysql_host "127.0.0.1" //Has to be a string
5#define mysql_user "root" //Has to be a string
6#define mysql_password "1234" //There is none for wamp unless you set one.
7#define mysql_database "sa-mp" //Has to be a string
8new MoneyGiven[MAX_PLAYERS]; //Explained in the paragraph above.
9new Logged[MAX_PLAYERS]; //The variable to check if the player is logged.
10new IsRegistered[MAX_PLAYERS];
11
12//NOTE:Passwordstring has already been escaped. If you want to use
13//this in another script, make sure that you escape the passwordstring
14//before you
15stock MySQL_Register(playerid, passwordstring[])
16{
17 new query[200], pname[24], IP[16];
18 GetPlayerName(playerid, pname, 24);
19 GetPlayerIp(playerid, IP, 16);
20 format(query, sizeof(query), "INSERT INTO playerdata (user, password, score, money, IP) VALUES('%s', SHA1('%s'), 0, 0, '%s')", pname, passwordstring, IP);
21 mysql_query(query);
22 //We do not need to store or free a result as it
23 //is not a select statement. We can now send the
24 //client a registration success message and set the
25 //Login variable to 1.
26 SendClientMessage(playerid, -1, "You have been registered on this server!");
27 Logged[playerid] = 1; //Sets the login variable
28 return 1;
29}
30
31stock MySQL_Login(playerid)
32{
33 new query[300], pname[24], savingstring[20];
34 GetPlayerName(playerid, pname, 24);
35 format(query, sizeof(query), "SELECT * FROM playerdata WHERE user = '%s'", pname);
36 //We only select the variables that we want to use.
37 //We don't need things like the password string or the user string.
38 mysql_query(query); //Queries the result
39 mysql_store_result(); //Store a result because it's a SELECT statement.
40 while(mysql_fetch_row_format(query,"|"))
41 {
42 //We use while so that it does a single query, not multiple
43 //Especially when we have more variables. If there is more
44 //Variables, you should just split the line with sscanf. To
45 //Make it easier.
46 mysql_fetch_field_row(savingstring, "score"); SetPlayerScore(playerid, strval(savingstring));
47 mysql_fetch_field_row(savingstring, "money"); MoneyGiven[playerid] = strval(savingstring);
48 //If you are wondering why I'm using savingstring instead
49 //Of a variable like using MoneyGiven right away, it's because
50 //mysql_fetch_field_row requires a string.
51 }
52 mysql_free_result(); //We must always free a stored result
53 SendClientMessage(playerid, -1, "You have been logged in!"); //Sends the client a message.
54 Logged[playerid] = 1; //Sets our logged in variable to one
55 return 1;
56}
57
58main()
59{
60 print("\n----------------------------------");
61 print(" Blank Gamemode by your name here");
62 print("----------------------------------\n");
63}
64
65public OnGameModeInit()
66{
67 mysql_connect(mysql_host, mysql_user, mysql_database, mysql_password);
68 mysql_query("CREATE TABLE IF NOT EXISTS playerdata(user VARCHAR(24), password VARCHAR(40), score INT(20), money INT(20), IP VARCHAR(16) )");
69 AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
70 return 1;
71}
72
73public OnGameModeExit()
74{
75 return 1;
76}
77
78public OnPlayerRequestSpawn(playerid)
79{
80 if(!Logged[playerid]) //If the player isn't logged in and (s)he tries to spawn.
81 {
82 if(!IsRegistered[playerid]) //If the player isn't registered
83 {
84 ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT, "Register","Your user is {FF0000}not{FFFFFF} registered! Please {0000FF}register{FFFFFF} with a password below!\n {FF0000}ERROR:You must register before spawning!","Register","Cancel"); //Shows our register dialog :).
85 return 0; //Prevents the player from spawning
86 }
87 if(IsRegistered[playerid] == 1) //Our handy variable comes into use now
88 {
89 ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_INPUT, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!\n{FF0000} You must login before you spawn!","Login","Cancel"); //Shows our login dialog :).
90 return 0; //Prevents the player from spawning
91 }
92 }
93 return 1;
94}
95
96public OnPlayerConnect(playerid)
97{
98 MoneyGiven[playerid] = -1; //Resets the variable that you will discover later in the tutorial.
99 new query[200], pname[24]; //Creates our variables.
100 GetPlayerName(playerid, pname, 24); //Gets the players name
101 format(query, sizeof(query), "SELECT IP FROM `playerdata` WHERE user = '%s' LIMIT 1", pname); //Formats the query, view above the code for a explanation
102 mysql_query(query); //This is our query function to query the string
103 mysql_store_result(); //We store the result.
104 new rows = mysql_num_rows(); //We get how many rows the query returned.
105 if(!rows)
106 {
107 //If the rows are equal to 0. This means that the query did not find
108 //anyone under the name we connected under in the database.
109 //So here we send the player the register dialog.
110 ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT, "Register","Your user is {FF0000}not{FFFFFF} registered! Please {0000FF}register{FFFFFF} with a password below!","Register","Cancel"); //Shows our register dialog :).
111 }
112 if(rows == 1)
113 {
114 //If the rows are equal to 1, this means there is a player already registered
115 //so we can initiate the login dialog to the player or check if the players
116 //current IP is the same one as in the database.
117 new IP[2][16]; //We create a variable with two IP strings, one for retrieving the mysql field and one for GetPlayerIP.
118 mysql_fetch_field_row(IP[0],"IP");
119 GetPlayerIp(playerid, IP[1], 16);
120 if(strlen(IP[0]) != 0 && !strcmp(IP[0], IP[1], true)) //Checks that the MySQL IP has a value and that they are the same.
121 {
122 MySQL_Login(playerid);
123 }
124 else if(!strlen(IP[0]) || strcmp(IP[0], IP[1], true))
125 {
126 ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_INPUT, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!","Login","Cancel"); //Shows our login dialog :).
127 IsRegistered[playerid] = 1; //Sets the registered variable to 1 (Shows that the player is registered).
128 }
129 }
130 mysql_free_result();
131 //You must always free the mysql result to avoid
132 //there being massive memory usage.
133 return 1;
134}
135
136public OnPlayerDisconnect(playerid, reason)
137{
138 if(Logged[playerid] == 1)
139 {
140 //If the player disconnects before registering,
141 //we want to make sure it doesn't try update
142 //so we check if the player is logged in.
143 new score = GetPlayerScore(playerid); //Gets players score
144 new money = GetPlayerMoney(playerid); //Gets players money
145 new query[200], pname[24]; //Creates the variables
146 GetPlayerName(playerid, pname, 24); //Gets the players name.
147 format(query, sizeof(query), "UPDATE playerdata SET score=%d, money=%d WHERE user='%s'", score, money, pname);
148 mysql_query(query);
149 //No need to store a result for a update string
150 }
151 return 1;
152}
153
154public OnPlayerSpawn(playerid)
155{
156 if(MoneyGiven[playerid] != -1)
157 {
158 GivePlayerMoney(playerid, MoneyGiven[playerid]);
159 MoneyGiven[playerid] = -1;
160 }
161 //Gives the player money if they haven't received it yet
162 return 1;
163}
164
165public OnPlayerCommandText(playerid, cmdtext[])
166{
167 if (strcmp("/comando", cmdtext, true, 10) == 0)
168 {
169 SendClientMessage(playerid,-1,"...");
170 return 1;
171 }
172 return 0;
173}
174
175public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
176{
177 if(dialogid == 15000) //If Dialog is our register dialog
178 {
179 if(response) //If they click the button register
180 {
181 if(!strlen(inputtext) || strlen(inputtext) > 100) //Password is not 1 to 100 characters
182 {
183 SendClientMessage(playerid, 0xFF0000, "You must insert a password between 1-100 characters!"); //Sends the client a error message
184 ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT, "Register","Your user is {FF0000}not{FFFFFF} registered! Please {0000FF}register{FFFFFF} with a password below!\n {FF0000}ERROR:Please enter a password between 1-100 characters!","Register","Cancel"); //Shows our register dialog :).
185 }
186 else if(strlen(inputtext) > 0 && strlen(inputtext) < 100)
187 {
188 new escpass[100];
189 mysql_real_escape_string(inputtext, escpass);
190 MySQL_Register(playerid, escpass);
191 }
192 //If the password is between 1 and 100 characters then we will
193 //call our MySQL_register function which will register the player.
194 }
195 if(!response)
196 {
197 SendClientMessage(playerid, 0xFF0000, "You must register before logging in!"); //Sends the client a error message
198 ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT, "Register","Your user is {FF0000}not{FFFFFF} registered! Please {0000FF}register{FFFFFF} with a password below!\n {FF0000}ERROR:Please enter a password !","Register","Cancel"); //Shows our register dialog :).
199 }
200 }
201 if(dialogid == 15500) //Dialog login
202 {
203 if(!response) //If they click the cancel button
204 {
205 SendClientMessage(playerid, 0xFF0000, "You must login before you spawn!"); //Sends the client a error message
206 ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_INPUT, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!\n{FF0000} You must login before you spawn!","Login","Cancel"); //Shows our login dialog :).
207 }
208 if(response) //If the player clicked login
209 {
210 new query[200], pname[24], escapepass[100]; //
211 GetPlayerName(playerid, pname, 24); //Gets the players name
212 mysql_real_escape_string(inputtext, escapepass); //We escape the inputtext to avoid SQL injections.
213 format(query, sizeof(query), "SELECT `user` FROM playerdata WHERE user = '%s' AND password = SHA1('%s')", pname, escapepass);
214 mysql_query(query);
215 mysql_store_result();
216 new numrows = mysql_num_rows();
217 if(numrows == 1) MySQL_Login(playerid);
218 //This means that there is a user in the database with the same
219 //password that we typed, we now proceed by using the login function.
220 if(!numrows)
221 {
222 //This means that the password that the player
223 //typed was incorrect and we will resend the dialog.
224 ShowPlayerDialog(playerid, 15500, DIALOG_STYLE_INPUT, "Login","Your user is {FF0000}registered{FFFFFF}! Please {0000FF}login{FFFFFF} with your password below!\n{FF0000} The password you typed was incorrect!","Login","Cancel"); //Shows our login dialog :).
225 SendClientMessage(playerid, 0xFF0000, "Incorrect password!"); //Sends the client a error message
226 }
227 mysql_free_result(); //Remember to always free a result if you stored one!
228 }
229 }
230 return 1;
231}