1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-05-31 13:37:45 +00:00
sapphire/src/servers/sapphire_zone/Social/Manager/SocialMgr.h

165 lines
3.2 KiB
C
Raw Normal View History

2017-12-25 05:26:36 -02:00
#ifndef _SOCIALMGR_H
#define _SOCIALMGR_H
2017-11-21 17:01:05 +00:00
2018-03-09 09:50:35 -03:00
#include <Common.h>
2017-11-21 17:01:05 +00:00
#include <map>
#include <cstdint>
#include <boost/enable_shared_from_this.hpp>
2018-03-15 13:21:17 -03:00
#include <boost/shared_ptr.hpp>
2018-01-25 15:32:33 -02:00
#include <sapphire_zone/Forwards.h>
#include <Social/Group.h>
2017-11-21 17:01:05 +00:00
2018-03-05 22:07:41 -03:00
#include "Forwards.h"
2018-03-15 13:21:17 -03:00
#include <Social/FriendList.h>
#include <Database/DatabaseDef.h>
#include "Framework.h"
extern Core::Framework g_fw;
2018-03-05 22:07:41 -03:00
2017-11-21 17:01:05 +00:00
namespace Core {
namespace Social {
2017-11-21 17:01:05 +00:00
template< class T >
class SocialMgr
2017-11-21 17:01:05 +00:00
{
2017-12-07 04:15:20 -02:00
public:
2018-03-05 22:07:41 -03:00
2018-03-15 13:21:17 -03:00
SocialMgr() : m_groupCount( 0 ),
m_lastGroupId( 0 ),
m_groups{}
{
}
~SocialMgr()
{
2017-12-07 04:15:20 -02:00
2018-03-15 13:21:17 -03:00
}
bool init();
//bool< FriendList > init();
boost::shared_ptr< T > findGroupByInviteIdForPlayer( uint64_t playerId ) const
{
auto it = m_invites.find( playerId );
if ( it != m_invites.end() )
{
return findGroupById( it->second );
}
return nullptr;
}
2018-03-15 13:21:17 -03:00
boost::shared_ptr< T > findGroupById( uint64_t groupId ) const
{
auto it = m_groups.find( groupId );
if ( it != m_groups.end() )
{
return it->second;
}
return nullptr;
2018-03-15 13:21:17 -03:00
}
bool hasInvite( uint64_t playerId ) const
{
auto it = m_invites.find( playerId );
if ( it != m_invites.end() )
{
return true;
}
return false;
}
2017-12-07 04:15:20 -02:00
2018-03-15 13:21:17 -03:00
uint64_t loadFriendsList( uint32_t characterId );
protected:
// those would be implemented in T, so you'd have T.m_type and T.m_maxEntries
// GroupType m_type{ GroupType::None };
// uint32_t m_maxEntries{ 0xFFFFFFFF };
2018-03-15 13:21:17 -03:00
uint64_t generateGroupId()
{
m_lastGroupId++;
return m_lastGroupId;
}
2018-03-09 12:09:05 -03:00
uint64_t m_groupCount;
std::map< uint64_t, uint64_t > m_invites;
2018-03-09 12:09:05 -03:00
uint64_t m_lastGroupId;
2018-03-15 13:21:17 -03:00
std::map< uint64_t, boost::shared_ptr< T > > m_groups;
private:
2018-03-09 12:09:05 -03:00
2017-11-21 17:01:05 +00:00
};
}
2018-03-02 02:54:09 -03:00
};
2018-03-15 13:21:17 -03:00
// Specialization
template< class T >
bool Core::Social::SocialMgr< T >::init()
{
return true;
}
template<> inline
uint64_t Core::Social::SocialMgr< Core::Social::FriendList >::loadFriendsList( uint32_t characterId )
{
auto pDb = g_fw.get< Db::DbWorkerPool< Db::CharaDbConnection > >();
auto res = pDb->query( "SELECT CharacterId, CharacterIdList, InviteDataList "
"FROM charainfofriendlist "
"WHERE CharacterId = " + std::to_string( characterId ) );
if ( !res->next() )
return false;
uint64_t ownerId = res->getUInt64( 1 );
auto groupID = generateGroupId();
2018-03-18 01:25:38 -03:00
auto friendsList = Social::FriendList( groupID, ownerId );
2018-03-15 13:21:17 -03:00
2018-03-18 01:25:38 -03:00
// Insert friend content IDs from binary data
std::vector< char > friends;
friends = res->getBlobVector( 2 );
if( friends.size() )
2018-03-15 13:21:17 -03:00
{
2018-03-18 01:25:38 -03:00
std::vector< uint64_t > list( friends.size() / 8 );
// todo: fix this garbage check
if( list.at( 0 ) != 0 )
2018-03-15 13:21:17 -03:00
{
2018-03-18 01:25:38 -03:00
friendsList.getMembers().insert( list.begin(), list.end() );
2018-03-15 13:21:17 -03:00
}
2018-03-18 01:25:38 -03:00
}
2018-03-15 13:21:17 -03:00
2018-03-18 01:25:38 -03:00
// Insert invite data from binary data
2018-03-15 13:21:17 -03:00
2018-03-18 01:25:38 -03:00
std::vector< char > inviteData;
inviteData = res->getBlobVector( 3 );
if( inviteData.size() )
{
std::vector< Social::FriendEntry > list( friends.size() / 8 );
friendsList.getEntries().insert( list.begin(), list.end() );
}
2018-03-15 13:21:17 -03:00
2018-03-18 01:25:38 -03:00
auto friendListPtr = boost::make_shared< Social::FriendList >( friendsList );
2018-03-15 13:21:17 -03:00
m_groups[groupID] = friendListPtr;
return groupID;
}
2018-03-06 04:34:53 -03:00
#endif /* ! _SOCIALMGR_H */