Advertisement
Guest User

Codigo de criação de casas

a guest
Jun 26th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. //======================================[ INCLUDES ]====================================||
  2. #include <a_samp>
  3. #include <sscanf2>
  4. #include <streamer>
  5. #include <a_mysql>
  6. #include <Pawn.CMD>
  7. #include <YSI\y_iterate>
  8. //======================================[ DEFINES ]====================================||
  9. #define MAX_HOUSES (64)
  10.  
  11. #define HOST "localhost"
  12. #define USER "root"
  13. #define PASS ""
  14. #define DTBS "samp"
  15. //======================================[ ENUMS ]====================================||
  16. enum HouseInfos{
  17. cID,
  18. cProprietario[MAX_PLAYER_NAME],
  19. cValor,
  20. cInterior,
  21. cVirtualWorld,
  22. cComprado,
  23.  
  24. Text3D:cText,
  25. cMapIcon,
  26. cPickUp,
  27.  
  28. Float:cPosX,
  29. Float:cPosY,
  30. Float:cPosZ,
  31.  
  32. Float:InteriorX,
  33. Float:InteriorY,
  34. Float:InteriorZ
  35.  
  36. }
  37.  
  38.  
  39. new Iterator:HouseIterator<MAX_HOUSES>;
  40. static Casas[MAX_HOUSES][HouseInfos];
  41. new MySQL: ConexaoID;
  42.  
  43. //------------------------------------//
  44. public OnGameModeInit()
  45. {
  46. ConectarDB();
  47. return 1;
  48. }
  49. //------------------------------------//
  50. stock GetPlayerNameEx(playerid)
  51. {
  52. static Name[MAX_PLAYER_NAME];
  53. GetPlayerName(playerid, Name, sizeof(Name));
  54. return Name;
  55. }
  56. //------------------------------------//-//------------------------------------//
  57. stock ConectarDB() // Conecta na DataBase
  58. {
  59. ConexaoID = mysql_connect(HOST, USER, PASS, DTBS);
  60. if(ConexaoID == MYSQL_INVALID_HANDLE || mysql_errno(ConexaoID) != 0)
  61. {
  62. print("[MYSQL] Conexão falhou.");
  63. }else{
  64. print("[MYSQL] Conexão bem sucedida");
  65. CarregarCasasInfo();
  66. }
  67. }
  68. //------------------------------------//-//------------------------------------//
  69. forward CarregarCasasInfo();
  70. public CarregarCasasInfo()
  71. {
  72. new Cache:cache = mysql_query(ConexaoID, "SELECT * FROM `Casas`", true);
  73.  
  74. if(cache_num_rows())
  75. {
  76. for(new i=0, rows = cache_num_rows(); i < rows; i++)
  77. {
  78. cache_get_value_name_int(i, "cID", Casas[i][cID]);
  79.  
  80. cache_get_value_name(i, "cProprietario", Casas[i][cProprietario], MAX_PLAYER_NAME);
  81. cache_get_value_name_int(i, "cValor", Casas[i][cValor]);
  82. cache_get_value_name_int(i, "cInterior", Casas[i][cInterior]);
  83. cache_get_value_name_int(i, "cVirtualWorld", Casas[i][cVirtualWorld]);
  84. cache_get_value_name_int(i, "cComprado", Casas[i][cComprado]);
  85.  
  86. cache_get_value_name_float(i, "cPosX", Casas[i][cPosX]);
  87. cache_get_value_name_float(i, "cPosY", Casas[i][cPosY]);
  88. cache_get_value_name_float(i, "cPosZ", Casas[i][cPosZ]);
  89. cache_get_value_name_float(i, "InteriorX", Casas[i][InteriorX]);
  90. cache_get_value_name_float(i, "InteriorY", Casas[i][InteriorY]);
  91. cache_get_value_name_float(i, "InteriorZ", Casas[i][InteriorZ]);
  92.  
  93.  
  94. UpdateHouse(i);
  95. Iter_Add(HouseIterator, i);
  96. }
  97. }
  98. cache_delete(cache);
  99. return 1;
  100. }
  101. //------------------------------------//-//------------------------------------//
  102. stock UpdateHouse(id)
  103. {
  104. new string[254];
  105. if(Casas[id][cComprado] == 0)
  106. {
  107. format(string, sizeof(string), "ID: {FFFF33}%d | %d{FFFFFF}\n\nProprietario: {FFFF33}N/A.{FFFFFF}\nValor: {FFFF33}%dR${FFFFFF}.\nUse {FFFF33}/comprarcasa {FFFFFF}para comprar.", Casas[id][cID], Casas[id][cVirtualWorld], Casas[id][cValor]);
  108.  
  109. Casas[id][cText] = CreateDynamic3DTextLabel(string, -1, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 15.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, 0, 0);
  110. Casas[id][cPickUp] = CreateDynamicPickup((Casas[id][cComprado] != 0) ? (19522) : (1273), 23, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 0, 0);
  111. Casas[id][cMapIcon] = CreateDynamicMapIcon(Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], (Casas[id][cComprado] != 0) ? (32) : (31), 0, 0, 0);
  112. }else{
  113. format(string, sizeof(string), "ID: {FFFF33}%d{FFFFFF}\n\nProprietario: {FFFF33}%s\nUse {FFFF33}/entrar {FFFFFF}para entrar na casa.", Casas[id][cID], Casas[id][cProprietario]);
  114. Casas[id][cText] = CreateDynamic3DTextLabel(string, -1, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 15.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, 0, 0);
  115. Casas[id][cPickUp] = CreateDynamicPickup((Casas[id][cComprado] != 0) ? (19522) : (1273), 23, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 0, 0);
  116. Casas[id][cMapIcon] = CreateDynamicMapIcon(Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], (Casas[id][cComprado] != 0) ? (32) : (31), 0, 0, 0);
  117. }
  118.  
  119. return 1;
  120. }
  121. //------------------------------------//-//------------------------------------//
  122. CreateHouse(price, Float:x, Float:y, Float:z)
  123. {
  124. new id = Iter_Free(HouseIterator);
  125.  
  126. if (id == cellmin) // HouseIterator atingiu o limite
  127. return -1; // -1 = invalid house id
  128.  
  129. Casas[id][cProprietario][0] = EOS;
  130. Casas[id][cComprado] = 0;
  131.  
  132. Casas[id][cValor] = price;
  133.  
  134. Casas[id][cInterior] = 0;
  135.  
  136. Casas[id][cPosX] = x;
  137. Casas[id][cPosY] = y;
  138. Casas[id][cPosZ] = z;
  139.  
  140. Casas[id][InteriorX] = 0.0;
  141. Casas[id][InteriorY] = 0.0;
  142. Casas[id][InteriorZ] = 0.0;
  143.  
  144. new query[200];
  145. mysql_format(ConexaoID, query, sizeof(query), "INSERT INTO `Casas` ( `cValor`, `cPosX`, `cPosY`, `cPosZ`, `cComprado` , `cVirtualWorld` ) VALUES ( %i , %f , %f , %f, %i , %i )",
  146. Casas[id][cValor],
  147. Casas[id][cPosX],
  148. Casas[id][cPosY],
  149. Casas[id][cPosZ],
  150. Casas[id][cComprado],
  151. Casas[id][cVirtualWorld]);
  152. mysql_tquery(ConexaoID, query, "OnInsertHouse", "i", id);
  153.  
  154. printf("[Casas]Casa ID %i criada com sucesso.", id);
  155.  
  156. Iter_Add(HouseIterator, id);
  157. return id;
  158. }
  159. //------------------------------------//-//------------------------------------//
  160. forward OnInsertHouse(id);
  161. public OnInsertHouse(id)
  162. {
  163. new index = cache_insert_id();
  164. Casas[id][cID] = index;
  165. UpdateHouse(id);
  166. printf("[Casas]Casa ID %i | %i inserida com sucesso na database.", index, Casas[id][cVirtualWorld]);
  167. }
  168. //------------------------------------//-//------------------------------------//
  169. CMD:criarcasa(playerid, params[])
  170. {
  171. new Float:x, Float:y, Float:z;
  172.  
  173. GetPlayerPos(playerid, x, y, z);
  174.  
  175. new id = CreateHouse(strval(params), x, y, z);
  176. if (id == -1)
  177. return SendClientMessage(playerid, -1, "Limite atingido.");
  178.  
  179. SendClientMessage(playerid, -1, "Casa criada com sucesso.");
  180. return 1;
  181. }
  182. //------------------------------------//-//------------------------------------//
  183. CMD:comprarcasa(playerid, params[])
  184. {
  185. new id = GetPlayerNearestHouse(playerid);
  186.  
  187. if (id == -1)
  188. return SendClientMessage(playerid, -1, "[Casas] Você não está em um icone de uma casa");
  189.  
  190. if(Casas[id][cComprado] == 1)
  191. return SendClientMessage(playerid, -1, "[Casas] Essa casa já tem dono");
  192.  
  193. if(GetPlayerMoney(playerid) < Casas[id][cValor])
  194. return SendClientMessage(playerid, -1, "[Casas] Você não tem dinheiro suficiente");
  195.  
  196. GivePlayerMoney(playerid, -Casas[id][cValor]);
  197.  
  198. DestroyDynamicMapIcon(Casas[id][cMapIcon]);
  199. DestroyDynamicPickup(Casas[id][cPickUp]);
  200. DestroyDynamic3DTextLabel(Casas[id][cText]);
  201.  
  202. Casas[id][cComprado] = 1;
  203. Casas[id][cProprietario] = EOS;
  204. strcat(Casas[id][cProprietario], GetPlayerNameEx(playerid), MAX_PLAYER_NAME);
  205.  
  206. new query[140];
  207. new string[60];
  208. mysql_format(ConexaoID, query, sizeof(query), "UPDATE `Casas` SET `cProprietario`='%e', `cComprado`='%i' WHERE `cID`='%i' ", Casas[id][cProprietario], Casas[id][cComprado], Casas[id][cID]);
  209. mysql_query(ConexaoID, query);
  210.  
  211. format(string, sizeof(string), "[Casas] O(A) %s comprou a casa ID %d.", Casas[id][cProprietario], Casas[id][cID]);
  212. SendClientMessageToAll(-1, string);
  213. UpdateHouse(id);
  214. return 1;
  215. }
  216. //------------------------------------//-//------------------------------------//
  217. CMD:vendercasa(playerid)
  218. {
  219. new id = GetPlayerNearestHouse(playerid);
  220. new Name[MAX_HOUSES];
  221. Name[id] = EOS;
  222. strcat(Name[id], GetPlayerNameEx(playerid), MAX_PLAYER_NAME);
  223. if (id == -1)
  224. return SendClientMessage(playerid, -1, "[Casas] Você não está em um icone de uma casa");
  225. if(Casas[id][cComprado] == 0)
  226. return SendClientMessage(playerid, -1, "[Casas] Essa casa não tem dono");
  227. if(Casas[id][cProprietario] != Name[id])
  228. return SendClientMessage(playerid, -1, "[Casas] Você não é dono dessa casa");
  229.  
  230.  
  231. DestroyDynamicMapIcon(Casas[id][cMapIcon]);
  232. DestroyDynamicPickup(Casas[id][cPickUp]);
  233. DestroyDynamic3DTextLabel(Casas[id][cText]);
  234.  
  235. new string[128];
  236. new query[150];
  237. GivePlayerMoney(playerid, Casas[id][cValor] * 75 / 100);
  238. format(string, sizeof(string), "[Casas] O(A) %s vendeu a sua casa ID %d", Casas[id][cProprietario], Casas[id][cID]);
  239. SendClientMessageToAll(-1, string);
  240.  
  241. Casas[id][cComprado] = 0;
  242.  
  243.  
  244. mysql_format(ConexaoID, query, sizeof(query), "UPDATE `Casas` SET `cProprietario`='', `cComprado`='0' WHERE `cID`='%i' ", Casas[id][cID]);
  245. mysql_query(ConexaoID, query);
  246.  
  247.  
  248.  
  249. UpdateHouse(id);
  250. return 1;
  251. }
  252. //------------------------------------//-//------------------------------------//
  253. GetPlayerNearestHouse(playerid, Float:range = 2.5)
  254. {
  255. foreach (new i : HouseIterator)
  256. {
  257. if (IsPlayerInRangeOfPoint(playerid, range, Casas[i][cPosX], Casas[i][cPosY], Casas[i][cPosZ]))
  258. return i;
  259. }
  260. return -1;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement