05.04.2017, 18:04
Hello guys,
I'm trying to save my position OnPlayerDisconnect and load it when you connect through MYSQL. When I start the server I can't seem to spawn, and no table is created for the player. Without the savepos everything works fine.
My code:
I'm trying to save my position OnPlayerDisconnect and load it when you connect through MYSQL. When I start the server I can't seem to spawn, and no table is created for the player. Without the savepos everything works fine.
My code:
Код:
enum PLAYERDATA { ID, Name[25], Password[65], Salt[11], PasswordFails, Float:PosX, Float:PosY, Float:PosZ, Kills, Deaths, Score, Cash, Cache: Player_Cache, bool:LoggedIn } new pInfo[MAX_PLAYERS][PLAYERDATA]; new MySQLOpt: option_id = mysql_init_options(); mysql_set_option(option_id, AUTO_RECONNECT, true); Database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE, option_id); if(Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0) { print("Couldn't connect to the MySQL server, closing."); SendRconCommand("exit"); return 1; } print("Connected to the MySQL server."); mysql_tquery(Database, "CREATE TABLE IF NOT EXISTS `PLAYERS` (`ID` int(11) NOT NULL AUTO_INCREMENT,`USERNAME` varchar(24) NOT NULL,`PASSWORD` char(65) NOT NULL,`SALT` char(11) NOT NULL, `POSX` float(10), `POSY` float(10), `POSZ` float(10), `SCORE` mediumint(7), `KILLS` mediumint(7), `CASH` mediumint(7) NOT NULL DEFAULT '0',`DEATHS` mediumint(7) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`), UNIQUE KEY `USERNAME` (`USERNAME`))"); public OnPlayerDisconnect(playerid, reason) { new Float:pos[4]; GetPlayerPos(playerid, pos[0], pos[1], pos[2]); Corrupt_Check[playerid]++; new DB_Query[256]; mysql_format(Database, DB_Query, sizeof(DB_Query), "UPDATE `PLAYERS` SET `POSX` = %f, `POSY` = %f, `POSZ` = %f, `SCORE` = %d, `CASH` = %d, `KILLS` = %d, `DEATHS` = %d WHERE `ID` = %d LIMIT 1", pos[0], pos[1], pos[2], pInfo[playerid][Score], pInfo[playerid][Cash], pInfo[playerid][Kills], pInfo[playerid][Deaths], pInfo[playerid][ID]); mysql_tquery(Database, DB_Query); if(cache_is_valid(pInfo[playerid][Player_Cache])) { cache_delete(pInfo[playerid][Player_Cache]); pInfo[playerid][Player_Cache] = MYSQL_INVALID_CACHE; } pInfo[playerid][LoggedIn] = false; return 1; } public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) { switch (dialogid) { case DIALOG_LOGIN: { if(!response) return Kick(playerid); new Salted_Key[65]; SHA256_PassHash(inputtext, pInfo[playerid][Salt], Salted_Key, 65); if(strcmp(Salted_Key, pInfo[playerid][Password]) == 0) { cache_set_active(pInfo[playerid][Player_Cache]); cache_get_value_int(0, "ID", pInfo[playerid][ID]); cache_get_value_int(0, "KILLS", pInfo[playerid][Kills]); cache_get_value_int(0, "DEATHS", pInfo[playerid][Deaths]); cache_get_value_int(0, "SCORE", pInfo[playerid][Score]); cache_get_value_int(0, "CASH", pInfo[playerid][Cash]); SetPlayerScore(playerid, pInfo[playerid][Score]); ResetPlayerMoney(playerid); GivePlayerMoney(playerid, pInfo[playerid][Cash]); cache_delete(pInfo[playerid][Player_Cache]); pInfo[playerid][Player_Cache] = MYSQL_INVALID_CACHE; pInfo[playerid][LoggedIn] = true; new string[128]; format(string, sizeof(string), "~w~Welcome back~y~, %s!", GetName(playerid)); GameTextForPlayer(playerid, string,4000,1); TogglePlayerSpectating(playerid, false); ForceClassSelection(playerid); SetPlayerPos(playerid, pInfo[playerid][PosX], pInfo[playerid][PosY], pInfo[playerid][PosZ]); SpawnPlayer(playerid); } else { new String[150]; pInfo[playerid][PasswordFails] += 1; if (pInfo[playerid][PasswordFails] >= 3) { format(String, sizeof(String), "%s has been kicked from the server. Reason: (%d/3) Login fails.", GetName(playerid), pInfo[playerid][PasswordFails]); SendClientMessageToAll(COLOR_RED, String); Kick(playerid); } else { ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, ""DIALOGCOLOR_WHITE"Login", ""DIALOGCOLOR_WHITE"Welcome back to "DIALOGCOLOR_YELLOW""SERVER_NAME" Roleplay"DIALOGCOLOR_WHITE".\nPlease enter your password below to login to your account:\n"DIALOGCOLOR_RED"You have entered an incorrect password! %d/3 login attempts remaining.\n", "Login", "Quit"); } } } case DIALOG_REGISTER: { if(!response) return Kick(playerid); if(strlen(inputtext) <= 4 || strlen(inputtext) > 20) { ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, ""DIALOGCOLOR_WHITE"Register", ""DIALOGCOLOR_WHITE"Welcome to "DIALOGCOLOR_YELLOW""SERVER_NAME" Roleplay."DIALOGCOLOR_WHITE"\nPlease register your account by typing a password below:\n"DIALOGCOLOR_RED"Invalid password length, your password should be between 5 - 20.", "Register", "Quit"); } else { for (new i = 0; i < 10; i++) { pInfo[playerid][Salt][i] = random(79) + 47; } pInfo[playerid][Salt][10] = 0; SHA256_PassHash(inputtext, pInfo[playerid][Salt], pInfo[playerid][Password], 65); new DB_Query[225]; mysql_format(Database, DB_Query, sizeof(DB_Query), "INSERT INTO `PLAYERS` (`USERNAME`, `PASSWORD`, `SALT`, `POSX`, `POSY`, `POSZ`, `SCORE`, `KILLS`, `CASH`, `DEATHS`)\ VALUES ('%e', '%s', '%e', '258.4893', '-41.8008', '1002.0234', '1', '0', '0', '0')", pInfo[playerid][Name], pInfo[playerid][Password], pInfo[playerid][Salt]); mysql_tquery(Database, DB_Query, "OnPlayerRegister", "d", playerid); TogglePlayerSpectating(playerid, false); ForceClassSelection(playerid); SetPlayerPos(playerid, 258.4893,-41.8008,1002.0234); SpawnPlayer(playerid); } } } return 1; }