Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //======================================[ INCLUDES ]====================================||
- #include <a_samp>
- #include <sscanf2>
- #include <streamer>
- #include <a_mysql>
- #include <Pawn.CMD>
- #include <YSI\y_iterate>
- //======================================[ DEFINES ]====================================||
- #define MAX_HOUSES (64)
- #define HOST "localhost"
- #define USER "root"
- #define PASS ""
- #define DTBS "samp"
- //======================================[ ENUMS ]====================================||
- enum HouseInfos{
- cID,
- cProprietario[MAX_PLAYER_NAME],
- cValor,
- cInterior,
- cVirtualWorld,
- cComprado,
- Text3D:cText,
- cMapIcon,
- cPickUp,
- Float:cPosX,
- Float:cPosY,
- Float:cPosZ,
- Float:InteriorX,
- Float:InteriorY,
- Float:InteriorZ
- }
- new Iterator:HouseIterator<MAX_HOUSES>;
- static Casas[MAX_HOUSES][HouseInfos];
- new MySQL: ConexaoID;
- //------------------------------------//
- public OnGameModeInit()
- {
- ConectarDB();
- return 1;
- }
- //------------------------------------//
- stock GetPlayerNameEx(playerid)
- {
- static Name[MAX_PLAYER_NAME];
- GetPlayerName(playerid, Name, sizeof(Name));
- return Name;
- }
- //------------------------------------//-//------------------------------------//
- stock ConectarDB() // Conecta na DataBase
- {
- ConexaoID = mysql_connect(HOST, USER, PASS, DTBS);
- if(ConexaoID == MYSQL_INVALID_HANDLE || mysql_errno(ConexaoID) != 0)
- {
- print("[MYSQL] Conexão falhou.");
- }else{
- print("[MYSQL] Conexão bem sucedida");
- CarregarCasasInfo();
- }
- }
- //------------------------------------//-//------------------------------------//
- forward CarregarCasasInfo();
- public CarregarCasasInfo()
- {
- new Cache:cache = mysql_query(ConexaoID, "SELECT * FROM `Casas`", true);
- if(cache_num_rows())
- {
- for(new i=0, rows = cache_num_rows(); i < rows; i++)
- {
- cache_get_value_name_int(i, "cID", Casas[i][cID]);
- cache_get_value_name(i, "cProprietario", Casas[i][cProprietario], MAX_PLAYER_NAME);
- cache_get_value_name_int(i, "cValor", Casas[i][cValor]);
- cache_get_value_name_int(i, "cInterior", Casas[i][cInterior]);
- cache_get_value_name_int(i, "cVirtualWorld", Casas[i][cVirtualWorld]);
- cache_get_value_name_int(i, "cComprado", Casas[i][cComprado]);
- cache_get_value_name_float(i, "cPosX", Casas[i][cPosX]);
- cache_get_value_name_float(i, "cPosY", Casas[i][cPosY]);
- cache_get_value_name_float(i, "cPosZ", Casas[i][cPosZ]);
- cache_get_value_name_float(i, "InteriorX", Casas[i][InteriorX]);
- cache_get_value_name_float(i, "InteriorY", Casas[i][InteriorY]);
- cache_get_value_name_float(i, "InteriorZ", Casas[i][InteriorZ]);
- UpdateHouse(i);
- Iter_Add(HouseIterator, i);
- }
- }
- cache_delete(cache);
- return 1;
- }
- //------------------------------------//-//------------------------------------//
- stock UpdateHouse(id)
- {
- new string[254];
- if(Casas[id][cComprado] == 0)
- {
- 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]);
- 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);
- Casas[id][cPickUp] = CreateDynamicPickup((Casas[id][cComprado] != 0) ? (19522) : (1273), 23, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 0, 0);
- Casas[id][cMapIcon] = CreateDynamicMapIcon(Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], (Casas[id][cComprado] != 0) ? (32) : (31), 0, 0, 0);
- }else{
- 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]);
- 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);
- Casas[id][cPickUp] = CreateDynamicPickup((Casas[id][cComprado] != 0) ? (19522) : (1273), 23, Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], 0, 0);
- Casas[id][cMapIcon] = CreateDynamicMapIcon(Casas[id][cPosX], Casas[id][cPosY], Casas[id][cPosZ], (Casas[id][cComprado] != 0) ? (32) : (31), 0, 0, 0);
- }
- return 1;
- }
- //------------------------------------//-//------------------------------------//
- CreateHouse(price, Float:x, Float:y, Float:z)
- {
- new id = Iter_Free(HouseIterator);
- if (id == cellmin) // HouseIterator atingiu o limite
- return -1; // -1 = invalid house id
- Casas[id][cProprietario][0] = EOS;
- Casas[id][cComprado] = 0;
- Casas[id][cValor] = price;
- Casas[id][cInterior] = 0;
- Casas[id][cPosX] = x;
- Casas[id][cPosY] = y;
- Casas[id][cPosZ] = z;
- Casas[id][InteriorX] = 0.0;
- Casas[id][InteriorY] = 0.0;
- Casas[id][InteriorZ] = 0.0;
- new query[200];
- mysql_format(ConexaoID, query, sizeof(query), "INSERT INTO `Casas` ( `cValor`, `cPosX`, `cPosY`, `cPosZ`, `cComprado` , `cVirtualWorld` ) VALUES ( %i , %f , %f , %f, %i , %i )",
- Casas[id][cValor],
- Casas[id][cPosX],
- Casas[id][cPosY],
- Casas[id][cPosZ],
- Casas[id][cComprado],
- Casas[id][cVirtualWorld]);
- mysql_tquery(ConexaoID, query, "OnInsertHouse", "i", id);
- printf("[Casas]Casa ID %i criada com sucesso.", id);
- Iter_Add(HouseIterator, id);
- return id;
- }
- //------------------------------------//-//------------------------------------//
- forward OnInsertHouse(id);
- public OnInsertHouse(id)
- {
- new index = cache_insert_id();
- Casas[id][cID] = index;
- UpdateHouse(id);
- printf("[Casas]Casa ID %i | %i inserida com sucesso na database.", index, Casas[id][cVirtualWorld]);
- }
- //------------------------------------//-//------------------------------------//
- CMD:criarcasa(playerid, params[])
- {
- new Float:x, Float:y, Float:z;
- GetPlayerPos(playerid, x, y, z);
- new id = CreateHouse(strval(params), x, y, z);
- if (id == -1)
- return SendClientMessage(playerid, -1, "Limite atingido.");
- SendClientMessage(playerid, -1, "Casa criada com sucesso.");
- return 1;
- }
- //------------------------------------//-//------------------------------------//
- CMD:comprarcasa(playerid, params[])
- {
- new id = GetPlayerNearestHouse(playerid);
- if (id == -1)
- return SendClientMessage(playerid, -1, "[Casas] Você não está em um icone de uma casa");
- if(Casas[id][cComprado] == 1)
- return SendClientMessage(playerid, -1, "[Casas] Essa casa já tem dono");
- if(GetPlayerMoney(playerid) < Casas[id][cValor])
- return SendClientMessage(playerid, -1, "[Casas] Você não tem dinheiro suficiente");
- GivePlayerMoney(playerid, -Casas[id][cValor]);
- DestroyDynamicMapIcon(Casas[id][cMapIcon]);
- DestroyDynamicPickup(Casas[id][cPickUp]);
- DestroyDynamic3DTextLabel(Casas[id][cText]);
- Casas[id][cComprado] = 1;
- Casas[id][cProprietario] = EOS;
- strcat(Casas[id][cProprietario], GetPlayerNameEx(playerid), MAX_PLAYER_NAME);
- new query[140];
- new string[60];
- 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]);
- mysql_query(ConexaoID, query);
- format(string, sizeof(string), "[Casas] O(A) %s comprou a casa ID %d.", Casas[id][cProprietario], Casas[id][cID]);
- SendClientMessageToAll(-1, string);
- UpdateHouse(id);
- return 1;
- }
- //------------------------------------//-//------------------------------------//
- CMD:vendercasa(playerid)
- {
- new id = GetPlayerNearestHouse(playerid);
- new Name[MAX_HOUSES];
- Name[id] = EOS;
- strcat(Name[id], GetPlayerNameEx(playerid), MAX_PLAYER_NAME);
- if (id == -1)
- return SendClientMessage(playerid, -1, "[Casas] Você não está em um icone de uma casa");
- if(Casas[id][cComprado] == 0)
- return SendClientMessage(playerid, -1, "[Casas] Essa casa não tem dono");
- if(Casas[id][cProprietario] != Name[id])
- return SendClientMessage(playerid, -1, "[Casas] Você não é dono dessa casa");
- DestroyDynamicMapIcon(Casas[id][cMapIcon]);
- DestroyDynamicPickup(Casas[id][cPickUp]);
- DestroyDynamic3DTextLabel(Casas[id][cText]);
- new string[128];
- new query[150];
- GivePlayerMoney(playerid, Casas[id][cValor] * 75 / 100);
- format(string, sizeof(string), "[Casas] O(A) %s vendeu a sua casa ID %d", Casas[id][cProprietario], Casas[id][cID]);
- SendClientMessageToAll(-1, string);
- Casas[id][cComprado] = 0;
- mysql_format(ConexaoID, query, sizeof(query), "UPDATE `Casas` SET `cProprietario`='', `cComprado`='0' WHERE `cID`='%i' ", Casas[id][cID]);
- mysql_query(ConexaoID, query);
- UpdateHouse(id);
- return 1;
- }
- //------------------------------------//-//------------------------------------//
- GetPlayerNearestHouse(playerid, Float:range = 2.5)
- {
- foreach (new i : HouseIterator)
- {
- if (IsPlayerInRangeOfPoint(playerid, range, Casas[i][cPosX], Casas[i][cPosY], Casas[i][cPosZ]))
- return i;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement