2022-04-30 09:41:17 -04:00
package engine.net.client.handlers ;
2024-04-05 07:59:44 -04:00
import engine.mbEnums ;
import engine.mbEnums.DispatchChannel ;
2022-04-30 09:41:17 -04:00
import engine.exception.MsgSendException ;
import engine.gameManager.BuildingManager ;
import engine.gameManager.SessionManager ;
import engine.net.Dispatch ;
import engine.net.DispatchMessage ;
import engine.net.client.ClientConnection ;
import engine.net.client.msg.ClientNetMsg ;
import engine.net.client.msg.PlaceAssetMsg ;
import engine.net.client.msg.TransferGoldToFromBuildingMsg ;
import engine.net.client.msg.UpdateGoldMsg ;
2024-03-24 09:42:27 -04:00
import engine.objects.Building ;
import engine.objects.CharacterItemManager ;
import engine.objects.Item ;
import engine.objects.PlayerCharacter ;
2022-04-30 09:41:17 -04:00
import org.pmw.tinylog.Logger ;
/*
* @Author:
* @Summary: Processes application protocol message which transfers
* gold between a building's strongbox and a player character.
*/
public class TransferGoldToFromBuildingMsgHandler extends AbstractClientMsgHandler {
2023-07-15 09:23:48 -04:00
public TransferGoldToFromBuildingMsgHandler ( ) {
super ( TransferGoldToFromBuildingMsg . 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
PlayerCharacter player ;
Building building ;
CharacterItemManager itemMan ;
Item goldItem ;
TransferGoldToFromBuildingMsg msg ;
Dispatch dispatch ;
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
if ( player = = null )
return true ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
msg = ( TransferGoldToFromBuildingMsg ) baseMsg ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
building = BuildingManager . getBuildingFromCache ( msg . getObjectID ( ) ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( building = = null )
return true ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( msg . getDirection ( ) = = 2 ) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( ! ManageCityAssetMsgHandler . playerCanManageNotFriends ( player , building ) )
return true ;
if ( building . setReserve ( msg . getUnknown01 ( ) , player ) ) {
dispatch = Dispatch . borrow ( player , msg ) ;
DispatchMessage . dispatchMsgDispatch ( dispatch , DispatchChannel . SECONDARY ) ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// if (building.getTimeStamp(MBServerStatics.STRONGBOX_DELAY_STRING) > System.currentTimeMillis()){
// ErrorPopupMsg.sendErrorMsg(player, MBServerStatics.STRONGBOX_DELAY_OUTPUT);
// return true;
// }
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//building.getTimestamps().put(MBServerStatics.STRONGBOX_DELAY_STRING, System.currentTimeMillis() + MBServerStatics.ONE_MINUTE);
2022-04-30 09:41:17 -04:00
2024-03-18 10:01:29 -04:00
itemMan = player . charItemManager ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
goldItem = itemMan . getGoldInventory ( ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( goldItem = = null ) {
Logger . error ( " Could not access gold item " ) ;
return true ;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Update in-game gold values for player and building
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if ( ! itemMan . transferGoldToFromBuilding ( msg . getAmount ( ) , building ) )
return true ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
UpdateGoldMsg ugm = new UpdateGoldMsg ( player ) ;
ugm . configure ( ) ;
dispatch = Dispatch . borrow ( player , ugm ) ;
2024-04-05 07:59:44 -04:00
DispatchMessage . dispatchMsgDispatch ( dispatch , mbEnums . DispatchChannel . SECONDARY ) ;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Refresh the player's inventory if it's currently open
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Refresh the tree's window to update strongbox
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
msg . setAmount ( building . getStrongboxValue ( ) ) ;
dispatch = Dispatch . borrow ( player , msg ) ;
2024-04-05 07:59:44 -04:00
DispatchMessage . dispatchMsgDispatch ( dispatch , mbEnums . 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
}