Files
caliverse_server/GameServer/Contents/BeaconShop/PacketHandler/BeaconShopRegisterItemPacketHandler.cs
2025-05-01 07:20:41 +09:00

117 lines
4.8 KiB
C#

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using ServerCore;
using ServerBase;
using ServerCommon;
using static ClientToGameReq.Types;
using static ClientToGameRes.Types;
namespace GameServer.PacketHandler;
[PacketHandler(typeof(ClientToGameReq), typeof(ClientToGameReq.Types.C2GS_REQ_BEACON_SHOP_REGISTER_ITEM), typeof(BeaconShopRegisterItemPacketHandler), typeof(GameLoginListener))]
public class BeaconShopRegisterItemPacketHandler : PacketRecvHandler
{
public static bool send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(Player player, Result result, BeaconShopItem? beaconShopItem, CommonResult? commonResult = null)
{
var ack_packet = new ClientToGame();
ack_packet.Response = new ClientToGameRes();
ack_packet.Response.ErrorCode = result.ErrorCode;
ack_packet.Response.AckBeaconShopRegisterItem = new GS2C_ACK_BEACON_SHOP_REGISTER_ITEM();
if (result.isSuccess() && beaconShopItem != null)
{
var beacon_shop_attribute = beaconShopItem.getEntityAttribute<BeaconShopItemAttribute>();
NullReferenceCheckHelper.throwIfNull(beacon_shop_attribute, () => $"beacon_shop_attribute is null !!!");
ack_packet.Response.AckBeaconShopRegisterItem.ItemGuid = beacon_shop_attribute.ItemGuid;
ack_packet.Response.AckBeaconShopRegisterItem.ItemMetaId = (int)beacon_shop_attribute.ItemMetaId;
ack_packet.Response.AckBeaconShopRegisterItem.ItemAmount = beacon_shop_attribute.ItemStackCount;
ack_packet.Response.AckBeaconShopRegisterItem.SellingPrice = beacon_shop_attribute.PriceForUnit;
ack_packet.Response.AckBeaconShopRegisterItem.BeaconGuid = beacon_shop_attribute.BeaconGuid;
ack_packet.Response.AckBeaconShopRegisterItem.SellingFinishTime = Timestamp.FromDateTime(beacon_shop_attribute.SellingFinishTime);
ack_packet.Response.AckBeaconShopRegisterItem.CommonResult = commonResult;
}
if (false == GameServerApp.getServerLogic().onSendPacket(player, ack_packet))
{
return false;
}
return true;
}
public override async Task<Result> onProcessPacket(ISession entityWithSession, IMessage recvMessage)
{
var result = new Result();
var err_msg = string.Empty;
var entity_player = entityWithSession as Player;
NullReferenceCheckHelper.throwIfNull(entity_player, () => $"entity_player is null !!!" );
var beacon_shop_action = entity_player.getEntityAction<BeaconShopAction>();
if (beacon_shop_action == null)
{
err_msg = $"Failed to get beacon shop action : {nameof(BeaconShopAction)}";
result.setFail(ServerErrorCode.EntityActionNotFound, err_msg);
Log.getLogger().error(err_msg);
send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(entity_player, result, null);
return result;
}
var game_msg = recvMessage as ClientToGame;
if (game_msg == null)
{
err_msg = $"Failed to cast ClientToGame !!! : {nameof(ClientToGame.Request.ReqBeaconShopRegisterItem)}";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(entity_player, result, null);
return result;
}
var request = game_msg.Request.ReqBeaconShopRegisterItem;
if (request.ItemGuid == string.Empty ||
request.BeaconGuid == string.Empty ||
request.ItemAmount <= 0 ||
request.SellingPrice <= 0)
{
err_msg = $"Invalid Request Argument !!! : ItemGuid : {request.ItemGuid}, BeaconGuid : {request.BeaconGuid}, ItemAmount : {request.ItemAmount}, SellingPrice : {request.SellingPrice}";
result.setFail(ServerErrorCode.BeaconShopInvalidArgument, err_msg);
Log.getLogger().error(result.toBasicString());
send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(entity_player, result, null);
return result;
}
result = await beacon_shop_action.tryRegisterItemToBeaconShop(request.ItemGuid, (UInt16)request.ItemAmount, request.SellingPrice, request.BeaconGuid);
if (result.isFail())
{
send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(entity_player, result, null);
return result;
}
return result;
}
public override async Task onProcessPacketException(ISession entityWithSession, IMessage recvMessage
, Result errorResult)
{
await Task.CompletedTask;
var player = entityWithSession as Player;
NullReferenceCheckHelper.throwIfNull(player, () => $"player is null !!! - {entityWithSession.toBasicString()}");
send_S2C_ACK_BEACON_SHOP_REGISTER_ITEM(player, errorResult, null);
}
}