2021-12-05 17:54:36 +00:00
# include < CDPropertyEntranceComponentTable . h >
2022-03-28 06:46:43 +00:00
# include <chrono>
2021-12-05 17:54:36 +00:00
# include "PropertyEntranceComponent.h"
# include "PropertySelectQueryProperty.h"
# include "RocketLaunchpadControlComponent.h"
# include "Character.h"
# include "GameMessages.h"
# include "dLogger.h"
# include "Database.h"
# include "PropertyManagementComponent.h"
2022-03-28 03:04:45 +00:00
# include "UserManager.h"
2021-12-05 17:54:36 +00:00
PropertyEntranceComponent : : PropertyEntranceComponent ( uint32_t componentID , Entity * parent ) : Component ( parent )
{
this - > propertyQueries = { } ;
auto table = CDClientManager : : Instance ( ) - > GetTable < CDPropertyEntranceComponentTable > ( " PropertyEntranceComponent " ) ;
const auto & entry = table - > GetByID ( componentID ) ;
this - > m_MapID = entry . mapID ;
this - > m_PropertyName = entry . propertyName ;
}
void PropertyEntranceComponent : : OnUse ( Entity * entity )
{
GameMessages : : SendPropertyEntranceBegin ( m_Parent - > GetObjectID ( ) , entity - > GetSystemAddress ( ) ) ;
AMFArrayValue args ;
auto * state = new AMFStringValue ( ) ;
state - > SetStringValue ( " property_menu " ) ;
args . InsertValue ( " state " , state ) ;
GameMessages : : SendUIMessageServerToSingleClient ( entity , entity - > GetSystemAddress ( ) , " pushGameState " , & args ) ;
delete state ;
}
void PropertyEntranceComponent : : OnEnterProperty ( Entity * entity , uint32_t index , bool returnToZone , const SystemAddress & sysAddr )
{
LWOCLONEID cloneId = 0 ;
if ( index = = - 1 & & ! returnToZone )
{
cloneId = entity - > GetCharacter ( ) - > GetPropertyCloneID ( ) ;
}
else if ( index = = - 1 & & returnToZone )
{
cloneId = 0 ;
}
else if ( index > = 0 )
{
index + + ;
const auto & pair = propertyQueries . find ( entity - > GetObjectID ( ) ) ;
if ( pair = = propertyQueries . end ( ) ) return ;
const auto & query = pair - > second ;
if ( index > = query . size ( ) ) return ;
cloneId = query [ index ] . CloneId ;
}
auto * launcher = m_Parent - > GetComponent < RocketLaunchpadControlComponent > ( ) ;
if ( launcher = = nullptr )
{
return ;
}
launcher - > SetSelectedCloneId ( entity - > GetObjectID ( ) , cloneId ) ;
launcher - > Launch ( entity , LWOOBJID_EMPTY , LWOMAPID_INVALID , cloneId ) ;
}
2022-03-28 10:37:12 +00:00
void PropertyEntranceComponent : : OnPropertyEntranceSync ( Entity * entity , bool includeNullAddress , bool includeNullDescription , bool playerOwn , bool updateUi , int32_t numResults , int32_t lReputationTime , int32_t sortMethod , int32_t startIndex , std : : string filterText , const SystemAddress & sysAddr ) {
2021-12-05 17:54:36 +00:00
auto * launchpadComponent = m_Parent - > GetComponent < RocketLaunchpadControlComponent > ( ) ;
2022-03-28 10:37:12 +00:00
if ( launchpadComponent = = nullptr ) return ;
2021-12-05 17:54:36 +00:00
std : : vector < PropertySelectQueryProperty > entries { } ;
PropertySelectQueryProperty playerEntry { } ;
auto * character = entity - > GetCharacter ( ) ;
2022-03-28 10:24:49 +00:00
auto newpropertyLookup = Database : : CreatePreppedStmt ( " SELECT * FROM properties WHERE owner_id = ? AND zone_id = ? " ) ;
newpropertyLookup - > setInt ( 1 , character - > GetID ( ) ) ;
newpropertyLookup - > setInt ( 2 , launchpadComponent - > GetTargetZone ( ) ) ;
auto results = newpropertyLookup - > executeQuery ( ) ;
playerEntry . CloneId = character - > GetPropertyCloneID ( ) ;
playerEntry . OwnerName = " " ;
playerEntry . Name = " " ;
playerEntry . Description = " " ;
playerEntry . Reputation = 0 ;
playerEntry . IsBestFriend = true ;
playerEntry . IsFriend = true ;
playerEntry . IsModeratorApproved = false ;
playerEntry . IsAlt = true ;
playerEntry . IsOwned = false ;
playerEntry . AccessType = 0 ;
playerEntry . DatePublished = 0 ;
if ( results - > next ( ) ) {
const auto propertyId = results - > getUInt64 ( 1 ) ;
const auto owner = results - > getUInt64 ( 2 ) ;
const auto cloneId = results - > getUInt64 ( 4 ) ;
const auto name = results - > getString ( 5 ) . asStdString ( ) ;
const auto description = results - > getString ( 6 ) . asStdString ( ) ;
const auto privacyOption = results - > getInt ( 9 ) ;
const auto modApproved = results - > getBoolean ( 10 ) ;
const auto dateUpdated = results - > getInt64 ( 11 ) ;
const auto reputation = results - > getInt ( 14 ) ;
playerEntry . Name = name ;
playerEntry . Description = description ;
playerEntry . DatePublished = dateUpdated ;
playerEntry . IsModeratorApproved = modApproved ;
playerEntry . IsAlt = true ;
playerEntry . IsOwned = true ;
}
2021-12-05 17:54:36 +00:00
entries . push_back ( playerEntry ) ;
sql : : ResultSet * propertyEntry ;
sql : : PreparedStatement * propertyLookup ;
2022-03-28 03:04:45 +00:00
std : : string orderBy = " " ;
int32_t privacyType = 2 ;
2021-12-05 17:54:36 +00:00
const auto moderating = entity - > GetGMLevel ( ) > = GAME_MASTER_LEVEL_LEAD_MODERATOR ;
2022-03-28 03:04:45 +00:00
2022-03-28 10:24:49 +00:00
std : : string baseQuery = " SELECT p.* FROM properties as p JOIN charinfo as ci ON ci.prop_clone_id = p.clone_id where p.zone_id = ? AND (p.description LIKE ? OR p.name LIKE ? OR ci.name LIKE ?) AND p.mod_approved >= ? AND p.privacy_option >= ? " ;
// In case the query is for friends.
std : : string friendsList = " AND p.owner_id IN ( " ;
2022-03-28 03:04:45 +00:00
// We change how we sort this query based on what the requested order is.
if ( sortMethod = = SORT_TYPE_FEATURED | | sortMethod = = SORT_TYPE_FRIENDS ) {
2022-03-28 10:24:49 +00:00
auto friendsListQuery = Database : : CreatePreppedStmt ( " SELECT * FROM (SELECT CASE WHEN player_id = ? THEN friend_id WHEN friend_id = ? THEN player_id END AS requested_player FROM dlu.friends ) AS fr WHERE requested_player IS NOT NULL ORDER BY requested_player DESC; " ) ;
friendsListQuery - > setInt64 ( 1 , entity - > GetObjectID ( ) ) ;
friendsListQuery - > setInt64 ( 2 , entity - > GetObjectID ( ) ) ;
// friendsListQuery->setInt(3, numResults);
// friendsListQuery->setInt(4, startIndex);
auto friendsListQueryResult = friendsListQuery - > executeQuery ( ) ;
while ( friendsListQueryResult - > next ( ) ) {
auto playerIDToConvert = friendsListQueryResult - > getInt64 ( 1 ) ;
playerIDToConvert = GeneralUtils : : ClearBit ( playerIDToConvert , OBJECT_BIT_CHARACTER ) ;
playerIDToConvert = GeneralUtils : : ClearBit ( playerIDToConvert , OBJECT_BIT_PERSISTENT ) ;
friendsList = friendsList + std : : to_string ( playerIDToConvert ) + " , " ;
}
// Replace trailing comma with the closing parenthesis.
friendsList . replace ( friendsList . size ( ) - 1 , 2 , " ) " ) ;
orderBy = friendsList + " ORDER BY ci.name ASC " ;
2022-03-28 03:04:45 +00:00
privacyType = 1 ;
}
else if ( sortMethod = = SORT_TYPE_RECENT ) {
2022-03-28 10:24:49 +00:00
orderBy = " ORDER BY p.last_updated DESC " ;
2022-03-28 03:04:45 +00:00
}
else if ( sortMethod = = SORT_TYPE_REPUTATION ) {
2022-03-28 10:24:49 +00:00
orderBy = " ORDER BY p.reputation DESC, p.last_updated DESC " ;
2022-03-28 03:04:45 +00:00
}
else {
2022-03-28 10:24:49 +00:00
orderBy = " ORDER BY p.last_updated DESC " ;
2021-12-05 17:54:36 +00:00
}
2022-03-28 10:24:49 +00:00
auto finishedQuery = baseQuery + orderBy + " LIMIT ? OFFSET ?; " ;
propertyLookup = Database : : CreatePreppedStmt ( finishedQuery ) ;
2021-12-05 17:54:36 +00:00
2022-03-28 03:04:45 +00:00
const std : : string searchString = " % " + filterText + " % " ;
Game : : logger - > Log ( " PropertyEntranceComponent " , " %s \n " , searchString . c_str ( ) ) ;
propertyLookup - > setUInt ( 1 , launchpadComponent - > GetTargetZone ( ) ) ;
propertyLookup - > setString ( 2 , searchString . c_str ( ) ) ;
2022-03-28 10:24:49 +00:00
propertyLookup - > setString ( 3 , searchString . c_str ( ) ) ;
propertyLookup - > setString ( 4 , searchString . c_str ( ) ) ;
propertyLookup - > setInt ( 5 , entity - > GetGMLevel ( ) > = GAME_MASTER_LEVEL_LEAD_MODERATOR | | sortMethod = = SORT_TYPE_FRIENDS ? 0 : 1 ) ;
propertyLookup - > setInt ( 6 , privacyType ) ;
propertyLookup - > setInt ( 7 , numResults ) ;
propertyLookup - > setInt ( 8 , startIndex ) ;
2022-03-28 03:04:45 +00:00
Game : : logger - > Log ( " PropertyEntranceComponent " , " Querying target zone %i with search string %s and ordering by %s starting at index %i. Entity is %s. \n " , launchpadComponent - > GetTargetZone ( ) , searchString . c_str ( ) , orderBy . c_str ( ) , startIndex , entity - > GetGMLevel ( ) > = GAME_MASTER_LEVEL_LEAD_MODERATOR ? " a moderator " : " not a moderator " ) ;
propertyEntry = propertyLookup - > executeQuery ( ) ;
2021-12-05 17:54:36 +00:00
while ( propertyEntry - > next ( ) )
{
const auto propertyId = propertyEntry - > getUInt64 ( 1 ) ;
2022-03-28 06:46:43 +00:00
const auto owner = propertyEntry - > getInt ( 2 ) ;
2021-12-05 17:54:36 +00:00
const auto cloneId = propertyEntry - > getUInt64 ( 4 ) ;
const auto name = propertyEntry - > getString ( 5 ) . asStdString ( ) ;
const auto description = propertyEntry - > getString ( 6 ) . asStdString ( ) ;
const auto privacyOption = propertyEntry - > getInt ( 9 ) ;
2022-03-28 03:04:45 +00:00
const auto modApproved = propertyEntry - > getBoolean ( 10 ) ;
2022-03-28 10:24:49 +00:00
const auto dateUpdated = propertyEntry - > getInt ( 11 ) ;
const auto reputation = propertyEntry - > getUInt ( 14 ) ;
Game : : logger - > Log ( " PropertyEntranceComponent " , " Property being loaded is %i with reputation %lu \n " , owner , reputation ) ;
2021-12-05 17:54:36 +00:00
PropertySelectQueryProperty entry { } ;
auto * nameLookup = Database : : CreatePreppedStmt ( " SELECT name FROM charinfo WHERE prop_clone_id = ?; " ) ;
nameLookup - > setUInt64 ( 1 , cloneId ) ;
auto * nameResult = nameLookup - > executeQuery ( ) ;
if ( ! nameResult - > next ( ) )
{
delete nameLookup ;
Game : : logger - > Log ( " PropertyEntranceComponent " , " Failed to find property owner name for %llu! \n " , cloneId ) ;
continue ;
}
else
{
2022-03-28 06:46:43 +00:00
entry . IsOwned = owner = = entity - > GetCharacter ( ) - > GetID ( ) ;
2021-12-05 17:54:36 +00:00
entry . OwnerName = nameResult - > getString ( 1 ) . asStdString ( ) ;
}
2022-03-28 03:04:45 +00:00
if ( modApproved )
2021-12-05 17:54:36 +00:00
{
entry . Name = name ;
entry . Description = description ;
}
2022-03-28 06:46:43 +00:00
2022-03-28 03:04:45 +00:00
// Convert owner char id to LWOOBJID
LWOOBJID ownerObjId = owner ;
ownerObjId = GeneralUtils : : SetBit ( ownerObjId , OBJECT_BIT_CHARACTER ) ;
ownerObjId = GeneralUtils : : SetBit ( ownerObjId , OBJECT_BIT_PERSISTENT ) ;
2022-03-28 06:46:43 +00:00
auto friendCheck = Database : : CreatePreppedStmt ( " SELECT best_friend FROM friends WHERE (player_id = ? AND friend_id = ?) OR (player_id = ? AND friend_id = ?) " ) ;
2022-03-28 03:04:45 +00:00
friendCheck - > setInt64 ( 1 , entity - > GetObjectID ( ) ) ;
friendCheck - > setInt64 ( 2 , ownerObjId ) ;
friendCheck - > setInt64 ( 3 , ownerObjId ) ;
friendCheck - > setInt64 ( 4 , entity - > GetObjectID ( ) ) ;
auto friendResult = friendCheck - > executeQuery ( ) ;
// If we got a result than the two players are friends.
if ( friendResult - > next ( ) ) {
entry . IsFriend = true ;
2022-03-28 06:46:43 +00:00
if ( friendResult - > getBoolean ( 1 ) = = true ) {
entry . IsBestFriend = true ;
} else {
entry . IsBestFriend = false ;
}
2022-03-28 03:04:45 +00:00
}
else {
entry . IsFriend = false ;
2022-03-28 06:46:43 +00:00
entry . IsBestFriend = false ;
}
auto isAltQuery = Database : : CreatePreppedStmt ( " SELECT id FROM charinfo where account_id in (SELECT account_id from charinfo WHERE id = ?) AND id = ?; " ) ;
isAltQuery - > setInt ( 1 , character - > GetID ( ) ) ;
isAltQuery - > setInt ( 2 , owner ) ;
auto isAltQueryResults = isAltQuery - > executeQuery ( ) ;
if ( isAltQueryResults - > next ( ) ) {
entry . IsAlt = true ;
} else {
entry . IsAlt = false ;
2022-03-28 03:04:45 +00:00
}
2022-03-28 06:46:43 +00:00
delete isAltQuery ;
isAltQuery = nullptr ;
2022-03-28 10:24:49 +00:00
entry . DatePublished = dateUpdated ;
2021-12-05 17:54:36 +00:00
entry . Reputation = reputation ;
entry . CloneId = cloneId ;
2022-03-28 03:04:45 +00:00
entry . IsModeratorApproved = modApproved = = true ;
entry . AccessType = privacyOption ;
2022-03-28 10:24:49 +00:00
entry . PerformanceCost = entity - > GetObjectID ( ) ;
2021-12-05 17:54:36 +00:00
entries . push_back ( entry ) ;
delete nameLookup ;
}
delete propertyLookup ;
2022-03-28 10:24:49 +00:00
2021-12-05 17:54:36 +00:00
propertyQueries [ entity - > GetObjectID ( ) ] = entries ;
GameMessages : : SendPropertySelectQuery (
m_Parent - > GetObjectID ( ) ,
startIndex ,
entries . size ( ) > = numResults ,
character - > GetPropertyCloneID ( ) ,
false ,
true ,
entries ,
sysAddr
) ;
}