31.07.2010, 06:05
PHP код:
MySQL Error (0): Failed to connect. Access denied for user 'root'@'localhost' (using password: YES).
[02:02:15] Script[gamemodes/COPSAR~1.amx]: Run time error 20: "Invalid index parameter (bad entry point)"
[02:02:15] Number of vehicle models: 0
[02:02:56] --- Server Shutting Down.
[02:02:56] SA:MP MySQL Plugin 1.2.1 Unloaded.
pawn Код:
#include <a_samp>
//Gstyles include
#include <mysql>
//DCMD (Will be used for our register and login commands later in the tutorial
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
//The following information is only for our wamp server, when we are ready to upload it to our server for use you will need to change it
#define MYSQL_HOST "localhost" //Standard wamp host
#define MYSQL_USER "root" //Standard wamp user
#define MYSQL_PASS "" //Standard wamp password (Nothing)
#define MYSQL_DB "sa-mp" //This is the database we are going to use (I'll show you how to set it up later :) )
//I credit whoever made this on sa-mp market :P
public OnGameModeInit()
{
if(!mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS))
{
print("SQL connection attempt 1 FAILED!");
if(!mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS))
{
print("SQL connection attempt 2 FAILED!");
if(!mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS))
{
print("SQL connection attempt 3 FAILED!");
return 1;
}
}
}
return 1;
}
public OnPlayerConnect(playerid)
{
new Query[200], Pname[24]; //Creates the Query string and the Pname
GetPlayerName(playerid, Pname, 24); //Gets the players name
format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'", Pname); //Selects the line where the playername is the player
mysql_query(Query); //Querys the string
mysql_store_result(); //Stores the result
if(mysql_num_rows() != 0) //If the user is found
{
new PIP[56]; //Creates the IP string
GetPlayerIp(playerid, PIP, 56); //Gets the players IP
format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `IP` = '%s'", Pname, PIP); //Checks if the players IP is the same, if so it will auto login :)
mysql_query(Query); //Queries the result
mysql_store_result(); //Stores the result
if(mysql_num_rows() != 0) // If the players IP is the same in the DB
{
new line[750];
SetPVarInt(playerid, "Logged", 1); //Sets the logged variable to 1
SendClientMessage(playerid, 0x009600AA, "Auto Logged in!");
if(mysql_fetch_row(line)) //Fetches the line
{
new data[3][50]; //The data strings
new data2[4]; //The data variables
sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
SetPlayerScore(playerid, data2[2]); //Sets players score
GivePlayerMoney(playerid, data2[3]); //Sets players cash
mysql_free_result();
}
}
if(!mysql_num_rows())
{
SendClientMessage(playerid, 0x009600AA, "This account is registered, please login"); //User is registered but IP does not match
ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
}
}
else
{
ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
SendClientMessage(playerid, 0x009600AA, "This account is not registered, please register!"); // User is not registed
}
mysql_free_result(); //Frees the result :)
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(dialogid == 14600)
{
if(response)
{
if(!strlen(inputtext)) //If the player doesn't enter a pass
{
ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
SendClientMessage(playerid, 0xF60000AA, "Please enter a password");
}
new PIP[50];
new Query[200], Pname[24];
GetPlayerName(playerid, Pname, 24); //Players name
GetPlayerIp(playerid, PIP, 50); //Players IP
format(Query, sizeof(Query), "INSERT INTO `playerinfo` (`user`, `password`, `kills`, `deaths`, `score`, `money`, IP) VALUES ('%s', '%s', 0, 0, 0, 0, '%s')", Pname, inputtext, PIP); //Insert string
mysql_query(Query); //Queries
GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
SetPVarInt(playerid, "Logged", 1);
}
}
if(dialogid == 15000)
{
if(response)
{
new Query[200], Pname[24];
GetPlayerName(playerid, Pname, 24);
format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `password` = '%s'", Pname, inputtext);
mysql_query(Query);
mysql_store_result();
if(!mysql_num_rows())
{
SendClientMessage(playerid, 0xF60000AA, "Invalid password!");
SetPVarInt(playerid, "WrongPass", GetPVarInt(playerid, "WrongPass") + 1);
ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
if(GetPVarInt(playerid, "WrongPass") == 3)
{
SendClientMessage(playerid, 0xF60000AA, "Max password tries exceeded!!");
Kick(playerid);
}
}
else
{
new line[750]; //I know it's big D=
if(mysql_fetch_row(line)) //Fetches the line
{
new data[3][50]; //The data strings
new data2[4]; //The data variables
sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
SetPlayerScore(playerid, data2[2]); //Sets players score
SetPVarInt(playerid, "MoneyGiven", data2[3]); //Sets players cash
SendClientMessage(playerid, 0x0000D9AA, "Logged in!");
mysql_free_result();
}
}
}
}
return 1;
}
public OnPlayerRequestSpawn(playerid)
{
if(GetPVarInt(playerid, "Logged") == 0)
{
new Query[200], Pname[24]; //Variables
GetPlayerName(playerid, Pname, 24); //Gets the player name
format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'" , Pname); //MySQL string
mysql_query(Query); //Queries
mysql_store_result(); //Stores
if(!mysql_num_rows()) ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel"); //If the user is not found it will show the register dialog
else ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel"); //If the user is not found it will show the login dialog
return 0; //Player won't spawn
}
return 1;
}
public OnPlayerSpawn(playerid)
{
if(GetPVarInt(playerid, "Logged") == 1 && GetPVarInt(playerid, "MoneyGiven") > 0)
{
GivePlayerMoney(playerid, GetPVarInt(playerid, "MoneyGiven")); //Cant give cash on OnPlayerConnect
SetPVarInt(playerid, "MoneyGiven", 0);
}
return 1;
}
public OnPlayerDeath(playerid, killerid, reason)
{
SetPVarInt(playerid, "Deaths", GetPVarInt(playerid, "Deaths") + 1); //Adds a death to the person who died
if(killerid != INVALID_PLAYER_ID) SetPVarInt(playerid, "Kills", GetPVarInt(playerid, "Kills") + 1); // Adds a kill to the killer unless the player suicided.
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(GetPVarInt(playerid, "Logged") == 1) //Only saves if the player was logged in
{
new query[300]; //Variables
new Pname[24]; //Variables
GetPlayerName(playerid, Pname, 24); //Gets the player name
format(query, sizeof(query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d', `kills` = '%d', `deaths` = '%d' WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), GetPVarInt(playerid, "Kills"), GetPVarInt(playerid, "Deaths"), Pname); //Saves all the info
mysql_query(query); //queries
mysql_free_result(); //Frees the result
}
return 1;
}
stock sscanf(string[], format[], {Float,_}:...)
{
#if defined isnull
if (isnull(string))
#else
if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
#endif
{
return format[0];
}
#pragma tabsize 4
new
formatPos = 0,
stringPos = 0,
paramPos = 2,
paramCount = numargs(),
delim = ' ';
while (string[stringPos] && string[stringPos] <= ' ')
{
stringPos++;
}
while (paramPos < paramCount && string[stringPos])
{
switch (format[formatPos++])
{
case '\0':
{
return 0;
}
case 'i', 'd':
{
new
neg = 1,
num = 0,
ch = string[stringPos];
if (ch == '-')
{
neg = -1;
ch = string[++stringPos];
}
do
{
stringPos++;
if ('0' <= ch <= '9')
{
num = (num * 10) + (ch - '0');
}
else
{
return -1;
}
}
while ((ch = string[stringPos]) > ' ' && ch != delim);
setarg(paramPos, 0, num * neg);
}
case 'h', 'x':
{
new
num = 0,
ch = string[stringPos];
do
{
stringPos++;
switch (ch)
{
case 'x', 'X':
{
num = 0;
continue;
}
case '0' .. '9':
{
num = (num << 4) | (ch - '0');
}
case 'a' .. 'f':
{
num = (num << 4) | (ch - ('a' - 10));
}
case 'A' .. 'F':
{
num = (num << 4) | (ch - ('A' - 10));
}
default:
{
return -1;
}
}
}
while ((ch = string[stringPos]) > ' ' && ch != delim);
setarg(paramPos, 0, num);
}
case 'c':
{
setarg(paramPos, 0, string[stringPos++]);
}
case 'f':
{
new changestr[16], changepos = 0, strpos = stringPos;
while(changepos < 16 && string[strpos] && string[strpos] != delim)
{
changestr[changepos++] = string[strpos++];
}
changestr[changepos] = '\0';
setarg(paramPos,0,_:floatstr(changestr));
}
case 'p':
{
delim = format[formatPos++];
continue;
}
case '\'':
{
new
end = formatPos - 1,
ch;
while ((ch = format[++end]) && ch != '\'') {}
if (!ch)
{
return -1;
}
format[end] = '\0';
if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
{
if (format[end + 1])
{
return -1;
}
return 0;
}
format[end] = '\'';
stringPos = ch + (end - formatPos);
formatPos = end + 1;
}
case 'u':
{
new
end = stringPos - 1,
id = 0,
bool:num = true,
ch;
while ((ch = string[++end]) && ch != delim)
{
if (num)
{
if ('0' <= ch <= '9')
{
id = (id * 10) + (ch - '0');
}
else
{
num = false;
}
}
}
if (num && IsPlayerConnected(id))
{
setarg(paramPos, 0, id);
}
else
{
#if !defined foreach
#define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
#define __SSCANF_FOREACH__
#endif
string[end] = '\0';
num = false;
new
name[MAX_PLAYER_NAME];
id = end - stringPos;
foreach (Player, playerid)
{
GetPlayerName(playerid, name, sizeof (name));
if (!strcmp(name, string[stringPos], true, id))
{
setarg(paramPos, 0, playerid);
num = true;
break;
}
}
if (!num)
{
setarg(paramPos, 0, INVALID_PLAYER_ID);
}
string[end] = ch;
#if defined __SSCANF_FOREACH__
#undef foreach
#undef __SSCANF_FOREACH__
#endif
}
stringPos = end;
}
case 's', 'z':
{
new
i = 0,
ch;
if (format[formatPos])
{
while ((ch = string[stringPos++]) && ch != delim)
{
setarg(paramPos, i++, ch);
}
if (!i)
{
return -1;
}
}
else
{
while ((ch = string[stringPos++]))
{
setarg(paramPos, i++, ch);
}
}
stringPos--;
setarg(paramPos, i, '\0');
}
default:
{
continue;
}
}
while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
{
stringPos++;
}
while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
{
stringPos++;
}
paramPos++;
}
do
{
if ((delim = format[formatPos++]) > ' ')
{
if (delim == '\'')
{
while ((delim = format[formatPos++]) && delim != '\'') {}
}
else if (delim != 'z')
{
return delim;
}
}
}
while (delim > ' ');
return 0;
}