2017-08-08 13:53:47 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
2017-12-18 12:36:52 +01:00
|
|
|
#include <common/Util/Util.h>
|
|
|
|
#include <common/Network/PacketContainer.h>
|
2017-12-08 15:38:25 +01:00
|
|
|
#include "Network/GameConnection.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
#include "Session.h"
|
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
#include "Actor/Player.h"
|
2018-01-08 21:42:44 +01:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <common/Logging/Logger.h>
|
|
|
|
|
|
|
|
extern Core::Logger g_log;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
Core::Session::Session( uint32_t sessionId )
|
|
|
|
: m_sessionId( sessionId )
|
2017-10-04 00:02:16 +02:00
|
|
|
, m_lastDataTime( static_cast< uint32_t >( Util::getTimeSeconds() ) )
|
|
|
|
, m_lastSqlTime( static_cast< uint32_t >( Util::getTimeSeconds() ) )
|
2017-10-06 12:54:03 +02:00
|
|
|
, m_isValid( false )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
// boost::posix_time::ptime now = boost::date_time::not_a_date_time;
|
|
|
|
// now = boost::posix_time::microsec_clock::universal_time();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::Session::~Session()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
void Core::Session::setZoneConnection( Network::GameConnectionPtr pZoneCon )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
pZoneCon->m_conType = Network::ConnectionType::Zone;
|
|
|
|
m_pZoneConnection = pZoneCon;
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
void Core::Session::setChatConnection( Network::GameConnectionPtr pChatCon )
|
2017-08-20 20:48:55 +02:00
|
|
|
{
|
|
|
|
pChatCon->m_conType = Network::ConnectionType::Chat;
|
|
|
|
m_pChatConnection = pChatCon;
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
Core::Network::GameConnectionPtr Core::Session::getZoneConnection() const
|
|
|
|
{
|
|
|
|
return m_pZoneConnection;
|
|
|
|
}
|
|
|
|
|
2017-08-20 20:48:55 +02:00
|
|
|
Core::Network::GameConnectionPtr Core::Session::getChatConnection() const
|
|
|
|
{
|
|
|
|
return m_pChatConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
bool Core::Session::loadPlayer()
|
|
|
|
{
|
|
|
|
|
|
|
|
m_pPlayer = Entity::PlayerPtr( new Entity::Player() );
|
|
|
|
|
|
|
|
if( !m_pPlayer->load( m_sessionId, shared_from_this() ) )
|
2017-10-06 12:54:03 +02:00
|
|
|
{
|
|
|
|
m_isValid = false;
|
2017-08-08 13:53:47 +02:00
|
|
|
return false;
|
2017-10-06 12:54:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_isValid = true;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::close()
|
|
|
|
{
|
|
|
|
if( m_pZoneConnection )
|
|
|
|
m_pZoneConnection->Disconnect();
|
|
|
|
|
2017-10-06 00:13:29 +02:00
|
|
|
if( m_pChatConnection )
|
|
|
|
m_pChatConnection->Disconnect();
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
// remove the session from the player
|
|
|
|
if( m_pPlayer )
|
|
|
|
// reset the zone, so the zone handler knows to remove the actor
|
|
|
|
m_pPlayer->setCurrentZone( nullptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Core::Session::getId() const
|
|
|
|
{
|
|
|
|
return m_sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Core::Session::getLastDataTime() const
|
|
|
|
{
|
|
|
|
return m_lastDataTime;
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:02:16 +02:00
|
|
|
uint32_t Core::Session::getLastSqlTime() const
|
|
|
|
{
|
|
|
|
return m_lastSqlTime;
|
|
|
|
}
|
|
|
|
|
2017-10-06 12:54:03 +02:00
|
|
|
bool Core::Session::isValid() const
|
|
|
|
{
|
|
|
|
return m_isValid;
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
void Core::Session::updateLastDataTime()
|
|
|
|
{
|
2017-10-04 00:02:16 +02:00
|
|
|
m_lastDataTime = static_cast< uint32_t >( Util::getTimeSeconds() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::updateLastSqlTime()
|
|
|
|
{
|
|
|
|
m_lastSqlTime = static_cast< uint32_t >( Util::getTimeSeconds() );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:42:44 +01:00
|
|
|
void Core::Session::startReplay( const std::string& folderpath )
|
|
|
|
{
|
2018-01-08 23:23:09 +01:00
|
|
|
if( !boost::filesystem::exists( folderpath ) )
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
|
|
|
getPlayer()->sendDebug( "Couldn't find folder." );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_replayCache.clear();
|
|
|
|
|
2018-01-08 23:23:09 +01:00
|
|
|
std::vector< std::tuple< uint64_t, std::string > > loadedSets;
|
2018-01-08 21:42:44 +01:00
|
|
|
|
|
|
|
for( auto it = boost::filesystem::directory_iterator( boost::filesystem::path( folderpath ) ); it != boost::filesystem::directory_iterator(); ++it )
|
|
|
|
{
|
|
|
|
// Get the filename of the current element
|
2018-01-08 23:23:09 +01:00
|
|
|
auto fileName = it->path().filename().string();
|
|
|
|
auto unixTime = atoi( fileName.substr( 0, 10 ).c_str() );
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-01-08 23:23:09 +01:00
|
|
|
if( unixTime > 1000000000)
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
2018-01-08 23:23:09 +01:00
|
|
|
loadedSets.push_back( std::tuple< uint64_t, std::string >( unixTime, it->path().string() ) );
|
2018-01-08 21:42:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 23:23:09 +01:00
|
|
|
sort( loadedSets.begin(), loadedSets.end(), []( const std::tuple< uint64_t, std::string >& left, const std::tuple< uint64_t, std::string >& right)
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
2018-01-08 23:23:09 +01:00
|
|
|
return std::get< 0 >( left ) < std::get< 0 >( right );
|
2018-01-08 21:42:44 +01:00
|
|
|
} );
|
|
|
|
|
2018-01-08 23:23:09 +01:00
|
|
|
int startTime = std::get< 0 >( loadedSets.at( 0 ) );
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-01-08 23:23:09 +01:00
|
|
|
for( auto set : loadedSets )
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
2018-01-08 23:23:09 +01:00
|
|
|
m_replayCache.push_back( std::tuple<uint64_t, std::string>( Util::getTimeSeconds() + ( std::get< 0 >( set ) - startTime ), std::get< 1 >( set ) ) );
|
|
|
|
g_log.info( "Registering " + std::get< 1 >( set ) + " for " + std::to_string( std::get< 0 >( set ) - startTime ) );
|
2018-01-08 21:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getPlayer()->sendDebug( "Registered " + std::to_string( m_replayCache.size() ) + " sets for replay" );
|
|
|
|
m_isReplaying = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::stopReplay()
|
|
|
|
{
|
|
|
|
m_isReplaying = false;
|
|
|
|
m_replayCache.clear();
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
void Core::Session::update()
|
|
|
|
{
|
2018-01-08 21:42:44 +01:00
|
|
|
if( m_isReplaying )
|
|
|
|
{
|
|
|
|
int at = 0;
|
2018-01-08 23:23:09 +01:00
|
|
|
for( const auto& set : m_replayCache )
|
|
|
|
{
|
|
|
|
if( std::get< 0 >( set ) == Util::getTimeSeconds() )
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
2018-01-08 23:23:09 +01:00
|
|
|
m_pZoneConnection->injectPacket( std::get< 1 >( set ), *getPlayer().get() );
|
2018-01-08 21:42:44 +01:00
|
|
|
m_replayCache.erase( m_replayCache.begin() + at );
|
|
|
|
}
|
|
|
|
at++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
if( m_pZoneConnection )
|
|
|
|
{
|
|
|
|
m_pZoneConnection->processInQueue();
|
|
|
|
|
|
|
|
// SESSION LOGIC
|
|
|
|
m_pPlayer->update( Util::getTimeMs() );
|
|
|
|
|
2017-10-04 00:02:16 +02:00
|
|
|
if( ( static_cast< uint32_t >( Util::getTimeSeconds() ) - static_cast< uint32_t >( getLastSqlTime() ) ) > 10 )
|
|
|
|
{
|
|
|
|
updateLastSqlTime();
|
|
|
|
m_pPlayer->updateSql();
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
m_pZoneConnection->processOutQueue();
|
|
|
|
}
|
|
|
|
|
2017-11-16 23:20:59 +01:00
|
|
|
if( m_pChatConnection )
|
2017-08-20 20:48:55 +02:00
|
|
|
{
|
|
|
|
m_pChatConnection->processInQueue();
|
|
|
|
m_pChatConnection->processOutQueue();
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Core::Entity::PlayerPtr Core::Session::getPlayer() const
|
|
|
|
{
|
|
|
|
return m_pPlayer;
|
|
|
|
}
|
|
|
|
|