PageRenderTime 282ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/server/scripts/Commands/cs_instance.cpp

https://gitlab.com/Eternal-WoW/TrinityCore-1
C++ | 329 lines | 260 code | 44 blank | 25 comment | 51 complexity | 24e8e20bbaddaf670096517c7735052f MD5 | raw file
  1. /*
  2. * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /* ScriptData
  18. Name: instance_commandscript
  19. %Complete: 100
  20. Comment: All instance related commands
  21. Category: commandscripts
  22. EndScriptData */
  23. #include "ScriptMgr.h"
  24. #include "Chat.h"
  25. #include "Group.h"
  26. #include "InstanceSaveMgr.h"
  27. #include "InstanceScript.h"
  28. #include "MapManager.h"
  29. #include "Player.h"
  30. #include "Language.h"
  31. class instance_commandscript : public CommandScript
  32. {
  33. public:
  34. instance_commandscript() : CommandScript("instance_commandscript") { }
  35. ChatCommand* GetCommands() const override
  36. {
  37. static ChatCommand instanceCommandTable[] =
  38. {
  39. { "listbinds", rbac::RBAC_PERM_COMMAND_INSTANCE_LISTBINDS, false, &HandleInstanceListBindsCommand, "", NULL },
  40. { "unbind", rbac::RBAC_PERM_COMMAND_INSTANCE_UNBIND, false, &HandleInstanceUnbindCommand, "", NULL },
  41. { "stats", rbac::RBAC_PERM_COMMAND_INSTANCE_STATS, true, &HandleInstanceStatsCommand, "", NULL },
  42. { "savedata", rbac::RBAC_PERM_COMMAND_INSTANCE_SAVEDATA, false, &HandleInstanceSaveDataCommand, "", NULL },
  43. { "setbossstate", rbac::RBAC_PERM_COMMAND_INSTANCE_SET_BOSS_STATE, true, &HandleInstanceSetBossStateCommand, "", NULL },
  44. { "getbossstate", rbac::RBAC_PERM_COMMAND_INSTANCE_GET_BOSS_STATE, true, &HandleInstanceGetBossStateCommand, "", NULL },
  45. { NULL, 0, false, NULL, "", NULL }
  46. };
  47. static ChatCommand commandTable[] =
  48. {
  49. { "instance", rbac::RBAC_PERM_COMMAND_INSTANCE, true, NULL, "", instanceCommandTable },
  50. { NULL, 0, false, NULL, "", NULL }
  51. };
  52. return commandTable;
  53. }
  54. static std::string GetTimeString(uint64 time)
  55. {
  56. uint64 days = time / DAY, hours = (time % DAY) / HOUR, minute = (time % HOUR) / MINUTE;
  57. std::ostringstream ss;
  58. if (days)
  59. ss << days << "d ";
  60. if (hours)
  61. ss << hours << "h ";
  62. ss << minute << 'm';
  63. return ss.str();
  64. }
  65. static bool HandleInstanceListBindsCommand(ChatHandler* handler, char const* /*args*/)
  66. {
  67. Player* player = handler->getSelectedPlayer();
  68. if (!player)
  69. player = handler->GetSession()->GetPlayer();
  70. uint32 counter = 0;
  71. for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
  72. {
  73. Player::BoundInstancesMap &binds = player->GetBoundInstances(Difficulty(i));
  74. for (Player::BoundInstancesMap::const_iterator itr = binds.begin(); itr != binds.end(); ++itr)
  75. {
  76. InstanceSave* save = itr->second.save;
  77. std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
  78. handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_INFO, itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficultyID(), save->CanReset() ? "yes" : "no", timeleft.c_str());
  79. counter++;
  80. }
  81. }
  82. handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_PLAYER_BINDS, counter);
  83. counter = 0;
  84. if (Group* group = player->GetGroup())
  85. {
  86. for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
  87. {
  88. Group::BoundInstancesMap &binds = group->GetBoundInstances(Difficulty(i));
  89. for (Group::BoundInstancesMap::const_iterator itr = binds.begin(); itr != binds.end(); ++itr)
  90. {
  91. InstanceSave* save = itr->second.save;
  92. std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
  93. handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_INFO, itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficultyID(), save->CanReset() ? "yes" : "no", timeleft.c_str());
  94. counter++;
  95. }
  96. }
  97. }
  98. handler->PSendSysMessage(LANG_COMMAND_LIST_BIND_GROUP_BINDS, counter);
  99. return true;
  100. }
  101. static bool HandleInstanceUnbindCommand(ChatHandler* handler, char const* args)
  102. {
  103. if (!*args)
  104. return false;
  105. Player* player = handler->getSelectedPlayer();
  106. if (!player)
  107. player = handler->GetSession()->GetPlayer();
  108. char* map = strtok((char*)args, " ");
  109. char* pDiff = strtok(NULL, " ");
  110. int8 diff = -1;
  111. if (pDiff)
  112. diff = atoi(pDiff);
  113. uint16 counter = 0;
  114. uint16 MapId = 0;
  115. if (strcmp(map, "all") != 0)
  116. {
  117. MapId = uint16(atoi(map));
  118. if (!MapId)
  119. return false;
  120. }
  121. for (uint8 i = 0; i < MAX_DIFFICULTY; ++i)
  122. {
  123. Player::BoundInstancesMap &binds = player->GetBoundInstances(Difficulty(i));
  124. for (Player::BoundInstancesMap::iterator itr = binds.begin(); itr != binds.end();)
  125. {
  126. InstanceSave* save = itr->second.save;
  127. if (itr->first != player->GetMapId() && (!MapId || MapId == itr->first) && (diff == -1 || diff == save->GetDifficultyID()))
  128. {
  129. std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
  130. handler->PSendSysMessage(LANG_COMMAND_INST_UNBIND_UNBINDING, itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no", save->GetDifficultyID(), save->CanReset() ? "yes" : "no", timeleft.c_str());
  131. player->UnbindInstance(itr, Difficulty(i));
  132. counter++;
  133. }
  134. else
  135. ++itr;
  136. }
  137. }
  138. handler->PSendSysMessage(LANG_COMMAND_INST_UNBIND_UNBOUND, counter);
  139. return true;
  140. }
  141. static bool HandleInstanceStatsCommand(ChatHandler* handler, char const* /*args*/)
  142. {
  143. handler->PSendSysMessage(LANG_COMMAND_INST_STAT_LOADED_INST, sMapMgr->GetNumInstances());
  144. handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERS_IN, sMapMgr->GetNumPlayersInInstances());
  145. handler->PSendSysMessage(LANG_COMMAND_INST_STAT_SAVES, sInstanceSaveMgr->GetNumInstanceSaves());
  146. handler->PSendSysMessage(LANG_COMMAND_INST_STAT_PLAYERSBOUND, sInstanceSaveMgr->GetNumBoundPlayersTotal());
  147. handler->PSendSysMessage(LANG_COMMAND_INST_STAT_GROUPSBOUND, sInstanceSaveMgr->GetNumBoundGroupsTotal());
  148. return true;
  149. }
  150. static bool HandleInstanceSaveDataCommand(ChatHandler* handler, char const* /*args*/)
  151. {
  152. Player* player = handler->GetSession()->GetPlayer();
  153. Map* map = player->GetMap();
  154. if (!map->IsDungeon())
  155. {
  156. handler->PSendSysMessage(LANG_NOT_DUNGEON);
  157. handler->SetSentErrorMessage(true);
  158. return false;
  159. }
  160. if (!((InstanceMap*)map)->GetInstanceScript())
  161. {
  162. handler->PSendSysMessage(LANG_NO_INSTANCE_DATA);
  163. handler->SetSentErrorMessage(true);
  164. return false;
  165. }
  166. ((InstanceMap*)map)->GetInstanceScript()->SaveToDB();
  167. return true;
  168. }
  169. static bool HandleInstanceSetBossStateCommand(ChatHandler* handler, char const* args)
  170. {
  171. if (!*args)
  172. return false;
  173. char* param1 = strtok((char*)args, " ");
  174. char* param2 = strtok(nullptr, " ");
  175. char* param3 = strtok(nullptr, " ");
  176. uint32 encounterId = 0;
  177. int32 state = 0;
  178. Player* player = nullptr;
  179. std::string playerName;
  180. // Character name must be provided when using this from console.
  181. if (!param2 || (!param3 && !handler->GetSession()))
  182. {
  183. handler->PSendSysMessage(LANG_CMD_SYNTAX);
  184. handler->SetSentErrorMessage(true);
  185. return false;
  186. }
  187. if (!param3)
  188. player = handler->GetSession()->GetPlayer();
  189. else
  190. {
  191. playerName = param3;
  192. if (normalizePlayerName(playerName))
  193. player = sObjectAccessor->FindPlayerByName(playerName);
  194. }
  195. if (!player)
  196. {
  197. handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND);
  198. handler->SetSentErrorMessage(true);
  199. return false;
  200. }
  201. Map* map = player->GetMap();
  202. if (!map->IsDungeon())
  203. {
  204. handler->PSendSysMessage(LANG_NOT_DUNGEON);
  205. handler->SetSentErrorMessage(true);
  206. return false;
  207. }
  208. if (!map->ToInstanceMap()->GetInstanceScript())
  209. {
  210. handler->PSendSysMessage(LANG_NO_INSTANCE_DATA);
  211. handler->SetSentErrorMessage(true);
  212. return false;
  213. }
  214. encounterId = atoi(param1);
  215. state = atoi(param2);
  216. // Reject improper values.
  217. if (state > TO_BE_DECIDED || encounterId > map->ToInstanceMap()->GetInstanceScript()->GetEncounterCount())
  218. {
  219. handler->PSendSysMessage(LANG_BAD_VALUE);
  220. handler->SetSentErrorMessage(true);
  221. return false;
  222. }
  223. map->ToInstanceMap()->GetInstanceScript()->SetBossState(encounterId, (EncounterState)state);
  224. handler->PSendSysMessage(LANG_COMMAND_INST_SET_BOSS_STATE, encounterId, state);
  225. return true;
  226. }
  227. static bool HandleInstanceGetBossStateCommand(ChatHandler* handler, char const* args)
  228. {
  229. if (!*args)
  230. return false;
  231. char* param1 = strtok((char*)args, " ");
  232. char* param2 = strtok(nullptr, " ");
  233. uint32 encounterId = 0;
  234. Player* player = nullptr;
  235. std::string playerName;
  236. // Character name must be provided when using this from console.
  237. if (!param1 || (!param2 && !handler->GetSession()))
  238. {
  239. handler->PSendSysMessage(LANG_CMD_SYNTAX);
  240. handler->SetSentErrorMessage(true);
  241. return false;
  242. }
  243. if (!param2)
  244. player = handler->GetSession()->GetPlayer();
  245. else
  246. {
  247. playerName = param2;
  248. if (normalizePlayerName(playerName))
  249. player = sObjectAccessor->FindPlayerByName(playerName);
  250. }
  251. if (!player)
  252. {
  253. handler->PSendSysMessage(LANG_PLAYER_NOT_FOUND);
  254. handler->SetSentErrorMessage(true);
  255. return false;
  256. }
  257. Map* map = player->GetMap();
  258. if (!map->IsDungeon())
  259. {
  260. handler->PSendSysMessage(LANG_NOT_DUNGEON);
  261. handler->SetSentErrorMessage(true);
  262. return false;
  263. }
  264. if (!map->ToInstanceMap()->GetInstanceScript())
  265. {
  266. handler->PSendSysMessage(LANG_NO_INSTANCE_DATA);
  267. handler->SetSentErrorMessage(true);
  268. return false;
  269. }
  270. encounterId = atoi(param1);
  271. if (encounterId > map->ToInstanceMap()->GetInstanceScript()->GetEncounterCount())
  272. {
  273. handler->PSendSysMessage(LANG_BAD_VALUE);
  274. handler->SetSentErrorMessage(true);
  275. return false;
  276. }
  277. uint32 state = map->ToInstanceMap()->GetInstanceScript()->GetBossState(encounterId);
  278. handler->PSendSysMessage(LANG_COMMAND_INST_GET_BOSS_STATE, encounterId, state);
  279. return true;
  280. }
  281. };
  282. void AddSC_instance_commandscript()
  283. {
  284. new instance_commandscript();
  285. }