2023-02-02 02:30:31 -03:00
|
|
|
#include <Network/PacketWrappers/ChatToChannelPacket.h>
|
2021-11-27 00:53:57 +01:00
|
|
|
#include <Logging/Logger.h>
|
|
|
|
#include <Service.h>
|
|
|
|
|
|
|
|
#include "ChatChannelMgr.h"
|
2023-02-25 15:58:11 +01:00
|
|
|
#include "PlayerMgr.h"
|
2021-11-27 00:53:57 +01:00
|
|
|
|
|
|
|
#include "Actor/Player.h"
|
|
|
|
#include "WorldServer.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Network/GameConnection.h"
|
|
|
|
|
|
|
|
using namespace Sapphire;
|
|
|
|
using namespace Sapphire::Network;
|
|
|
|
using namespace Sapphire::Network::Packets;
|
|
|
|
using namespace Sapphire::World::Manager;
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
const uint64_t ChatChannelMgr::createChatChannel( Common::ChatChannelType type )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
auto& server = Common::Service< World::WorldServer >::ref();
|
|
|
|
|
|
|
|
// get next id for new channel
|
|
|
|
|
|
|
|
uint32_t cNo = m_lastChatNo;
|
|
|
|
|
|
|
|
m_lastChatNo++;
|
|
|
|
|
|
|
|
uint16_t chatType = static_cast< uint16_t >( type );
|
|
|
|
uint16_t worldId = server.getWorldId();
|
|
|
|
|
|
|
|
Data::ChatChannel cId;
|
|
|
|
|
|
|
|
cId.data.ChannelNo = cNo;
|
|
|
|
cId.data.ChannelType = type;
|
|
|
|
cId.data.WorldId = worldId;
|
|
|
|
|
|
|
|
// create our new chat channel
|
|
|
|
|
|
|
|
Data::ChatChannelMembers newChatChannel = {};
|
|
|
|
|
|
|
|
m_channels[ cId.ChannelID ] = newChatChannel;
|
|
|
|
|
|
|
|
Logger::debug( "Chat channel ID "
|
|
|
|
+ std::to_string( cId.ChannelID )
|
|
|
|
+ " created"
|
|
|
|
);
|
|
|
|
|
|
|
|
return cId.ChannelID;
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
void ChatChannelMgr::addToChannel( uint64_t channelId, Entity::Player& player )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
if( !isChannelValid( channelId ) )
|
|
|
|
{
|
|
|
|
// channel id is invalid
|
|
|
|
|
|
|
|
Logger::warn( "Attempted to add player "
|
|
|
|
+ std::to_string( player.getId() )
|
|
|
|
+ " to invalid channel ID "
|
|
|
|
+ std::to_string( channelId )
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& channelMembers = m_channels[ channelId ];
|
2023-02-02 02:30:31 -03:00
|
|
|
auto id = player.getId();
|
2021-11-27 00:53:57 +01:00
|
|
|
|
2023-02-02 02:30:31 -03:00
|
|
|
if( std::find( channelMembers.begin(), channelMembers.end(), id ) == channelMembers.end() )
|
|
|
|
m_channels[ channelId ].emplace_back( id );
|
2021-11-27 00:53:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
void ChatChannelMgr::removeFromChannel( uint64_t channelId, Entity::Player& player )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
if( !isChannelValid( channelId ) )
|
|
|
|
{
|
|
|
|
// channel id is invalid
|
|
|
|
|
|
|
|
Logger::warn( "Attempted to remove player "
|
|
|
|
+ std::to_string( player.getId() )
|
|
|
|
+ " from invalid channel ID "
|
|
|
|
+ std::to_string( channelId )
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& channelMembers = m_channels[ channelId ];
|
2023-02-02 02:30:31 -03:00
|
|
|
auto id = player.getId();
|
2021-11-27 00:53:57 +01:00
|
|
|
|
2023-02-02 02:30:31 -03:00
|
|
|
auto it = std::find( channelMembers.begin(), channelMembers.end(), id );
|
2021-11-27 00:53:57 +01:00
|
|
|
if( it != channelMembers.end() )
|
|
|
|
channelMembers.erase( it );
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
void ChatChannelMgr::sendMessageToChannel( uint64_t channelId, Entity::Player& sender, const std::string& message )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
if( !isChannelValid( channelId ) )
|
|
|
|
{
|
|
|
|
// channel id is invalid
|
|
|
|
|
|
|
|
Logger::warn( "Attempted to send message from player "
|
|
|
|
+ std::to_string( sender.getId() )
|
|
|
|
+ " to invalid channel ID "
|
|
|
|
+ std::to_string( channelId )
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& channelMembers = m_channels[ channelId ];
|
|
|
|
|
|
|
|
auto& server = Common::Service< World::WorldServer >::ref();
|
2023-02-25 15:58:11 +01:00
|
|
|
auto& playerMgr = Common::Service< World::Manager::PlayerMgr >::ref();
|
2023-02-02 02:30:31 -03:00
|
|
|
|
2021-11-27 00:53:57 +01:00
|
|
|
// send message to all players in chat channel
|
2023-02-02 02:30:31 -03:00
|
|
|
for( const auto id : channelMembers )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
// skip sender from getting their own message
|
2023-02-02 02:30:31 -03:00
|
|
|
if( id == sender.getId() )
|
2021-11-27 00:53:57 +01:00
|
|
|
continue;
|
|
|
|
|
2023-02-02 02:30:31 -03:00
|
|
|
auto pSession = server.getSession( id );
|
2021-11-27 00:53:57 +01:00
|
|
|
|
2023-02-02 02:30:31 -03:00
|
|
|
// check if player is online to recv message
|
2021-11-27 00:53:57 +01:00
|
|
|
if( !pSession )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-25 15:58:11 +01:00
|
|
|
auto pPlayer = playerMgr.getPlayer( id );
|
2021-11-27 00:53:57 +01:00
|
|
|
|
2023-02-02 02:30:31 -03:00
|
|
|
// prepare message packet, associating message and sender info with channel data
|
|
|
|
auto chatToChannelPacket = std::make_shared< Packets::Server::ChatToChannelPacket >( *pPlayer, sender, channelId, message );
|
2021-11-27 00:53:57 +01:00
|
|
|
pSession->getChatConnection()->queueOutPacket( chatToChannelPacket );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
bool ChatChannelMgr::isChannelValid( uint64_t channelId ) const
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
return !( m_channels.find( channelId ) == m_channels.end() );
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:43 +01:00
|
|
|
const Data::ChatChannelMembers& ChatChannelMgr::getChatChannel( uint64_t channelId )
|
2021-11-27 00:53:57 +01:00
|
|
|
{
|
|
|
|
bool channelValid = isChannelValid( channelId );
|
|
|
|
assert( channelValid );
|
|
|
|
|
|
|
|
return m_channels[ channelId ];
|
|
|
|
}
|