307 lines
9.2 KiB
C++
307 lines
9.2 KiB
C++
#include "precompile.h"
|
|
|
|
#include "chatmgr.h"
|
|
#include "player.h"
|
|
#include "playermgr.h"
|
|
#include "guild.h"
|
|
#include "guildmgr.h"
|
|
#include "typeconvert.h"
|
|
|
|
void ChatMgr::Init()
|
|
{
|
|
|
|
}
|
|
|
|
void ChatMgr::UnInit()
|
|
{
|
|
|
|
}
|
|
|
|
void ChatMgr::FillSMUpdateChatRedPointNotify(Player* hum, cs::SMUpdateChatRedPointNotify& msg)
|
|
{
|
|
if (world_msgrec_.curr_id > hum->world_channel_last_id) {
|
|
msg.add_has_unread_msg_channels(kCCWorld);
|
|
}
|
|
if (hum->GuildId() != 0) {
|
|
auto itr = guild_msgrec_.find(hum->GuildId());
|
|
if (itr != guild_msgrec_.end()) {
|
|
if (itr->second.curr_id > hum->guild_channel_last_id) {
|
|
msg.add_has_unread_msg_channels(kCCGuild);
|
|
}
|
|
}
|
|
}
|
|
ChatedUserRec* chated_user = GetChatedUser(hum->AccountId());
|
|
if (chated_user && chated_user->has_unread_msg) {
|
|
msg.add_has_unread_msg_channels(kCCPrivate);
|
|
}
|
|
}
|
|
|
|
void ChatMgr::FillSMUpdatePrivateChatRedPointNotify(Player* hum,
|
|
cs::SMUpdatePrivateChatRedPointNotify& msg)
|
|
{
|
|
ChatedUserRec* chated_user = GetChatedUser(hum->AccountId());
|
|
if (chated_user) {
|
|
for (auto& pair : chated_user->users) {
|
|
if (pair.second.curr_id < pair.second.last_id) {
|
|
msg.add_has_unread_msg_accounts(pair.first);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void ChatMgr::ProcWorldChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
++world_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(world_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
//p->set_receiver(world_msg_id_);
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
world_msgrec_.curr_id = world_msg_id_;
|
|
world_msgrec_.msg_list.push_back(p);
|
|
world_msgrec_.PopAndDelete(50);
|
|
PlayerMgr::Instance()->TraversePlayer
|
|
([](Player* hum)
|
|
{
|
|
ChatMgr::Instance()->SyncWorldChatMsg(hum);
|
|
});
|
|
}
|
|
|
|
void ChatMgr::ProcPrivateChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
if (hum->AccountId() == msg.target()) {
|
|
return;
|
|
}
|
|
Friend* target = hum->GetFriendById(msg.target());
|
|
if (!target) {
|
|
return;
|
|
}
|
|
++private_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(private_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
{
|
|
TypeConvert::Convert(target->base_data, *p->mutable_receiver()->mutable_base_data());
|
|
TypeConvert::Convert(target->temp_custom_data, *p->mutable_receiver()->mutable_temp_custom_data());
|
|
}
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
AddChatedUser(hum->AccountId(), msg.target(), p, private_msg_id_);
|
|
AddChatedUser(msg.target(), hum->AccountId(), p, private_msg_id_);
|
|
|
|
ChatMgr::Instance()->SyncPrivateChatMsg(hum);
|
|
Player* target_hum = PlayerMgr::Instance()->GetPlayerByAccountId(msg.target());
|
|
if (target_hum) {
|
|
ChatMgr::Instance()->SyncPrivateChatMsg(target_hum);
|
|
}
|
|
}
|
|
|
|
void ChatMgr::ProcGuildChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
if (hum->GuildId() == 0) {
|
|
return;
|
|
}
|
|
++guild_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(guild_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
auto itr = guild_msgrec_.find(hum->GuildId());
|
|
if (itr != guild_msgrec_.end()) {
|
|
itr->second.msg_list.push_back(p);
|
|
itr->second.PopAndDelete(50);
|
|
} else {
|
|
ChatMsgRec msgrec;
|
|
msgrec.curr_id = guild_msg_id_;
|
|
guild_msgrec_[hum->GuildId()] = msgrec;
|
|
}
|
|
Guild* guild = GuildMgr::Instance()->GetGuild(hum->GuildId());
|
|
if (guild) {
|
|
guild->TraverseMember
|
|
(
|
|
[] (GuildMember* member)
|
|
{
|
|
Player* hum = PlayerMgr::Instance()->GetPlayerByAccountId(member->account_id);
|
|
if (hum) {
|
|
ChatMgr::Instance()->SyncGuildChatMsg(hum);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|
|
|
|
void ChatMgr::ProcTeamChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
++temp_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(temp_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
//p->set_receiver(world_msg_id_);
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
cs::SMChatMsgNotify notifymsg;
|
|
*notifymsg.add_msg_list() = *p;
|
|
for (auto& member_id : msg.members()) {
|
|
Player* hum = PlayerMgr::Instance()->GetPlayerByAccountId(member_id);
|
|
if (hum) {
|
|
hum->SendMsg(notifymsg);
|
|
}
|
|
}
|
|
delete p;
|
|
}
|
|
|
|
void ChatMgr::ProcBigHornChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
++temp_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(temp_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
//p->set_receiver(world_msg_id_);
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
cs::SMChatMsgNotify notifymsg;
|
|
*notifymsg.add_msg_list() = *p;
|
|
PlayerMgr::Instance()->TraversePlayer
|
|
([¬ifymsg](Player* hum)
|
|
{
|
|
hum->SendMsg(notifymsg);
|
|
});
|
|
delete p;
|
|
}
|
|
|
|
void ChatMgr::ProcLoopMsgChat(Player* hum, const cs::CMSendChatMsg& msg)
|
|
{
|
|
++temp_msg_id_;
|
|
|
|
cs::MFChatMsg* p = new cs::MFChatMsg();
|
|
p->set_msg_uuid(temp_msg_id_);
|
|
hum->FillMFUserInfo(p->mutable_sender());
|
|
//p->set_receiver(world_msg_id_);
|
|
p->set_chat_channel(msg.chat_channel());
|
|
p->set_msg_type(msg.msg_type());
|
|
p->set_msg_body(msg.msg_body());
|
|
p->set_send_time(time(nullptr));
|
|
|
|
cs::SMChatMsgNotify notifymsg;
|
|
*notifymsg.add_msg_list() = *p;
|
|
PlayerMgr::Instance()->TraversePlayer
|
|
([¬ifymsg](Player* hum)
|
|
{
|
|
hum->SendMsg(notifymsg);
|
|
});
|
|
delete p;
|
|
}
|
|
|
|
ChatedUserRec* ChatMgr::GetChatedUser(const std::string& account_id)
|
|
{
|
|
auto itr = private_chated_users_.find(account_id);
|
|
return itr != private_chated_users_.end() ? &itr->second : nullptr;
|
|
}
|
|
|
|
void ChatMgr::SyncWorldChatMsg(Player* hum)
|
|
{
|
|
if (hum->chat_channel == kCCWorld) {
|
|
cs::SMChatMsgNotify notifymsg;
|
|
for (cs::MFChatMsg* chat_msg : world_msgrec_.msg_list) {
|
|
if (chat_msg->msg_uuid() > hum->world_channel_last_id) {
|
|
*notifymsg.add_msg_list() = *chat_msg;
|
|
hum->world_channel_last_id = chat_msg->msg_uuid();
|
|
}
|
|
}
|
|
if (notifymsg.msg_list().size() > 0) {
|
|
hum->SendMsg(notifymsg);
|
|
}
|
|
} else {
|
|
hum->MarkNewMsg();
|
|
}
|
|
}
|
|
|
|
void ChatMgr::SyncPrivateChatMsg(Player* hum)
|
|
{
|
|
if (hum->chat_channel == kCCPrivate) {
|
|
ChatedUserRec* chated_user = GetChatedUser(hum->AccountId());
|
|
if (chated_user) {
|
|
cs::SMChatMsgNotify notifymsg;
|
|
auto itr = chated_user->users.find(hum->private_target);
|
|
if (itr != chated_user->users.end()) {
|
|
for (cs::MFChatMsg* chat_msg : itr->second.msg_list) {
|
|
if (chat_msg->msg_uuid() > itr->second.curr_id) {
|
|
*notifymsg.add_msg_list() = *chat_msg;
|
|
itr->second.curr_id = chat_msg->msg_uuid();
|
|
}
|
|
}
|
|
}
|
|
if (notifymsg.msg_list().size() > 0) {
|
|
hum->SendMsg(notifymsg);
|
|
}
|
|
}
|
|
} else {
|
|
hum->MarkNewMsg();
|
|
}
|
|
}
|
|
|
|
void ChatMgr::SyncGuildChatMsg(Player* hum)
|
|
{
|
|
if (hum->GuildId() == 0) {
|
|
return;
|
|
}
|
|
auto itr = guild_msgrec_.find(hum->GuildId());
|
|
if (itr != guild_msgrec_.end()) {
|
|
if (hum->chat_channel == kCCGuild) {
|
|
cs::SMChatMsgNotify notifymsg;
|
|
for (cs::MFChatMsg* chat_msg : itr->second.msg_list) {
|
|
if (chat_msg->msg_uuid() > hum->guild_channel_last_id) {
|
|
*notifymsg.add_msg_list() = *chat_msg;
|
|
hum->guild_channel_last_id = chat_msg->msg_uuid();
|
|
}
|
|
}
|
|
if (notifymsg.msg_list().size() > 0) {
|
|
hum->SendMsg(notifymsg);
|
|
}
|
|
} else {
|
|
hum->MarkNewMsg();
|
|
}
|
|
}
|
|
}
|
|
|
|
void ChatMgr::AddChatedUser(const std::string& sender_id, const std::string& receiver_id,
|
|
cs::MFChatMsg* chat_msg, long long last_id)
|
|
{
|
|
auto itr = private_chated_users_.find(sender_id);
|
|
if (itr == private_chated_users_.end()) {
|
|
private_chated_users_[sender_id] = ChatedUserRec();
|
|
itr = private_chated_users_.find(sender_id);
|
|
}
|
|
itr->second.has_unread_msg = true;
|
|
auto itr2 = itr->second.users.find(receiver_id);
|
|
if (itr2 == itr->second.users.end()) {
|
|
itr->second.users[receiver_id] = ChatMsgRec();
|
|
itr2 = itr->second.users.find(receiver_id);
|
|
}
|
|
itr2->second.last_id = last_id;
|
|
itr2->second.msg_list.push_back(chat_msg);
|
|
itr2->second.PopAndDelete(50);
|
|
}
|