You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
423 lines
16 KiB
423 lines
16 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Sog;
|
|
using ProtoCSStruct;
|
|
using System.Text.Json.Nodes;
|
|
using Newtonsoft.Json.Linq;
|
|
using TencentCloud.Ame.V20190916.Models;
|
|
|
|
namespace Game
|
|
{
|
|
public class BagHandler : BasePacketHandler
|
|
{
|
|
|
|
public override int GetServiceType()
|
|
{
|
|
return GameServiceType.BagHandler;
|
|
}
|
|
|
|
//销毁的时候置空
|
|
public override void Dispose()
|
|
{
|
|
}
|
|
|
|
public BagHandler()
|
|
{
|
|
}
|
|
|
|
|
|
public override void HandlerClientPacket(PlayerSession playerSession, StructPacket packet)
|
|
{
|
|
PlayerOnGame player = playerSession.Player;
|
|
if (player == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (packet.MsgID)
|
|
{
|
|
case (int)CSGameMsgID.UseItemReq:
|
|
OnUseItemReq(player, packet);
|
|
break;
|
|
case (int)CSGameMsgID.QueryBagChangeHistoryListReq:
|
|
OnQueryBagChangeHistoryListReq(player, packet);
|
|
break;
|
|
case (int)CSGameMsgID.SetItemNewgetReq:
|
|
SetItemNewgetReq(player, packet);
|
|
break;
|
|
case (int)CSGameMsgID.GainPowerReq:
|
|
PlayerDataSvc.GainPower(player, packet);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override void HandlerServerPacket(uint serverID, StructPacket packet)
|
|
{
|
|
switch (packet.MsgID)
|
|
{
|
|
case (int)SSGameMsgID.OperationCostItemReq:
|
|
{
|
|
OnCostItemReq(serverID, packet);
|
|
break;
|
|
}
|
|
case (int)SSGameMsgID.Reward:
|
|
{
|
|
OnGMReward(serverID, packet);
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public static void OnGMReward(uint serverID, StructPacket packet)
|
|
{
|
|
SSGMRewardRes res = new SSGMRewardRes();
|
|
ref SSGMReward req = ref packet.GetMessage<SSGMReward>();
|
|
res.Rid = req.Rid;
|
|
res.Reward.CopyFrom(ref req.Reward);
|
|
res.Id = req.Id;
|
|
res.Times = req.Times;
|
|
PlayerOnGame player = GameServerUtils.GetPlayerTableOp().GetPlayerByUid(req.Rid);
|
|
if (player == null)
|
|
{
|
|
res.Json.SetString("player not online");
|
|
GameServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.RewardRes, ref res, packet.ObjectID);
|
|
return;
|
|
}
|
|
List<RewardItemRW> list = new List<RewardItemRW>();
|
|
for (int i = 0; i < req.Times; i++)
|
|
{
|
|
List<RewardItemRW> list1 = RewardSvc.Reward(player, req.Reward.GetString(), false);
|
|
list.AddRange(list1);
|
|
}
|
|
list = RewardSvc.MergeReward(list);
|
|
//RewardSvc.ApplyGiveReward(player, list);
|
|
|
|
JArray arr = JArray.FromObject(list);
|
|
res.Json.SetString(arr.ToString());
|
|
|
|
GameServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.RewardRes, ref res, packet.ObjectID);
|
|
}
|
|
/**
|
|
* GM 扣除玩家道具
|
|
*/
|
|
public static void OnCostItemReq(uint serverID, StructPacket packet)
|
|
{
|
|
SSOperationCostItemRes res = new SSOperationCostItemRes();
|
|
ref SSOperationCostItemReq req = ref packet.GetMessage<SSOperationCostItemReq>();
|
|
PlayerOnGame player = GameServerUtils.GetPlayerTableOp().GetPlayerByUid(packet.ObjectID);
|
|
if (player == null)
|
|
{
|
|
req.Model = 1; //转到dbserver
|
|
GameServerUtils.GetPacketSender()
|
|
.SendToWorldServer((int)SSGameMsgID.OperationCostItemReq, ref req, packet.ObjectID);
|
|
|
|
return;
|
|
}
|
|
|
|
string itemId = req.ItemId.GetString();
|
|
int value = req.CostNum;
|
|
|
|
UnifyOp unifyOp = new UnifyOp(player, BillChangeItemReason.GmCmd);
|
|
var itemDesc = ItemDescMgr.Instance.GetConfig(itemId);
|
|
if (itemDesc == null)
|
|
{
|
|
player.Error("BagHandler.OnCostItemReq item config not found {0}", itemId);
|
|
res.Ret = -1;
|
|
GameServerUtils.GetPacketSender()
|
|
.SendToWorldServer((int)SSGameMsgID.OperationCostItemRes, ref res, packet.ObjectID);
|
|
return;
|
|
}
|
|
|
|
if (value < 0)
|
|
{
|
|
unifyOp.CostItem(itemId, -value);
|
|
}
|
|
else
|
|
{
|
|
unifyOp.AddItem(itemId, value);
|
|
}
|
|
|
|
var ret = unifyOp.DoOp(true, true);
|
|
res.Ret = (int)ret;
|
|
|
|
player.MakeDirty();
|
|
GameServerUtils.GetPacketSender()
|
|
.SendToWorldServer((int)SSGameMsgID.OperationCostItemRes, ref res, packet.ObjectID);
|
|
}
|
|
|
|
public static void SetItemNewgetReq(PlayerOnGame player, StructPacket packet)
|
|
{
|
|
ref CSSetItemNewgetReq req = ref packet.GetMessage<CSSetItemNewgetReq>();
|
|
CSSetItemNewgetRes res = new CSSetItemNewgetRes();
|
|
res.ItemID = req.ItemID;
|
|
var itemId = req.ItemID;
|
|
|
|
var desc = ItemDescMgr.Instance.GetConfigByInternal(itemId);
|
|
UnifyOp bagOp = new UnifyOp(player, BillChangeItemReason.Bag, "Bag");
|
|
var existCount = bagOp.GetItemCount(desc.itemID);
|
|
if(existCount == 0)
|
|
{
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.SetItemNewgetRes, ref res);
|
|
return;
|
|
}
|
|
ref var item = ref bagOp.GetItem(desc.itemID);
|
|
item.NewGet = false;
|
|
res.Ret = CSErrCode.None;
|
|
|
|
player.SendToClient((int)CSGameMsgID.SetItemNewgetRes, ref res);
|
|
}
|
|
/**
|
|
* 玩家在背包内使用道具
|
|
*/
|
|
public static void OnUseItemReq(PlayerOnGame player, StructPacket packet)
|
|
{
|
|
ref CSUseItemReq req = ref packet.GetMessage<CSUseItemReq>();
|
|
CSUseItemRes res = new CSUseItemRes();
|
|
res.ItemID = req.ItemID;
|
|
res.UseCount = req.UseCount;
|
|
res.ChooseItemIndex = req.ChooseItemIndex;
|
|
|
|
//判断数量和能否使用
|
|
if (req.UseCount <= 0)
|
|
{
|
|
player.Error("BagHandler.OnUseItemReq uid {0} item id {1} use count {2} error,", player.UserID,
|
|
req.ItemID, req.UseCount);
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
var itemId=req.ItemID ;
|
|
|
|
var desc = ItemDescMgr.Instance.GetConfigByInternal(itemId);
|
|
if (desc == null)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
var index =UnifyOp.GetIndexById(player, desc.itemID);
|
|
var value = index < 0 ? 0 : player.RoleData.PandoraBag.ItemList[index].Count;
|
|
if (value < req.UseCount)
|
|
{
|
|
res.Ret = CSErrCode.NotEnoughItem;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
if (desc.isShowBag == 0) //不在背包中的道具不能在这使用
|
|
{
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
List<TypeIDValueString> items = new List<TypeIDValueString>();
|
|
var type = desc.type;
|
|
UnifyOp bagOp = new UnifyOp(player, BillChangeItemReason.Bag, "Bag");
|
|
var existCount = bagOp.GetItemCount(desc.itemID);
|
|
if (existCount == 0 || existCount < req.UseCount)
|
|
{
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
bagOp.CostItem(desc.itemID, req.UseCount);
|
|
switch (type)
|
|
{
|
|
case ItemType.RandomBox:
|
|
{
|
|
for (int count = 0; count < req.UseCount; count++)
|
|
{
|
|
var drop = RewardSvc.Reward(player, desc.useType, false, false);
|
|
for (int i = 0; i < drop.Count; i++)
|
|
{
|
|
//掉落重新改
|
|
bagOp.AddGoods(drop[i].type, drop[i].code, drop[i].amount);
|
|
GenericUtils.Merge(items, drop[i].type, drop[i].code, (int)drop[i].amount);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case ItemType.ChooseBox:
|
|
{
|
|
if (req.ChooseItemIndex < 0)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
if (req.ChooseItemIndex > desc.chooseItemId.Length)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
var targetId = desc.chooseItemId[req.ChooseItemIndex];
|
|
var targetType = desc.chooseItemType[req.ChooseItemIndex];
|
|
if (targetType == 1) //item config 道具表
|
|
{
|
|
var targetDesc = ItemDescMgr.Instance.GetConfig(targetId);
|
|
if (targetDesc == null)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (int count = 0; count < req.UseCount; count++)
|
|
{
|
|
bagOp.AddGoods(
|
|
desc.chooseItemType[req.ChooseItemIndex],
|
|
desc.chooseItemId[req.ChooseItemIndex],
|
|
desc.chooseItemNum[req.ChooseItemIndex]);
|
|
GenericUtils.Merge(items, desc.chooseItemType[req.ChooseItemIndex],desc.chooseItemId[req.ChooseItemIndex],desc.chooseItemNum[req.ChooseItemIndex]);
|
|
}
|
|
}
|
|
break;
|
|
case ItemType.ChooseGem:
|
|
{
|
|
if (req.ChooseItemIndex < 0)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
if (req.ChooseItemIndex > desc.chooseItemId.Length)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
var targetId = desc.chooseItemId[req.ChooseItemIndex];
|
|
var targetType = (int)GoodsType.Gem;
|
|
|
|
var targetDesc = GemDescMgr.Instance.GetConfig(targetId);
|
|
if (targetDesc == null)
|
|
{
|
|
res.Ret = CSErrCode.DescNotFound;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
|
|
for (int count = 0; count < req.UseCount; count++)
|
|
{
|
|
bagOp.AddGoods(
|
|
targetType,
|
|
desc.chooseItemId[req.ChooseItemIndex],
|
|
1);
|
|
GenericUtils.Merge(items, targetType, desc.chooseItemId[req.ChooseItemIndex], 1);
|
|
}
|
|
}
|
|
break;
|
|
case ItemType.MixItem:
|
|
{
|
|
var mergeNum = req.UseCount / Math.Max(1, int.Parse(desc.useType));
|
|
if (mergeNum < 1)
|
|
{
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
bagOp.AddGoods(desc.mixItemType, desc.mixItemId, desc.mixItemNum * mergeNum);
|
|
GenericUtils.Merge(items, desc.mixItemType,desc.mixItemId,desc.mixItemNum * mergeNum);
|
|
}
|
|
break;
|
|
case ItemType.FixBox:
|
|
{
|
|
var pass = UseConditionCheck(player, desc.itemID);
|
|
if (!pass)
|
|
{
|
|
res.Ret = CSErrCode.Fail;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
var length = desc.packItemId.Length;
|
|
for (var i = 0; i < length; i++)
|
|
{
|
|
bagOp.AddGoods(desc.packItemType[i], desc.packItemId[i], desc.packItemNum[i] * req.UseCount);
|
|
|
|
GenericUtils.Merge(items, desc.packItemType[i],desc.packItemId[i],desc.packItemNum[i] * req.UseCount);
|
|
}
|
|
}
|
|
break;
|
|
case ItemType.Exp:
|
|
LevelUtils.AddExp(player, req.UseCount, BillChangeItemReason.Bag);
|
|
break;
|
|
}
|
|
|
|
var ret = bagOp.DoOp( true, true, true);
|
|
if (ret != CSErrCode.None)
|
|
{
|
|
res.Ret = ret;
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
return;
|
|
}
|
|
|
|
items.ForEach(item => { res.GetItems.Add(item); });
|
|
player.MakeDirty();
|
|
player.SendToClient((int)CSGameMsgID.UseItemRes, ref res);
|
|
}
|
|
|
|
private static bool UseConditionCheck(PlayerOnGame player, string itemId)
|
|
{
|
|
var desc = ItemDescMgr.Instance.GetConfig(itemId);
|
|
if (desc.useConditions.Sum() <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
for (var i = 0; i < desc.useConditions.Length; i++)
|
|
{
|
|
switch (desc.useConditions[i])
|
|
{
|
|
case (int)PandoraItemUseType.Level:
|
|
{
|
|
var value = desc.useConditionsNum[i];
|
|
if (player.RoleBase.Level< value)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
private void OnQueryBagChangeHistoryListReq(PlayerOnGame player, StructPacket packet)
|
|
{
|
|
CSQueryBagChangeHistoryListRes res = new CSQueryBagChangeHistoryListRes();
|
|
|
|
res.DBBagChangeRecordList = player.unimportanceData.DBBagChangeRecordList;
|
|
|
|
player.SendToClient((int)CSGameMsgID.QueryBagChangeHistoryListRes, ref res);
|
|
|
|
player.Trace("BagHandler.OnQueryBagChangeHistoryListReq uid {0} success, record count {1}"
|
|
, player.UserID, res.DBBagChangeRecordList.Count);
|
|
}
|
|
|
|
|
|
//开礼包
|
|
}
|
|
}
|