1
0

hotels.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // This function begins to load all hotels
  2. forward LoadHotels();
  3. public LoadHotels() {
  4. printf("[Hotel System] Query made to fetch hotels...");
  5. new hotelQuery[100];
  6. mysql_format(sqlGameConnection, hotelQuery, sizeof(hotelQuery), "SELECT * FROM `hotels`");
  7. mysql_pquery(sqlGameConnection, hotelQuery, "OnHotelsLoaded");
  8. return 1;
  9. }
  10. // Called when all hotels have been loaded
  11. forward OnHotelsLoaded();
  12. public OnHotelsLoaded() {
  13. new hotelRows = cache_num_rows();
  14. if(hotelRows < 1) {
  15. printf("[Hotel System] No hotels found in the database.");
  16. return 1;
  17. }
  18. printf("[Hotel System] %i hotels found. Start loading...", hotelRows);
  19. for(new i = 0; i < hotelRows; i++) {
  20. new foundhID = cache_get_field_content_int(i, "ID");
  21. new foundhOwnerID = cache_get_field_content_int(i, "OwnerID");
  22. new foundhName[MAX_HOTEL_NAME_SIZE];
  23. cache_get_field_content(i, "Name", foundhName);
  24. CreateHotel(foundhID, foundhOwnerID, foundhName);
  25. }
  26. return 1;
  27. }
  28. // This function saves all hotels
  29. SaveHotels() {
  30. for(new i = 0; i < MAX_HOTELS; i++) {
  31. if(HotelInfo[i][hUsed]) SaveHotel(i);
  32. }
  33. return 1;
  34. }
  35. // This function saves a hotel room
  36. SaveHotel(hotelID) {
  37. if(!HotelInfo[hotelID][hUsed])
  38. return false;
  39. new hotelSaveQuery[500];
  40. format(hotelSaveQuery, sizeof(hotelSaveQuery),
  41. "REPLACE INTO `hotels`\
  42. (ID,\
  43. OwnerID,\
  44. Name)\
  45. VALUES(%d, %d, %e)",
  46. HotelInfo[hotelID][hSQLID],
  47. HotelInfo[hotelID][hOwnerID],
  48. HotelInfo[hotelID][hName]
  49. );
  50. mysql_pquery(sqlGameConnection, hotelSaveQuery);
  51. printf("[Hotel System] Hotel \"%s\" saved. (SQL %i)", HotelInfo[hotelID][hName], HotelInfo[hotelID][hSQLID]);
  52. return 1;
  53. }
  54. // Create a new hotel
  55. CreateHotel(sethID, sethOwnerID, sethName[]) {
  56. // Find an available hotel
  57. new availableHotel = -1;
  58. for(new i = 0; i < MAX_HOTELS; i++) {
  59. if(!HotelInfo[i][hUsed]) {
  60. availableHotel = i;
  61. break;
  62. }
  63. }
  64. if(availableHotel == -1) {
  65. printf("Max amount of hotels reached! (%i)", MAX_HOTELS);
  66. return -1;
  67. }
  68. HotelInfo[availableHotel][hUsed] = true;
  69. HotelInfo[availableHotel][hSQLID] = sethID;
  70. HotelInfo[availableHotel][hOwnerID] = sethOwnerID;
  71. format(HotelInfo[availableHotel][hName], MAX_HOTEL_NAME_SIZE, "%s", sethName);
  72. printf("[Hotel System] Hotel %i created: %s. (SQL %i)", availableHotel, sethName, sethID);
  73. return availableHotel;
  74. }
  75. // Returns the amount of hotels
  76. GetHotelCount() {
  77. new hotelCount = 0;
  78. for(new i = 0; i < MAX_HOTELS; i++)
  79. if(HotelInfo[i][hUsed]) hotelCount++;
  80. return hotelCount;
  81. }
  82. // Returns the amount of hotel rooms that a hotel has
  83. GetHotelRoomCount(hotelSQLID) {
  84. new roomCount = 0;
  85. for(new i = 0; i < MAX_HOTELROOMS; i++)
  86. if(HotelRoomInfo[i][hUsed] && HotelRoomInfo[i][hHotelID] == hotelSQLID) roomCount++;
  87. return roomCount;
  88. }
  89. // Returns the name of a hotel
  90. GetHotelName(hotelSQLID) {
  91. new hotelReturnName[MAX_HOTEL_NAME_SIZE];
  92. for(new i = 0; i < MAX_HOTELS; i++) {
  93. if(!HotelInfo[i][hUsed] || HotelInfo[i][hSQLID] != hotelSQLID) continue;
  94. format(hotelReturnName, sizeof(hotelReturnName), "%s", HotelInfo[i][hName]);
  95. break;
  96. }
  97. if(!strlen(hotelReturnName))
  98. format(hotelReturnName, sizeof(hotelReturnName), "Invalid hotel");
  99. return hotelReturnName;
  100. }
  101. // Command to show all current hotels
  102. CMD:hotels(playerid, params[]) {
  103. if(PlayerInfo[playerid][pDev] < 2)
  104. return AdmErrorMsg;
  105. if(!GetHotelCount())
  106. return SendClientMessage(playerid, COLOR_GREY, "There are currently no hotels.");
  107. // Format dialog string
  108. new hotelStr[2048];
  109. format(hotelStr, sizeof(hotelStr), "* %i Hotel(s) found:\n", GetHotelCount());
  110. for(new i = 0; i < MAX_HOTELS; i++) {
  111. if(!HotelInfo[i][hUsed]) continue;
  112. format(hotelStr, sizeof(hotelStr), "%s\n{FFD830}[%i] \"%s\", %i rooms.", hotelStr, i, HotelInfo[i][hName], GetHotelRoomCount(HotelInfo[i][hSQLID]));
  113. }
  114. // Show the dialog
  115. ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "{FFD830}List of hotels", hotelStr, "Close", "");
  116. return 1;
  117. }
  118. // Command to create a new hotel
  119. CMD:createhotel(playerid, params[]) {
  120. if(PlayerInfo[playerid][pDev] < 2)
  121. return AdmErrorMsg;
  122. new newHotelName[MAX_HOTEL_NAME_SIZE];
  123. if(sscanf(params, "s[50]", newHotelName))
  124. return SendClientMessage(playerid, COLOR_WHITE, "{00BFFF}USAGE:{FFFFFF} /createhotel [hotel name]");
  125. if(!strlen(newHotelName))
  126. return SendClientMessage(playerid, COLOR_GREY, "Enter a valid hotel name.");
  127. // Check if we can make a hotel at this moment
  128. new freeHotelSlot;
  129. for(new i = 0; i < MAX_HOTELS; i++) {
  130. if(!HotelInfo[i][hUsed]) {
  131. freeHotelSlot = i;
  132. break;
  133. }
  134. }
  135. if(freeHotelSlot == -1)
  136. return SendClientMessage(playerid, COLOR_GREY, "The maximum number of hotels has been reached.");
  137. // Insert a new hotel into the DB
  138. new hotelQuery[128];
  139. mysql_format(sqlGameConnection, hotelQuery, sizeof(hotelQuery), "INSERT INTO `hotels`(`Name`) VALUES('%e');", newHotelName);
  140. mysql_pquery(sqlGameConnection, hotelQuery, "OnHotelCreated", "is", playerid, newHotelName);
  141. return 1;
  142. }
  143. // Called when a hotel is created
  144. forward OnHotelCreated(playerid, hotelName[]);
  145. public OnHotelCreated(playerid, hotelName[]) {
  146. // Create the new hotel in-game
  147. new theHSQLID = cache_insert_id(sqlGameConnection);
  148. new createdHotelID = CreateHotel(theHSQLID, 0, hotelName);
  149. // Send message
  150. new hotelStr[128];
  151. format(hotelStr, sizeof(hotelStr), "* You have successfully created the hotel: \"%s\". (ID %i)", hotelName, createdHotelID);
  152. SendClientMessage(playerid, COLOR_LIGHTBLUE, hotelStr);
  153. return 1;
  154. }
  155. // Command to edit a hotel
  156. CMD:edithotel(playerid, params[]) {
  157. if(PlayerInfo[playerid][pDev] < 2)
  158. return AdmErrorMsg;
  159. new hotelID, editOption[20];
  160. if(sscanf(params, "is[20]", hotelID, editOption)) {
  161. SendClientMessage(playerid, COLOR_WHITE, "{00BFFF}USAGE:{FFFFFF} /edithotel [hotel ID] [option]");
  162. SendClientMessage(playerid, COLOR_GREY, "Options: owner");
  163. return 1;
  164. }
  165. if(hotelID < 0 || hotelID > MAX_HOTELS)
  166. return SendClientMessage(playerid, COLOR_GREY, "You entered an invalid hotel ID.");
  167. if(!HotelInfo[hotelID][hUsed])
  168. return SendClientMessage(playerid, COLOR_GREY, "This hotel ID is not in use.");
  169. // Edit the hotel owner
  170. if(strmatch(editOption, "owner")) {
  171. new newOwner[30];
  172. if(sscanf(params, "is[20]s[30]", hotelID, editOption, newOwner))
  173. return SendClientMessage(playerid, COLOR_WHITE, "{00BFFF}USAGE:{FFFFFF} /edithotel [hotel ID] owner [playerid/partOfName/none]");
  174. // Makes the hotel "server owned"
  175. new newOwnerID, notifyStr[128];
  176. if(strmatch(newOwner, "none")) {
  177. if(HotelInfo[hotelID][hOwnerID] != 0)
  178. return SendClientMessage(playerid, COLOR_GREY, "This hotel is already owned by the server.");
  179. newOwnerID = 0;
  180. format(notifyStr, sizeof(notifyStr), "* The hotel \"%s\" (%i) is now owned by the server.", HotelInfo[hotelID][hName], hotelID);
  181. }
  182. else { // Makes a player the owner of the hotel
  183. new foundHotelUser = ReturnUser(newOwner);
  184. if(foundHotelUser == INVALID_PLAYER_ID)
  185. return SendClientMessage(playerid, COLOR_GREY, "Unknown player.");
  186. newOwnerID = PlayerInfo[foundHotelUser][pID];
  187. if(HotelInfo[hotelID][hOwnerID] == newOwnerID)
  188. return SendClientMessage(playerid, COLOR_GREY, "This hotel is already owned by that player.");
  189. format(notifyStr, sizeof(notifyStr), "* The hotel \"%s\" (%i) is now owned by %s.", HotelInfo[hotelID][hName], hotelID, PlayerOOCName(foundHotelUser));
  190. }
  191. // Set the owner
  192. HotelInfo[hotelID][hOwnerID] = newOwnerID;
  193. // Send the message
  194. SendClientMessage(playerid, COLOR_LIGHTBLUE, notifyStr);
  195. }
  196. else return cmd_edithotel(playerid, "");
  197. return 1;
  198. }