using Sog; using ProtoCSStruct; namespace Game { public class MarketShopHandler : BasePacketHandler { public override int GetServiceType() { return GameServiceType.MarketShopHandler; } //销毁的时候置空 public override void Dispose() { } public MarketShopHandler() { } public override void HandlerClientPacket(PlayerSession playerSession, StructPacket packet) { PlayerOnGame player = playerSession.Player; if (player == null) { return; } switch (packet.MsgID) { case (int)CSGameMsgID.MarketShopBuyGoodsReq: OnMarketShopBuyGoodsReq(player, packet); break; case (int)CSGameMsgID.MarketShopRefreshReq: OnMarketShopRefreshReq(player, packet); break; default: TraceLog.Error("MarketShopHandler.HandlerClientPacket uid {0} msgId {1} not handle", player.UserID, packet.MsgID); break; } } public override void HandlerServerPacket(uint serverID, StructPacket packet) { } private void OnMarketShopBuyGoodsReq(PlayerOnGame player, StructPacket packet) { ref var req = ref packet.GetMessage(); var res = new CSMarketShopBuyGoodsRes { ShopId = req.ShopId, BuyTimes = req.BuyTimes }; if (req.BuyTimes <= 0) { res.Ret = CSErrCode.SysFailure; player.SendToClient((int) CSGameMsgID.MarketShopBuyGoodsRes, ref res); return; } int index = MarketShopSvc.GetShopDataIndex(player, req.ShopId); if (index == -1) { TraceLog.Error("MarketShopHandler.OnMarketShopBuyGoodsReq uid {0} shopId {1} not exist", player.UserID, req.ShopId); res.Ret = CSErrCode.MarketShopNotExist; player.SendToClient((int) CSGameMsgID.MarketShopBuyGoodsRes, ref res); return; } ref OneShopData shopData = ref MarketShopSvc.GetShopDataByIndex(player, index); OnBuyGoods(player, ref shopData, ref req, ref res); } public static void OnAdvBuyGoods(PlayerOnGame player,int shopId,int goodsCfgId) { var desc = MarketShopGoodsDescMgr.Instance.GetConfig(shopId, goodsCfgId); if (desc == null) { return; } var goodsId = desc.InternalId; var index = MarketShopSvc.GetShopDataIndex(player, shopId); if (index == -1) { return; } var res = new CSMarketShopBuyGoodsRes { ShopId = shopId, BuyTimes = 1 }; ref var shopData = ref MarketShopSvc.GetShopDataByIndex(player, index); index = MarketShopSvc.GetOneGridGoodsPosByGoodsIndex(ref shopData, goodsId); if (index < 0) { return; } ref var gridGoods = ref shopData.GridGoods[index]; var req = new CSMarketShopBuyGoodsReq { ShopId = shopId, GoodsId = goodsId, BuyTimes = 1 }; res.Ret = MarketShopSvc.MarketShopBuyGoods(player, ref req, ref shopData, ref gridGoods, ref res); if (res.Ret == CSErrCode.None) { gridGoods.CurBuyTimes += 1; res.GridGoods = gridGoods; var statChgOp = new RoleStatChgOp(player); statChgOp.NotifyClient(); } player.SendToClient((int)CSGameMsgID.MarketShopBuyGoodsRes, ref res); } public void OnBuyGoods(PlayerOnGame player, ref OneShopData shopData, ref CSMarketShopBuyGoodsReq req, ref CSMarketShopBuyGoodsRes res) { int index = MarketShopSvc.GetOneGridGoodsPosByGoodsIndex(ref shopData, req.GoodsId); if (index < 0) { TraceLog.Error("MarketShopHandler.OnBuyGoods uid {0} shopId {1} goodsIndex {2} not exist" , player.UserID, req.ShopId, req.GoodsId); res.Ret = CSErrCode.DescNotFound; player.SendToClient((int)CSGameMsgID.MarketShopBuyGoodsRes, ref res); return; } ref OneGridGoods gridGoods = ref shopData.GridGoods[index]; res.Ret = MarketShopSvc.MarketShopBuyGoods(player, ref req, ref shopData, ref gridGoods,ref res); if (res.Ret == CSErrCode.None) { gridGoods.CurBuyTimes += req.BuyTimes; res.GridGoods = gridGoods; RoleStatChgOp statChgOp = new RoleStatChgOp(player); statChgOp.NotifyClient(); } player.SendToClient((int)CSGameMsgID.MarketShopBuyGoodsRes, ref res); } public int OnMarketShopRefreshReq(PlayerOnGame player, StructPacket packet) { ref var req = ref packet.GetMessage(); var res = new CSMarketShopRefreshRes {Ret = CSErrCode.None}; MarketShopDesc desc = MarketShopDescMgr.Instance.GetConfig(req.ShopId); if (desc == null) { TraceLog.Error("MarketShopSvc.OnMarketShopRefreshReq shopId {0} not in table", req.ShopId); return -1; } int index = MarketShopSvc.GetShopDataIndex(player, req.ShopId); if (index == -1) { TraceLog.Error("MarketShopHandler.OnMarketShopRefreshReq uid {0} shopId {1} not exist" , player.UserID, req.ShopId); res.Ret = CSErrCode.MarketShopNotExist; player.SendToClient((int)CSGameMsgID.MarketShopRefreshRes, ref res); return -1; } ref OneShopData shopData = ref MarketShopSvc.GetShopDataByIndex(player, index); // 手动刷新 if (req.IsNeedRefresh) { res.Ret = MarketShopSvc.HandRefreshShop(player, desc, ref shopData); } player.MakeDirty(); res.ShopData = shopData; player.SendToClient((int)CSGameMsgID.MarketShopRefreshRes, ref res); return 0; } } }