From 3cb4e792115005d82157fb62a27e724a2bb900c0 Mon Sep 17 00:00:00 2001 From: Mordred Date: Sat, 10 Nov 2018 23:47:19 +0100 Subject: [PATCH] Cleanup and errorhandling --- src/common/Network/CommonActorControl.h | 2 +- .../Scripts/common/CmnDefHousingSignboard.cpp | 25 +++++++++- src/servers/sapphire_zone/Actor/Player.cpp | 2 +- src/servers/sapphire_zone/Actor/Player.h | 2 +- src/servers/sapphire_zone/Zone/HousingMgr.cpp | 2 +- .../sapphire_zone/Zone/HousingZone.cpp | 46 +++++++++++++------ src/servers/sapphire_zone/Zone/HousingZone.h | 10 +++- src/servers/sapphire_zone/Zone/Land.cpp | 4 +- 8 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/common/Network/CommonActorControl.h b/src/common/Network/CommonActorControl.h index ee2833e2..d255465a 100644 --- a/src/common/Network/CommonActorControl.h +++ b/src/common/Network/CommonActorControl.h @@ -184,7 +184,7 @@ enum ActorControlType : AchievementPopup = 0x203, - Unk7 = 0x205, // LogMessage? + LogMsg = 0x205, // LogMessage? AchievementMsg = 0x206, SetItemLevel = 0x209, diff --git a/src/servers/Scripts/common/CmnDefHousingSignboard.cpp b/src/servers/Scripts/common/CmnDefHousingSignboard.cpp index 1f3d2509..04928389 100644 --- a/src/servers/Scripts/common/CmnDefHousingSignboard.cpp +++ b/src/servers/Scripts/common/CmnDefHousingSignboard.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include + using namespace Core; @@ -25,7 +28,27 @@ public: auto pTerritory = player.getCurrentZone(); auto pHousing = std::dynamic_pointer_cast< HousingZone >( pTerritory ); - pHousing->playerPurchseLand( player, activeLand.plot, result.param2 ); + PurchaseResult res = pHousing->purchseLand( player, activeLand.plot, + static_cast< uint8_t >( result.param2 ) ); + + + switch( res ) + { + case PurchaseResult::SUCCESS: + { + auto screenMsgPkt = Network::Packets::Server::makeActorControl143( player.getId(), + Network::ActorControl::DutyQuestScreenMsg, + m_id, 0x98 ); + player.queuePacket( screenMsgPkt ); + } + case PurchaseResult::ERR_NOT_ENOUGH_GIL: + { + auto errorMsg = Network::Packets::Server::makeActorControl143( player.getId(), + Network::ActorControl::LogMsg, + 4027 ); + player.queuePacket( errorMsg ); + } + } } }; diff --git a/src/servers/sapphire_zone/Actor/Player.cpp b/src/servers/sapphire_zone/Actor/Player.cpp index 8fb4790d..0e4b793d 100644 --- a/src/servers/sapphire_zone/Actor/Player.cpp +++ b/src/servers/sapphire_zone/Actor/Player.cpp @@ -106,7 +106,7 @@ Core::Entity::Player::~Player() { } -void Core::Entity::Player::injectPacket( std::string path ) +void Core::Entity::Player::injectPacket( const std::string& path ) { auto pServerZone = g_fw.get< ServerZone >(); auto session = pServerZone->getSession( getId() ); diff --git a/src/servers/sapphire_zone/Actor/Player.h b/src/servers/sapphire_zone/Actor/Player.h index d1aa1e22..e2707642 100644 --- a/src/servers/sapphire_zone/Actor/Player.h +++ b/src/servers/sapphire_zone/Actor/Player.h @@ -46,7 +46,7 @@ namespace Core::Entity void autoAttack( CharaPtr pTarget ) override; - void injectPacket( std::string path ); + void injectPacket( const std::string& path ); // EventHandlers ////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/servers/sapphire_zone/Zone/HousingMgr.cpp b/src/servers/sapphire_zone/Zone/HousingMgr.cpp index 700d372a..48f4cdcd 100644 --- a/src/servers/sapphire_zone/Zone/HousingMgr.cpp +++ b/src/servers/sapphire_zone/Zone/HousingMgr.cpp @@ -58,7 +58,7 @@ Core::LandPtr Core::HousingMgr::getLandByOwnerId( uint32_t id ) for( const auto& hZoneIt : m_housingZonePtrMap ) { auto pHousingZone = hZoneIt.second; - for( uint8_t landId = 0;landId < 60;landId++ ) + for( uint8_t landId = 0; landId < 60; landId++ ) { if( pHousingZone->getLand( landId )->getPlayerOwner() == id ) { diff --git a/src/servers/sapphire_zone/Zone/HousingZone.cpp b/src/servers/sapphire_zone/Zone/HousingZone.cpp index 1a0dd152..685cf8c9 100644 --- a/src/servers/sapphire_zone/Zone/HousingZone.cpp +++ b/src/servers/sapphire_zone/Zone/HousingZone.cpp @@ -154,29 +154,45 @@ bool Core::HousingZone::isPlayerSubInstance( Entity::Player& player ) return player.getPos().x < -15000.0f; //ToDo: get correct pos } -void Core::HousingZone::playerPurchseLand( Entity::Player & player, uint8_t plot, uint8_t state ) +Core::PurchaseResult Core::HousingZone::purchseLand( Entity::Player& player, uint8_t plot, uint8_t state ) { - uint32_t plotPrice = getLand( plot )->getCurrentPrice(); - if( plotPrice <= player.getCurrency( CurrencyType::Gil ) ) + + auto plotPrice = getLand( plot )->getCurrentPrice(); + auto gilAvailable = player.getCurrency( CurrencyType::Gil ); + auto pLand = getLand( plot ); + + if( !pLand ) + return PurchaseResult::ERR_INTERNAL; + + if( pLand->getState() != HouseState::forSale ) + return PurchaseResult::ERR_NOT_AVAILABLE; + + if( gilAvailable < plotPrice ) + return PurchaseResult::ERR_NOT_ENOUGH_GIL; + + auto pHousing = std::dynamic_pointer_cast< HousingZone >( player.getCurrentZone() ); + + switch( state ) { - auto pHousing = std::dynamic_pointer_cast< HousingZone >( player.getCurrentZone() ); - if( state == 1 ) //TODO: add Free company purchase + case 1: player.sendDebug( "Free company house purchase aren't supported at this time." ); - if( state == 2 ) //Private Purchase - { - getLand( plot )->setPlayerOwner( player.getId() ); - getLand( plot )->setState( HouseState::sold ); + return PurchaseResult::ERR_INTERNAL; + case 2: player.removeCurrency( CurrencyType::Gil, plotPrice ); - player.setLandPermissions( LandPermissionSlot::Private, 0x0B, plot, pHousing->getWardNum(), pHousing->getTerritoryTypeId() ); + pLand->setPlayerOwner( player.getId() ); + pLand->setState( HouseState::sold ); + player.setLandPermissions( LandPermissionSlot::Private, 0x0B, plot, + pHousing->getWardNum(), pHousing->getTerritoryTypeId() ); player.sendLandPermissions(); - - getLand( plot )->UpdateLandDb(); + pLand->UpdateLandDb(); sendLandUpdate( plot ); - } + return PurchaseResult::SUCCESS; + + default: + return PurchaseResult::ERR_INTERNAL; } - //else - //TOD: add error msg - insufficient gil + } void Core::HousingZone::onUpdate( uint32_t currTime ) diff --git a/src/servers/sapphire_zone/Zone/HousingZone.h b/src/servers/sapphire_zone/Zone/HousingZone.h index 47b3a490..a93be9d2 100644 --- a/src/servers/sapphire_zone/Zone/HousingZone.h +++ b/src/servers/sapphire_zone/Zone/HousingZone.h @@ -6,6 +6,14 @@ namespace Core { + enum class PurchaseResult + { + SUCCESS, + ERR_NOT_ENOUGH_GIL, + ERR_NOT_AVAILABLE, + ERR_INTERNAL, + }; + class HousingZone : public Zone { public: @@ -26,7 +34,7 @@ namespace Core void sendLandUpdate( uint8_t landId ); bool isPlayerSubInstance( Entity::Player& player ); - void playerPurchseLand( Entity::Player& player, uint8_t plot, uint8_t state ); + PurchaseResult purchseLand( Entity::Player& player, uint8_t plot, uint8_t state ); /* returns current ward number for this zone */ uint8_t getWardNum() const; diff --git a/src/servers/sapphire_zone/Zone/Land.cpp b/src/servers/sapphire_zone/Zone/Land.cpp index 96b7d6eb..52ab103d 100644 --- a/src/servers/sapphire_zone/Zone/Land.cpp +++ b/src/servers/sapphire_zone/Zone/Land.cpp @@ -32,7 +32,7 @@ Core::Land::Land( uint16_t zoneId, uint8_t wardNum, uint8_t landId, uint32_t lan m_landId( landId ), m_currentPrice( 0 ), m_minPrice( 0 ), - m_nextDrop( Util::getTimeSeconds() + 21600 ), + m_nextDrop( static_cast< uint32_t >( Util::getTimeSeconds() ) + 21600 ), m_ownerPlayerId( 0 ), m_landSetId( landSetId ), m_landInfo( info ) @@ -260,7 +260,7 @@ uint32_t Core::Land::getMaxItems() uint32_t Core::Land::getDevaluationTime() { - return m_nextDrop - Util::getTimeSeconds(); + return m_nextDrop - static_cast< uint32_t >( Util::getTimeSeconds() ); } void Core::Land::setLandTag( uint8_t slot, uint8_t tag )