Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a_samp>
- // change MAX_PLAYERS to the amount of players (slots) you want
- // It is by default 1000 (as of 0.3.7 version)
- #undef MAX_PLAYERS
- #define MAX_PLAYERS 100
- #include <a_mysql>
- #include <sscanf2>
- #include <zcmd>
- #define _DEBUG 6
- #include <YSI\y_debug>
- #include <YSI\y_text>
- #include <YSI\y_languages>
- #include <YSI\y_inline>
- #include <YSI\y_master>
- // MySQL configuration
- #define MYSQL_HOST "127.0.0.1"
- #define MYSQL_USER "root"
- #define MYSQL_PASSWORD ""
- #define MYSQL_DATABASE "tdm"
- // how many seconds until it kicks the player for taking too long to login
- #define SECONDS_TO_LOGIN 60
- // default spawn point: Las Venturas (The High Roller)
- /*#define DEFAULT_POS_X 1958.3783
- #define DEFAULT_POS_Y 1343.1572
- #define DEFAULT_POS_Z 15.3746
- #define DEFAULT_POS_A 270.1425*/
- loadtext mensajes[todoxd];
- // MySQL connection handle
- new MySQL: g_SQL;
- // player data
- enum E_PLAYERS
- {
- ORM: ORM_ID,
- ID,
- Name[MAX_PLAYER_NAME],
- Password[65], // the output of SHA256_PassHash function (which was added in 0.3.7 R1 version) is always 256 bytes in length, or the equivalent of 64 Pawn cells
- Dinero,
- Admin,
- Muertes,
- Victimas,
- Baneado,
- Razon,
- Idioma,
- bool: IsLoggedIn,
- LoginAttempts,
- LoginTimer
- };
- new Player[MAX_PLAYERS][E_PLAYERS];
- enum E_EQUIPOS
- {
- eID,
- eNombre[40],
- Float:eSpawn[4],
- eSkin[3],
- eColor,
- eColorHex[8]
- };
- //10 = Maximo de Equipos
- new Equipos[10][E_EQUIPOS];
- new CuantosEquipos; //esto sera para saber cuantos equipos
- new
- Language:gLEspanol,
- Language:gLEnglish;
- new g_MysqlRaceCheck[MAX_PLAYERS];
- //dialog data
- enum
- {
- DIALOG_UNUSED,
- DIALOG_LOGIN,
- DIALOG_REGISTER,
- DIALOG_EQUIPO
- };
- main() {}
- public OnGameModeInit()
- {
- new MySQLOpt: option_id = mysql_init_options();
- mysql_set_option(option_id, AUTO_RECONNECT, true); // it automatically reconnects when loosing connection to mysql server
- g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id); // AUTO_RECONNECT is enabled for this connection handle only
- if (g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
- {
- print("MySQL connection failed. Server is shutting down.");
- SendRconCommand("exit"); // close the server if there is no connection
- return 1;
- }
- print("MySQL connection is successful.");
- SetGameModeText("TDM-Dev");
- gLEnglish = Langs_Add("ES", "Español");
- gLEnglish = Langs_Add("EN", "English");
- CargarEquipos();
- DisableInteriorEnterExits();
- // if the table has been created, the "SetupPlayerTable" function does not have any purpose so you may remove it completely
- //SetupPlayerTable();
- return 1;
- }
- public OnGameModeExit()
- {
- // save all player data before closing connection
- for (new i = 0, j = GetPlayerPoolSize(); i <= j; i++) // GetPlayerPoolSize function was added in 0.3.7 version and gets the highest playerid currently in use on the server
- {
- if (IsPlayerConnected(i))
- {
- // reason is set to 1 for normal 'Quit'
- OnPlayerDisconnect(i, 1);
- }
- }
- mysql_close(g_SQL);
- return 1;
- }
- public OnPlayerConnect(playerid)
- {
- Langs_SetPlayerLanguage(playerid, gLEspanol);
- g_MysqlRaceCheck[playerid]++;
- // reset player data
- static const empty_player[E_PLAYERS];
- Player[playerid] = empty_player;
- GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
- // create orm instance and register all needed variables
- new ORM: ormid = Player[playerid][ORM_ID] = orm_create("usuarios", g_SQL);
- orm_addvar_int(ormid, Player[playerid][ID], "ID");
- orm_addvar_string(ormid, Player[playerid][Name], MAX_PLAYER_NAME, "Nombre");
- orm_addvar_string(ormid, Player[playerid][Password], 65, "Password");
- orm_addvar_int(ormid, Player[playerid][Dinero], "Dinero");
- orm_addvar_int(ormid, Player[playerid][Admin], "Admin");
- orm_addvar_int(ormid, Player[playerid][Muertes], "Muertes");
- orm_addvar_int(ormid, Player[playerid][Victimas], "Victimas");
- orm_addvar_int(ormid, Player[playerid][Baneado], "Ban");
- orm_addvar_string(ormid, Player[playerid][Razon], 65, "Razon");
- //orm_addvar_float(ormid, Player[playerid][X_Pos], "x");
- orm_setkey(ormid, "Nombre");
- // tell the orm system to load all data, assign it to our variables and call our callback when ready
- orm_load(ormid, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);
- return 1;
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- g_MysqlRaceCheck[playerid]++;
- UpdatePlayerData(playerid);
- // if the player was kicked before the time expires (30 seconds), kill the timer
- if (Player[playerid][LoginTimer])
- {
- KillTimer(Player[playerid][LoginTimer]);
- Player[playerid][LoginTimer] = 0;
- }
- // sets "IsLoggedIn" to false when the player disconnects, it prevents from saving the player data twice when "gmx" is used
- Player[playerid][IsLoggedIn] = false;
- return 1;
- }
- public OnPlayerSpawn(playerid)
- {
- // spawn the player to their last saved position
- /*SetPlayerInterior(playerid, 0);
- SetPlayerPos(playerid, DEFAULT_POS_X, DEFAULT_POS_Y, DEFAULT_POS_Z);
- SetPlayerFacingAngle(playerid, DEFAULT_POS_A);*/
- SetCameraBehindPlayer(playerid);
- return 1;
- }
- public OnPlayerDeath(playerid, killerid, reason)
- {
- UpdatePlayerDeaths(playerid);
- UpdatePlayerKills(killerid);
- SendDeathMessage(killerid, playerid, reason);
- return 1;
- }
- //-----------------------------------------------------
- // DIALOGS
- forward DialogUnused(pid, dialogid, response, listitem, string:inputtext[]);
- public DialogUnused(pid, dialogid, response, listitem, string:inputtext[])
- {
- return 1;
- }
- forward DialogLogin(playerid, dialogid, response, listitem, string:inputtext[]);
- public DialogLogin(playerid, dialogid, response, listitem, string:inputtext[])
- {
- if (!response) return Kick(playerid);
- if (strcmp(inputtext, Player[playerid][Password]) == 0)
- {
- if(Player[playerid][Baneado])
- {
- Text_MessageBox(playerid, DialogUnused, $TITULO_BANEADO, $DIALOG_LOGIN_BANEADO, $BOTON_SALIR, "", Player[playerid][Razon]);
- DelayedKick(playerid);
- return 1;
- }
- //correct password, spawn the player
- Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $INICIO_SESION_EXITO, $BOTON_OK, "");
- KillTimer(Player[playerid][LoginTimer]);
- Player[playerid][LoginTimer] = 0;
- Player[playerid][IsLoggedIn] = true;
- // spawn the player to their last saved position after login
- SeleccionarEquipo(playerid);
- }
- else
- {
- Player[playerid][LoginAttempts]++;
- if (Player[playerid][LoginAttempts] >= 3)
- {
- Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $DIALOG_LOGIN_PASS_KICK, $BOTON_SALIR, "");
- DelayedKick(playerid);
- }
- else Text_PasswordBox(playerid, DialogLogin, $TITULO_LOGIN, $DIALOG_LOGIN_PASS_INCORRECTA, $BOTON_LOGIN, $BOTON_SALIR);
- }
- return 1;
- }
- forward DialogRegistro(playerid, dialogid, response, listitem, string:inputtext[]);
- public DialogRegistro(playerid, dialogid, response, listitem, string:inputtext[])
- {
- if (!response) return Kick(playerid);
- if (strlen(inputtext) <= 5) return Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO_PASS_CORTA, $BOTON_REGISTRARSE, $BOTON_SALIR);
- if (strlen(inputtext) >= 65) return Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO_PASS_CORTA, $BOTON_REGISTRARSE, $BOTON_SALIR);
- format(Player[playerid][Password], 65, "%s", inputtext[0]);
- // sends an query
- orm_save(Player[playerid][ORM_ID], "OnPlayerRegister", "d", playerid);
- return 1;
- }
- forward DialogEquipo(playerid, dialogid, response, listitem, string:inputtext[]);
- public DialogEquipo(playerid, dialogid, response, listitem, string:inputtext[])
- {
- if(listitem > 3) return 1;
- Text_Send(playerid, $EQUIPO_SELECCIONADO, Equipos[listitem][eNombre]);
- new skinr = random(3);
- SetSpawnInfo(playerid, listitem, Equipos[listitem][eSkin][skinr], Equipos[listitem][eSpawn][0], Equipos[listitem][eSpawn][1], Equipos[listitem][eSpawn][2], Equipos[listitem][eSpawn][3], 0, 0, 0, 0, 0, 0);
- SpawnPlayer(playerid);
- SetPlayerColor(playerid, Equipos[listitem][eColor]);
- return 1;
- }
- forward DialogIdiomaR(playerid, dialogid, response, listitem, string:inputtext[]);
- public DialogIdiomaR(playerid, dialogid, response, listitem, string:inputtext[])
- {
- if(response)
- {
- Langs_SetPlayerLanguage(playerid, gLEspanol);
- Player[playerid][Idioma] = 0;
- }
- else
- {
- Langs_SetPlayerLanguage(playerid, gLEnglish);
- Player[playerid][Idioma] = 1;
- }
- Text_Send(playerid, $SELECCIONADO_IDIOMA);
- Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO, $BOTON_REGISTRARSE, $BOTON_SALIR, Player[playerid][Name]);
- return 1;
- }
- forward DialogIdioma(playerid, dialogid, response, listitem, string:inputtext[]);
- public DialogIdioma(playerid, dialogid, response, listitem, string:inputtext[])
- {
- if(response)
- {
- Langs_SetPlayerLanguage(playerid, gLEspanol);
- Player[playerid][Idioma] = 0;
- }
- else
- {
- Langs_SetPlayerLanguage(playerid, gLEnglish);
- Player[playerid][Idioma] = 1;
- }
- Text_Send(playerid, $SELECCIONADO_IDIOMA);
- return 1;
- }
- //-----------------------------------------------------
- forward OnPlayerDataLoaded(playerid, race_check);
- public OnPlayerDataLoaded(playerid, race_check)
- {
- /* race condition check:
- player A connects -> SELECT query is fired -> this query takes very long
- while the query is still processing, player A with playerid 2 disconnects
- player B joins now with playerid 2 -> our laggy SELECT query is finally finished, but for the wrong player
- what do we do against it?
- we create a connection count for each playerid and increase it everytime the playerid connects or disconnects
- we also pass the current value of the connection count to our OnPlayerDataLoaded callback
- then we check if current connection count is the same as connection count we passed to the callback
- if yes, everything is okay, if not, we just kick the player
- */
- if (race_check != g_MysqlRaceCheck[playerid]) return Kick(playerid);
- orm_setkey(Player[playerid][ORM_ID], "ID");
- switch (orm_errno(Player[playerid][ORM_ID]))
- {
- case ERROR_OK:
- {
- switch(Player[playerid][Idioma])
- {
- case 0:
- {
- Langs_SetPlayerLanguage(playerid, gLEspanol);
- }
- case 1:
- {
- Langs_SetPlayerLanguage(playerid, gLEnglish);
- }
- }
- Text_PasswordBox(playerid, DialogLogin, $TITULO_LOGIN, $DIALOG_LOGIN, $BOTON_LOGIN, $BOTON_SALIR, Player[playerid][Name]);
- // from now on, the player has 60 seconds to login
- Player[playerid][LoginTimer] = SetTimerEx("OnLoginTimeout", SECONDS_TO_LOGIN * 1000, false, "d", playerid);
- }
- case ERROR_NO_DATA:
- {
- Text_MessageBox(playerid, DialogIdiomaR, $TITULO_SELECCION_IDIOMA, $SELECCION_IDIOMA, $IDIOMA_ESPAÑOL, $IDIOMA_INGLES);
- }
- }
- return 1;
- }
- forward OnLoginTimeout(playerid);
- public OnLoginTimeout(playerid)
- {
- // reset the variable that stores the timerid
- Player[playerid][LoginTimer] = 0;
- Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $TIMEOUT_LOGIN, $BOTON_SALIR, "");
- DelayedKick(playerid);
- return 1;
- }
- forward OnPlayerRegister(playerid);
- public OnPlayerRegister(playerid)
- {
- Text_MessageBox(playerid, DialogUnused, $TITULO_REGISTRO, $REGISTRO_EXITO, $BOTON_OK, "");
- Player[playerid][IsLoggedIn] = true;
- SeleccionarEquipo(playerid);
- return 1;
- }
- forward _KickPlayerDelayed(playerid);
- public _KickPlayerDelayed(playerid)
- {
- Kick(playerid);
- return 1;
- }
- forward CargarVehiculos();
- public CargarVehiculos()
- {
- new query[256];
- format(query, sizeof(query), "SELECT * FROM `vehicles`");
- mysql_tquery(g_SQL, query, "CargarAutos", "i", 3);
- return 1;
- }
- forward CargarEquipos();
- public CargarEquipos()
- {
- new query[200];
- format(query, sizeof(query), "SELECT * FROM `equipos`");
- mysql_tquery(g_SQL, query, "EquiposCargados", "i", 3);
- return 1;
- }
- forward EquiposCargados(resultid, extraid, ConnectionHandle);
- public EquiposCargados(resultid, extraid, ConnectionHandle)
- {
- new Rows;
- if(resultid != 0)
- {
- cache_get_row_count(Rows);
- }
- switch(resultid)
- {
- case 3:
- {
- for( new i = 0; i < Rows; i++ )
- {
- cache_get_value_name_int(i, "ID", Equipos[i][eID]);
- cache_get_value_name(i, "Nombre", Equipos[i][eNombre]);
- cache_get_value_name_float(i, "SpawnX", Equipos[i][eSpawn][0]);
- cache_get_value_name_float(i, "SpawnY", Equipos[i][eSpawn][1]);
- cache_get_value_name_float(i, "SpawnZ", Equipos[i][eSpawn][2]);
- cache_get_value_name_float(i, "SpawnA", Equipos[i][eSpawn][3]);
- cache_get_value_name_int(i, "Skin1", Equipos[i][eSkin][0]);
- cache_get_value_name_int(i, "Skin2", Equipos[i][eSkin][1]);
- cache_get_value_name_int(i, "Skin3", Equipos[i][eSkin][2]);
- cache_get_value_name_int(i, "Color", Equipos[i][eColor]);
- cache_get_value_name(i, "ColorHex", Equipos[i][eColorHex]);
- }
- CuantosEquipos = Rows;
- }
- }
- return 1;
- }
- //-----------------------------------------------------
- SeleccionarEquipo(playerid)
- {
- new string[500];
- format(string,sizeof(string), "{%s}%s \n", Equipos[0][eColorHex], Equipos[0][eNombre]);
- for( new i = 1; i < CuantosEquipos; i++ )
- {
- format(string,sizeof(string), "%s{%s}%s \n",string, Equipos[i][eColorHex], Equipos[i][eNombre]);
- }
- Text_ListBox(playerid, DialogEquipo, $TITULO_SELECCION_EQUIPO, string, $BOTON_SELECCIONAR, "");
- return 1;
- }
- DelayedKick(playerid, time = 500)
- {
- SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
- return 1;
- }
- UpdatePlayerData(playerid)
- {
- if (Player[playerid][IsLoggedIn] == false) return 0;
- // if the client crashed, it's not possible to get the player's position in OnPlayerDisconnect callback
- // so we will use the last saved position (in case of a player who registered and crashed/kicked, the position will be the default spawn point)
- // it is important to store everything in the variables registered in ORM instance
- // orm_save sends an UPDATE query
- orm_save(Player[playerid][ORM_ID]);
- orm_destroy(Player[playerid][ORM_ID]);
- return 1;
- }
- UpdatePlayerDeaths(playerid)
- {
- if (Player[playerid][IsLoggedIn] == false) return 0;
- Player[playerid][Muertes]++;
- Player[playerid][Dinero] -= 500;
- orm_update(Player[playerid][ORM_ID]);
- return 1;
- }
- UpdatePlayerKills(killerid)
- {
- // we must check before if the killer wasn't valid (connected) player to avoid run time error 4
- if (killerid == INVALID_PLAYER_ID) return 0;
- if (Player[killerid][IsLoggedIn] == false) return 0;
- Player[killerid][Victimas]++;
- Player[killerid][Dinero] += 500;
- orm_update(Player[killerid][ORM_ID]);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement