Advertisement
pmiranda

[SQLite] Deathmatch

May 25th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.78 KB | None | 0 0
  1. #include <a_samp>
  2.  
  3.  
  4.  
  5. /* -----------------------------
  6.  
  7.         Deathmatch 1.0
  8.             por Pedro Miranda
  9.  
  10.  
  11.     Skype: pedro.miranda91
  12.  
  13. ----------------------------- */
  14. main();
  15.  
  16.  
  17.  
  18. new DB:main_database;
  19.  
  20.  
  21. enum {
  22.     DIALOG_REGISTER,
  23.     DIALOG_LOGIN,
  24.     DIALOG_RANKING
  25. }
  26.  
  27.  
  28.  
  29. public OnGameModeInit() {
  30.  
  31.     main_database = db_open("sqlite.db");
  32.     db_query(main_database, "CREATE TABLE IF NOT EXISTS `player_info` (`name`, `password`, `money`, `kills`, `deaths`)");
  33.  
  34.     SendRconCommand("mapname Los Santos");
  35.     SendRconCommand("gamemodetext DM 1.0");
  36.     SendRconCommand("hostname Deathmatch 1.0");
  37.  
  38.     for(new i; i < 300; ++i)
  39.         AddPlayerClass(i, 2232.2813, -1158.4985, 29.7969, 0.3858, 24, 9999, 25, 9999, 34, 9999);
  40.  
  41.     UsePlayerPedAnims();
  42.     return true;
  43. }
  44.  
  45.  
  46. public OnGameModeExit() {
  47.  
  48.     db_close(main_database);
  49.     return true;
  50. }
  51.  
  52.  
  53. public OnPlayerConnect(playerid) {
  54.  
  55.     new
  56.         playername[25],
  57.         stringoutput[128],
  58.         DBResult:queryresult;
  59.  
  60.     GetPlayerName(playerid, playername, sizeof playername);
  61.  
  62.     format(stringoutput, sizeof stringoutput, "SELECT * FROM `player_info` WHERE `name` = '%s'", DB_Escape(playername));
  63.     queryresult = db_query(main_database, stringoutput);
  64.  
  65.     if(db_num_rows(queryresult)) {
  66.         format(stringoutput, sizeof stringoutput, "{ffffff}Olá, {1e90ff}%s{ffffff}!\n\nDigite sua senha abaixo para logar.", playername);
  67.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", stringoutput, "Login", "Sair");
  68.     }
  69.     else {
  70.         format(stringoutput, sizeof stringoutput, "{ffffff}Olá, {1e90ff}%s{ffffff}!\nVocê ainda não é registrado no servidor.\n\nDigite uma senha abaixo para se registrar.", playername);
  71.         ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", stringoutput, "Registrar", "Sair");
  72.     }
  73.     db_free_result(queryresult);
  74.     return true;
  75. }
  76.  
  77.  
  78. public OnPlayerDisconnect(playerid, reason) {
  79.  
  80.     if(GetPVarInt(playerid, "playerLogged")) {
  81.  
  82.         new playername[25], stringoutput[150];
  83.  
  84.         GetPlayerName(playerid, playername, sizeof playername);
  85.  
  86.         format(stringoutput, sizeof stringoutput, "UPDATE player_info SET money = %d, kills = %d, deaths = %d WHERE name = '%s'",
  87.             GetPlayerMoney(playerid),
  88.             GetPVarInt(playerid, "playerKills"),
  89.             GetPVarInt(playerid, "playerDeaths"),
  90.             DB_Escape(playername));
  91.  
  92.         db_query(main_database, stringoutput);
  93.  
  94.         format(stringoutput, 64, "Server: {ffffff}%s deixou o servidor.", playername);
  95.         SendClientMessageToAll(0xff0000ff, stringoutput);
  96.  
  97.         TextDrawDestroy(Text:GetPVarInt(playerid, "playerkTD"));
  98.         TextDrawDestroy(Text:GetPVarInt(playerid, "playerdTD"));
  99.     }
  100.     return true;
  101. }
  102.  
  103.  
  104. public OnPlayerRequestClass(playerid, classid) {
  105.  
  106.     SetPlayerVirtualWorld(playerid, (playerid + 1));
  107.     SetPlayerPos(playerid, 1025.5751, -2140.7637, 39.2032);
  108.     SetPlayerCameraPos(playerid, 1019.7552, -2141.0266, 37.7274);
  109.     SetPlayerCameraLookAt(playerid, 1021.9974, -2140.9907, 38.3012);
  110.     SetPlayerFacingAngle(playerid, 82.6125);
  111.     return true;
  112. }
  113.  
  114.  
  115. public OnPlayerDeath(playerid, killerid, reason) {
  116.  
  117.     new
  118.         playername[25],
  119.         stringoutput[128];
  120.  
  121.     GivePlayerMoney(playerid, 100); // Fix default money taking
  122.  
  123.     SetPVarInt(playerid, "playerDeaths", (GetPVarInt(playerid, "playerDeaths") + 1));
  124.     if(killerid != 0xffff) {
  125.         if(reason < 42) {
  126.             new killername[25];
  127.             GetPlayerName(killerid, killername, sizeof killername);
  128.  
  129.             SetPVarInt(killerid, "playerKills", (GetPVarInt(playerid, "playerKills") + 1));
  130.  
  131.             format(stringoutput, sizeof stringoutput, "    Vish! %s te matou e levou toda sua grana!", killername);
  132.             SendClientMessage(playerid, 0xff0000ff, stringoutput);
  133.  
  134.             format(stringoutput, sizeof stringoutput, "    Boa garoto!! Você matou %s e levou toda a grana do otário!!", playername);
  135.             SendClientMessage(playerid, 0xf6f600ff, stringoutput);
  136.  
  137.             GivePlayerMoney(killerid, GetPlayerMoney(playerid));
  138.             ResetPlayerMoney(playerid);
  139.  
  140.             format(stringoutput, sizeof stringoutput, "Kills: ~y~%02d", GetPVarInt(killerid, "playerKills"));
  141.             TextDrawSetString(Text:GetPVarInt(killerid, "playerkTD"), stringoutput);
  142.         }
  143.     }
  144.  
  145.     format(stringoutput, sizeof stringoutput, "Mortes: ~y~%02d", GetPVarInt(playerid, "playerDeaths"));
  146.     TextDrawSetString(Text:GetPVarInt(playerid, "playerdTD"), stringoutput);
  147.  
  148.     TextDrawHideForPlayer(playerid, Text:GetPVarInt(playerid, "playerkTD"));
  149.     TextDrawHideForPlayer(playerid, Text:GetPVarInt(playerid, "playerdTD"));
  150.     return true;
  151. }
  152.  
  153.  
  154. public OnPlayerRequestSpawn(playerid) {
  155.  
  156.     return GetPVarInt(playerid, "playerLogged");
  157. }
  158.  
  159.  
  160. public OnPlayerSpawn(playerid) {
  161.  
  162.     SetPlayerVirtualWorld(playerid, 0);
  163.  
  164.     TextDrawShowForPlayer(playerid, Text:GetPVarInt(playerid, "playerkTD"));
  165.     TextDrawShowForPlayer(playerid, Text:GetPVarInt(playerid, "playerdTD"));
  166.     return true;
  167. }
  168.  
  169.  
  170. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) {
  171.  
  172.     new
  173.         playername[25],
  174.         stringoutput[150],
  175.         DBResult:queryresult;
  176.  
  177.     GetPlayerName(playerid, playername, sizeof playername);
  178.  
  179.     if(dialogid == DIALOG_REGISTER) {
  180.  
  181.         if(!response)
  182.             return Kick(playerid);
  183.  
  184.         if(!(4 < strlen(inputtext) < 33))
  185.             return ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{ffffff}Registro", "{ffffff}Sua senha deve ter no mínimo 5 e no máximo 32 caracteres.\n\nPor favor, tente novamente.", "Registrar", "Sair");
  186.  
  187.         format(stringoutput, sizeof stringoutput, "INSERT INTO player_info (name, password, money, kills, deaths) VALUES ('%s', '%s', 5000, 0, 0)", DB_Escape(playername), DB_Escape(inputtext));
  188.         db_query(main_database, stringoutput);
  189.  
  190.         format(stringoutput, sizeof stringoutput, "{ffffff}Muito bem, {1e90ff}%s{ffffff}!\nVocê se registrou com sucesso.\n\nDigite sua senha abaixo para logar.", playername);
  191.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", stringoutput, "Login", "Sair");
  192.     }
  193.     else if(dialogid == DIALOG_LOGIN) {
  194.  
  195.         if(!response)
  196.             return Kick(playerid);
  197.  
  198.         format(stringoutput, sizeof stringoutput, "SELECT * FROM player_info WHERE name = '%s' AND password = '%s'", DB_Escape(playername), DB_Escape(inputtext));
  199.         queryresult = db_query(main_database, stringoutput);
  200.  
  201.         if(db_num_rows(queryresult)) {
  202.             SetPVarInt(playerid, "playerLogged", 1);
  203.  
  204.             db_get_field_assoc(queryresult, "money", stringoutput, 32);
  205.             ResetPlayerMoney(playerid); GivePlayerMoney(playerid, strval(stringoutput));
  206.  
  207.             db_get_field_assoc(queryresult, "kills", stringoutput, 32);
  208.             SetPVarInt(playerid, "playerKills", strval(stringoutput));
  209.  
  210.             db_get_field_assoc(queryresult, "deaths", stringoutput, 32);
  211.             SetPVarInt(playerid, "playerDeaths", strval(stringoutput));
  212.  
  213.             SetPVarInt(playerid, "playerkTD", _:TextDrawCreate(498.000000, 101.000000, "Kills:"));
  214.             TextDrawBackgroundColor(Text:GetPVarInt(playerid, "playerkTD"), 255);
  215.             TextDrawFont(Text:GetPVarInt(playerid, "playerkTD"), 1);
  216.             TextDrawLetterSize(Text:GetPVarInt(playerid, "playerkTD"), 0.500000, 1.000000);
  217.             TextDrawColor(Text:GetPVarInt(playerid, "playerkTD"), -1);
  218.             TextDrawSetOutline(Text:GetPVarInt(playerid, "playerkTD"), 1);
  219.             TextDrawSetProportional(Text:GetPVarInt(playerid, "playerkTD"), 1);
  220.  
  221.             SetPVarInt(playerid, "playerdTD", _:TextDrawCreate(497.000000, 112.000000, "Mortes:"));
  222.             TextDrawBackgroundColor(Text:GetPVarInt(playerid, "playerdTD"), 255);
  223.             TextDrawFont(Text:GetPVarInt(playerid, "playerdTD"), 1);
  224.             TextDrawLetterSize(Text:GetPVarInt(playerid, "playerdTD"), 0.500000, 1.000000);
  225.             TextDrawColor(Text:GetPVarInt(playerid, "playerdTD"), -1);
  226.             TextDrawSetOutline(Text:GetPVarInt(playerid, "playerdTD"), 1);
  227.             TextDrawSetProportional(Text:GetPVarInt(playerid, "playerdTD"), 1);
  228.  
  229.             format(stringoutput, sizeof stringoutput, "Kills: ~y~%02d", GetPVarInt(playerid, "playerKills"));
  230.             TextDrawSetString(Text:GetPVarInt(playerid, "playerkTD"), stringoutput);
  231.  
  232.             format(stringoutput, sizeof stringoutput, "Mortes: ~r~%02d", GetPVarInt(playerid, "playerDeaths"));
  233.             TextDrawSetString(Text:GetPVarInt(playerid, "playerdTD"), stringoutput);
  234.  
  235.             format(stringoutput, sizeof stringoutput, "Server: {ffffff}%s conectou-se!", playername);
  236.             SendClientMessageToAll(0x32cd32ff, stringoutput);
  237.         }
  238.         else {
  239.             ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{ffffff}Login", "{ffffff}Senha incorreta!\n\nPor favor, tente novamente.", "Login", "Sair");
  240.         }
  241.         db_free_result(queryresult);
  242.     }
  243.     return true;
  244. }
  245.  
  246.  
  247. public OnPlayerCommandText(playerid, cmdtext[]) {
  248.  
  249.     if(!strcmp("ranking", cmdtext[1], true)) {
  250.  
  251.         static
  252.             i,
  253.             result[32],
  254.             playerkills,
  255.             dialogstring[64 * 10],
  256.             DBResult:queryresult;
  257.  
  258.         queryresult = db_query(main_database, "SELECT name, kills FROM player_info ORDER BY kills DESC LIMIT 10");
  259.  
  260.         for(i = 0; i != db_num_rows(queryresult); ++i) {
  261.             db_get_field_assoc(queryresult, "kills", result, sizeof result);
  262.             playerkills = strval(result);
  263.  
  264.             db_get_field_assoc(queryresult, "name", result, sizeof result);
  265.  
  266.             format(dialogstring, sizeof dialogstring, "%s %02d. %s - %02d assassinatos\n", dialogstring, (i + 1), result, playerkills);
  267.             db_next_row(queryresult);
  268.         }
  269.         db_free_result(queryresult);
  270.         ShowPlayerDialog(playerid, DIALOG_RANKING, DIALOG_STYLE_LIST, "{ffffff}Ranking", dialogstring, "Fechar", "");
  271.         return true;
  272.     }
  273.  
  274.     return SendClientMessage(playerid, 0x1e90ffff, "Server: {ffffff}Este comando não existe!");
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. stock DB_Escape(text[])
  282. {
  283.     new
  284.         ret[80 * 2],
  285.         ch,
  286.         i,
  287.         j;
  288.     while ((ch = text[i++]) && j < sizeof (ret))
  289.     {
  290.         if (ch == '\'')
  291.         {
  292.             if (j < sizeof (ret) - 2)
  293.             {
  294.                 ret[j++] = '\'';
  295.                 ret[j++] = '\'';
  296.             }
  297.         }
  298.         else if (j < sizeof (ret))
  299.         {
  300.             ret[j++] = ch;
  301.         }
  302.         else
  303.         {
  304.             j++;
  305.         }
  306.     }
  307.     ret[sizeof (ret) - 1] = '\0';
  308.     return ret;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement