2018-03-02 07:22:25 -03:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2017-08-08 13:53:47 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Util/Util.h>
|
|
|
|
#include <Network/PacketContainer.h>
|
|
|
|
#include <Logging/Logger.h>
|
2018-03-02 07:22:25 -03:00
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
#include "Network/GameConnection.h"
|
2018-03-02 07:22:25 -03:00
|
|
|
#include "Actor/Player.h"
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
#include "Session.h"
|
2018-03-02 07:22:25 -03:00
|
|
|
#include "Framework.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-03-09 00:06:44 +01:00
|
|
|
extern Core::Framework g_fw;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-02-02 17:37:51 +01:00
|
|
|
Core::Session::Session( uint32_t sessionId ) :
|
2018-08-29 21:40:59 +02:00
|
|
|
m_sessionId( sessionId ),
|
|
|
|
m_lastDataTime( Util::getTimeSeconds() ),
|
|
|
|
m_lastSqlTime( Util::getTimeSeconds() ),
|
|
|
|
m_isValid( false )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
pZoneCon->m_conType = Network::ConnectionType::Zone;
|
|
|
|
m_pZoneConnection = pZoneCon;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:38:25 +01:00
|
|
|
void Core::Session::setChatConnection( Network::GameConnectionPtr pChatCon )
|
2017-08-20 20:48:55 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
pChatCon->m_conType = Network::ConnectionType::Chat;
|
|
|
|
m_pChatConnection = pChatCon;
|
2017-08-20 20:48:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
Core::Network::GameConnectionPtr Core::Session::getZoneConnection() const
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_pZoneConnection;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 20:48:55 +02:00
|
|
|
Core::Network::GameConnectionPtr Core::Session::getChatConnection() const
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_pChatConnection;
|
2017-08-20 20:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
bool Core::Session::loadPlayer()
|
|
|
|
{
|
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_pPlayer = Entity::make_Player();
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( !m_pPlayer->load( m_sessionId, shared_from_this() ) )
|
|
|
|
{
|
|
|
|
m_isValid = false;
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_isValid = true;
|
|
|
|
|
|
|
|
return true;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::close()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_pZoneConnection )
|
|
|
|
m_pZoneConnection->Disconnect();
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_pChatConnection )
|
|
|
|
m_pChatConnection->Disconnect();
|
2017-10-06 00:13:29 +02:00
|
|
|
|
2018-08-29 21:40:59 +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 );
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Core::Session::getId() const
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_sessionId;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 01:22:58 +11:00
|
|
|
int64_t Core::Session::getLastDataTime() const
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_lastDataTime;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:37:51 +01:00
|
|
|
int64_t Core::Session::getLastSqlTime() const
|
2017-10-04 00:02:16 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_lastSqlTime;
|
2017-10-04 00:02:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 12:54:03 +02:00
|
|
|
bool Core::Session::isValid() const
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_isValid;
|
2017-10-06 12:54:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
void Core::Session::updateLastDataTime()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_lastDataTime = Util::getTimeSeconds();
|
2017-10-04 00:02:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::updateLastSqlTime()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_lastSqlTime = Util::getTimeSeconds();
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 23:38:27 +01:00
|
|
|
void Core::Session::startReplay( const std::string& path )
|
2018-01-08 21:42:44 +01:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
auto pLog = g_fw.get< Logger >();
|
|
|
|
if( !boost::filesystem::exists( path ) )
|
|
|
|
{
|
|
|
|
getPlayer()->sendDebug( "Couldn't find folder." );
|
|
|
|
return;
|
|
|
|
}
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_replayCache.clear();
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
std::vector< std::tuple< uint64_t, std::string > > loadedSets;
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
for( auto it = boost::filesystem::directory_iterator( boost::filesystem::path( path ) );
|
|
|
|
it != boost::filesystem::directory_iterator(); ++it )
|
|
|
|
{
|
|
|
|
// Get the filename of the current element
|
|
|
|
auto fileName = it->path().filename().string();
|
|
|
|
auto unixTime = std::stoull( fileName.substr( 0, 14 ).c_str() );
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( unixTime > 1000000000 )
|
|
|
|
{
|
|
|
|
loadedSets.push_back( std::tuple< uint64_t, std::string >( unixTime, it->path().string() ) );
|
|
|
|
}
|
|
|
|
}
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
sort( loadedSets.begin(), loadedSets.end(),
|
|
|
|
[]( const std::tuple< uint64_t, std::string >& left, const std::tuple< uint64_t, std::string >& right )
|
|
|
|
{
|
|
|
|
return std::get< 0 >( left ) < std::get< 0 >( right );
|
|
|
|
} );
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
uint64_t startTime = std::get< 0 >( loadedSets.at( 0 ) );
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
for( auto set : loadedSets )
|
|
|
|
{
|
|
|
|
m_replayCache.push_back( std::tuple< uint64_t, std::string >(
|
|
|
|
Util::getTimeMs() + ( std::get< 0 >( set ) - startTime ), std::get< 1 >( set ) ) );
|
2018-01-08 23:38:27 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
pLog->info( "Registering " + std::get< 1 >( set ) + " for " + std::to_string( std::get< 0 >( set ) - startTime ) );
|
|
|
|
}
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
getPlayer()->sendDebug( "Registered " + std::to_string( m_replayCache.size() ) + " sets for replay" );
|
|
|
|
m_isReplaying = true;
|
2018-01-08 21:42:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::stopReplay()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
m_isReplaying = false;
|
|
|
|
m_replayCache.clear();
|
2018-01-08 21:42:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-08 23:38:27 +01:00
|
|
|
void Core::Session::processReplay()
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
int at = 0;
|
|
|
|
for( const auto& set : m_replayCache )
|
|
|
|
{
|
|
|
|
if( std::get< 0 >( set ) <= Util::getTimeMs() )
|
|
|
|
{
|
|
|
|
m_pZoneConnection->injectPacket( std::get< 1 >( set ), *getPlayer().get() );
|
|
|
|
m_replayCache.erase( m_replayCache.begin() + at );
|
|
|
|
//g_framework.getLogger().info( "Sent for " + std::to_string( std::get< 0 >( set ) ) + ", left: " + std::to_string( m_replayCache.size() ) );
|
|
|
|
}
|
|
|
|
at++;
|
|
|
|
}
|
2018-01-09 18:54:09 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_replayCache.size() == 0 )
|
|
|
|
m_isReplaying = false;
|
2018-01-09 18:54:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::sendReplayInfo()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
std::string message = std::to_string( m_replayCache.size() ) + " Sets left in cache, ";
|
2018-01-09 18:54:09 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_isReplaying )
|
|
|
|
message += " is active";
|
|
|
|
else
|
|
|
|
message += " is idle";
|
2018-01-09 18:54:09 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
getPlayer()->sendDebug( message );
|
2018-01-08 23:38:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Session::update()
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_isReplaying )
|
|
|
|
processReplay();
|
|
|
|
|
|
|
|
if( m_pZoneConnection )
|
|
|
|
{
|
|
|
|
m_pZoneConnection->processInQueue();
|
2018-01-08 21:42:44 +01:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
// SESSION LOGIC
|
|
|
|
m_pPlayer->update( Util::getTimeMs() );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +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
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
m_pZoneConnection->processOutQueue();
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-08-29 21:40:59 +02:00
|
|
|
if( m_pChatConnection )
|
|
|
|
{
|
|
|
|
m_pChatConnection->processInQueue();
|
|
|
|
m_pChatConnection->processOutQueue();
|
|
|
|
}
|
2017-08-20 20:48:55 +02:00
|
|
|
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Core::Entity::PlayerPtr Core::Session::getPlayer() const
|
|
|
|
{
|
2018-08-29 21:40:59 +02:00
|
|
|
return m_pPlayer;
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
|
|
|
|