Advertisement
Guest User

TDM.pwn

a guest
Jul 25th, 2017
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.53 KB | None | 0 0
  1. #include    <a_samp>
  2.  
  3. // change MAX_PLAYERS to the amount of players (slots) you want
  4. // It is by default 1000 (as of 0.3.7 version)
  5. #undef      MAX_PLAYERS
  6. #define     MAX_PLAYERS         100
  7.  
  8. #include    <a_mysql>
  9. #include    <sscanf2>
  10. #include    <zcmd>
  11.  
  12. #define _DEBUG 6
  13.  
  14. #include <YSI\y_debug>
  15. #include <YSI\y_text>
  16. #include <YSI\y_languages>
  17. #include <YSI\y_inline>
  18. #include <YSI\y_master>
  19.  
  20.  
  21. // MySQL configuration
  22. #define     MYSQL_HOST          "127.0.0.1"
  23. #define     MYSQL_USER          "root"
  24. #define     MYSQL_PASSWORD      ""
  25. #define     MYSQL_DATABASE      "tdm"
  26.  
  27. // how many seconds until it kicks the player for taking too long to login
  28. #define  SECONDS_TO_LOGIN       60
  29.  
  30. // default spawn point: Las Venturas (The High Roller)
  31. /*#define   DEFAULT_POS_X       1958.3783
  32. #define     DEFAULT_POS_Y       1343.1572
  33. #define     DEFAULT_POS_Z       15.3746
  34. #define     DEFAULT_POS_A       270.1425*/
  35.  
  36. loadtext mensajes[todoxd];
  37.  
  38. // MySQL connection handle
  39. new MySQL: g_SQL;
  40.  
  41. // player data
  42. enum E_PLAYERS
  43. {
  44.     ORM: ORM_ID,
  45.  
  46.     ID,
  47.     Name[MAX_PLAYER_NAME],
  48.     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
  49.     Dinero,
  50.     Admin,
  51.     Muertes,
  52.     Victimas,
  53.     Baneado,
  54.     Razon,
  55.     Idioma,
  56.    
  57.  
  58.     bool: IsLoggedIn,
  59.     LoginAttempts,
  60.     LoginTimer
  61. };
  62. new Player[MAX_PLAYERS][E_PLAYERS];
  63.  
  64. enum E_EQUIPOS
  65. {
  66.     eID,
  67.     eNombre[40],
  68.     Float:eSpawn[4],
  69.     eSkin[3],
  70.     eColor,
  71.     eColorHex[8]
  72. };
  73.  
  74. //10 = Maximo de Equipos
  75. new Equipos[10][E_EQUIPOS];
  76. new CuantosEquipos; //esto sera para saber cuantos equipos
  77.  
  78. new
  79.     Language:gLEspanol,
  80.     Language:gLEnglish;
  81.  
  82. new g_MysqlRaceCheck[MAX_PLAYERS];
  83.  
  84. //dialog data
  85. enum
  86. {
  87.     DIALOG_UNUSED,
  88.  
  89.     DIALOG_LOGIN,
  90.     DIALOG_REGISTER,
  91.     DIALOG_EQUIPO
  92. };
  93.  
  94. main() {}
  95.  
  96. public OnGameModeInit()
  97. {
  98.     new MySQLOpt: option_id = mysql_init_options();
  99.  
  100.     mysql_set_option(option_id, AUTO_RECONNECT, true); // it automatically reconnects when loosing connection to mysql server
  101.  
  102.     g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id); // AUTO_RECONNECT is enabled for this connection handle only
  103.     if (g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
  104.     {
  105.         print("MySQL connection failed. Server is shutting down.");
  106.         SendRconCommand("exit"); // close the server if there is no connection
  107.         return 1;
  108.     }
  109.  
  110.     print("MySQL connection is successful.");
  111.     SetGameModeText("TDM-Dev");
  112.    
  113.     gLEnglish = Langs_Add("ES", "Español");
  114.     gLEnglish = Langs_Add("EN", "English");
  115.    
  116.     CargarEquipos();
  117.     DisableInteriorEnterExits();
  118.     // if the table has been created, the "SetupPlayerTable" function does not have any purpose so you may remove it completely
  119.     //SetupPlayerTable();
  120.     return 1;
  121. }
  122.  
  123. public OnGameModeExit()
  124. {
  125.     // save all player data before closing connection
  126.     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
  127.     {
  128.         if (IsPlayerConnected(i))
  129.         {
  130.             // reason is set to 1 for normal 'Quit'
  131.             OnPlayerDisconnect(i, 1);
  132.         }
  133.     }
  134.  
  135.     mysql_close(g_SQL);
  136.     return 1;
  137. }
  138.  
  139. public OnPlayerConnect(playerid)
  140. {
  141.     Langs_SetPlayerLanguage(playerid, gLEspanol);
  142.     g_MysqlRaceCheck[playerid]++;
  143.  
  144.     // reset player data
  145.     static const empty_player[E_PLAYERS];
  146.     Player[playerid] = empty_player;
  147.  
  148.     GetPlayerName(playerid, Player[playerid][Name], MAX_PLAYER_NAME);
  149.  
  150.     // create orm instance and register all needed variables
  151.     new ORM: ormid = Player[playerid][ORM_ID] = orm_create("usuarios", g_SQL);
  152.  
  153.     orm_addvar_int(ormid, Player[playerid][ID], "ID");
  154.     orm_addvar_string(ormid, Player[playerid][Name], MAX_PLAYER_NAME, "Nombre");
  155.     orm_addvar_string(ormid, Player[playerid][Password], 65, "Password");
  156.     orm_addvar_int(ormid, Player[playerid][Dinero], "Dinero");
  157.     orm_addvar_int(ormid, Player[playerid][Admin], "Admin");
  158.     orm_addvar_int(ormid, Player[playerid][Muertes], "Muertes");
  159.     orm_addvar_int(ormid, Player[playerid][Victimas], "Victimas");
  160.     orm_addvar_int(ormid, Player[playerid][Baneado], "Ban");
  161.     orm_addvar_string(ormid, Player[playerid][Razon], 65, "Razon");
  162.     //orm_addvar_float(ormid, Player[playerid][X_Pos], "x");
  163.    
  164.     orm_setkey(ormid, "Nombre");
  165.  
  166.     // tell the orm system to load all data, assign it to our variables and call our callback when ready
  167.     orm_load(ormid, "OnPlayerDataLoaded", "dd", playerid, g_MysqlRaceCheck[playerid]);
  168.     return 1;
  169. }
  170.  
  171. public OnPlayerDisconnect(playerid, reason)
  172. {
  173.     g_MysqlRaceCheck[playerid]++;
  174.  
  175.     UpdatePlayerData(playerid);
  176.  
  177.     // if the player was kicked before the time expires (30 seconds), kill the timer
  178.     if (Player[playerid][LoginTimer])
  179.     {
  180.         KillTimer(Player[playerid][LoginTimer]);
  181.         Player[playerid][LoginTimer] = 0;
  182.     }
  183.  
  184.     // sets "IsLoggedIn" to false when the player disconnects, it prevents from saving the player data twice when "gmx" is used
  185.     Player[playerid][IsLoggedIn] = false;
  186.     return 1;
  187. }
  188.  
  189. public OnPlayerSpawn(playerid)
  190. {
  191.     // spawn the player to their last saved position
  192.     /*SetPlayerInterior(playerid, 0);
  193.     SetPlayerPos(playerid, DEFAULT_POS_X, DEFAULT_POS_Y, DEFAULT_POS_Z);
  194.     SetPlayerFacingAngle(playerid, DEFAULT_POS_A);*/
  195.  
  196.     SetCameraBehindPlayer(playerid);
  197.     return 1;
  198. }
  199.  
  200. public OnPlayerDeath(playerid, killerid, reason)
  201. {
  202.     UpdatePlayerDeaths(playerid);
  203.     UpdatePlayerKills(killerid);
  204.     SendDeathMessage(killerid, playerid, reason);
  205.     return 1;
  206. }
  207.  
  208. //-----------------------------------------------------
  209. //              DIALOGS
  210. forward DialogUnused(pid, dialogid, response, listitem, string:inputtext[]);
  211. public DialogUnused(pid, dialogid, response, listitem, string:inputtext[])
  212. {
  213.     return 1;
  214. }
  215.  
  216. forward DialogLogin(playerid, dialogid, response, listitem, string:inputtext[]);
  217. public DialogLogin(playerid, dialogid, response, listitem, string:inputtext[])
  218. {
  219.     if (!response) return Kick(playerid);
  220.     if (strcmp(inputtext, Player[playerid][Password]) == 0)
  221.     {
  222.         if(Player[playerid][Baneado])
  223.         {
  224.             Text_MessageBox(playerid, DialogUnused, $TITULO_BANEADO, $DIALOG_LOGIN_BANEADO, $BOTON_SALIR, "", Player[playerid][Razon]);
  225.             DelayedKick(playerid);
  226.             return 1;
  227.         }
  228.            
  229.         //correct password, spawn the player
  230.         Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $INICIO_SESION_EXITO, $BOTON_OK, "");
  231.  
  232.         KillTimer(Player[playerid][LoginTimer]);
  233.         Player[playerid][LoginTimer] = 0;
  234.         Player[playerid][IsLoggedIn] = true;
  235.                
  236.  
  237.         // spawn the player to their last saved position after login
  238.         SeleccionarEquipo(playerid);
  239.     }
  240.     else
  241.     {
  242.         Player[playerid][LoginAttempts]++;
  243.         if (Player[playerid][LoginAttempts] >= 3)
  244.         {
  245.             Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $DIALOG_LOGIN_PASS_KICK, $BOTON_SALIR, "");
  246.             DelayedKick(playerid);
  247.         }
  248.         else Text_PasswordBox(playerid, DialogLogin, $TITULO_LOGIN, $DIALOG_LOGIN_PASS_INCORRECTA, $BOTON_LOGIN, $BOTON_SALIR);
  249.     }
  250.     return 1;
  251. }
  252.  
  253. forward DialogRegistro(playerid, dialogid, response, listitem, string:inputtext[]);
  254. public DialogRegistro(playerid, dialogid, response, listitem, string:inputtext[])
  255. {
  256.     if (!response) return Kick(playerid);
  257.     if (strlen(inputtext) <= 5) return Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO_PASS_CORTA, $BOTON_REGISTRARSE, $BOTON_SALIR);
  258.     if (strlen(inputtext) >= 65) return Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO_PASS_CORTA, $BOTON_REGISTRARSE, $BOTON_SALIR);
  259.  
  260.     format(Player[playerid][Password], 65, "%s", inputtext[0]);
  261.  
  262.     // sends an      query
  263.     orm_save(Player[playerid][ORM_ID], "OnPlayerRegister", "d", playerid);
  264.     return 1;
  265. }
  266.  
  267. forward DialogEquipo(playerid, dialogid, response, listitem, string:inputtext[]);
  268. public DialogEquipo(playerid, dialogid, response, listitem, string:inputtext[])
  269. {
  270.     if(listitem > 3) return 1;
  271.     Text_Send(playerid, $EQUIPO_SELECCIONADO, Equipos[listitem][eNombre]);
  272.    
  273.     new skinr = random(3);
  274.  
  275.     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);
  276.     SpawnPlayer(playerid);
  277.     SetPlayerColor(playerid, Equipos[listitem][eColor]);
  278.  
  279.     return 1;
  280. }
  281.  
  282. forward DialogIdiomaR(playerid, dialogid, response, listitem, string:inputtext[]);
  283. public DialogIdiomaR(playerid, dialogid, response, listitem, string:inputtext[])
  284. {
  285.     if(response)
  286.     {
  287.         Langs_SetPlayerLanguage(playerid, gLEspanol);
  288.         Player[playerid][Idioma] = 0;
  289.     }
  290.     else
  291.     {
  292.         Langs_SetPlayerLanguage(playerid, gLEnglish);
  293.         Player[playerid][Idioma] = 1;
  294.     }
  295.     Text_Send(playerid, $SELECCIONADO_IDIOMA);
  296.     Text_PasswordBox(playerid, DialogRegistro, $TITULO_REGISTRO, $DIALOG_REGISTRO, $BOTON_REGISTRARSE, $BOTON_SALIR, Player[playerid][Name]);
  297.     return 1;
  298. }
  299.  
  300. forward DialogIdioma(playerid, dialogid, response, listitem, string:inputtext[]);
  301. public DialogIdioma(playerid, dialogid, response, listitem, string:inputtext[])
  302. {
  303.     if(response)
  304.     {
  305.         Langs_SetPlayerLanguage(playerid, gLEspanol);
  306.         Player[playerid][Idioma] = 0;
  307.     }
  308.     else
  309.     {
  310.         Langs_SetPlayerLanguage(playerid, gLEnglish);
  311.         Player[playerid][Idioma] = 1;
  312.     }
  313.     Text_Send(playerid, $SELECCIONADO_IDIOMA);
  314.     return 1;
  315. }
  316.  
  317. //-----------------------------------------------------
  318.  
  319.  
  320. forward OnPlayerDataLoaded(playerid, race_check);
  321. public OnPlayerDataLoaded(playerid, race_check)
  322. {
  323.     /*  race condition check:
  324.         player A connects -> SELECT query is fired -> this query takes very long
  325.         while the query is still processing, player A with playerid 2 disconnects
  326.         player B joins now with playerid 2 -> our laggy SELECT query is finally finished, but for the wrong player
  327.  
  328.         what do we do against it?
  329.         we create a connection count for each playerid and increase it everytime the playerid connects or disconnects
  330.         we also pass the current value of the connection count to our OnPlayerDataLoaded callback
  331.         then we check if current connection count is the same as connection count we passed to the callback
  332.         if yes, everything is okay, if not, we just kick the player
  333.     */
  334.     if (race_check != g_MysqlRaceCheck[playerid]) return Kick(playerid);
  335.  
  336.     orm_setkey(Player[playerid][ORM_ID], "ID");
  337.  
  338.     switch (orm_errno(Player[playerid][ORM_ID]))
  339.     {
  340.         case ERROR_OK:
  341.         {
  342.             switch(Player[playerid][Idioma])
  343.             {
  344.                 case 0:
  345.                 {
  346.                     Langs_SetPlayerLanguage(playerid, gLEspanol);
  347.                 }
  348.                 case 1:
  349.                 {
  350.                     Langs_SetPlayerLanguage(playerid, gLEnglish);
  351.                 }
  352.             }
  353.             Text_PasswordBox(playerid, DialogLogin, $TITULO_LOGIN, $DIALOG_LOGIN, $BOTON_LOGIN, $BOTON_SALIR, Player[playerid][Name]);
  354.  
  355.             // from now on, the player has 60 seconds to login
  356.             Player[playerid][LoginTimer] = SetTimerEx("OnLoginTimeout", SECONDS_TO_LOGIN * 1000, false, "d", playerid);
  357.         }
  358.         case ERROR_NO_DATA:
  359.         {
  360.             Text_MessageBox(playerid, DialogIdiomaR, $TITULO_SELECCION_IDIOMA, $SELECCION_IDIOMA, $IDIOMA_ESPAÑOL, $IDIOMA_INGLES);
  361.         }
  362.     }
  363.     return 1;
  364. }
  365.  
  366. forward OnLoginTimeout(playerid);
  367. public OnLoginTimeout(playerid)
  368. {
  369.     // reset the variable that stores the timerid
  370.     Player[playerid][LoginTimer] = 0;
  371.  
  372.     Text_MessageBox(playerid, DialogUnused, $TITULO_LOGIN, $TIMEOUT_LOGIN, $BOTON_SALIR, "");
  373.     DelayedKick(playerid);
  374.     return 1;
  375. }
  376.  
  377. forward OnPlayerRegister(playerid);
  378. public OnPlayerRegister(playerid)
  379. {
  380.     Text_MessageBox(playerid, DialogUnused, $TITULO_REGISTRO, $REGISTRO_EXITO, $BOTON_OK, "");
  381.  
  382.     Player[playerid][IsLoggedIn] = true;
  383.  
  384.     SeleccionarEquipo(playerid);
  385.     return 1;
  386. }
  387.  
  388.  
  389.  
  390. forward _KickPlayerDelayed(playerid);
  391. public _KickPlayerDelayed(playerid)
  392. {
  393.     Kick(playerid);
  394.     return 1;
  395. }
  396.  
  397. forward CargarVehiculos();
  398. public CargarVehiculos()
  399. {
  400.     new query[256];
  401.     format(query, sizeof(query), "SELECT * FROM `vehicles`");
  402.     mysql_tquery(g_SQL, query, "CargarAutos", "i", 3);
  403.     return 1;
  404. }
  405.  
  406. forward CargarEquipos();
  407. public CargarEquipos()
  408. {
  409.     new query[200];
  410.     format(query, sizeof(query), "SELECT * FROM `equipos`");
  411.     mysql_tquery(g_SQL, query, "EquiposCargados", "i", 3);
  412.     return 1;
  413. }
  414.  
  415. forward EquiposCargados(resultid, extraid, ConnectionHandle);
  416. public EquiposCargados(resultid, extraid, ConnectionHandle)
  417. {
  418.     new Rows;
  419.     if(resultid != 0)
  420.     {
  421.         cache_get_row_count(Rows);
  422.     }
  423.     switch(resultid)
  424.     {
  425.         case 3:
  426.         {
  427.             for( new i = 0; i < Rows; i++ )
  428.             {
  429.                 cache_get_value_name_int(i, "ID", Equipos[i][eID]);
  430.                 cache_get_value_name(i, "Nombre", Equipos[i][eNombre]);
  431.                 cache_get_value_name_float(i, "SpawnX", Equipos[i][eSpawn][0]);
  432.                 cache_get_value_name_float(i, "SpawnY", Equipos[i][eSpawn][1]);
  433.                 cache_get_value_name_float(i, "SpawnZ", Equipos[i][eSpawn][2]);
  434.                 cache_get_value_name_float(i, "SpawnA", Equipos[i][eSpawn][3]);
  435.                 cache_get_value_name_int(i, "Skin1", Equipos[i][eSkin][0]);
  436.                 cache_get_value_name_int(i, "Skin2", Equipos[i][eSkin][1]);
  437.                 cache_get_value_name_int(i, "Skin3", Equipos[i][eSkin][2]);
  438.                 cache_get_value_name_int(i, "Color", Equipos[i][eColor]);
  439.                 cache_get_value_name(i, "ColorHex", Equipos[i][eColorHex]);
  440.             }
  441.             CuantosEquipos = Rows;
  442.         }
  443.     }
  444.     return 1;  
  445. }
  446.  
  447. //-----------------------------------------------------
  448.  
  449. SeleccionarEquipo(playerid)
  450. {
  451.     new string[500];
  452.     format(string,sizeof(string), "{%s}%s \n", Equipos[0][eColorHex], Equipos[0][eNombre]);
  453.     for( new i = 1; i < CuantosEquipos; i++ )
  454.     {
  455.         format(string,sizeof(string), "%s{%s}%s \n",string, Equipos[i][eColorHex], Equipos[i][eNombre]);
  456.     }
  457.     Text_ListBox(playerid, DialogEquipo, $TITULO_SELECCION_EQUIPO, string, $BOTON_SELECCIONAR, "");
  458.     return 1;
  459. }
  460.  
  461. DelayedKick(playerid, time = 500)
  462. {
  463.     SetTimerEx("_KickPlayerDelayed", time, false, "d", playerid);
  464.     return 1;
  465. }
  466.  
  467. UpdatePlayerData(playerid)
  468. {
  469.     if (Player[playerid][IsLoggedIn] == false) return 0;
  470.  
  471.     // if the client crashed, it's not possible to get the player's position in OnPlayerDisconnect callback
  472.     // 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)
  473.  
  474.     // it is important to store everything in the variables registered in ORM instance
  475.    
  476.     // orm_save sends an UPDATE query
  477.     orm_save(Player[playerid][ORM_ID]);
  478.     orm_destroy(Player[playerid][ORM_ID]);
  479.     return 1;
  480. }
  481.  
  482. UpdatePlayerDeaths(playerid)
  483. {
  484.     if (Player[playerid][IsLoggedIn] == false) return 0;
  485.  
  486.     Player[playerid][Muertes]++;
  487.     Player[playerid][Dinero] -= 500;
  488.  
  489.     orm_update(Player[playerid][ORM_ID]);
  490.     return 1;
  491. }
  492.  
  493. UpdatePlayerKills(killerid)
  494. {
  495.     // we must check before if the killer wasn't valid (connected) player to avoid run time error 4
  496.     if (killerid == INVALID_PLAYER_ID) return 0;
  497.     if (Player[killerid][IsLoggedIn] == false) return 0;
  498.  
  499.     Player[killerid][Victimas]++;
  500.     Player[killerid][Dinero] += 500;
  501.    
  502.  
  503.     orm_update(Player[killerid][ORM_ID]);
  504.     return 1;
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement