Chainksain

tutorial10.pwn

Apr 23rd, 2020
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 14.88 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <sscanf2>
  4. #include <zcmd>
  5. #include <DOF2>
  6.  
  7. #define function%0(%1) forward %0(%1); public %0(%1)
  8.  
  9. #define MAX_PASS_LEN 16
  10.  
  11. enum //Dialogs
  12. {
  13.     DIALOG_LOGIN,
  14.     DIALOG_REGISTRO
  15. };
  16.  
  17. new MySQL:DBConn, host[16], username[MAX_PLAYER_NAME], database[MAX_PLAYER_NAME], pass[16];
  18.  
  19. enum PLAYER_DATA
  20. {
  21.     accid,
  22.     nome[MAX_PLAYER_NAME],
  23.     senha[MAX_PASS_LEN],
  24.     admin,
  25.     Float:posX,
  26.     Float:posY,
  27.     Float:posZ,
  28.     Float:angulo,
  29.     Float:health,
  30.     Float:armor,
  31.     money,
  32.     interior,
  33.     vw,
  34.     skin,
  35.     score,
  36.     ORM:ormid,
  37.     bool:logado
  38. };
  39. new PlayerData[MAX_PLAYERS][PLAYER_DATA];
  40.  
  41. main()
  42. {
  43.     print("\n----------------------------------");
  44.     print(" Blank Gamemode by your name here");
  45.     print("----------------------------------\n");
  46. }
  47.  
  48. public OnGameModeInit()
  49. {
  50.     SetGameModeText("Blank Script");
  51.     LoadMap("maps/favela_objects.txt");
  52.     DataBaseInit();
  53.     return 1;
  54. }
  55.  
  56. public OnGameModeExit()
  57. {
  58.     DOF2_Exit();
  59.     mysql_close(DBConn);
  60.     return 1;
  61. }
  62.  
  63. public OnPlayerRequestClass(playerid, classid)
  64. {
  65.     return 1;
  66. }
  67.  
  68. public OnPlayerConnect(playerid)
  69. {
  70.     RemoveBuildingFromFile(playerid, "maps/favela_buildings.txt");
  71.     LoadPlayerData(playerid);
  72.     return 1;
  73. }
  74.  
  75. public OnPlayerDisconnect(playerid, reason)
  76. {
  77.     if(PlayerData[playerid][logado] == true)
  78.     {
  79.         SavePlayerData(playerid);
  80.  
  81.         new rsnstr[3][] =
  82.         {
  83.             "Timeout/Crash",
  84.             "Saiu",
  85.             "Kickado/Banido"
  86.         };
  87.         new string[64];
  88.         format(string, sizeof(string), "%s foi desconectado (%s)", GetPlayerNameEx(playerid), rsnstr[reason]);
  89.         SendClientMessageToAll(-1, string);
  90.     }
  91.  
  92.     orm_destroy(PlayerData[playerid][ormid]);
  93.     for(new PLAYER_DATA:i; i < PLAYER_DATA; i++)
  94.         PlayerData[playerid][i] = 0;
  95.  
  96.     return 1;
  97. }
  98.  
  99. public OnPlayerSpawn(playerid)
  100. {
  101.     if(PlayerData[playerid][logado] == false)
  102.     {
  103.         SetPlayerData(playerid);
  104.         PlayerData[playerid][logado] = true;
  105.     }
  106.     return 1;
  107. }
  108.  
  109. public OnPlayerDeath(playerid, killerid, reason)
  110. {
  111.     return 1;
  112. }
  113.  
  114. public OnVehicleSpawn(vehicleid)
  115. {
  116.     return 1;
  117. }
  118.  
  119. public OnVehicleDeath(vehicleid, killerid)
  120. {
  121.     return 1;
  122. }
  123.  
  124. public OnPlayerText(playerid, text[])
  125. {
  126.     return 1;
  127. }
  128.  
  129. public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
  130. {
  131.     return 1;
  132. }
  133.  
  134. public OnPlayerExitVehicle(playerid, vehicleid)
  135. {
  136.     return 1;
  137. }
  138.  
  139. public OnPlayerStateChange(playerid, newstate, oldstate)
  140. {
  141.     return 1;
  142. }
  143.  
  144. public OnPlayerEnterCheckpoint(playerid)
  145. {
  146.     return 1;
  147. }
  148.  
  149. public OnPlayerLeaveCheckpoint(playerid)
  150. {
  151.     return 1;
  152. }
  153.  
  154. public OnPlayerEnterRaceCheckpoint(playerid)
  155. {
  156.     return 1;
  157. }
  158.  
  159. public OnPlayerLeaveRaceCheckpoint(playerid)
  160. {
  161.     return 1;
  162. }
  163.  
  164. public OnRconCommand(cmd[])
  165. {
  166.     return 1;
  167. }
  168.  
  169. public OnPlayerRequestSpawn(playerid)
  170. {
  171.     return 1;
  172. }
  173.  
  174. public OnObjectMoved(objectid)
  175. {
  176.     return 1;
  177. }
  178.  
  179. public OnPlayerObjectMoved(playerid, objectid)
  180. {
  181.     return 1;
  182. }
  183.  
  184. public OnPlayerPickUpPickup(playerid, pickupid)
  185. {
  186.     return 1;
  187. }
  188.  
  189. public OnVehicleMod(playerid, vehicleid, componentid)
  190. {
  191.     return 1;
  192. }
  193.  
  194. public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
  195. {
  196.     return 1;
  197. }
  198.  
  199. public OnVehicleRespray(playerid, vehicleid, color1, color2)
  200. {
  201.     return 1;
  202. }
  203.  
  204. public OnPlayerSelectedMenuRow(playerid, row)
  205. {
  206.     return 1;
  207. }
  208.  
  209. public OnPlayerExitedMenu(playerid)
  210. {
  211.     return 1;
  212. }
  213.  
  214. public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
  215. {
  216.     return 1;
  217. }
  218.  
  219. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  220. {
  221.     return 1;
  222. }
  223.  
  224. public OnRconLoginAttempt(ip[], password[], success)
  225. {
  226.     return 1;
  227. }
  228.  
  229. public OnPlayerUpdate(playerid)
  230. {
  231.     return 1;
  232. }
  233.  
  234. public OnPlayerStreamIn(playerid, forplayerid)
  235. {
  236.     return 1;
  237. }
  238.  
  239. public OnPlayerStreamOut(playerid, forplayerid)
  240. {
  241.     return 1;
  242. }
  243.  
  244. public OnVehicleStreamIn(vehicleid, forplayerid)
  245. {
  246.     return 1;
  247. }
  248.  
  249. public OnVehicleStreamOut(vehicleid, forplayerid)
  250. {
  251.     return 1;
  252. }
  253.  
  254. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  255. {
  256.     switch(dialogid)
  257.     {
  258.         case DIALOG_LOGIN:
  259.         {
  260.             if(!response)
  261.                 Kick(playerid);
  262.             else
  263.             {
  264.                 if(strlen(inputtext) < 1 || strcmp(PlayerData[playerid][senha], inputtext))
  265.                 {
  266.                     SendClientMessage(playerid, -1, "ERRO: Senha incorreta!");
  267.                     ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Digite sua senha para logar:", "Logar", "Sair");
  268.                 }
  269.                 else
  270.                 {
  271.                     SendClientMessage(playerid, -1, "Logado com sucesso!");
  272.                     SetSpawnInfo(playerid, NO_TEAM, PlayerData[playerid][skin],
  273.                     PlayerData[playerid][posX], PlayerData[playerid][posY],PlayerData[playerid][posZ],
  274.                     PlayerData[playerid][angulo],
  275.                     0, 0, 0, 0, 0, 0);
  276.                     SpawnPlayer(playerid);
  277.                 }
  278.             }
  279.         }
  280.         case DIALOG_REGISTRO:
  281.         {
  282.             if(!response)
  283.                 Kick(playerid);
  284.             else
  285.             {
  286.                 if(strlen(inputtext) < 1 || strlen(inputtext) > 16)
  287.                 {
  288.                     SendClientMessage(playerid, -1, "ERRO: Sua senha deve conter entre 1 e 16 caracteres!");
  289.                     ShowPlayerDialog(playerid, DIALOG_REGISTRO, DIALOG_STYLE_PASSWORD, "Registro", "Entre com uma senha para se registrar:", "Registrar", "Sair");
  290.                 }
  291.                 else
  292.                 {
  293.                     SendClientMessage(playerid, -1, "Registrado com sucesso!");
  294.                     format(PlayerData[playerid][senha], MAX_PASS_LEN, "%s", inputtext);
  295.                     orm_insert(PlayerData[playerid][ormid]);
  296.                     PlayerData[playerid][logado] = true;
  297.                     SetSpawnInfo(playerid, NO_TEAM, 0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
  298.                     SpawnPlayer(playerid);
  299.                 }
  300.             }
  301.         }
  302.     }
  303.  
  304.     return 1;
  305. }
  306.  
  307. public OnPlayerClickPlayer(playerid, clickedplayerid, source)
  308. {
  309.     return 1;
  310. }
  311.  
  312. public OnPlayerClickMap(playerid, Float:fX, Float:fY, Float:fZ)
  313. {
  314.     return 1;
  315. }
  316.  
  317. // FUNCOES
  318.  
  319. function OnPlayerLogin(playerid)
  320. {
  321.     orm_setkey(PlayerData[playerid][ormid], "id");
  322.  
  323.     if(orm_errno(PlayerData[playerid][ormid]) == ERROR_OK) // jogador ja existe na database
  324.         ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "Login", "Digite sua senha para logar:", "Logar", "Sair");
  325.     else // jogador não encontrado na database
  326.         ShowPlayerDialog(playerid, DIALOG_REGISTRO, DIALOG_STYLE_PASSWORD, "Registro", "Entre com uma senha para se registrar:", "Registrar", "Sair");
  327.     return 1;
  328. }
  329.  
  330. function DataBaseInit()
  331. {
  332.     //new MYSQL:DBConn, host[16], username[MAX_PLAYER_NAME], database[MAX_PLAYER_NAME], pass[16];
  333.     LoadDBSettings("dbconfig.ini");
  334.     DBConn = mysql_connect(host, username, pass, database);
  335.     if(mysql_errno() == 0)
  336.     {
  337.         printf("[MySQL] Database '%s' conectada com sucesso!", database);
  338.         print("[MySQL] Verificando tabelas...");
  339.  
  340.         mysql_query(DBConn, "CREATE TABLE IF NOT EXISTS Player (\
  341.         id int NOT NULL AUTO_INCREMENT,\
  342.         nome varchar(25) NOT NULL,\
  343.         senha varchar(255) NOT NULL,\
  344.         admin int DEFAULT 0,\
  345.         PRIMARY KEY(id));", false);
  346.  
  347.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS health float DEFAULT 100;", false);
  348.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS armor float DEFAULT 100;", false);
  349.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS dinheiro int DEFAULT 100;", false);
  350.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS posX double DEFAULT 0;", false);
  351.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS posY double DEFAULT 0;", false);
  352.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS posZ double DEFAULT 0;", false);
  353.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS angulo double DEFAULT 0;", false);
  354.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS interior int DEFAULT 0;", false);
  355.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS vw int DEFAULT 0;", false);
  356.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS score int DEFAULT 0;", false);
  357.         mysql_query(DBConn, "ALTER TABLE Player ADD IF NOT EXISTS skin int DEFAULT 0;", false);
  358.  
  359.         print("[MySQL] Tabela 'Players' verificada com sucesso!");
  360.     }
  361.     else
  362.     {
  363.         printf("[MySQL] ERRO: Não foi possível se conectar a database '%s'!", database);
  364.         SendRconCommand("exit");
  365.     }
  366.  
  367.     return 1;
  368. }
  369.  
  370. function RemoveBuildingFromFile(playerid, const file[])
  371. {
  372.     new File:map = fopen(file, io_read);
  373.     if(map)
  374.     {
  375.         new string[256];
  376.         while(fread(map, string))
  377.         {
  378.             new modelid, Float:pos[3], Float:rad;
  379.             if(!sscanf(string, "dffff", modelid, pos[0], pos[1], pos[2], rad))
  380.                 RemoveBuildingForPlayer(playerid, modelid, pos[0], pos[1], pos[2], rad);
  381.             else
  382.                 return printf("Carregamento do mapa '%s' falhou para o jogador %s(%d).", file, GetPlayerNameEx(playerid), playerid);
  383.         }
  384.     }
  385.     return printf("Jogador %s(%d) carregou o mapa '%s' com êxito.", GetPlayerNameEx(playerid), playerid, file);
  386. }
  387.  
  388. function LoadMap(const file[])
  389. {
  390.     printf("Carregando Mapa '%s'...", file);
  391.     new File:map = fopen(file, io_read);
  392.     if(map)
  393.     {
  394.         new string[256];
  395.         while(fread(map, string))
  396.         {
  397.             new modelid, Float:pos[3], Float:rot[3];
  398.             if(sscanf(string, "dffffff", modelid, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2]))
  399.                 return printf("Erro ao carregar o mapa '%s'!",file);
  400.             else
  401.                 CreateObject(modelid, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2]);
  402.                
  403.         }
  404.         return printf("Mapa '%s' carregado com sucesso!",file);
  405.     }
  406.     else return printf("Erro ao carregar o mapa '%s'!",file);
  407. }
  408.  
  409. // COMANDOS
  410.  
  411. CMD:pos(playerid, const params[])
  412. {
  413.     new Float:pos[3], giveplayerid;
  414.     if(sscanf(params, "ufff", giveplayerid, pos[0], pos[1], pos[2]))
  415.         return SendClientMessage(playerid, -1, "USO: /pos [id] [x] [y] [z]");
  416.  
  417.     if(!IsPlayerConnected(giveplayerid) || giveplayerid == INVALID_PLAYER_ID)
  418.         return SendClientMessage(playerid, -1, "ERRO: ID Inválido!");
  419.  
  420.     SetPlayerPos(giveplayerid, pos[0], pos[1], pos[2]);
  421.     return 1;
  422. }
  423.  
  424. CMD:info(playerid, const params[])
  425. {
  426.     new giveplayerid, string[64];
  427.    
  428.     if(sscanf(params, "u", giveplayerid))
  429.         return SendClientMessage(playerid, -1, "USO: /info [id]");
  430.    
  431.     if(!IsPlayerConnected(giveplayerid) || giveplayerid == INVALID_PLAYER_ID)
  432.         return SendClientMessage(playerid, -1, "ERRO: ID Inválido!");
  433.  
  434.     format(string, sizeof(string), "Jogador: %s", GetPlayerNameEx(giveplayerid));
  435.     SendClientMessage(playerid, -1, string);
  436.  
  437.     format(string, sizeof(string), "ID: %d", giveplayerid);
  438.     SendClientMessage(playerid, -1, string);
  439.  
  440.     format(string, sizeof(string), "Score: %d", GetPlayerScore(giveplayerid));
  441.     SendClientMessage(playerid, -1, string);
  442.  
  443.     format(string, sizeof(string), "Dinheiro: %d", GetPlayerMoney(giveplayerid));
  444.     SendClientMessage(playerid, -1, string);
  445.        
  446.     format(string, sizeof(string), "Life: %f", GetPlayerHealthEx(giveplayerid));
  447.     SendClientMessage(playerid, -1, string);
  448.  
  449.     format(string, sizeof(string), "Armor: %f", GetPlayerArmourEx(giveplayerid));
  450.     SendClientMessage(playerid, -1, string);
  451.  
  452.     new Float:pos[3];
  453.     GetPlayerPos(giveplayerid, pos[0], pos[1], pos[2]);
  454.     format(string, sizeof(string), "PosX: %f PosY: %f PosZ: %f", pos[0], pos[1], pos[2]);
  455.     SendClientMessage(playerid, -1, string);
  456.  
  457.     format(string, sizeof(string),"Int: %d", GetPlayerInterior(giveplayerid));
  458.     SendClientMessage(playerid, -1, string);
  459.  
  460.     format(string, sizeof(string),"Virtual World: %d", GetPlayerVirtualWorld(giveplayerid));
  461.     SendClientMessage(playerid, -1, string);
  462.  
  463.     return 1;
  464. }
  465.  
  466. // PROCEDIMENTOS
  467.  
  468. stock SavePlayerData(playerid)
  469. {
  470.     PlayerData[playerid][armor] = GetPlayerArmourEx(playerid);
  471.     PlayerData[playerid][health] = GetPlayerHealthEx(playerid);
  472.     PlayerData[playerid][money] = GetPlayerMoney(playerid);
  473.     PlayerData[playerid][skin] = GetPlayerSkin(playerid);
  474.     PlayerData[playerid][interior] = GetPlayerInterior(playerid);
  475.     PlayerData[playerid][vw] = GetPlayerVirtualWorld(playerid);
  476.     PlayerData[playerid][score] = GetPlayerScore(playerid);
  477.     GetPlayerFacingAngle(playerid, PlayerData[playerid][angulo]);
  478.     GetPlayerPos(playerid,
  479.     PlayerData[playerid][posX],
  480.     PlayerData[playerid][posY],
  481.     PlayerData[playerid][posZ]);
  482.  
  483.     orm_update(PlayerData[playerid][ormid]);
  484. }
  485.  
  486. stock SetPlayerData(playerid)
  487. {
  488.     SetPlayerScore(playerid, PlayerData[playerid][score]);
  489.     SetPlayerHealth(playerid, PlayerData[playerid][health]);
  490.     SetPlayerArmour(playerid, PlayerData[playerid][armor]);
  491.     GivePlayerMoney(playerid, PlayerData[playerid][money]);
  492.     SetPlayerInterior(playerid, PlayerData[playerid][interior]);
  493.     SetPlayerVirtualWorld(playerid, PlayerData[playerid][vw]);
  494. }
  495.  
  496. stock LoadPlayerData(playerid)
  497. {
  498.     PlayerData[playerid][logado] = false;
  499.     format(PlayerData[playerid][nome], MAX_PLAYER_NAME, "%s", GetPlayerNameEx(playerid));
  500.  
  501.     PlayerData[playerid][ormid] = orm_create("Player", DBConn);
  502.  
  503.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][accid], "id");
  504.     orm_addvar_string(PlayerData[playerid][ormid], PlayerData[playerid][nome], MAX_PLAYER_NAME, "nome");
  505.     orm_addvar_string(PlayerData[playerid][ormid], PlayerData[playerid][senha], MAX_PASS_LEN, "senha");
  506.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][admin], "admin");
  507.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][posX], "posX");
  508.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][posY], "posY");
  509.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][posZ], "posZ");
  510.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][angulo], "angulo");
  511.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][health], "health");
  512.     orm_addvar_float(PlayerData[playerid][ormid], PlayerData[playerid][armor], "armor");
  513.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][money], "dinheiro");
  514.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][score], "score");
  515.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][skin], "skin");
  516.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][interior], "interior");
  517.     orm_addvar_int(PlayerData[playerid][ormid], PlayerData[playerid][vw], "vw");
  518.  
  519.     orm_setkey(PlayerData[playerid][ormid], "nome");
  520.     orm_select(PlayerData[playerid][ormid], "OnPlayerLogin", "d", playerid);
  521. }
  522.  
  523. stock LoadDBSettings(filename[])
  524. {
  525.     //new host[16], username[MAX_PLAYER_NAME], database[MAX_PLAYER_NAME], pass[16];
  526.     if(DOF2_FileExists(filename))
  527.     {
  528.         format(host, sizeof(host), "%s", DOF2_GetString(filename,"address"));
  529.         format(username, sizeof(username), "%s", DOF2_GetString(filename,"username"));
  530.         format(database, sizeof(database), "%s", DOF2_GetString(filename,"database"));
  531.         format(pass, sizeof(pass), "%s", DOF2_GetString(filename,"password"));
  532.     }
  533.     else
  534.     {
  535.         DOF2_CreateFile(filename);
  536.         DOF2_SetString(filename,"address","127.0.0.1");
  537.         DOF2_SetString(filename,"username","root");
  538.         DOF2_SetString(filename,"database", " ");
  539.         DOF2_SetString(filename,"password", " ");
  540.         DOF2_SaveFile();
  541.     }
  542. }
  543.  
  544. stock GetPlayerNameEx(playerid)
  545. {
  546.     new PlayerName[MAX_PLAYER_NAME];
  547.     GetPlayerName(playerid, PlayerName, sizeof(PlayerName));
  548.     return PlayerName;
  549. }
  550.  
  551. function Float:GetPlayerHealthEx(playerid)
  552. {
  553.     new Float:playerhealth;
  554.     GetPlayerHealth(playerid, playerhealth);
  555.     return playerhealth;
  556. }
  557.  
  558. function Float:GetPlayerArmourEx(playerid)
  559. {
  560.     new Float:playerarmor;
  561.     GetPlayerArmour(playerid, playerarmor);
  562.     return playerarmor;
  563. }
Add Comment
Please, Sign In to add comment