1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-28 15:17:46 +00:00
sapphire/src/world/Territory/House.cpp

190 lines
4.8 KiB
C++
Raw Normal View History

2018-11-19 09:40:44 +01:00
#include <set>
2018-11-19 10:04:54 +01:00
#include <string.h>
2018-11-19 09:40:44 +01:00
#include <Logging/Logger.h>
#include <Exd/ExdDataGenerated.h>
2018-11-25 01:55:53 +11:00
#include <Database/DatabaseDef.h>
2018-11-26 23:32:22 +11:00
#include <Database/ZoneDbConnection.h>
2018-11-19 09:40:44 +01:00
#include "House.h"
#include <unordered_map>
#include "Framework.h"
extern Sapphire::Framework g_fw;
2018-11-19 09:40:44 +01:00
Sapphire::House::House( uint32_t houseId, uint32_t landSetId, Common::LandIdent ident, const std::string& estateName,
const std::string& estateComment ) :
2018-11-19 09:40:44 +01:00
m_houseId( houseId ),
m_landSetId( landSetId ),
m_landIdent( ident ),
m_estateName( estateName ),
m_estateComment( estateComment )
{
// auto pDB = g_fw.get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
//
// // todo: convert to prepared statement
// auto res = pDB->query( "SELECT * FROM house WHERE HouseId = " + std::to_string( houseId ) );
//
// if( !res->next() )
// {
// g_fw.get< Sapphire::Logger >()->info( "Creating house House#" + std::to_string( houseId ) + " in LandSet#" + std::to_string( landSetId ) );
//
// auto stmt = pDB->getPreparedStatement( Db::HOUSING_HOUSE_INS );
//
// stmt->setUInt( 1, m_landSetId );
// stmt->setUInt( 2, m_houseId );
//
// pDB->execute( stmt );
//
// // todo: make this nicer/configurable?
// m_estateName = "Estate #" + std::to_string( m_landIdent.landId + 1 );
// }
// else
// {
// m_estateComment = res->getString( "Comment" );
// m_estateName = res->getString( "HouseName" );
//
// auto housePartModels = res->getBlobVector( "HousePartModels" );
// auto housePartColours = res->getBlobVector( "HousePartColours" );
//
// auto models = reinterpret_cast< uint32_t* >( &housePartModels[ 0 ] );
// for( auto i = 0; i < 8; i++ )
// {
2018-12-22 15:58:54 +11:00
// m_houseModelsCache[ i ] = { models[ i ], housePartColours[ i ] };
// }
//
// auto houseInteriorModels = res->getBlobVector( "HouseInteriorModels" );
//
// auto interiorModels = reinterpret_cast< uint32_t* >( &houseInteriorModels[ 0 ] );
// for( auto i = 0; i < 10; i++ )
// {
2018-12-22 15:58:54 +11:00
// m_houseInteriorModels[ i ] = interiorModels[ i ];
// }
// }
}
Sapphire::House::~House() = default;
2018-11-26 23:32:22 +11:00
void Sapphire::House::updateHouseDb()
2018-11-26 23:32:22 +11:00
{
auto pDB = g_fw.get< Db::DbWorkerPool< Db::ZoneDbConnection > >();
// BuildTime = 1, Aetheryte = 2, Comment = 3, HouseName = 4, Endorsements = 5,
// HousePartModels = 6, HousePartColours = 7, HouseId = 8
auto stmt = pDB->getPreparedStatement( Db::HOUSING_HOUSE_UP );
2018-12-01 21:40:30 +01:00
stmt->setUInt( 9, m_houseId );
2018-11-26 23:32:22 +11:00
stmt->setInt64( 1, m_buildTime );
stmt->setInt( 2, 0 );
stmt->setString( 3, m_estateComment );
stmt->setString( 4, m_estateName );
2018-11-26 23:32:22 +11:00
stmt->setUInt64( 5, 0 );
std::vector< uint32_t > models;
std::vector< uint8_t > colours;
for( auto i = 0; i < 8; i++ )
{
2018-12-22 15:58:54 +11:00
auto& part = m_houseModelsCache[ i ];
2018-11-26 23:32:22 +11:00
models.push_back( part.first );
colours.push_back( part.second );
}
// todo: this is shit
std::vector< uint8_t > tmpModels( models.size() * 4 );
memcpy( tmpModels.data(), models.data(), tmpModels.size() );
stmt->setBinary( 6, tmpModels );
stmt->setBinary( 7, colours );
2018-11-19 09:40:44 +01:00
2018-12-01 21:40:30 +01:00
models.clear();
for( auto i = 0; i < 10; i++ )
{
2018-12-22 15:58:54 +11:00
models.push_back( m_houseInteriorModels[ i ] );
2018-12-01 21:40:30 +01:00
}
std::vector< uint8_t > tmp2Models( models.size() * 4 );
memcpy( tmp2Models.data(), models.data(), tmp2Models.size() );
stmt->setBinary( 8, tmp2Models );
2018-11-26 23:32:22 +11:00
pDB->execute( stmt );
2018-11-19 09:40:44 +01:00
}
uint32_t Sapphire::House::getLandSetId() const
2018-11-19 09:40:44 +01:00
{
return m_landSetId;
}
Sapphire::Common::LandIdent Sapphire::House::getLandIdent() const
2018-11-19 09:40:44 +01:00
{
return m_landIdent;
2018-11-19 10:32:58 +01:00
}
uint32_t Sapphire::House::getHouseId() const
2018-11-19 09:40:44 +01:00
{
return m_houseId;
}
uint8_t Sapphire::House::getHousePartColor( Common::HousePartSlot slot ) const
2018-11-19 09:40:44 +01:00
{
2018-12-22 15:58:54 +11:00
return m_houseModelsCache[ slot ].second;
2018-11-19 09:40:44 +01:00
}
2018-12-01 21:40:30 +01:00
uint32_t Sapphire::House::getHouseInteriorPart( Common::HousingInteriorSlot slot ) const
{
2018-12-22 15:58:54 +11:00
return m_houseInteriorModels[ slot ];
2018-12-01 21:40:30 +01:00
}
void Sapphire::House::setHousePart( Common::HousePartSlot slot, uint32_t id )
2018-11-19 09:40:44 +01:00
{
2018-12-22 15:58:54 +11:00
m_houseModelsCache[ slot ].first = id;
2018-11-19 09:40:44 +01:00
}
void Sapphire::House::setHousePartColor( Common::HousePartSlot slot, uint32_t id )
2018-11-19 09:40:44 +01:00
{
2018-12-22 15:58:54 +11:00
m_houseModelsCache[ slot ].second = id;
2018-11-19 09:40:44 +01:00
}
2018-12-01 21:40:30 +01:00
void Sapphire::House::setHouseInteriorPart( Common::HousingInteriorSlot slot, uint32_t id )
{
2018-12-22 15:58:54 +11:00
m_houseInteriorModels[ slot ] = id;
2018-12-01 21:40:30 +01:00
}
uint32_t Sapphire::House::getHousePart( Common::HousePartSlot slot ) const
2018-11-19 09:40:44 +01:00
{
2018-12-22 15:58:54 +11:00
return m_houseModelsCache[ slot ].first;
}
2018-12-22 15:58:54 +11:00
Sapphire::House::HouseModelsArray const& Sapphire::House::getHouseModels() const
{
2018-12-22 15:58:54 +11:00
return m_houseModelsCache;
2018-11-27 21:11:02 +11:00
}
const std::string& Sapphire::House::getHouseName() const
2018-11-27 21:11:02 +11:00
{
return m_estateName;
2018-11-27 21:11:02 +11:00
}
const std::string& Sapphire::House::getHouseGreeting() const
2018-11-27 21:11:02 +11:00
{
return m_estateComment;
2018-11-28 00:05:57 +11:00
}
void Sapphire::House::setHouseGreeting( const std::string& greeting )
2018-11-28 00:05:57 +11:00
{
m_estateComment = greeting;
2018-11-28 00:05:57 +11:00
updateHouseDb();
}
void Sapphire::House::setHouseName( const std::string& name )
2018-11-28 00:05:57 +11:00
{
m_estateName = name;
2018-11-28 00:05:57 +11:00
updateHouseDb();
2018-11-25 01:55:53 +11:00
}