/* =================================== Advanced MySQL registration system. By Carlton. =================================== */ #include #include #include #define BLUE 0x0000FFFF #define SQL_HOST "" #define SQL_USER "" #define SQL_PASSWORD "" #define SQL_DB "" #define SQL_TABLE "" // The table to store the users in. #define SQL_CHECK_ACCOUNT 1 #define SQL_CHECK_IP 2 #define SQL_CHECK_LOGIN 3 #define SQL_REGISTER_PLAYER 4 #define SQL_SAVE_ACCOUNT 5 #define ERROR_MESSAGE1 "The autologin failed, because your IP address didn't match the name." #define ERROR_MESSAGE2 "The password you have given does not match." #define REGMSSG "You have registered a account." #define LOGINMSSG1 "You have logged in." #define LOGINMSSG2 "You have autologged in" #define function%0(%1) stock%0(%1) //#define AutoLogin // - Remove the // if you want a autologin when a player spawns. //#define Force_Login // - Remove the // if you want to force a player to login and register when that player joins. // Do not use AutoLogin and Force_Login at the same time! new RegistrationSystemConnection, stringsize[256], pname[MAX_PLAYER_NAME]; enum Accinfo { bool:Account, bool:Logged, pip[16], Float: pHealth, Float: pArmour, } new AccountData[MAX_PLAYERS][Accinfo]; enum PA { IP[30], Money, AdminLevel, Float: Health, Float: Armour, Bank } new PlayerAccount[MAX_PLAYERS][PA]; enum Esc { Escape[128] } new SQL_Escape[Esc]; function SavePlayerAccount(playerid) { GetPlayerName(playerid, pname, sizeof(pname)); GetPlayerHealth(playerid, AccountData[playerid][pHealth]); GetPlayerArmour(playerid, AccountData[playerid][pArmour]); format(stringsize, sizeof(stringsize), "UPDATE "SQL_TABLE" SET Money = %d, AdminLevel = %d, Health = %f, Armour = %f, Bank = %d WHERE Name = '%s'", GetPlayerMoney(playerid), PlayerAccount[playerid][AdminLevel], AccountData[playerid][pHealth], AccountData[playerid][pArmour], PlayerAccount[playerid][Bank], pname); mysql_query(stringsize, SQL_SAVE_ACCOUNT, playerid, RegistrationSystemConnection); } function OnPlayerLogin(playerid) { // In this function you can make a user forcespawn. This is called after someone logged in.. GivePlayerMoney(playerid, PlayerAccount[playerid][Money]); SetPlayerHealth(playerid, PlayerAccount[playerid][Health]); SetPlayerHealth(playerid, PlayerAccount[playerid][Armour]); AccountData[playerid][Logged] = true; } function OnPlayerRegister(playerid) { // In this function you can make a user forcespawn. This is called after someone registered. AccountData[playerid][Account] = true; #if defined Force_Login { ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); } #endif } function ConnectToDB() { mysql_debug(1); RegistrationSystemConnection = mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASSWORD); if(mysql_ping(RegistrationSystemConnection) == -1) { mysql_reconnect(RegistrationSystemConnection); } } function LoginPlayer(playerid, reason) { GetPlayerName(playerid, pname, sizeof(pname)); format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE"", pname); mysql_query(stringsize, -1, -1, RegistrationSystemConnection); mysql_store_result(); new playerfilesplit[8][128], playerloadingarray[128]; mysql_fetch_row_format(playerloadingarray,"|", RegistrationSystemConnection); split(playerloadingarray, playerfilesplit, '|'); PlayerAccount[playerid][Money] = strval(playerfilesplit[3]); PlayerAccount[playerid][AdminLevel] = strval(playerfilesplit[4]); PlayerAccount[playerid][Health] = floatstr(playerfilesplit[5]); PlayerAccount[playerid][Armour] = floatstr(playerfilesplit[6]); PlayerAccount[playerid][Bank] = strval(playerfilesplit[7]); mysql_free_result(RegistrationSystemConnection); switch(reason) { case 1: { SendClientMessage(playerid, BLUE, LOGINMSSG1); } case 2: { SendClientMessage(playerid, BLUE, LOGINMSSG2); } } OnPlayerLogin(playerid); } function RegisterPlayer(playerid, inputtedpassword[]) { GetPlayerName(playerid, pname, sizeof(pname)); //GetPlayerIp(playerid, AccountData[playerid][pip], sizeof(AccountData[playerid][pip]) ); new plrIP[20]; GetPlayerIp(playerid, plrIP, sizeof(plrIP)); GetPlayerHealth(playerid, AccountData[playerid][pHealth]); GetPlayerArmour(playerid, AccountData[playerid][pArmour]); format(stringsize, sizeof(stringsize), "INSERT INTO "SQL_TABLE" (Name, Password, IP, Money, AdminLevel, Health, Armour, Bank) VALUES('%s', md5('%s'), '%s', %d, 0, '%f', '%f', 0)", pname, inputtedpassword, plrIP, GetPlayerMoney(playerid), AccountData[playerid][pHealth], AccountData[playerid][pArmour]); mysql_query(stringsize, SQL_REGISTER_PLAYER, playerid, RegistrationSystemConnection); OnPlayerRegister(playerid); } function split(const strsrc[], strdest[][], delimiter) { new i, li; new aNum; new len; while(i <= strlen(strsrc)){ if(strsrc[i]==delimiter || i==strlen(strsrc)){ len = strmid(strdest[aNum], strsrc, li, i, 128); strdest[aNum][len] = 0; li = i+1; aNum++; } i++; } return 1; } public OnFilterScriptInit() { ConnectToDB(); } public OnFilterScriptExit() { mysql_close(RegistrationSystemConnection); } public OnPlayerConnect(playerid) { GetPlayerName(playerid, pname, sizeof(pname)); format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s'", pname); // mysql_query(stringsize, SQL_CHECK_ACCOUNT, playerid, RegistrationSystemConnection); mysql_query(stringsize, -1,-1, RegistrationSystemConnection); mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 0) { AccountData[playerid][Account] = true; } else AccountData[playerid][Account] = false; mysql_free_result(RegistrationSystemConnection); #if defined AutoLogin { if(AccountData[playerid][Account] == true) { new plrIP[20]; GetPlayerIp(playerid, plrIP, sizeof(plrIP)); format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s' AND IP = '%s'", pname, plrIP); mysql_query(stringsize, -1, -1, RegistrationSystemConnection); mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 0) { LoginPlayer(playerid, 2); } else return SendClientMessage(playerid, BLUE, ERROR_MESSAGE1); mysql_free_result(RegistrationSystemConnection); } } #endif #if defined Force_Login { if(AccountData[playerid][Account] == true) { ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); } else ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel"); } #endif return 1; } public OnPlayerDisconnect(playerid) { SavePlayerAccount(playerid); } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { switch(dialogid) { case 1: { if(!strlen(inputtext)) return ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); if(!response) return ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); mysql_real_escape_string(inputtext, SQL_Escape[Escape], RegistrationSystemConnection); format(stringsize, sizeof(stringsize), "SELECT * FROM "SQL_TABLE" WHERE Name = '%s' AND Password = md5('%s') LIMIT 1", pname, SQL_Escape[Escape]); mysql_query(stringsize, -1, -1, RegistrationSystemConnection); mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 0) { LoginPlayer(playerid, 1); } else return SendClientMessage(playerid, BLUE, ERROR_MESSAGE2); } case 2: { if(!strlen(inputtext)) return ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel"); if(!response) return ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel"); mysql_real_escape_string(inputtext, SQL_Escape[Escape], RegistrationSystemConnection); RegisterPlayer(playerid, SQL_Escape[Escape]); } } return 0; } public OnQueryFinish( query[], resultid, extraid, connectionHandle ) { switch(resultid) { /* case SQL_CHECK_ACCOUNT: { mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 0) { AccountData[extraid][Account] = true; } else AccountData[extraid][Account] = false; mysql_free_result(RegistrationSystemConnection); } case SQL_CHECK_LOGIN: { mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 1) { LoginPlayer(extraid, 1); } else return SendClientMessage(extraid, BLUE, ERROR_MESSAGE2); mysql_free_result(RegistrationSystemConnection); } case SQL_CHECK_IP: { mysql_store_result(RegistrationSystemConnection); if(mysql_num_rows(RegistrationSystemConnection) > 0) { LoginPlayer(extraid, 2); } else return SendClientMessage(extraid, BLUE, ERROR_MESSAGE1); mysql_free_result(RegistrationSystemConnection); }*/ case SQL_REGISTER_PLAYER: { SendClientMessage(extraid, BLUE, REGMSSG); } case SQL_SAVE_ACCOUNT: { } } return 1; } command(register, playerid, params[]) { if(AccountData[playerid][Account] == false && AccountData[playerid][Logged] == false) { ShowPlayerDialog(playerid,2,DIALOG_STYLE_INPUT,"Register","Enter your password below:","Register","Cancel"); } return 1; } command(login, playerid, params[]) { if(AccountData[playerid][Account] == true && AccountData[playerid][Logged] == false) { ShowPlayerDialog(playerid,1,DIALOG_STYLE_INPUT,"Login","Enter your password below:","Login","Cancel"); } return 1; }