11.09.2012, 15:03
hello I have developed a small system for Registration/Login , I have used mysql to save account and plugin "regex"
table mysql :
table mysql :
Код:
/* MySQL Data Transfer Source Host: localhost Source Database: rp_ls Target Host: localhost Target Database: rp_ls Date: 2012-09-11 11:09:07 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for accounts -- ---------------------------- CREATE TABLE `accounts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(26) NOT NULL, `password` varchar(26) NOT NULL, `skin` smallint(5) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `accounts` VALUES ('1', 'Karim_Squalli', '123456as', '4');
pawn Код:
#include <a_samp>
#include <regex>
#include <a_mysql>
#define COLOR_RED 10
#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASS ""
#define MYSQL_DB "rp_ls"
#define DIALOG_LOGIN 0
#define DIALOG_REGISTER 1
#define DIALOG_LOGIN_CAP "Login Menu"
#define DIALOG_LOGIN_CONTENT "Hello, please insert your password :"
#define DIALOG_REGISTER_CAP "Registration Menu"
#define DIALOG_REGISTER_CONTENT "hi, your are new player.\n For register please insert your password"
forward IsValidRpName(playerName[]);
forward KickPlayer(playerid);
forward CheckPlayerRegistration(playerid);
forward LoadPlayerData(playerid);
enum E_PLAYER_DATA
{
pName[MAX_PLAYER_NAME],
pSkin,
}
new gConnection;
new gPlayerData[MAX_PLAYERS][E_PLAYER_DATA];
public OnFilterScriptInit()
{
gConnection = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS);
print("\n--------------------------------------");
print(" ****** AccountSystem -- By SqualliKarim *****");
print("--------------------------------------\n");
return 1;
}
public OnPlayerConnect(playerid)
{
new playerName[MAX_PLAYER_NAME];
GetPlayerName(playerid, playerName, MAX_PLAYER_NAME);
if(!IsValidRpName(playerName))
SetTimerEx("KickPlayer", 1000, false, "%i", playerid);
new escapedQuery[256];
mysql_format(gConnection, escapedQuery, "SELECT * FROM accounts WHERE name = '%s'", playerName);
mysql_function_query(gConnection, escapedQuery, true, "CheckPlayerRegistration", "i", playerid);
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_LOGIN :
{
if(!response) return Kick(playerid);
new escapedQuery[256];
mysql_format(gConnection, escapedQuery, "SELECT * FROM accounts WHERE name = '%s' AND password = '%e'",
GetPlayerNameEx(playerid), inputtext);
mysql_function_query(gConnection, escapedQuery, true, "LoadPlayerData", "i", playerid);
}
case DIALOG_REGISTER :
{
if(!response) return Kick(playerid);
if( strlen(inputtext) > 6 && strlen(inputtext) < 26 )
{
new escapedQuery[256];
mysql_format(gConnection, escapedQuery, "INSERT INTO accounts(name, password, skin) VALUES('%s', '%e', '%i')",
GetPlayerNameEx(playerid), inputtext, random(300));
mysql_function_query(gConnection, escapedQuery, false, "", "");
}
else
{
SendClientMessage(playerid, COLOR_RED, "please tip a password betwen 6 and 26 caractйre");
ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, DIALOG_REGISTER_CAP, DIALOG_REGISTER_CONTENT, "Accept", "Cancel");
}
}
}
return 1;
}
public CheckPlayerRegistration(playerid)
{
new rows, fields;
cache_get_data(rows, fields);
printf("%i, %i", rows, fields);
if(rows) ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, DIALOG_LOGIN_CAP, DIALOG_LOGIN_CONTENT, "Login", "Cancel");
else ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_INPUT, DIALOG_REGISTER_CAP, DIALOG_REGISTER_CONTENT, "Accept", "Cancel");
}
public LoadPlayerData(playerid)
{
new rows, fields;
cache_get_data(rows, fields);
if(rows)
{
new temp[256];
gPlayerData[playerid][pName] = GetPlayerNameEx(playerid);
cache_get_field_content(0, "skin", temp); gPlayerData[playerid][pSkin] = strval(temp);
DeletePVar(playerid, "ATTEMPS");
}
else
{
new str[128];
SetPVarInt(playerid, "ATTEMPS", GetPVarInt(playerid, "ATTEMPS") +1);
format(str, sizeof(str), "Attempts remaining : %i", 3 - GetPVarInt(playerid, "ATTEMPS"));
SendClientMessage(playerid, COLOR_RED, str);
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, DIALOG_LOGIN_CAP, DIALOG_LOGIN_CONTENT, "Login", "Cancel");
if(GetPVarInt(playerid, "ATTEMPS") == 3)
{
SendClientMessage(playerid, COLOR_RED, "you have been kicked");
SendClientMessage(playerid, COLOR_RED, "Reason : Attemps Connection Remain");
Kick(playerid);
}
}
}
public IsValidRpName(playerName[])
{
if( regex_match(playerName, "^[A-Z]{1}[a-z]{2,8}_[A-Z]{1}[a-z]{2,8}")) return 1;
return 0;
}
public KickPlayer(playerid)
{
Kick(playerid);
}
stock GetPlayerNameEx(playerid)
{
new playerName[MAX_PLAYER_NAME];
GetPlayerName(playerid, playerName, MAX_PLAYER_NAME);
return playerName;
}