2022-04-30 09:41:17 -04:00
package engine.net.client.handlers ;
import engine.Enum ;
import engine.Enum.GameObjectType ;
import engine.exception.MsgSendException ;
import engine.gameManager.BuildingManager ;
import engine.gameManager.DbManager ;
import engine.gameManager.SessionManager ;
import engine.net.Dispatch ;
import engine.net.DispatchMessage ;
import engine.net.client.ClientConnection ;
import engine.net.client.msg.* ;
import engine.objects.Building ;
import engine.objects.PlayerCharacter ;
import org.pmw.tinylog.Logger ;
import java.time.LocalDateTime ;
import static engine.net.client.msg.ErrorPopupMsg.sendErrorPopup ;
/*
*
* @Summary: Processes application protocol message where a
* client requests that a building be upgraded.
*/
public class UpgradeAssetMsgHandler extends AbstractClientMsgHandler {
2023-07-15 09:23:48 -04:00
// Constructor
public UpgradeAssetMsgHandler () {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
super ( UpgradeAssetMessage . class );
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
protected boolean _handleNetMsg ( ClientNetMsg baseMsg , ClientConnection origin ) throws MsgSendException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Member variable declaration
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
UpgradeAssetMessage msg ;
ManageCityAssetsMsg outMsg ;
PlayerCharacter player ;
int buildingUUID ;
Building buildingToRank ;
LocalDateTime dateToUpgrade ;
int nextRank ;
int rankCost ;
Dispatch dispatch ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Assign member variables
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
msg = ( UpgradeAssetMessage ) baseMsg ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Grab pointer to the requesting player
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
player = SessionManager . getPlayerCharacter ( origin );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Grab pointer to the building from the cache
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
buildingUUID = msg . getBuildingUUID ();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
buildingToRank = ( Building ) DbManager . getObject ( GameObjectType . Building , buildingUUID );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Early exit if building not in cache.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( buildingToRank == null ) {
Logger . error ( "Attempt to upgrade null building by " + player . getName ());
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Early exit for building that is already ranking
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( buildingToRank . isRanking ()) {
Logger . error ( "Attempt to upgrade a building already ranking by " + player . getName ());
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Calculate and set time/cost to upgrade
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
nextRank = ( buildingToRank . getRank () + 1 );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( buildingToRank . getBlueprint () == null )
return true ;
if ( buildingToRank . getBlueprint (). getMaxRank () < nextRank || nextRank == 8 ) {
ErrorPopupMsg . sendErrorMsg ( player , "Building is already at it's Max rank." );
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
rankCost = buildingToRank . getBlueprint (). getRankCost ( nextRank );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// SEND NOT ENOUGH GOLD ERROR
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( ! buildingToRank . hasFunds ( rankCost )) {
ErrorPopupMsg . sendErrorPopup ( player , 127 ); // Not enough gold in strongbox
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( rankCost > buildingToRank . getStrongboxValue ()) {
sendErrorPopup ( player , 127 );
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Validation appears good. Let's now process the upgrade
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
if ( buildingToRank . getCity () != null ) {
buildingToRank . getCity (). transactionLock . writeLock (). lock ();
try {
if ( ! buildingToRank . transferGold ( - rankCost , false )) {
sendErrorPopup ( player , 127 );
return true ;
}
} catch ( Exception e ) {
Logger . error ( e );
} finally {
buildingToRank . getCity (). transactionLock . writeLock (). unlock ();
}
} else if ( ! buildingToRank . transferGold ( - rankCost , false )) {
sendErrorPopup ( player , 127 );
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
dateToUpgrade = LocalDateTime . now (). plusHours ( buildingToRank . getBlueprint (). getRankTime ( nextRank ));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
BuildingManager . setUpgradeDateTime ( buildingToRank , dateToUpgrade , 0 );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Schedule upgrade job
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
BuildingManager . submitUpgradeJob ( buildingToRank );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Refresh the client's manage asset window
// *** Refactor : We have some of these unknowns
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outMsg = new ManageCityAssetsMsg ( player , buildingToRank );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Action TYPE
outMsg . actionType = 3 ;
outMsg . setTargetType ( buildingToRank . getObjectType (). ordinal ());
outMsg . setTargetID ( buildingToRank . getObjectUUID ());
outMsg . setTargetType3 ( buildingToRank . getObjectType (). ordinal ());
outMsg . setTargetID3 ( buildingToRank . getObjectUUID ());
outMsg . setAssetName1 ( buildingToRank . getName ());
outMsg . setUnknown54 ( 1 );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
dispatch = Dispatch . borrow ( player , outMsg );
DispatchMessage . dispatchMsgDispatch ( dispatch , Enum . DispatchChannel . SECONDARY );
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} catch ( Exception e ) {
PlaceAssetMsg . sendPlaceAssetError ( player . getClientConnection (), 1 , "A Serious error has occurred. Please post details for to ensure transaction integrity" );
}
return true ;
}
2022-04-30 09:41:17 -04:00
}