Code:
////////////////////////////////////////////////////////////////////////////////
#include <a_samp>
#include <a_mysql>
////////////////////////////////////////////////////////////////////////////////
#define MYSQL_HOST "ip"
#define MYSQL_USER "username"
#define MYSQL_DB "database"
#define MYSQL_PASS "password"
////////////////////////////////////////////////////////////////////////////////
#define COLOR_RED 0x00FFFF
#define COLOR_YELLOW 0xFFFFFF
////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAYER_PASSWORD 24
////////////////////////////////////////////////////////////////////////////////
#define LOGIN_DIALOG 1
#define REGISTER_DIALOG 2
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
enum PlayerInfo_t
{
Name[MAX_PLAYER_NAME],
Password[MAX_PLAYER_PASSWORD],
Money,
Score,
Admin,
LoggedIn,
AccountExists,
}
////////////////////////////////////////////////////////////////////////////////
new PlayerInfo[MAX_PLAYERS][PlayerInfo_t];
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//(Created by Westie)
explode(const sSource[], aExplode[][], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[]) // Created by Westie
{
new
iNode,
iPointer,
iPrevious = -1,
iDelimiter = strlen(sDelimiter);
while(iNode < iVertices)
{
iPointer = strfind(sSource, sDelimiter, false, iPointer);
if(iPointer == -1)
{
strmid(aExplode[iNode], sSource, iPrevious, strlen(sSource), iLength);
break;
}
else
{
strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
}
iPrevious = (iPointer += iDelimiter);
++iNode;
}
return iPrevious;
}
////////////////////////////////////////////////////////////////////////////////
CheckAccountExists(account[])
{
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = '%s'", account);
mysql_query(string);
mysql_store_result();
new value;
value = mysql_num_rows();
mysql_free_result();
return value;
}
////////////////////////////////////////////////////////////////////////////////
ConnectMySQL()
{
if(mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS))
print("[SERVER-XA][MySQL] Connection to the MySQL Database was successfully!");
else
print("[SERVER-XA][MySQL] Could not connect to the MySQL Database!");
}
CheckMySQL()
{
if(mysql_ping() == -1)
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS);
}
////////////////////////////////////////////////////////////////////////////////
RegisterPlayer(playerid, password[])
{
if(PlayerInfo[playerid][AccountExists])
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>You're already registered!");
if(PlayerInfo[playerid][LoggedIn])
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>You're already logged in!");
if(strlen(password) < 3 || strlen(password) >= 32)
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>Your password is too short or too long!");
CheckMySQL();
new string[128];
format(string, sizeof(string), "INSERT INTO Users (Name,Password) VALUES ('%s','%s')", PlayerInfo[playerid][ Name], password);
mysql_query(string);
PlayerInfo[playerid][AccountExists] = 1;
SendClientMessage(playerid, COLOR_YELLOW, "[SERVER-XA][ACCOUNT] >>Your account has been created, please login now!");
LoginPlayer(playerid, password);
return 1;
}
LoginPlayer(playerid, password[])
{
if(!PlayerInfo[playerid][AccountExists])
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>You're not registered!");
if(PlayerInfo[playerid][LoggedIn])
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>You're already logged in!");
if(strlen(password) < 3 || strlen(password) >= 32)
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>Your password is too short or too long!");
CheckMySQL();
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = '%s' AND Password = '%s'", PlayerInfo[playerid][Name], password);
mysql_query(string);
mysql_store_result();
if(!mysql_num_rows())
return SendClientMessage(playerid, COLOR_RED, "[SERVER-XA][ACCOUNT] >>Incorrect password!");
new row[128];
new field[4][32];
mysql_fetch_row_format(row, "|");
explode(row, field, "|");
mysql_free_result();
format(PlayerInfo[playerid][Password], 32, "%s", field[1]);
PlayerInfo[playerid][Admin] = strval(field[2]);
PlayerInfo[playerid][Money] = strval(field[3]);
GivePlayerMoney(playerid,PlayerInfo[playerid][Money]);
format(string, sizeof(string), "Welcome back %s, XATTACK!",PlayerInfo[playerid][Name]);
SendClientMessage(playerid, COLOR_YELLOW, string);
PlayerInfo[playerid][LoggedIn] = 1;
return 1;
}
SavePlayer(playerid)
{
if(!PlayerInfo[playerid][LoggedIn])
return 0;
PlayerInfo[playerid][Money] = GetPlayerMoney(playerid);
CheckMySQL();
new string[256];
format(string, sizeof(string), "UPDATE Users SET Password='%s',Admin='%d',Money='%d' WHERE Name='%s'",PlayerInfo[playerid][Password], PlayerInfo[playerid][Admin], PlayerInfo[playerid][Money], PlayerInfo[playerid][Name]);
mysql_query(string);
return 1;
}
////////////////////////////////////////////////////////////////////////////////
main()
{
print("\n----------------------------------");
print(" XAttack Gamemode Running.........");
print("----------------------------------\n");
}
////////////////////////////////////////////////////////////////////////////////
public OnGameModeInit()
{
SetGameModeText("LTX XAttack");
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
ConnectMySQL();
return 1;
}
public OnGameModeExit()
{
return 1;
}
////////////////////////////////////////////////////////////////////////////////
public OnPlayerRequestClass(playerid, classid)
{
SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
return 1;
}
public OnPlayerConnect(playerid)
{
GetPlayerName(playerid, PlayerInfo[playerid][Name], MAX_PLAYER_NAME);
if(CheckAccountExists(PlayerInfo[playerid][Name]))
{
PlayerInfo[playerid][AccountExists] = 1;
ShowPlayerDialog(playerid, LOGIN_DIALOG,DIALOG_STYLE_PASSWORD, "Login", "Enter your password to login.If you are a new user, this username is currently in use, please choose another username and restart the game.", "Login", "");
}
else
{
PlayerInfo[playerid][AccountExists] = 0;
ShowPlayerDialog(playerid, LOGIN_DIALOG,DIALOG_STYLE_PASSWORD, "Register", "This account is not registerd.Enter a password to register this account.", "Register", "");
}
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
SavePlayer(playerid);
PlayerInfo[playerid][Admin] = 0;
PlayerInfo[playerid][Money] = 0;
return 1;
}