ownership.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <YSI_Coding\y_hooks>
  2. #define MAX_OWNED_HOUSES 3
  3. #define MAX_OWNED_BUSINESSES 2
  4. new Iterator:OwnedHouse[MAX_PLAYERS]<MAX_PROPERTIES>;
  5. new Iterator:OwnedBusiness[MAX_PLAYERS]<MAX_PROPERTIES>;
  6. hook OnGameModeInit()
  7. {
  8. Iter_Init(OwnedHouse);
  9. Iter_Init(OwnedBusiness);
  10. }
  11. hook OnPlayerDisconnect(playerid, reason)
  12. {
  13. Iter_Clear(OwnedHouse[playerid]);
  14. Iter_Clear(OwnedBusiness[playerid]);
  15. }
  16. hook OnPlayerFirstSpawn(playerid)
  17. {
  18. foreach(new i : Property)
  19. {
  20. if(Property_GetOwnerSQLID(i) == Character_GetSQLID(playerid))
  21. {
  22. if(Iter_Contains(House, i))
  23. {
  24. Iter_Add(OwnedHouse[playerid], i);
  25. }
  26. else if(Iter_Contains(Business, i))
  27. {
  28. Iter_Add(OwnedBusiness[playerid], i);
  29. }
  30. }
  31. }
  32. }
  33. hook OnPropertyDeleted(propertyid)
  34. {
  35. foreach(new i : Player)
  36. {
  37. if(Character_GetSQLID(i) == Property_GetOwnerSQLID(propertyid))
  38. {
  39. Iter_Remove(OwnedHouse[i], propertyid);
  40. Iter_Remove(OwnedBusiness[i], propertyid);
  41. }
  42. Iter_Clear(PropertyKey[i]);
  43. }
  44. new query[128];
  45. mysql_format(MySQL_GetHandle(), query, sizeof(query), "DELETE FROM character_keys WHERE type = %d AND subject_id = %d", KEY_PROPERTY, propertyid);
  46. mysql_tquery(MySQL_GetHandle(), query);
  47. }
  48. CMD:buyproperty(playerid, params[])
  49. {
  50. new propertyid = GetCurrentProperty(playerid);
  51. if(propertyid == INVALID_PROPERTY_ID)
  52. {
  53. propertyid = GetNearbyProperty(playerid);
  54. if(propertyid == INVALID_PROPERTY_ID) return SendErrorMessage(playerid, "You need to be at one of the properties to buy it.");
  55. }
  56. if(Iter_Contains(OwnedHouse[playerid], propertyid) || Iter_Contains(OwnedBusiness[playerid], propertyid)) return SendErrorMessage(playerid, "You are the owner of this property.");
  57. if(IsPropertyAnEntrance(propertyid)) return SendErrorMessage(playerid, "You can't buy this property.");
  58. if(Property_GetOwnerSQLID(propertyid) != 0) return SendErrorMessage(playerid, "This property is not for sale.");
  59. new price = Property_GetPrice(propertyid);
  60. if(Player_GetCash(playerid) < price) return SendErrorMessageF(playerid, "You don't have enough money to buy this property, you need $%s.", MoneyFormat(price));
  61. if(IsPropertyAHouse(propertyid))
  62. {
  63. if(Iter_Count(OwnedHouse[playerid]) >= MAX_OWNED_HOUSES) return SendErrorMessageF(playerid, "You can't own more than %d houses.", MAX_OWNED_HOUSES);
  64. Iter_Add(OwnedHouse[playerid], propertyid);
  65. SendInfoMessageF(playerid, "You just bought this house (ID %d) for $%s at %s.", propertyid, MoneyFormat(price), Property_GetAddress(propertyid));
  66. }
  67. else if(IsPropertyABusiness(propertyid))
  68. {
  69. if(Iter_Count(OwnedBusiness[playerid]) >= MAX_OWNED_BUSINESSES) return SendErrorMessageF(playerid, "You can't own more than %d businesses.", MAX_OWNED_BUSINESSES);
  70. Iter_Add(OwnedBusiness[playerid], propertyid);
  71. SendInfoMessageF(playerid, "You just bought this business (ID %d) for $%s at %s.", propertyid, MoneyFormat(price), Property_GetAddress(propertyid));
  72. }
  73. Key_Give(playerid, KEY_PROPERTY, propertyid);
  74. Player_GiveCash(playerid, -price, true);
  75. Property_SetOwnerSQLID(propertyid, Character_GetSQLID(playerid));
  76. return 1;
  77. }
  78. CMD:sellproperty(playerid, params[])
  79. {
  80. new propertyid = GetCurrentProperty(playerid);
  81. if(propertyid == INVALID_PROPERTY_ID)
  82. {
  83. propertyid = GetNearbyProperty(playerid);
  84. if(propertyid == INVALID_PROPERTY_ID) return SendErrorMessage(playerid, "You need to be at one of the properties to sell it.");
  85. }
  86. if(Property_GetOwnerSQLID(propertyid) != Character_GetSQLID(playerid)) return SendErrorMessage(playerid, "You are not the owner of this property.");
  87. Iter_Remove(OwnedHouse[playerid], propertyid);
  88. Iter_Remove(OwnedBusiness[playerid], propertyid);
  89. Key_Remove(playerid, KEY_PROPERTY, propertyid);
  90. new price = Property_GetPrice(propertyid) / 2;
  91. Player_GiveCash(playerid, price, true);
  92. Property_SetOwnerSQLID(propertyid, 0);
  93. SendInfoMessageF(playerid, "You just sold this property (ID %d) for half of the initial price: $%s.", propertyid, MoneyFormat(price));
  94. return 1;
  95. }
  96. stock bool:IsPlayerOwningProperty(playerid, propertyid)
  97. {
  98. if(Iter_Contains(OwnedHouse[playerid], propertyid) || Iter_Contains(OwnedBusiness[playerid], propertyid))
  99. {
  100. return true;
  101. }
  102. return false;
  103. }