ChatChannel.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <string.h>
  2. #include "../../common/Log.h"
  3. #include "../../common/ConfigReader.h"
  4. #include "../../common/PacketStruct.h"
  5. #include "../World.h"
  6. #include "ChatChannel.h"
  7. extern ConfigReader configReader;
  8. extern ZoneList zone_list;
  9. #define CHAT_CHANNEL_JOIN 0
  10. #define CHAT_CHANNEL_LEAVE 1
  11. #define CHAT_CHANNEL_OTHER_JOIN 2
  12. #define CHAT_CHANNEL_OTHER_LEAVE 3
  13. ChatChannel::ChatChannel() {
  14. memset(name, 0, sizeof(name));
  15. memset(password, 0, sizeof(password));
  16. type = CHAT_CHANNEL_TYPE_NONE;
  17. level_restriction = 0;
  18. races = 0;
  19. classes = 0;
  20. }
  21. ChatChannel::~ChatChannel() {
  22. }
  23. bool ChatChannel::IsInChannel(int32 character_id) {
  24. vector<int32>::iterator itr;
  25. for (itr = clients.begin(); itr != clients.end(); itr++) {
  26. if (character_id == *itr)
  27. return true;
  28. }
  29. return false;
  30. }
  31. bool ChatChannel::JoinChannel(Client *client) {
  32. PacketStruct *packet_struct;
  33. vector<int32>::iterator itr;
  34. Client *to_client;
  35. //send the player join packet to the joining client
  36. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", client->GetVersion())) == NULL) {
  37. LogWrite(CHAT__ERROR, 0, "Chat", "Could not find packet 'WS_ChatChannelUpdate' when client %s was trying to join channel %s", client->GetPlayer()->GetName(), name);
  38. return false;
  39. }
  40. packet_struct->setDataByName("action", CHAT_CHANNEL_JOIN);
  41. packet_struct->setDataByName("channel_name", name);
  42. client->QueuePacket(packet_struct->serialize());
  43. safe_delete(packet_struct);
  44. clients.push_back(client->GetCharacterID());
  45. //loop through everyone else in the channel and send the "other" player join packet
  46. for (itr = clients.begin(); itr != clients.end(); itr++) {
  47. if (client->GetCharacterID() == *itr)
  48. continue;
  49. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  50. continue;
  51. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", to_client->GetVersion())) == NULL)
  52. continue;
  53. packet_struct->setDataByName("action", CHAT_CHANNEL_OTHER_JOIN);
  54. packet_struct->setDataByName("channel_name", name);
  55. packet_struct->setDataByName("player_name", client->GetPlayer()->GetName());
  56. to_client->QueuePacket(packet_struct->serialize());
  57. safe_delete(packet_struct);
  58. }
  59. return true;
  60. }
  61. bool ChatChannel::LeaveChannel(Client *client) {
  62. vector<int32>::iterator itr;
  63. PacketStruct *packet_struct;
  64. Client *to_client;
  65. bool ret = false;
  66. for (itr = clients.begin(); itr != clients.end(); itr++) {
  67. if (client->GetCharacterID() == *itr) {
  68. clients.erase(itr);
  69. ret = true;
  70. break;
  71. }
  72. }
  73. if (ret) {
  74. //send the packet to the leaving client
  75. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", client->GetVersion())) == NULL)
  76. return false;
  77. packet_struct->setDataByName("action", CHAT_CHANNEL_LEAVE);
  78. packet_struct->setDataByName("channel_name", name);
  79. client->QueuePacket(packet_struct->serialize());
  80. safe_delete(packet_struct);
  81. //send the leave packet to all other clients in the channel
  82. for (itr = clients.begin(); itr != clients.end(); itr++) {
  83. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  84. continue;
  85. if (to_client == client) // don't need to send to self.
  86. continue;
  87. if ((packet_struct = configReader.getStruct("WS_ChatChannelUpdate", to_client->GetVersion())) == NULL)
  88. continue;
  89. packet_struct->setDataByName("action", CHAT_CHANNEL_OTHER_LEAVE);
  90. packet_struct->setDataByName("channel_name", name);
  91. packet_struct->setDataByName("player_name", client->GetPlayer()->GetName());
  92. to_client->QueuePacket(packet_struct->serialize());
  93. safe_delete(packet_struct);
  94. }
  95. }
  96. return ret;
  97. }
  98. bool ChatChannel::TellChannel(Client *client, const char *message, const char* name2) {
  99. vector<int32>::iterator itr;
  100. PacketStruct *packet_struct;
  101. Client *to_client;
  102. for (itr = clients.begin(); itr != clients.end(); itr++) {
  103. if ((to_client = zone_list.GetClientByCharID(*itr)) == NULL)
  104. continue;
  105. if ((packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion())) == NULL)
  106. continue;
  107. packet_struct->setDataByName("unknown", 0);
  108. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  109. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  110. if (client)
  111. packet_struct->setDataByName("from", client->GetPlayer()->GetName());
  112. else
  113. packet_struct->setDataByName("from", name2);
  114. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  115. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  116. packet_struct->setDataByName("language", client->GetPlayer()->GetCurrentLanguage());
  117. packet_struct->setDataByName("message", message);
  118. packet_struct->setDataByName("channel_name", name);
  119. packet_struct->setDataByName("show_bubble", 1);
  120. if (client->GetPlayer()->GetCurrentLanguage() == 0 || to_client->GetPlayer()->HasLanguage(client->GetPlayer()->GetCurrentLanguage())) {
  121. packet_struct->setDataByName("understood", 1);
  122. }
  123. packet_struct->setDataByName("unknown4", 0);
  124. to_client->QueuePacket(packet_struct->serialize());
  125. safe_delete(packet_struct);
  126. }
  127. return true;
  128. }
  129. bool ChatChannel::TellChannelClient(Client* to_client, const char* message, const char* name2) {
  130. PacketStruct *packet_struct;
  131. if (string(name2).find('[') != string::npos)
  132. return true;
  133. packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion());
  134. if (packet_struct) {
  135. packet_struct->setDataByName("unknown", 0);
  136. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  137. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  138. packet_struct->setDataByName("from", name2);
  139. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  140. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  141. packet_struct->setDataByName("language", 0);
  142. packet_struct->setDataByName("message", message);
  143. packet_struct->setDataByName("channel_name", name);
  144. packet_struct->setDataByName("show_bubble", 1);
  145. packet_struct->setDataByName("understood", 1);
  146. packet_struct->setDataByName("unknown4", 0);
  147. to_client->QueuePacket(packet_struct->serialize());
  148. }
  149. safe_delete(packet_struct);
  150. return true;
  151. }
  152. bool ChatChannel::SendChannelUserList(Client *client) {
  153. vector<int32>::iterator itr;
  154. PacketStruct *packet_struct;
  155. Client *to_client;
  156. int8 i = 0;
  157. if ((packet_struct = configReader.getStruct("WS_WhoChannelQueryReply", client->GetVersion())) == NULL)
  158. return false;
  159. packet_struct->setDataByName("channel_name", name);
  160. packet_struct->setDataByName("unknown", 0);
  161. packet_struct->setArrayLengthByName("num_players", clients.size());
  162. for (itr = clients.begin(); itr != clients.end(); itr++) {
  163. if ((to_client = zone_list.GetClientByCharID(*itr)) != NULL)
  164. packet_struct->setArrayDataByName("player_name", client->GetPlayer()->GetName(), i++);
  165. else
  166. packet_struct->setArrayDataByName("player_name", "<Unknown>", i++);
  167. }
  168. client->QueuePacket(packet_struct->serialize());
  169. safe_delete(packet_struct);
  170. return true;
  171. }