2018-10-26 19:12:55 +02:00
|
|
|
|
|
|
|
#include <Connection.h>
|
|
|
|
#include <Network/Connection.h>
|
|
|
|
#include <Network/Hive.h>
|
|
|
|
#include <Network/PacketContainer.h>
|
|
|
|
|
|
|
|
#include "Network/GameConnection.h"
|
2018-11-20 21:32:13 +01:00
|
|
|
#include "ServerMgr.h"
|
2018-10-26 19:12:55 +02:00
|
|
|
#include "Framework.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Version.h>
|
|
|
|
#include <Logging/Logger.h>
|
2018-06-10 16:34:26 +00:00
|
|
|
#include <Config/ConfigMgr.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Exd/ExdDataGenerated.h>
|
2018-09-09 23:56:22 +02:00
|
|
|
#include <Database/DatabaseDef.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-02-28 11:14:25 +01:00
|
|
|
#include "Actor/Player.h"
|
2018-09-09 23:56:22 +02:00
|
|
|
#include "Actor/BNpcTemplate.h"
|
2018-02-28 11:14:25 +01:00
|
|
|
|
2018-10-26 19:12:55 +02:00
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
#include "Session.h"
|
|
|
|
|
2018-12-01 00:27:16 +11:00
|
|
|
#include "Manager/TerritoryMgr.h"
|
2018-12-02 02:01:41 +01:00
|
|
|
#include "Manager/LinkshellMgr.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-03-02 07:22:25 -03:00
|
|
|
#include "Script/ScriptMgr.h"
|
2018-12-02 02:01:41 +01:00
|
|
|
|
2018-12-16 14:26:38 +01:00
|
|
|
#include <Exd/ExdDataGenerated.h>
|
|
|
|
|
|
|
|
#include <Database/ZoneDbConnection.h>
|
|
|
|
#include <Database/DbWorkerPool.h>
|
|
|
|
#include "Manager/LinkshellMgr.h"
|
|
|
|
#include "Manager/TerritoryMgr.h"
|
|
|
|
#include "Manager/HousingMgr.h"
|
2018-12-22 22:25:03 +01:00
|
|
|
#include "Manager/DebugCommandMgr.h"
|
2018-12-16 14:26:38 +01:00
|
|
|
#include "Manager/PlayerMgr.h"
|
|
|
|
#include "Manager/ShopMgr.h"
|
2018-12-18 22:59:01 +11:00
|
|
|
#include "Manager/InventoryMgr.h"
|
2018-12-23 03:53:08 +01:00
|
|
|
#include "Manager/EventMgr.h"
|
2018-12-23 13:26:33 +01:00
|
|
|
#include "Manager/ItemMgr.h"
|
2018-12-31 23:20:36 +11:00
|
|
|
#include "Manager/MarketMgr.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-01 00:27:16 +11:00
|
|
|
using namespace Sapphire::World::Manager;
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::World::ServerMgr::ServerMgr( const std::string& configName, FrameworkPtr pFw ) :
|
|
|
|
Manager::BaseManager( pFw ),
|
2018-08-29 21:40:59 +02:00
|
|
|
m_configName( configName ),
|
|
|
|
m_bRunning( true ),
|
2018-11-14 09:22:17 +01:00
|
|
|
m_lastDBPingTime( 0 ),
|
|
|
|
m_worldId( 67 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::World::ServerMgr::~ServerMgr()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
size_t Sapphire::World::ServerMgr::getSessionCount() const
|
2017-09-14 17:07:58 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_sessionMapById.size();
|
2017-09-14 17:07:58 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
bool Sapphire::World::ServerMgr::loadSettings( int32_t argc, char* argv[] )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pConfig = framework()->get< Sapphire::ConfigMgr >();
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "Loading config " + m_configName );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
if( !pConfig->loadConfig( m_configName ) )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Error loading config " + m_configName );
|
|
|
|
Logger::fatal( "If this is the first time starting the server, we've copied the default one for your editing pleasure." );
|
2018-08-29 21:40:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:26:38 +01:00
|
|
|
m_port = pConfig->getValue< uint16_t >( "ZoneNetwork", "ListenPort", 54992 );
|
|
|
|
m_ip = pConfig->getValue< std::string >( "ZoneNetwork", "ListenIp", "0.0.0.0" );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::run( int32_t argc, char* argv[] )
|
2018-12-16 14:26:38 +01:00
|
|
|
{
|
|
|
|
using namespace Sapphire;
|
|
|
|
using namespace Sapphire::World;
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::init( "log/world" );
|
2018-12-16 14:26:38 +01:00
|
|
|
|
|
|
|
printBanner();
|
|
|
|
|
|
|
|
auto pConfig = std::make_shared< ConfigMgr >();
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< ConfigMgr >( pConfig );
|
2018-12-16 14:26:38 +01:00
|
|
|
if( !loadSettings( argc, argv ) )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Unable to load settings!" );
|
2018-12-16 14:26:38 +01:00
|
|
|
return;
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "Setting up generated EXD data" );
|
2018-12-16 14:26:38 +01:00
|
|
|
auto pExdData = std::make_shared< Data::ExdDataGenerated >();
|
2018-12-18 21:40:44 +11:00
|
|
|
auto dataPath = pConfig->getValue< std::string >( "GlobalParameters", "DataPath", "" );
|
|
|
|
if( !pExdData->init( dataPath ) )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Error setting up generated EXD data. Make sure that DataPath is set correctly in config.ini" );
|
|
|
|
Logger::fatal( "DataPath: " + dataPath );
|
2018-12-16 14:26:38 +01:00
|
|
|
return;
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< Data::ExdDataGenerated >( pExdData );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-11-29 16:55:48 +01:00
|
|
|
Sapphire::Db::ConnectionInfo info;
|
2018-10-25 17:48:22 +11:00
|
|
|
info.password = pConfig->getValue< std::string >( "Database", "Password", "" );
|
|
|
|
info.host = pConfig->getValue< std::string >( "Database", "Host", "127.0.0.1" );
|
|
|
|
info.database = pConfig->getValue< std::string >( "Database", "Database", "sapphire" );
|
|
|
|
info.port = pConfig->getValue< uint16_t >( "Database", "Port", 3306 );
|
|
|
|
info.user = pConfig->getValue< std::string >( "Database", "Username", "root" );
|
|
|
|
info.syncThreads = pConfig->getValue< uint8_t >( "Database", "SyncThreads", 2 );
|
|
|
|
info.asyncThreads = pConfig->getValue< uint8_t >( "Database", "AsyncThreads", 2 );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-16 14:26:38 +01:00
|
|
|
auto pDb = std::make_shared< Db::DbWorkerPool< Db::ZoneDbConnection > >();
|
|
|
|
Sapphire::Db::DbLoader loader;
|
2018-08-29 21:40:59 +02:00
|
|
|
loader.addDb( *pDb, info );
|
|
|
|
if( !loader.initDbs() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Database not initialized properly!" );
|
2018-08-29 21:40:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< Db::DbWorkerPool< Db::ZoneDbConnection > >( pDb );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "LinkshellMgr: Caching linkshells" );
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pLsMgr = std::make_shared< Manager::LinkshellMgr >( framework() );
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !pLsMgr->loadLinkshells() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Unable to load linkshells!" );
|
2018-08-29 21:40:59 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< Manager::LinkshellMgr >( pLsMgr );
|
2017-08-28 18:36:51 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
auto pScript = std::make_shared< Scripting::ScriptMgr >( framework() );
|
2018-12-16 14:26:38 +01:00
|
|
|
if( !pScript->init() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Failed to setup scripts!" );
|
2018-12-16 14:26:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< Scripting::ScriptMgr >( pScript );
|
2017-12-10 23:51:06 +11:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "TerritoryMgr: Setting up zones" );
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pTeriMgr = std::make_shared< Manager::TerritoryMgr >( framework() );
|
|
|
|
auto pHousingMgr = std::make_shared< Manager::HousingMgr >( framework() );
|
|
|
|
framework()->set< Manager::HousingMgr >( pHousingMgr );
|
|
|
|
framework()->set< Manager::TerritoryMgr >( pTeriMgr );
|
2018-12-22 11:33:21 +11:00
|
|
|
if( !pHousingMgr->init() )
|
|
|
|
{
|
2018-12-29 13:05:13 +11:00
|
|
|
Logger::fatal( "Failed to setup housing!" );
|
2018-12-22 11:33:21 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-16 14:26:38 +01:00
|
|
|
if( !pTeriMgr->init() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::fatal( "Failed to setup zones!" );
|
2018-12-16 14:26:38 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-31 23:20:36 +11:00
|
|
|
auto pMarketMgr = std::make_shared< Manager::MarketMgr >( framework() );
|
|
|
|
framework()->set< Manager::MarketMgr >( pMarketMgr );
|
|
|
|
|
|
|
|
if( !pMarketMgr->init() )
|
|
|
|
{
|
|
|
|
Logger::fatal( "Failed to setup market manager!" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-09 23:56:22 +02:00
|
|
|
loadBNpcTemplates();
|
|
|
|
|
2018-12-16 14:26:38 +01:00
|
|
|
Network::HivePtr hive( new Network::Hive() );
|
2018-12-23 03:53:08 +01:00
|
|
|
Network::addServerToHive< Network::GameConnection >( m_ip, m_port, hive, framework() );
|
2018-12-16 14:26:38 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
std::vector< std::thread > thread_list;
|
|
|
|
thread_list.emplace_back( std::thread( std::bind( &Network::Hive::Run, hive.get() ) ) );
|
2017-12-10 01:52:03 +11:00
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pDebugCom = std::make_shared< DebugCommandMgr >( framework() );
|
|
|
|
auto pPlayerMgr = std::make_shared< Manager::PlayerMgr >( framework() );
|
|
|
|
auto pShopMgr = std::make_shared< Manager::ShopMgr >( framework() );
|
2018-12-29 13:05:13 +11:00
|
|
|
auto pInventoryMgr = std::make_shared< Manager::InventoryMgr >( framework() );
|
2018-12-23 03:53:08 +01:00
|
|
|
auto pEventMgr = std::make_shared< Manager::EventMgr >( framework() );
|
2018-12-23 13:26:33 +01:00
|
|
|
auto pItemMgr = std::make_shared< Manager::ItemMgr >( framework() );
|
2018-12-16 14:26:38 +01:00
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
framework()->set< DebugCommandMgr >( pDebugCom );
|
|
|
|
framework()->set< Manager::PlayerMgr >( pPlayerMgr );
|
|
|
|
framework()->set< Manager::ShopMgr >( pShopMgr );
|
|
|
|
framework()->set< Manager::InventoryMgr >( pInventoryMgr );
|
2018-12-29 00:53:52 +01:00
|
|
|
framework()->set< Manager::EventMgr >( pEventMgr );
|
2018-12-23 13:26:33 +01:00
|
|
|
framework()->set< Manager::ItemMgr >( pItemMgr );
|
2018-12-16 14:26:38 +01:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "World server running on " + m_ip + ":" + std::to_string( m_port ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
mainLoop();
|
2017-10-01 17:58:11 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
for( auto& thread_entry : thread_list )
|
|
|
|
{
|
|
|
|
thread_entry.join();
|
|
|
|
}
|
2017-10-01 17:58:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
uint16_t Sapphire::World::ServerMgr::getWorldId() const
|
2018-11-14 09:22:17 +01:00
|
|
|
{
|
|
|
|
return m_worldId;
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::setWorldId( uint16_t worldId )
|
2018-11-14 09:22:17 +01:00
|
|
|
{
|
|
|
|
m_worldId = worldId;
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::printBanner() const
|
2017-10-03 12:09:51 +02:00
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "===========================================================" );
|
|
|
|
Logger::info( "Sapphire Server Project " );
|
|
|
|
Logger::info( "Version: " + Version::VERSION );
|
|
|
|
Logger::info( "Git Hash: " + Version::GIT_HASH );
|
|
|
|
Logger::info( "Compiled: " __DATE__ " " __TIME__ );
|
|
|
|
Logger::info( "===========================================================" );
|
2017-10-03 12:09:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::mainLoop()
|
2017-10-01 17:58:11 +02:00
|
|
|
{
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pTeriMgr = framework()->get< TerritoryMgr >();
|
|
|
|
auto pScriptMgr = framework()->get< Scripting::ScriptMgr >();
|
|
|
|
auto pDb = framework()->get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
|
2018-03-09 00:06:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
while( isRunning() )
|
|
|
|
{
|
2018-09-26 08:47:22 -04:00
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
auto currTime = Util::getTimeSeconds();
|
2018-01-27 23:52:49 +01:00
|
|
|
|
2018-11-11 12:10:57 +01:00
|
|
|
pTeriMgr->updateTerritoryInstances( static_cast< uint32_t >( currTime ) );
|
2017-12-14 22:30:06 +11:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
pScriptMgr->update();
|
2017-10-01 17:58:11 +02:00
|
|
|
|
2018-09-26 08:47:22 -04:00
|
|
|
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
2018-09-09 23:56:22 +02:00
|
|
|
for( auto sessionIt : m_sessionMapById )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
auto session = sessionIt.second;
|
|
|
|
if( session && session->getPlayer() )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2017-11-14 23:55:38 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
// if the player is in a zone, let the zone handler take care of his updates
|
|
|
|
// else do it here.
|
|
|
|
if( !session->getPlayer()->getCurrentZone() )
|
|
|
|
session->update();
|
2017-11-14 23:55:38 +01:00
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( currTime - m_lastDBPingTime > 3 )
|
|
|
|
{
|
|
|
|
pDb->keepAlive();
|
|
|
|
m_lastDBPingTime = currTime;
|
|
|
|
}
|
|
|
|
|
2018-09-09 23:56:22 +02:00
|
|
|
auto it = m_sessionMapById.begin();
|
|
|
|
for( ; it != m_sessionMapById.end(); )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
auto diff = std::difftime( currTime, it->second->getLastDataTime() );
|
2017-11-20 23:24:50 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
auto pPlayer = it->second->getPlayer();
|
2017-10-01 17:58:11 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
// remove session of players marked for removel ( logoff / kick )
|
|
|
|
if( pPlayer->isMarkedForRemoval() && diff > 5 )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
it->second->close();
|
|
|
|
// if( it->second.unique() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "[" + std::to_string( it->second->getId() ) + "] Session removal" );
|
2018-09-09 23:56:22 +02:00
|
|
|
it = m_sessionMapById.erase( it );
|
2018-08-29 21:40:59 +02:00
|
|
|
removeSession( pPlayer->getName() );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
// remove sessions that simply timed out
|
|
|
|
if( diff > 20 )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "[" + std::to_string( it->second->getId() ) + "] Session time out" );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
it->second->close();
|
|
|
|
// if( it->second.unique() )
|
|
|
|
{
|
2018-09-09 23:56:22 +02:00
|
|
|
it = m_sessionMapById.erase( it );
|
2018-08-29 21:40:59 +02:00
|
|
|
removeSession( pPlayer->getName() );
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
bool Sapphire::World::ServerMgr::createSession( uint32_t sessionId )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
std::lock_guard< std::mutex > lock( m_sessionMutex );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
const auto session_id_str = std::to_string( sessionId );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
auto it = m_sessionMapById.find( sessionId );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( it != m_sessionMapById.end() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::error( "[" + session_id_str + "] Error creating session" );
|
2018-08-29 21:40:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "[" + session_id_str + "] Creating new session" );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-25 01:57:50 +01:00
|
|
|
std::shared_ptr< Session > newSession( new Session( sessionId, framework() ) );
|
2018-08-29 21:40:59 +02:00
|
|
|
m_sessionMapById[ sessionId ] = newSession;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !newSession->loadPlayer() )
|
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::error( "[" + session_id_str + "] Error loading player " + session_id_str );
|
2018-08-29 21:40:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_sessionMapByName[ newSession->getPlayer()->getName() ] = newSession;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return true;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::removeSession( uint32_t sessionId )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_sessionMapById.erase( sessionId );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::World::SessionPtr Sapphire::World::ServerMgr::getSession( uint32_t id )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
//std::lock_guard<std::mutex> lock( m_sessionMutex );
|
|
|
|
auto it = m_sessionMapById.find( id );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( it != m_sessionMapById.end() )
|
|
|
|
return ( it->second );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return nullptr;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::World::SessionPtr Sapphire::World::ServerMgr::getSession( const std::string& playerName )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
//std::lock_guard<std::mutex> lock( m_sessionMutex );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
auto it = m_sessionMapByName.find( playerName );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( it != m_sessionMapByName.end() )
|
|
|
|
return ( it->second );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
return nullptr;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::removeSession( const std::string& playerName )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_sessionMapByName.erase( playerName );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
bool Sapphire::World::ServerMgr::isRunning() const
|
2017-10-01 17:58:11 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_bRunning;
|
2017-10-01 17:58:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
std::string Sapphire::World::ServerMgr::getPlayerNameFromDb( uint32_t playerId, bool forceDbLoad )
|
2018-11-13 00:03:09 +01:00
|
|
|
{
|
2018-11-23 11:46:39 +01:00
|
|
|
if( !forceDbLoad )
|
|
|
|
{
|
|
|
|
auto it = m_playerNameMapById.find( playerId );
|
2018-11-23 09:19:49 +01:00
|
|
|
|
2018-11-23 11:46:39 +01:00
|
|
|
if( it != m_playerNameMapById.end() )
|
|
|
|
return ( it->second );
|
|
|
|
}
|
2018-11-23 09:19:49 +01:00
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pDb = framework()->get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
|
2018-11-13 00:03:09 +01:00
|
|
|
auto res = pDb->query( "SELECT name FROM charainfo WHERE characterid = " + std::to_string( playerId ) );
|
|
|
|
|
|
|
|
if( !res->next() )
|
|
|
|
return "Unknown";
|
|
|
|
|
2018-11-23 09:19:49 +01:00
|
|
|
std::string playerName = res->getString( 1 );
|
2018-11-23 11:59:36 +01:00
|
|
|
updatePlayerName( playerId, playerName );
|
2018-11-23 09:19:49 +01:00
|
|
|
|
|
|
|
return playerName;
|
2018-11-13 00:03:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::updatePlayerName( uint32_t playerId, const std::string & playerNewName )
|
2018-11-23 09:42:51 +01:00
|
|
|
{
|
2018-11-23 11:04:19 +01:00
|
|
|
m_playerNameMapById[ playerId ] = playerNewName;
|
2018-11-23 09:42:51 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
void Sapphire::World::ServerMgr::loadBNpcTemplates()
|
2018-09-09 23:56:22 +02:00
|
|
|
{
|
2018-12-22 22:25:03 +01:00
|
|
|
auto pDb = framework()->get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
|
|
|
|
auto pTeriMgr = framework()->get< TerritoryMgr >();
|
2018-09-09 23:56:22 +02:00
|
|
|
|
|
|
|
auto stmt = pDb->getPreparedStatement( Db::ZoneDbStatements::ZONE_SEL_BNPCTEMPLATES );
|
|
|
|
|
|
|
|
auto res = pDb->query( stmt );
|
|
|
|
|
|
|
|
while( res->next() )
|
|
|
|
{
|
|
|
|
//Id, Name, bNPCBaseId, bNPCNameId, mainWeaponModel,
|
|
|
|
//secWeaponModel, aggressionMode, enemyType, pose,
|
|
|
|
//modelChara, displayFlags, Look, Models
|
|
|
|
|
|
|
|
auto id = res->getUInt( 1 );
|
|
|
|
auto name = res->getString( 2 );
|
|
|
|
auto bNPCBaseId = res->getUInt( 3 );
|
|
|
|
auto bNPCNameId = res->getUInt( 4 );
|
|
|
|
auto mainWeaponModel = res->getUInt64( 5 );
|
|
|
|
auto secWeaponModel = res->getUInt64( 6 );
|
|
|
|
auto aggressionMode = res->getUInt8( 7 );
|
|
|
|
auto enemyType = res->getUInt8( 8 );
|
|
|
|
auto pose = res->getUInt8( 9 );
|
|
|
|
auto modelChara = res->getUInt( 10 );
|
|
|
|
auto displayFlags = res->getUInt( 11 );
|
|
|
|
auto look = res->getBlobVector( 12 );
|
|
|
|
auto models = res->getBlobVector( 13 );
|
|
|
|
|
2018-10-25 12:44:51 +11:00
|
|
|
auto bnpcTemplate = std::make_shared< Entity::BNpcTemplate >(
|
2018-09-09 23:56:22 +02:00
|
|
|
id, bNPCBaseId, bNPCNameId, mainWeaponModel, secWeaponModel,
|
|
|
|
aggressionMode, enemyType, 0, pose, modelChara, displayFlags,
|
|
|
|
reinterpret_cast< uint32_t* >( &models[ 0 ] ),
|
|
|
|
reinterpret_cast< uint8_t* >( &look[ 0 ] ) );
|
|
|
|
|
|
|
|
m_bNpcTemplateMap[ name ] = bnpcTemplate;
|
|
|
|
}
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::debug( "BNpc Templates loaded: " + std::to_string( m_bNpcTemplateMap.size() ) );
|
2018-09-09 23:56:22 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::Entity::BNpcTemplatePtr Sapphire::World::ServerMgr::getBNpcTemplate( const std::string& key )
|
2018-09-09 23:56:22 +02:00
|
|
|
{
|
|
|
|
auto it = m_bNpcTemplateMap.find( key );
|
|
|
|
|
|
|
|
if( it == m_bNpcTemplateMap.end() )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:25:03 +01:00
|
|
|
Sapphire::Entity::BNpcTemplatePtr Sapphire::World::ServerMgr::getBNpcTemplate( uint32_t id )
|
2018-09-09 23:56:22 +02:00
|
|
|
{
|
|
|
|
for( auto entry : m_bNpcTemplateMap )
|
|
|
|
{
|
|
|
|
if( entry.second->getId() == id )
|
|
|
|
return entry.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2018-09-26 08:47:22 -04:00
|
|
|
}
|