ChatChannel.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 != NULL){
  111. packet_struct->setDataByName("from", client->GetPlayer()->GetName());
  112. } else {
  113. char name3[128];
  114. sprintf(name3,"[%s] from discord",name2);
  115. packet_struct->setDataByName("from", name3);
  116. }
  117. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  118. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  119. if(client != NULL){
  120. packet_struct->setDataByName("language", client->GetPlayer()->GetCurrentLanguage());
  121. }else{
  122. packet_struct->setDataByName("language", 0);
  123. }
  124. packet_struct->setDataByName("message", message);
  125. packet_struct->setDataByName("channel_name", name);
  126. packet_struct->setDataByName("show_bubble", 1);
  127. if(client != NULL){
  128. if (client->GetPlayer()->GetCurrentLanguage() == 0 || to_client->GetPlayer()->HasLanguage(client->GetPlayer()->GetCurrentLanguage())) {
  129. packet_struct->setDataByName("understood", 1);
  130. }
  131. } else {
  132. packet_struct->setDataByName("understood", 1);
  133. }
  134. packet_struct->setDataByName("unknown4", 0);
  135. to_client->QueuePacket(packet_struct->serialize());
  136. safe_delete(packet_struct);
  137. }
  138. return true;
  139. }
  140. bool ChatChannel::TellChannelClient(Client* to_client, const char* message, const char* name2) {
  141. PacketStruct *packet_struct;
  142. if (string(name2).find('[') != string::npos)
  143. return true;
  144. packet_struct = configReader.getStruct("WS_HearChat", to_client->GetVersion());
  145. if (packet_struct) {
  146. packet_struct->setDataByName("unknown", 0);
  147. packet_struct->setDataByName("from_spawn_id", 0xFFFFFFFF);
  148. packet_struct->setDataByName("to_spawn_id", 0xFFFFFFFF);
  149. packet_struct->setDataByName("from", name2);
  150. packet_struct->setDataByName("to", to_client->GetPlayer()->GetName());
  151. packet_struct->setDataByName("channel", to_client->GetMessageChannelColor(CHANNEL_CUSTOM_CHANNEL));
  152. packet_struct->setDataByName("language", 0);
  153. packet_struct->setDataByName("message", message);
  154. packet_struct->setDataByName("channel_name", name);
  155. packet_struct->setDataByName("show_bubble", 1);
  156. packet_struct->setDataByName("understood", 1);
  157. packet_struct->setDataByName("unknown4", 0);
  158. to_client->QueuePacket(packet_struct->serialize());
  159. }
  160. safe_delete(packet_struct);
  161. return true;
  162. }
  163. bool ChatChannel::SendChannelUserList(Client *client) {
  164. vector<int32>::iterator itr;
  165. PacketStruct *packet_struct;
  166. Client *to_client;
  167. int8 i = 0;
  168. if ((packet_struct = configReader.getStruct("WS_WhoChannelQueryReply", client->GetVersion())) == NULL)
  169. return false;
  170. packet_struct->setDataByName("channel_name", name);
  171. packet_struct->setDataByName("unknown", 0);
  172. packet_struct->setArrayLengthByName("num_players", clients.size());
  173. for (itr = clients.begin(); itr != clients.end(); itr++) {
  174. if ((to_client = zone_list.GetClientByCharID(*itr)) != NULL)
  175. packet_struct->setArrayDataByName("player_name", client->GetPlayer()->GetName(), i++);
  176. else
  177. packet_struct->setArrayDataByName("player_name", "<Unknown>", i++);
  178. }
  179. client->QueuePacket(packet_struct->serialize());
  180. safe_delete(packet_struct);
  181. return true;
  182. }