package game

import (
	"errors"
	"fmt"
	"math"
	"server/conf"
	baseCfg "server/conf/base"
	cardCfg "server/conf/card"
	champshipCfg "server/conf/champship"
	collectCfg "server/conf/collect"
	decorateCfg "server/conf/decorate"
	emojiCfg "server/conf/emoji"
	friendCfg "server/conf/friend"
	GuideTaskCfg "server/conf/guide_task"
	handbookCfg "server/conf/handbook"
	limitedTimeEventCfg "server/conf/limited_time_event"
	mergeDataCfg "server/conf/merge_data"
	miningCfg "server/conf/mining"
	orderCfg "server/conf/order"
	playroomCfg "server/conf/playroom"
	"server/db"
	"server/game/internal"
	"server/game/mod/activity"
	"server/game/mod/card"
	"server/game/mod/collect"
	"server/game/mod/friend"
	"server/game/mod/item"
	limitedTimeEvent "server/game/mod/limited_time_event"
	MsqMod "server/game/mod/msg"
	"server/game/mod/order"
	piggyBank "server/game/mod/piggy_bank"
	"server/game/mod/playroom"
	"server/game/mod/quest"
	GoUtil "server/game_util"
	"server/msg"
	"strconv"

	"server/pkg/github.com/name5566/leaf/gate"
	"server/pkg/github.com/name5566/leaf/log"

	"google.golang.org/protobuf/proto"
)

func ReqPlayerAsset(player *Player, buf []byte) error {
	player.PushClientRes(player.PlayerBaseMod.BackAsset())
	return nil
}

// 请求玩家基础信息
func ReqPlayerBaseInfofunction(player *Player, buf []byte) error {
	// 消息的发送者
	detail := &msg.ReqPlayerBaseInfo{}
	err := proto.Unmarshal(buf, detail)
	if err != nil {
		return err
	}

	player.PlayerBaseMod.ResPlayerBaseInfo(player)
	return nil
}

func ReqPlayerBriefProfileDataFunc(player *Player, buf []byte) error {
	detail := &msg.ReqPlayerBriefProfileData{}
	err := proto.Unmarshal(buf, detail)
	if err != nil {
		return err
	}
	Uid := int(detail.DwUin)
	PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(Uid)
	if PlayerSimpleData == nil {
		log.Debug("玩家不存在, Uid:%d", Uid)
		player.SendErrClienRes(&msg.ResPlayerBriefProfileData{
			DwUin: detail.DwUin,
		})
		return errors.New("玩家不存在")
	}
	player.PushClientRes(&msg.ResPlayerBriefProfileData{
		DwUin:       detail.DwUin,
		ImageFrame:  int32(PlayerSimpleData.Avatar),
		ImageIcon:   int32(PlayerSimpleData.Face),
		DecorateCnt: int32(PlayerSimpleData.Decorate),
		PicURL:      PlayerSimpleData.FaceBookPic,
		ActiveTime:  int32(max(PlayerSimpleData.Loginout, PlayerSimpleData.Login)),
		NickName:    PlayerSimpleData.Name,
		SetEmoji:    GoUtil.MapIntToInt32(PlayerSimpleData.Emoji),
	})
	return nil
}

func ReqFriendPlayerSimple(player *Player, buf []byte) error {
	detail := &msg.ReqFriendPlayerSimple{}
	err := proto.Unmarshal(buf, detail)
	if err != nil {
		return err
	}
	Uid := int(detail.Uid)
	PlayerSimpleData := G_GameLogicPtr.GetResFriendPlayerByUid(Uid)
	if PlayerSimpleData == nil {
		log.Debug("玩家不存在, Uid:%d", Uid)
		return errors.New("玩家不存在")
	}
	FriendMod := player.PlayMod.getFriendMod()
	PlayerSimpleData.AddTime = FriendMod.GetAddTime(Uid)
	PlayerSimpleData.Interact = FriendMod.GetInteractTime(Uid)
	player.PushClientRes(PlayerSimpleData)
	return nil
}

// 移除广告
func ReqRemoveAdFunc(player *Player, buf []byte) error {
	player.PlayerBaseMod.ReqRemoveAd(player, buf)
	player.TeLog("remove_ad", map[string]interface{}{})
	return nil
}

// 请求玩家棋盘信息
func ReqPlayerChessDataFunc(player *Player, buf []byte) error {
	detail := &msg.ReqPlayerChessData{}
	err := proto.Unmarshal(buf, detail)
	if err != nil {
		return err
	}
	playerChessData := &PlayerChessData{}
	playerChessData.ResPlayerChessData(player)
	return nil
}

func ReqBindFacebookAccount(player *Player, buf []byte) error {
	player.PlayerBaseMod.ReqBindFacebookAccount(player, buf)
	return nil
}

func ReqUnBindFacebook(player *Player, buf []byte) error {
	player.PlayerBaseMod.ReqUnBindFacebook(player, buf)
	return nil
}

func ReqOnlyBindFacebook(player *Player, buf []byte) error {
	player.PlayerBaseMod.ReqOnlyBindFacebook(player, buf)
	return nil
}

func ReqSynGameData(player *Player, buf []byte) error {
	player.PlayerBaseMod.ReqSynGameData(player, buf)
	return nil
}

func RegSetEneryFunc(player *Player, buf []byte) error {
	req := &msg.ReqSetEnergyMul{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	MaxEnergyMul := player.GetPlayerBaseMod().GetMaxEnergyMul(player)
	BaseMod := player.PlayMod.getBaseMod()
	EnergyMul := BaseMod.GetEnergyMul()
	if int(req.EnergyMul) > EnergyMul && int(req.EnergyMul) > MaxEnergyMul {
		player.SendErrClienRes(&msg.ResSetEnergyMul{
			ResultCode: msg.RES_CODE_FAIL,
			Msg:        "能量倍数超过最大值",
		})
		return errors.New("能量倍数超过最大值")
	}
	player.TeLog("mutil_merge_change", map[string]interface{}{
		"change_from": math.Pow(2, float64(BaseMod.GetEnergyMul())),
		"change_to":   math.Pow(2, float64(req.EnergyMul)),
		"is_auto":     false,
	})
	player.PlayMod.getBaseMod().SetEnergyMul(int(req.EnergyMul))
	OrderMod := player.PlayMod.getOrderMod()
	Lv := player.GetPlayerBaseMod().GetLevel()
	Emit := player.PlayMod.getChessMod().GetOrderEmit()
	ChessList := player.PlayMod.getChessMod().GetUnlockChessList()
	OrderMod.ChangeEnergyMul(Lv, Emit, int(req.EnergyMul), ChessList)
	player.InitOrderItem()
	player.PlayMod.save()
	player.PushClientRes(OrderMod.BackData())
	player.PushClientRes(player.PlayMod.getBaseMod().BackData())
	return nil
}

// 领取图鉴奖励
func ReqGetHandbookReward(player *Player, buf []byte) error {
	req := &msg.ReqGetHandbookReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	err = player.PlayMod.getHandbookMod().GetHandbookReward(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetHandbookReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	itemList := handbookCfg.GetHandbookCollectReward()
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_HandbookReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetHandbookReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PushClientRes(&msg.ResGetHandbookReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PushClientRes(player.PlayMod.getHandbookMod().BackData())
	player.PlayMod.save()
	player.TeLog("collection_reward", map[string]interface{}{
		"item_id":   int(req.ChessId),
		"item_name": mergeDataCfg.GetNameById(int(req.ChessId)),
	})
	return nil
}

func RegHandbookAllReward(player *Player, buf []byte) error {
	req := &msg.RegHandbookAllReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	AllChess := mergeDataCfg.GetAllChessBySeries(req.Type)
	Items, err := player.PlayMod.getHandbookMod().CollectItem(req.Type, AllChess)
	if err != nil {
		player.SendErrClienRes(&msg.ResHandbookAllReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_HandbookAllReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResHandbookAllReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_HANDBOOK_ACHIEVEMENT, req.Type)
	player.UpdateUserInfo()
	player.PushClientRes(&msg.ResHandbookAllReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	now := GoUtil.Now()
	NotifyAllFriend(player, &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_HANDBOOK_COLLECTION,
		From:  int(player.M_DwUin),
		SendT: now,
		End:   now + sevendays,
		Extra: MsqMod.HandbookMsg{
			Type: req.Type,
		},
	})
	player.TeLog("collection_like", map[string]interface{}{
		"collection_type":   req.Type,
		"collection_reward": Items,
	})
	player.PushClientRes(player.PlayMod.getHandbookMod().BackData())
	player.PlayMod.save()
	return nil
}

// 领取订单奖励
func ReqRewardOrder(player *Player, buf []byte) error {
	req := &msg.ReqRewardOrder{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	OrderMod := player.PlayMod.getOrderMod()
	ChessMod := player.PlayMod.getChessMod()
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	Item, mergeList, OrderType, Star, err := OrderMod.RewardOrder(int(req.OrderId))
	if OrderType == order.Pet_type {
		Item = player.FormatPetOrderItem(Item)
	}
	if OrderType == order.Super_type {
		virtualEnergy := order.GetVirtualEnergy(mergeList, ChessMod.GetStarEmitList())
		superOrderItem := limitedTimeEventCfg.GetSuperOrderReward(virtualEnergy)
		Item = item.Merge(Item, superOrderItem)
	}
	ActItem := player.GetActivityItem(GoUtil.Int32ToInt(req.ActType))
	Item = item.Merge(Item, ActItem)
	if !OrderMod.CheckSuperOrder() {
		if LimitedTimeEventMod.RemoveSuperOrder() {
			player.PushClientRes(&msg.LimitEventNotify{
				Id:   limitedTimeEvent.EVENT_TYPE_SUPER_ORDER,
				Type: limitedTimeEvent.EVENT_NOTIFY_TYPE_DEL,
			})
		}
	}
	if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_METEOR_SHOW) { //流星雨活动
		AddItem := LimitedTimeEventMod.GetMeteorReward(mergeList, ChessMod.GetStarEmitList())
		if len(AddItem) > 0 {
			player.TeLog("time_limited_event_action", map[string]interface{}{
				"event_type": limitedTimeEventCfg.GetEventName(limitedTimeEvent.EVENT_TYPE_METEOR_SHOW),
				"item_list":  AddItem,
			})
		}
		Item = item.Merge(Item, AddItem)
	}
	if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_MONEY_CAT) { //招财猫活动
		if OrderType != order.Super_type {
			AddItem := LimitedTimeEventMod.GetMoneyCatReward(mergeList)
			if len(AddItem) > 0 {
				player.TeLog("time_limited_event_action", map[string]interface{}{
					"event_type": limitedTimeEventCfg.GetEventName(limitedTimeEvent.EVENT_TYPE_MONEY_CAT),
					"item_list":  AddItem,
				})
			}
			Item = item.Merge(Item, AddItem)
		}
	}
	if err != nil {
		player.SendErrClienRes(&msg.ResRewardOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = ChessMod.FinishOrder(mergeList)
	if err != nil {
		player.SendErrClienRes(&msg.ResRewardOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = player.HandleItem(Item, msg.ITEM_POP_LABEL_OrderReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResRewardOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_CHEST_RAIN) { //宝箱雨活动
		ChestRainItems := LimitedTimeEventMod.GetChestReward(mergeList, ChessMod.GetStarEmitList())
		player.args["ResItemPopId"] = req.OrderId
		err = player.HandleItem(ChestRainItems, msg.ITEM_POP_LABEL_LimitEventChestRain.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResRewardOrder{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}

		if len(ChestRainItems) > 0 {
			player.PushClientRes(&msg.ResChessRainReward{
				Items: item.ItemToMsg(ChestRainItems),
				Id:    0,
			})
			player.TeLog("time_limited_event_action", map[string]interface{}{
				"event_type": limitedTimeEventCfg.GetEventName(limitedTimeEvent.EVENT_TYPE_CHEST_RAIN),
				"item_list":  ChestRainItems,
			})
		}
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResRewardOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	// 每日任务
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_FINISHORDER})
	order_star_reward := 0
	order_item_reward := []int{}
	for _, v := range Item {
		if v.Id == item.ITEM_STAR_ID {
			order_star_reward += v.Num
		} else {
			order_item_reward = append(order_item_reward, v.Id)
		}
	}
	preset_order_group := 0
	if OrderType != order.Preview_type && OrderType != order.Fixed_type {
		preset_order_group = -1
	}
	preset_order_group, preset_order_step := orderCfg.GetStartOrderInfo(int(req.OrderId))
	player.TeLog("order_finish", map[string]interface{}{
		"order_id":           int(req.OrderId),
		"order_item_id":      mergeList,
		"order_type":         OrderType,
		"order_star_reward":  order_star_reward,
		"order_item_reward":  order_item_reward,
		"order_star_value":   Star,
		"preset_order_group": preset_order_group,
		"preset_order_step":  preset_order_step,
	})
	player.PetItemGetLog(Item, nil, "Order")
	player.EmitRetireTrigger2()
	Lv := player.GetPlayerBaseMod().GetLevel()
	EnergyMul := player.PlayMod.getBaseMod().GetEnergyMul()
	Emit := ChessMod.GetOrderEmit()
	player.TriggerSeedSource()
	//NewOrder := TriggerSeedOrder(player)
	// if NewOrder == nil || len(NewOrder.MergeId) == 0 {
	// 	OrderMod.CreateOrder(Lv, Emit, EnergyMul)
	// } else {
	// 	OrderMod.CreateOrderSeed(NewOrder)
	// }
	player.TriggerOrder(Lv, order.TRIGGER_TYPE_ORDER, Emit, EnergyMul)
	player.AddOrder()
	player.AddHighOrder()
	player.AddHighOrder2()
	player.InitOrderItem()
	// 存钱罐增加钻石
	PiggyBankMod := player.PlayMod.getPiggyBankMod()
	PiggyBankMod.Trigger()

	// 锦标赛增加积分
	if player.GetPlayerBaseMod().GetLevel() >= 17 {
		ChessList := make([]int, 0)
		for _, v := range mergeList {
			color := mergeDataCfg.GetColorById(v)
			Emit := order.GetEmitByColor(ChessMod.GetStarEmitList(), color)
			EmitType := mergeDataCfg.GetEmitType(Emit)
			if EmitType == "auto" {
				continue
			}
			ChessList = append(ChessList, v)
		}
		ChampshipMod := player.PlayMod.getChampshipMod()
		ChampshipMod.AddScore(ChessList)
		player.HandleInChampshipRank()
	}
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	FriendTreasureMod.AddStar(Star / 20)
	player.PushClientRes(FriendTreasureMod.NotifyStar())
	// 限时事件增加进度
	LimitedTimeEventMod.AddProgress(player.GetPlayerBaseMod().GetLevel())
	player.PlayMod.save()
	player.PushClientRes(PiggyBankMod.BackData())
	player.PushClientRes(OrderMod.BackData())
	player.BackChampship()
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(LimitedTimeEventMod.ProgressBackData())
	player.PushClientRes(&msg.ResRewardOrder{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqCreatePetOrder(player *Player, buf []byte) error {
	player.CreatePetOrder()
	OrderMod := player.PlayMod.getOrderMod()
	player.PushClientRes(OrderMod.BackData())
	return nil
}

func ReqDelOrder(player *Player, buf []byte) error {
	req := &msg.ReqDelOrder{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	OrderMod := player.PlayMod.getOrderMod()
	DelOrder, err := OrderMod.DelOrder(int(req.OrderId))
	if err != nil {
		player.SendErrClienRes(&msg.ResDelOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PushAndSendClienRes(&msg.ResDelOrder{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("order_delete", map[string]interface{}{
		"order_id": int(req.OrderId),
		"order":    DelOrder,
	})
	player.PushClientRes(OrderMod.BackData())
	return nil
}

// 场景装饰
func ReqDecorate(player *Player, buf []byte) error {
	req := &msg.ReqDecorate{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DecorateMod := player.PlayMod.getDecorateMod()
	DecorateId := int(req.DecorateId)
	AreaId := int(req.AreaId)
	LimitEventMod := player.PlayMod.getLimitedTimeEventMod()
	DecorateOffIsExist := LimitEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_DECORATE_OFF)
	CostItem, PetExp, PartItem := DecorateMod.GetDecorateCostItem(AreaId, DecorateId, DecorateOffIsExist)
	err = player.HandleLoseItem(CostItem, msg.ITEM_POP_LABEL_DecorateCost.String()) // 扣除道具
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorate{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	AddItem, err := DecorateMod.Decorate(AreaId, DecorateId) // 装饰
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorate{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_DECORATE, A: []interface{}{1}})
	EnergyItem := DecorateMod.GetDecorateAddEnergy(1)
	AddItem = item.Merge(AddItem, EnergyItem)
	err = player.HandleItem(AddItem, msg.ITEM_POP_LABEL_DecorateAdd.String()) // 增加道具
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorate{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	_, err = player.GetPlayerBaseMod().AddExp(player, 10, PetExp)
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorate{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if AddItem != nil {
		player.TeLog("scene_reward", map[string]interface{}{
			"deco_step_id": fmt.Sprintf("%d_%d", DecorateMod.AreaId, DecorateMod.Progress),
			"item_list":    AddItem,
		})
	}
	if DecorateMod.GetAreaId() != AreaId { // 解锁上报
		player.TeLog("plot_unlock", map[string]interface{}{
			"plot_id": DecorateMod.GetAreaId(),
		})
	}
	player.TeLog("finish_deco", map[string]interface{}{
		"deco_step_id":  fmt.Sprintf("%d_%d", AreaId, DecorateId),
		"material_cost": PartItem,
	})
	FriendMod := player.PlayMod.getFriendMod()
	if AreaId == 1 && DecorateId == 20 {
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_RESTROOM, "")
	}
	if AreaId == 1 && DecorateId == 29 {
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_RESTAURANT, "")
	}
	if AreaId == 1 && DecorateId == 36 {
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_BATHROOM, "")
	}
	if AreaId == 1 && DecorateId == 44 {
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_CLOAKROOM, "")
	}
	if AreaId != DecorateMod.AreaId {
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_CHAPTER_SCENES, GoUtil.String(AreaId))
	}
	player.PlayMod.save()
	player.PushClientRes(DecorateMod.BackData())
	player.PushClientRes(&msg.ResDecorate{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.HandleInUserRank()
	player.BackUserInfo()
	player.UpdateUserInfo()
	return nil
}

// 场景装饰
func ReqDecorateAll(player *Player, buf []byte) error {
	req := &msg.ReqDecorateAll{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DecorateMod := player.PlayMod.getDecorateMod()
	AreaId := DecorateMod.GetAreaId()
	UnLockLv := decorateCfg.GetOneKeyDecorateUnlockLv()
	if player.GetPlayerBaseMod().GetLevel() < UnLockLv {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  fmt.Sprintf("等级不足%d级", UnLockLv),
		})
		return errors.New("等级不足")
	}
	PlayerBaseMod := player.GetPlayerBaseMod()
	LimitEventMod := player.PlayMod.getLimitedTimeEventMod()
	DecorateOffIsExist := LimitEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_DECORATE_OFF)
	CostItem, AddItem, DecorateNum, DecorateList, Log, PetExp := DecorateMod.DecorateAll(PlayerBaseMod.GetStar(), DecorateOffIsExist)
	err = player.HandleLoseItem(CostItem, msg.ITEM_POP_LABEL_DecorateCost.String()) // 扣除道具
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_DECORATE, A: []interface{}{DecorateNum}})
	EnergyItem := DecorateMod.GetDecorateAddEnergy(DecorateNum)
	AddItem = item.Merge(AddItem, EnergyItem)
	err = player.HandleItem(AddItem, msg.ITEM_POP_LABEL_DecorateAdd.String()) // 增加道具
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	_, err = player.GetPlayerBaseMod().AddExp(player, 10*DecorateNum, PetExp)
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	deco_step_id := []string{}
	for _, v := range DecorateList {
		deco_step_id = append(deco_step_id, fmt.Sprintf("%d_%d", AreaId, v))
	}
	player.TeLog("one_click_decoration", map[string]interface{}{
		"deco_step_id": fmt.Sprintf("%v", deco_step_id),
	})

	if len(Log) > 0 {
		for _, v := range Log {
			player.TeLog("scene_reward", v.(map[string]interface{}))
		}
	}

	player.PlayMod.save()
	player.PushClientRes(DecorateMod.BackData())
	player.PushClientRes(&msg.ResDecorate{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.HandleInUserRank()
	player.BackUserInfo()
	player.UpdateUserInfo()
	return nil
}

func ReqAreaReward(player *Player, buf []byte) error {
	req := &msg.ReqAreaReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DecorateMod := player.PlayMod.getDecorateMod()
	RewardItem, err := DecorateMod.GetDecorateReward(int(req.AreaId))
	if err != nil {
		player.SendErrClienRes(&msg.ResAreaReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(RewardItem, msg.ITEM_POP_LABEL_DecorateReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResAreaReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PushClientRes(&msg.ResAreaReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PushClientRes(DecorateMod.BackData())
	player.PlayMod.save()
	return nil
}

// -----------------------ChessApi----------------------
// 更新棋盘信息
func UpdatePlayerChessDataFunc(player *Player, buf []byte) error {
	data := &PlayerChessData{}
	err := data.UpdatePlayerChessData(player, buf)
	player.RedBackData()
	player.UpdateUserInfo()
	return err
}

// 从缓存中获取棋子
func ReqGetChessFromBuff(player *Player, buf []byte) error {
	req := &msg.ReqGetChessFromBuff{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	HandbookMod := player.PlayMod.getHandbookMod()
	OrderMod := player.PlayMod.getOrderMod()
	BaseMod := player.PlayMod.getBaseMod()
	ChessId := int(req.ChessId)
	err = ChessMod.GetChessFromBuff(ChessId)
	Update := HandbookMod.SetHandbook(ChessId)
	AddNewEmit := make([]int, 0)
	if Update {
		player.PushClientRes(HandbookMod.BackData())
		AddNewEmit = append(AddNewEmit, ChessId)
	}
	if err != nil {
		player.SendErrClienRes(&msg.ResGetChessFromBuff{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResGetChessFromBuff{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGetChessFromBuff", map[string]interface{}{
		"chess_id": ChessId,
	})
	Update = OrderMod.CreateExtraOrder([]int{ChessId}, AddNewEmit, ChessMod.GetUnlockChessList(), BaseMod.GetEnergyMul(), BaseMod.GetLevel())
	if Update {
		player.PushClientRes(OrderMod.BackData())
	}
	triggerComposeChess(player, ChessId, player.PlayMod.getBaseMod().EnergyMul, player.PlayMod.getChessMod().GetEmitList())
	player.EmitRetireTrigger1()
	player.InitOrderItem()
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResGetChessFromBuff{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

// 放置棋子进背包
func ReqPutPartInBag(player *Player, buf []byte) error {
	req := &msg.ReqPutPartInBag{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	err = ChessMod.PutPartInBag(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResPutPartInBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResPutPartInBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqPutPartInBag", map[string]interface{}{
		"chess_id": req.ChessId,
	})
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResPutPartInBag{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

// 放置棋子进背包
func ReqPutChessInBag(player *Player, buf []byte) error {
	req := &msg.ReqPutChessInBag{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	err = ChessMod.PutChessInBag(int(req.BagId), int(req.ChessId), int(req.EmitId))
	if err != nil {
		player.SendErrClienRes(&msg.ResPutChessInBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResPutChessInBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqPutChessInBag", map[string]interface{}{
		"chess_id": req.ChessId,
		"bag_id":   req.BagId,
		"emit_id":  req.EmitId,
	})
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResPutChessInBag{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

func ReqTakeChessOutBag(player *Player, buf []byte) error {
	req := &msg.ReqTakeChessOutBag{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	ChessId, err := ChessMod.TakeChessOutBag(int(req.BagId))
	if err != nil {
		player.SendErrClienRes(&msg.ResTakeChessOutBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResTakeChessOutBag{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	OrderMod := player.PlayMod.getOrderMod()
	BaseMod := player.PlayMod.getBaseMod()
	Update := OrderMod.CreateExtraOrder([]int{ChessId}, nil, ChessMod.GetUnlockChessList(), BaseMod.GetEnergyMul(), BaseMod.GetLevel())
	if Update {
		player.PushClientRes(OrderMod.BackData())
	}
	player.TeLog("ReqTakeChessOutBag", map[string]interface{}{
		"bag_id":   req.BagId,
		"chess_id": ChessId,
	})
	player.InitOrderItem()
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResTakeChessOutBag{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

func ReqBuyChessBagGrid(player *Player, buf []byte) error {
	req := &msg.ReqBuyChessBagGrid{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	CostItem := ChessMod.GetBuyChessBagGridCost()
	err = player.HandleItem(CostItem, msg.ITEM_POP_LABEL_BuyChessBagGrid.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessBagGrid{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = ChessMod.BuyChessBagGrid()
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessBagGrid{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResBuyChessBagGrid{
		Code: msg.RES_CODE_SUCCESS,
	})
	if len(CostItem) > 0 {
		player.TeLog("warehouse_unlock", map[string]interface{}{
			"unlock_type": "diamond",
			"unlock_cost": CostItem[0].Num,
		})
	}
	return nil
}

func ReqSeparateChess(player *Player, buf []byte) error {
	req := &msg.ReqSeparateChess{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	NewChess, err := ChessMod.SeparateChess(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResSeparateChess{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResSeparateChess{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqSeparateChess", map[string]interface{}{
		"chess_id":  req.ChessId,
		"new_chess": NewChess,
	})
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResSeparateChess{
		Code: msg.RES_CODE_SUCCESS,
	})
	triggerComposeChess(player, NewChess, player.PlayMod.getBaseMod().EnergyMul, player.PlayMod.getChessMod().GetEmitList())
	triggerComposeChess(player, NewChess, player.PlayMod.getBaseMod().EnergyMul, player.PlayMod.getChessMod().GetEmitList())
	player.TeLog("scissors_use", map[string]interface{}{
		"scissors_item_id": int(req.ChessId),
	})
	player.UpdateUserInfo()
	return nil
}

func ReqUpgradeChess(player *Player, buf []byte) error {
	req := &msg.ReqUpgradeChess{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	NewChess, err := ChessMod.UpgradeChess(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResUpgradeChess{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResUpgradeChess{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("wand_use", map[string]interface{}{
		"wand_item_id": req.ChessId,
		"new_chess":    NewChess,
	})
	Handbook := player.PlayMod.getHandbookMod()
	Handbook.SetHandbook(NewChess)
	triggerComposeChess(player, NewChess, player.PlayMod.getBaseMod().EnergyMul, player.PlayMod.getChessMod().GetEmitList())
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResUpgradeChess{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PushClientRes(Handbook.BackData())
	player.UpdateUserInfo()
	return nil
}

// 棋子转换
func ReqChessEx(player *Player, buf []byte) error {
	req := &msg.ReqChessEx{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	if req.CostDia < 0 {
		var itemList []*item.Item
		item := item.NewItem(item.ITEM_DIAMOND_ID, int(req.CostDia))
		itemList = append(itemList, item)
		err := player.HandleItem(itemList, msg.ITEM_POP_LABEL_ChessEx.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResChessEx{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
		player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_BUBBLE, A: []interface{}{}})
	}

	err = ChessMod.ExChess(int(req.OldChessId), int(req.NewChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResChessEx{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	HandbookMod := player.PlayMod.getHandbookMod()
	HandbookMod.SetHandbook(int(req.NewChessId))
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResChessEx{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	// 限时事件气泡 幸运猫
	if req.Type == msg.CHESS_EX_TYPE_CHESS_EX_EVENT {
		LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
		err := LimitedTimeEventMod.LuckyCat(int(req.NewChessId))
		if err != nil {
			player.SendErrClienRes(&msg.ResChessEx{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
		player.TeLog("LimitEventLuckyCat", map[string]interface{}{
			"req": int(req.NewChessId),
		})
	}

	// 限时事件 小学徒
	if req.Type == msg.CHESS_EX_TYPE_CHESS_EX_EVENT_LITTLE_APPRENTICE {
		LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
		if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_LITTLE_APPRENTICE) {
			player.SendErrClienRes(&msg.ResChessEx{
				Code: msg.RES_CODE_FAIL,
				Msg:  "小学徒活动已结束",
			})
			return err
		}

		player.TeLog("LimitEventLittleApprentice", map[string]interface{}{
			"req":  int(req.NewChessId),
			"star": req.CostStar,
		})
	}
	player.TeLog("ReqChessEx", map[string]interface{}{
		"old_chess_id": int(req.OldChessId),
		"new_chess_id": int(req.NewChessId),
		"cost_dia":     req.CostDia,
		"cost_star":    req.CostStar,
	})
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResChessEx{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 领取资源宝箱
func ReqSourceChest(player *Player, buf []byte) error {
	req := &msg.ReqSourceChest{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	ChestId := int(req.ChestId)
	Items, err := ChessMod.SourceChest(ChestId)
	if err != nil {
		player.SendErrClienRes(&msg.ResSourceChest{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_SourceChest.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResSourceChest{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResSourceChest{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.TeLog("ReqSourceChest", map[string]interface{}{
		"chest_id":  ChestId,
		"item_list": Items,
	})
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResSourceChest{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

// ---------------------CardApi----------------------

func ReqCardInfo(player *Player, buf []byte) error {
	CardMod := player.PlayMod.getCardMod()
	player.PushClientRes(CardMod.BackData())
	return nil
}

func ReqCardSeasonFirstReward(player *Player, buf []byte) error {
	req := &msg.ReqCardSeasonFirstReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	itemList, err := CardMod.SeasonFirstReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResCardSeasonFirstReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_CardSeasonFirstReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCardSeasonFirstReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqCardSeasonFirstReward", map[string]interface{}{
		"season_id": CardMod.EndTime,
		"item_list": itemList,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCardSeasonFirstReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 领取卡牌系列收集奖励
func ReqCardCollectReward(player *Player, buf []byte) error {
	req := &msg.ReqCardCollectReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	itemList, chess, err := CardMod.GetCollectReward(int(req.Color))
	if err != nil {
		player.SendErrClienRes(&msg.ResCardCollectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_CardCollectReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCardCollectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_CARD_ALBUM, fmt.Sprintf("%d", int(req.Color)))
	if chess != 0 {
		player.PlayMod.getChessMod().AddChessBuff(chess)
		player.PushClientRes(player.PlayMod.getOrderMod().BackData())
	}
	player.UpdateUserInfo()
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(&msg.ResCardCollectReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("card_book", map[string]interface{}{
		"season_id":    CardMod.EndTime,
		"card_book_id": int(req.Color),
		"item_list":    itemList,
	})
	return nil
}

// 兑换星星奖励
func ReqExStarReward(player *Player, buf []byte) error {
	req := &msg.ReqExStarReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	itemList, CostStar, err := CardMod.ExStarReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResExStarReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_ExStarReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResExStarReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("sticker_exchange", map[string]interface{}{
		// "pack_id":      int(req.Id),
		"sticker_cost": CostStar,
		"item_list":    itemList,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResExStarReward{
		Code: msg.RES_CODE_SUCCESS,
	})

	return nil
}

// 领取全收集奖励
func ReqAllCollectReward(player *Player, buf []byte) error {
	CardMod := player.PlayMod.getCardMod()
	itemList, itemList2, err := CardMod.AllCollectReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResAllCollectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_AllCollectReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResAllCollectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList2, msg.ITEM_POP_LABEL_AllCollectRewardHB.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResAllCollectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_COMPLETE_ALL_CARDS, "all")
	player.UpdateUserInfo()
	player.PlayMod.save()
	player.TeLog("ReqAllCollectReward", map[string]interface{}{
		"item_list":  itemList,
		"item_list2": itemList2,
	})
	player.PushClientRes(CardMod.BackData())
	player.PushClientRes(&msg.ResAllCollectReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// ---------------------GuildApi----------------------
func ReqGuideReward(player *Player, buf []byte) error {
	req := &msg.ReqGuideReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	GuideMod := player.PlayMod.getGuideMod()
	itemList, err := GuideMod.GetReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGuideReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_GuideReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGuideReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGuideReward", map[string]interface{}{
		"guide_id":  int(req.Id),
		"item_list": itemList,
	})
	player.PlayMod.save()
	player.PushClientRes(GuideMod.BackData())
	player.PushClientRes(&msg.ResGuideReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// -----------------dailyTaskApi---------------------
func ReqGetDailyTaskReward(player *Player, buf []byte) error {
	req := &msg.ReqGetDailyTaskReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DailyTaskMod := player.PlayMod.getDailyTaskMod()
	itemList, err := DailyTaskMod.GetDailyReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetDailyTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_DailyTaskReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetDailyTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(DailyTaskMod.BackData())
	player.PushClientRes(&msg.ResGetDailyTaskReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("daily_task", map[string]interface{}{
		"task_id":           int(req.Id),
		"item_list":         itemList,
		"task_type":         "daily",
		"task_score_reward": DailyTaskMod.GetWeekReward2(),
	})
	return nil
}

func ReqGetDailyWeekReward(player *Player, buf []byte) error {
	req := &msg.ReqGetDailyWeekReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DailyTaskMod := player.PlayMod.getDailyTaskMod()
	itemList, err := DailyTaskMod.GetWeekReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetDailyWeekReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_DailyWeekReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetDailyWeekReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGetDailyWeekReward", map[string]interface{}{
		"task_id":           int(req.Id),
		"item_list":         itemList,
		"task_type":         "week",
		"task_score_reward": 0,
	})
	player.PlayMod.save()
	player.PushClientRes(DailyTaskMod.BackData())
	player.PushClientRes(&msg.ResGetDailyWeekReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqDailyUnlock(player *Player, buf []byte) error {
	req := &msg.ReqDailyUnlock{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	DailyTaskMod := player.PlayMod.getDailyTaskMod()
	DailyTaskMod.Unlock()
	player.PlayMod.save()
	player.PushClientRes(DailyTaskMod.BackData())
	player.PushClientRes(&msg.ResDailyUnlock{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqGetGuideTaskReward(player *Player, buf []byte) error {
	req := &msg.ReqGetGuideTaskReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	GuideTaskMod := player.PlayMod.getGuideTaskMod()
	itemList, err := GuideTaskMod.GetTaskReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetGuideTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_GuideTaskReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetGuideTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("new_player_task", map[string]interface{}{
		"task_id":           int(req.Id),
		"item_list":         itemList,
		"task_gruop":        GuideTaskCfg.GetUnlock(int(req.Id)),
		"task_score_reward": GuideTaskCfg.GetActiveReward2(GuideTaskMod.Reward, GuideTaskMod.Active),
	})
	player.PlayMod.save()
	player.PushClientRes(GuideTaskMod.BackData())
	player.PushClientRes(&msg.ResGetGuideTaskReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

func ReqGetGuideActiveReward(player *Player, buf []byte) error {
	req := &msg.ReqGetGuideActiveReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	GuideTaskMod := player.PlayMod.getGuideTaskMod()
	itemList, err := GuideTaskMod.GetActiveReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetGuideActiveReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_GuideActiveReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetGuideActiveReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGetGuideActiveReward", map[string]interface{}{
		"active_id": int(req.Id),
		"item_list": itemList,
	})
	player.PlayMod.save()
	player.PushClientRes(GuideTaskMod.BackData())
	player.PushClientRes(&msg.ResGetGuideActiveReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

// 头像框
func ReqSetAvatar(player *Player, buf []byte) error {
	req := &msg.ReqSetAvatar{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	avatarMod := player.PlayMod.getAvatarMod()
	err = avatarMod.SetAvatar(int(req.Avatar))
	if err != nil {
		player.SendErrClienRes(&msg.ResSetAvatar{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResSetAvatar{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.BackUserInfo()
	player.UpdateUserInfo()
	b := false
	if req.Avatar == 0 {
		b = true
	}
	player.TeLog("avatar_set", map[string]interface{}{
		"avatar_id":   req.Avatar,
		"is_platform": b,
	})
	player.PlayerDecoSetLog("avatar", int(req.Avatar), "")
	return nil
}

// 头像
func ReqSetFace(player *Player, buf []byte) error {
	req := &msg.ReqSetFace{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FaceMod := player.PlayMod.getFaceMod()
	err = FaceMod.SetFace(int(req.Face))
	if err != nil {
		player.SendErrClienRes(&msg.ResSetFace{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResSetFace{
		Code: msg.RES_CODE_SUCCESS,
	})
	is_platform := false
	if req.Face == 0 {
		is_platform = true
	}
	player.TeLog("face_set", map[string]interface{}{
		"face_id":     int(req.Face),
		"is_platform": is_platform,
	})
	player.PlayerDecoSetLog("face", int(req.Face), "")
	player.BackUserInfo()
	player.UpdateUserInfo()
	return nil
}

// 购买能量
func ReqBuyEnergy(player *Player, buf []byte) error {
	req := &msg.ReqBuyEnergy{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	ChargeMod := player.PlayMod.getChargeMod()
	Item, Energy, Diamond := ChargeMod.BuyEnergy(player.IsWeeklyDiscount())
	err = player.HandleItem(Item, msg.ITEM_POP_LABEL_BuyEnergy.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyEnergy{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(BaseMod.BackData())
	player.PushClientRes(&msg.ResBuyEnergy{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.ChargeBackData()
	player.TeLog("buy_energy_diamond", map[string]interface{}{
		"diamond_cost": Diamond,
		"energy_num":   Energy,
	})
	return nil
}

func ReqGetEnergyByAD(player *Player, buf []byte) error {
	BaseMod := player.PlayMod.getBaseMod()
	Item, err := BaseMod.GetEnergyByAD()
	if err != nil {
		player.SendErrClienRes(&msg.ResGetEnergyByAD{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Item, msg.ITEM_POP_LABEL_GetEnergyByAD.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetEnergyByAD{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGetEnergyByAD", map[string]interface{}{
		"item_list": Item,
	})
	player.PlayMod.save()
	player.PushClientRes(BaseMod.BackData())
	player.PushClientRes(&msg.ResGetEnergyByAD{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 领取七日签到奖励
func ReqGetSevenLoginReward(player *Player, buf []byte) error {
	req := &msg.ReqGetSevenLoginReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	SevenLoginMod := player.PlayMod.getSevenLoginMod()
	itemList, err := SevenLoginMod.GetReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetSevenLoginReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_SevenLoginRewardLabel.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetSevenLoginReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(SevenLoginMod.BackData())
	player.PushClientRes(&msg.ResGetSevenLoginReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("weekly_gift", map[string]interface{}{
		"reward_type": "week",
		"reward_step": int(req.Id),
		"item_list":   itemList,
	})
	return nil
}

// 领取月进度奖励
func ReqGetMonthLoginReward(player *Player, buf []byte) error {
	req := &msg.ReqGetMonthLoginReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	SevenLoginMod := player.PlayMod.getSevenLoginMod()
	itemList, err := SevenLoginMod.GetMonthReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetMonthLoginReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = int(req.Id)
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_MonthLoginReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetMonthLoginReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqGetMonthLoginReward", map[string]interface{}{
		"reward_type": "month",
		"reward_step": int(req.Id),
		"item_list":   itemList,
	})
	player.PlayMod.save()
	player.PushClientRes(SevenLoginMod.BackData())
	player.PushClientRes(&msg.ResGetMonthLoginReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqFastProduceInfo(player *Player, buf []byte) error {
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	EndTime, Times := LimitedTimeEventMod.GetFastProduceInfo()
	player.PushClientRes(&msg.ResFastProduceInfo{
		EndTime: EndTime,
		Num:     int32(Times),
	})
	return nil
}

// 连击快手奖励
func ReqFastProduceReward(player *Player, buf []byte) error {
	req := &msg.ReqFastProduceReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	itemList, err := LimitedTimeEventMod.GetFastProduceReward(int(req.Energy))
	if err != nil {
		player.SendErrClienRes(&msg.ResFastProduceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_FastProduceReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResFastProduceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.TeLog("ReqFastProduceReward", map[string]interface{}{
		"energy":    int(req.Energy),
		"item_list": itemList,
	})
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(&msg.ResFastProduceReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqLimitSenceReward(player *Player, buf []byte) error {
	limitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	if !limitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_SENCE_DASH) { // 场景冲刺奖励
		player.SendErrClienRes(&msg.ResLimitSenceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "not exist",
		})
		return fmt.Errorf("not exist")

	}
	Id, Items := limitedTimeEventMod.GetSceneDashReward()
	player.args["ResItemPopId"] = Id
	err := player.HandleItem(Items, msg.ITEM_POP_LABEL_LimitSenceReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResLimitSenceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqLimitSenceReward", map[string]interface{}{
		"event_type": limitedTimeEvent.EVENT_TYPE_SENCE_DASH,
		"item_list":  Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResLimitSenceReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqGetGoldCard(player *Player, buf []byte) error {
	gold := player.GetGoldCard()
	if gold == nil {
		player.SendErrClienRes(&msg.ResGetGoldCard{
			Four: 0,
			Five: 0,
		})
		return fmt.Errorf("not exist")
	}
	player.PushClientRes(&msg.ResGetGoldCard{
		Four: int32(gold.Four),
		Five: int32(gold.Five),
	})
	return nil
}

// 搜索用户
func ReqSearchPlayer(player *Player, buf []byte) error {
	req := &msg.ReqSearchPlayer{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Uid, err := strconv.Atoi(req.Uid)
	list := make([]*msg.ResPlayerSimple, 0)
	if err == nil {
		SearchPlayer := G_GameLogicPtr.GetResSimplePlayerByUid(Uid)
		if SearchPlayer != nil && SearchPlayer.Level != 0 {
			list = append(list, SearchPlayer)
		}
	}
	data, err := db.SearchPlayer(req.Uid)
	if err != nil {
		player.SendErrClienRes(&msg.ResSearchPlayer{
			Code: 0,
		})
	}
	for _, v := range data {
		if player.M_DwUin == v.DwUin {
			continue
		}
		SearchPlayer := G_GameLogicPtr.GetResSimplePlayerByUid(int(v.DwUin))
		list = append(list, SearchPlayer)

	}
	// list 列表去重
	l := make(map[int]*msg.ResPlayerSimple)
	for _, v := range list {
		if _, ok := l[int(v.Uid)]; !ok {
			l[int(v.Uid)] = v
		}
	}
	l2 := make([]*msg.ResPlayerSimple, 0)
	for _, v := range l {
		l2 = append(l2, v)
	}
	player.PushClientRes(&msg.ResSearchPlayer{
		List: l2,
	})
	return nil
}

// 申请好友
func ReqApplyFriend(player *Player, buf []byte) error {
	req := &msg.ReqApplyFriend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Uid := int(req.Uid)
	FriendMod := player.PlayMod.getFriendMod()
	if Uid == int(player.M_DwUin) {
		player.SendErrClienRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "cannot apply yourself",
		})
		return fmt.Errorf("cannot apply yourself")
	}
	if FriendMod.CheckFriend(Uid) {
		player.SendErrClienRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "already friend",
		})
		return fmt.Errorf("already friend")
	}
	// 好友人数到达上限（2000人）时，玩家将无法再发送好友申请
	if FriendMod.GetFriendLen() >= friendCfg.GetFriendLimitNum() {
		player.SendErrClienRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "friend list full",
		})
		return fmt.Errorf("friend list full")
	}
	// 对于任何玩家而言，向自己在24小时内已从任意途径发送过好友申请的玩家再次发送好友申请时，该次申请不会被发出
	sendApplyTime := FriendMod.GetSendApplyTime(Uid)
	if sendApplyTime != 0 && GoUtil.Now()-sendApplyTime < 86400 {
		player.PushClientRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Uid:  req.Uid,
			Msg:  "already applied",
		})
		return fmt.Errorf("already applied")
	}
	now := GoUtil.Now()
	if req.Type == 1 {
		Items, err := FriendMod.GetSponsorReward()
		if err != nil {
			player.SendErrClienRes(&msg.ResApplyFriend{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
		BaseMod := player.PlayMod.getBaseMod()
		BaseMod.GetEnergyByAD()
		err = player.HandleItem(Items, msg.ITEM_POP_LABEL_ApplyFriendSponsor.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResApplyFriend{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
		player.AddLog(int(req.Uid), friend.LOG_TYPE_FRIEND_SPONSOR, "", now)
		FriendMgrSend(&MsqMod.Msg{
			Type:  MsqMod.HANDLE_TYPE_FRIEND_SPONSOER,
			From:  int(player.M_DwUin),
			To:    Uid,
			SendT: now,
			End:   now + sevendays,
		})
		player.PushClientRes(BaseMod.BackData())
		player.TeLog("friend_invite_reward", map[string]interface{}{
			"item_list": Items,
		})
	}
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_APPLY,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: now,
		End:   now + sevendays,
	}
	FriendMgrSend(m)
	player.PushClientRes(&msg.ResApplyFriend{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	return nil
}

// 同意申请
func ReqAgreeFriend(player *Player, buf []byte) error {
	req := &msg.ReqAgreeFriend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Uid := int(req.Uid)
	FriendMod := player.PlayMod.getFriendMod()
	if !FriendMod.CheckApply(Uid) {
		player.SendErrClienRes(&msg.ResAgreeFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "apply uid not exist",
		})
		return fmt.Errorf("apply uid not exist")
	}
	// 好友人数到达上限（2000人）时，玩家将无法再同意好友申请
	if FriendMod.GetFriendLen() >= friendCfg.GetFriendLimitNum() {
		player.SendErrClienRes(&msg.ResAgreeFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "friend list full",
		})
		return fmt.Errorf("friend list full")
	}
	// 新好友才可以打招呼
	if !FriendMod.CheckAddBefore(Uid) {
		FriendMod.AddReplyInfo(Uid, friend.REPLY_TYPE_GREETING, "", GoUtil.Now()+oneday, nil)
	}
	FriendMod.AddFriend(Uid)
	player.PushClientRes(&msg.ResAgreeFriend{
		Code:   msg.RES_CODE_SUCCESS,
		Uid:    req.Uid,
		Player: G_GameLogicPtr.GetResSimplePlayerByUid(int(req.Uid)),
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": Uid,
		"add_type":  "接受申请",
	})
	player.AddLog(Uid, friend.LOG_TYPE_FRIEND_BECOME, "", GoUtil.Now())
	m := &MsqMod.Msg{
		Type:  MsqMod.HADNLE_TYPE_AGREE,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	player.FriendApplyBackData()
	player.FriendLogBackData()
	player.PlayMod.save()
	return nil
}

// 删除好友
func ReqDelFriend(player *Player, buf []byte) error {
	req := &msg.ReqDelFriend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Uid := int(req.Uid)
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_DEL,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.DelFriend(Uid)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResDelFriend{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	player.AddLog(Uid, friend.LOG_TYPE_FRIEND_DELETE, "", GoUtil.Now())
	player.FriendListBackData()
	player.TeLog("friend_delete", map[string]interface{}{
		"player_id": Uid,
	})
	return nil
}

// 拒绝申请
func ReqRefuseFriend(player *Player, buf []byte) error {
	req := &msg.ReqRefuseFriend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Uid := int(req.Uid)
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.RefuseApply(Uid)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResRefuseFriend{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	now := GoUtil.Now()
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_REFUSE,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: now,
		End:   now + 86400*7,
	}
	FriendMgrSend(m)
	player.FriendApplyBackData()
	return nil
}

// 向好友请求卡牌
func ReqCardGive(player *Player, buf []byte) error {
	req := &msg.ReqCardGive{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}

	CardMod := player.PlayMod.getCardMod()
	EndTime := GoUtil.Now() + 86400
	FriendMod := player.PlayMod.getFriendMod()

	err = CardMod.RequestCard()
	if err != nil {
		player.SendErrClienRes(&msg.ResCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	if len(req.Uid) == 0 { // 请求的UID列表不为空
		player.SendErrClienRes(&msg.ResCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  "uid empty",
		})
		return fmt.Errorf("uid empty")
	}
	CardId := int(req.CardId)
	if CardId == 0 { // 请求交换的卡牌ID不为空
		player.SendErrClienRes(&msg.ResCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card id empty",
		})
		return fmt.Errorf("card id empty")
	}
	Id := GoUtil.CreateCardId(int(player.M_DwUin), 0, int(req.CardId))
	for _, v := range req.Uid {
		Uid := int(v)
		if !FriendMod.CheckFriend(Uid) {
			player.SendErrClienRes(&msg.ResCardGive{
				Code: msg.RES_CODE_FAIL,
				Msg:  "not friend",
			})
			return fmt.Errorf("not friend")
		}
		CardInfo := &card.CardInfo{
			Id:        Id,
			AUid:      int(player.M_DwUin),
			BUid:      Uid,
			CardId:    CardId,
			StartTime: GoUtil.Now(),
			EndTime:   EndTime,
			Type:      card.TYPE_CARD_GIVE,
			Status:    card.STATUS_CARD_GIVE_1,
		}
		player.AddLog(Uid, friend.LOG_TYPE_CARD_GIVE, strconv.Itoa(CardId), GoUtil.Now())
		err = CardMod.AddRequestCard(CardInfo) // 添加请求卡牌
		if err != nil {
			player.SendErrClienRes(&msg.ResCardGive{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
		m := &MsqMod.Msg{
			Type:  MsqMod.HANDLE_TYPE_REQ_CARD,
			From:  int(player.M_DwUin),
			To:    Uid,
			SendT: GoUtil.Now(),
			End:   EndTime,
			Extra: *CardInfo,
		}
		FriendMgrSend(m)
	}
	player.PushClientRes(&msg.ResCardGive{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(CardMod.NotifyTimes())
	player.PlayMod.save()
	player.TeLog("card_require", map[string]interface{}{
		"card_id":          CardId,
		"player_id_target": req.Uid,
	})
	return nil
}

// 同意卡牌请求
func ReqAgreeCardGive(player *Player, buf []byte) error {
	req := &msg.ReqAgreeCardGive{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil {
		player.SendErrClienRes(&msg.ResAgreeCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card apply not exist",
		})
		return fmt.Errorf("card apply not exist")
	}
	CardMod := player.PlayMod.getCardMod()
	err = player.SubCard(CardInfo.CardId)
	if err != nil {
		player.SendErrClienRes(&msg.ResAgreeCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card not exist",
		})
		return fmt.Errorf("card  not exist")
	}
	CardInfo.Status = card.STATUS_CARD_GIVE_2
	CardInfo.EndTime = 0
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_AGREE_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.AUid,
		SendT: GoUtil.Now(),
		Extra: *CardInfo,
	}
	FriendMgrSend(m)
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(CardMod.NotifyTimes())
	player.PushClientRes(&msg.ResAgreeCardGive{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	player.AddLog(CardInfo.AUid, friend.LOG_TYPE_CARD_GIVE_ACCEPT, strconv.Itoa(CardInfo.CardId), GoUtil.Now())
	player.PlayMod.save()
	player.TeLog("card_require_reply", map[string]interface{}{
		"card_id":            CardInfo.CardId,
		"player_id_requirer": CardInfo.AUid,
	})
	return nil
}

// 拒绝卡牌请求
func ReqRefuseCardGive(player *Player, buf []byte) error {
	req := &msg.ReqRefuseCardGive{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil {
		player.SendErrClienRes(&msg.ResRefuseCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card apply not exist",
		})
		return fmt.Errorf("card apply not exist")
	}
	FriendMod.DelCardInfo(req.Id)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResRefuseCardGive{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	player.TeLog("ReqRefuseCardGive", map[string]interface{}{
		"card_id": CardInfo.CardId,
	})
	now := GoUtil.Now()
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_REG_CARD_REFUSE,
		From:  int(player.M_DwUin),
		To:    CardInfo.AUid,
		SendT: now,
		End:   now + 86400,
		Extra: *CardInfo,
	}
	FriendMgrSend(m)
	return nil
}

// 赠送卡牌
func ReqCardSend(player *Player, buf []byte) error {
	req := &msg.ReqCardSend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	CardId := int(req.CardId)
	CardInfo, err := CardMod.SendCard(int(player.M_DwUin), int(req.Uid), CardId)
	if err != nil {
		player.SendErrClienRes(&msg.ResCardSend{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	CardInfo.Emoji = int(req.Emoji)
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_SEND_CARD,
		From:  int(player.M_DwUin),
		To:    int(req.Uid),
		SendT: GoUtil.Now(),
		End:   GoUtil.Now() + 86400,
		Extra: *CardInfo,
	}
	player.AddLog(int(req.Uid), friend.LOG_TYPE_CARD_SEND, strconv.Itoa(CardId), GoUtil.Now())
	player.PushClientRes(&msg.ResCardSend{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("card_send", map[string]interface{}{
		"card_id":          CardId,
		"player_id_target": int(req.Uid),
	})
	player.PlayerDecoSetLog("emoji", int(req.Emoji), "card_send")
	player.PushClientRes(CardMod.NotifyCard())
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyTimes())
	FriendMgrSend(m)
	return nil
}

// 卡牌交换
func ReqCardExchange(player *Player, buf []byte) error {
	req := &msg.ReqCardExchange{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	CardId := int(req.CardId)
	CardInfo, err := CardMod.ExchangeCard(int(player.M_DwUin), int(req.Uid), CardId)
	if err != nil {
		player.SendErrClienRes(&msg.ResCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	CardInfo.Emoji = int(req.Emoji)
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_EX_CARD,
		From:  int(player.M_DwUin),
		To:    int(req.Uid),
		SendT: GoUtil.Now(),
		End:   GoUtil.Now() + 86400,
		Extra: *CardInfo,
	}
	player.AddLog(int(req.Uid), friend.LOG_TYPE_CARD_EX_SEND, fmt.Sprintf("%d", int(req.CardId)), GoUtil.Now())

	player.PushClientRes(&msg.ResCardExchange{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PushClientRes(CardMod.NotifyCard())
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyTimes())
	FriendMgrSend(m)
	player.TeLog("card_exchange", map[string]interface{}{
		"card_id":          CardId,
		"player_id_target": int(req.Uid),
		"is_gift":          0,
	})
	return nil
}

// 选择卡牌进行交换
func ReqSelectCardExchange(player *Player, buf []byte) error {
	req := &msg.ReqSelectCardExchange{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardId := int(req.CardId)
	CardMod := player.PlayMod.getCardMod()
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil || CardInfo.EndTime < GoUtil.Now() {
		player.SendErrClienRes(&msg.ResSelectCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  "exchange time out",
		})
		return fmt.Errorf("exchange time out")
	}
	if CardInfo.ExId != 0 {
		player.SendErrClienRes(&msg.ResSelectCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card already selected",
		})
		return fmt.Errorf("card already selected")
	}
	CardInfo.ExId = CardId
	CardInfo.Status = card.STATUS_CARD_EX_2
	if cardCfg.CheckCardIsGold(CardInfo.CardId) || cardCfg.CheckCardIsGold(CardInfo.ExId) {
		err := CardMod.SubGoldTimes()
		if err != nil {
			player.SendErrClienRes(&msg.ResSelectCardExchange{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}
	FriendMod.DelCardInfo(req.Id)
	player.AddLog(CardInfo.AUid, friend.LOG_TYPE_CARD_SELECT_SEND, fmt.Sprintf("%d", CardInfo.ExId), GoUtil.Now())
	err = player.SubCard(CardId)
	if err != nil {
		player.SendErrClienRes(&msg.ResSelectCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(&msg.ResSelectCardExchange{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	player.PushClientRes(CardMod.NotifyCard())
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_SELECT_EX_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.AUid,
		SendT: GoUtil.Now(),
		Extra: *CardInfo,
	}
	FriendMgrSend(m)
	player.TeLog("card_exchange_reply", map[string]interface{}{
		"palyer_id_giver": CardInfo.AUid,
		"card_id_giver":   CardInfo.CardId,
		"card_id_reply":   CardInfo.ExId,
	})
	return nil
}

// 同意卡牌交换
func ReqAgreeCardExchange(player *Player, buf []byte) error {
	req := &msg.ReqSelectCardExchange{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)

	if CardInfo == nil || CardInfo.EndTime < GoUtil.Now() {
		player.SendErrClienRes(&msg.ResAgreeCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  "exchange time out",
		})
		return fmt.Errorf("exchange time out")
	}
	player.AddCard(CardInfo.ExId)
	CardMod.DelExCard(CardInfo)
	FriendMod.DelCardInfo(req.Id)
	if cardCfg.CheckCardIsGold(CardInfo.CardId) || cardCfg.CheckCardIsGold(CardInfo.ExId) {
		err := CardMod.SubGoldTimes()
		if err != nil {
			player.SendErrClienRes(&msg.ResAgreeCardExchange{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}
	player.AddLog(CardInfo.BUid, friend.LOG_TYPE_CARD_EX_SUCCESS_1, fmt.Sprintf("%d_%d", CardInfo.CardId, CardInfo.ExId), GoUtil.Now())
	CardInfo.Status = card.STATUS_CARD_EX_3
	CardInfo.EndTime = 0
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(&msg.ResAgreeCardExchange{
		Code:  msg.RES_CODE_SUCCESS,
		Id:    req.Id,
		Emoji: int32(CardInfo.Emoji),
	})
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_ARGREE_EX_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.BUid,
		Extra: *CardInfo,
		SendT: GoUtil.Now(),
	}
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(CardMod.NotifyTimes())
	FriendMgrSend(m)
	player.TeLog("card_exchange_finish", map[string]interface{}{
		"player_id_giver": CardInfo.AUid,
		"card_id_giver":   CardInfo.CardId,
		"card_id_reply":   CardInfo.ExId,
		"player_id_reply": CardInfo.BUid,
	})
	return nil
}

// 拒绝选择卡牌
func ReqRefuseCardSelect(player *Player, buf []byte) error {
	req := &msg.ReqRefuseCardSelect{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil {
		player.SendErrClienRes(&msg.ResRefuseCardSelect{
			Code: msg.RES_CODE_FAIL,
			Msg:  "exchange time out",
		})
		return fmt.Errorf("exchange time out")
	}
	if CardInfo.Status != 1 {
		player.SendErrClienRes(&msg.ResRefuseCardSelect{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card is not select status",
		})
		return fmt.Errorf("card is not select status")
	}
	FriendMod.DelCardInfo(req.Id)
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_REFUSE_SELECT_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.AUid,
		Extra: *CardInfo,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	player.PushClientRes(&msg.ResRefuseCardSelect{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	player.PlayMod.save()
	return nil
}

// 拒绝进行交换
func ReqRefuseCardExchange(player *Player, buf []byte) error {
	req := &msg.ReqRefuseCardExchange{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil || CardInfo.EndTime < GoUtil.Now() {
		player.SendErrClienRes(&msg.ResRefuseCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  "exchange time out",
		})
		return fmt.Errorf("exchange time out")
	}
	if CardInfo.Status != 2 {
		player.SendErrClienRes(&msg.ResRefuseCardExchange{
			Code: msg.RES_CODE_FAIL,
			Msg:  "card is not status 2",
		})
		return fmt.Errorf("card is not status 2")
	}
	FriendMod.DelCardInfo(req.Id)
	player.AddCard(CardInfo.CardId)
	CardMod.DelExCard(CardInfo)
	CardMod.AddExTimes(CardInfo)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResRefuseCardExchange{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_REFUSE_EX_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.BUid,
		Extra: *CardInfo,
		SendT: GoUtil.Now(),
	}
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(CardMod.NotifyTimes())
	FriendMgrSend(m)
	return nil
}

// 领取好友卡牌
func ReqGetFriendCard(player *Player, buf []byte) error {
	req := &msg.ReqRefuseCardExchange{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	FriendMod := player.PlayMod.getFriendMod()
	CardInfo := FriendMod.GetCardInfo(req.Id)
	if CardInfo == nil || CardInfo.EndTime != 0 {
		player.SendErrClienRes(&msg.ResGetFriendCard{
			Code: msg.RES_CODE_FAIL,
			Msg:  "exchange time out",
		})
		return fmt.Errorf("exchange time out")
	}
	FriendMod.DelCardInfo(req.Id)
	player.AddCard(CardInfo.CardId)
	uid := CardInfo.AUid
	if CardInfo.Type == card.TYPE_CARD_GIVE {
		uid = CardInfo.BUid
	}
	player.AddLog(uid, friend.LOG_TYPE_CARD_SEND_ACCEPT, fmt.Sprintf("%d", CardInfo.CardId), GoUtil.Now())
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGetFriendCard{
		Code:   msg.RES_CODE_SUCCESS,
		Id:     req.Id,
		CardId: int32(CardInfo.CardId),
		Emoji:  int32(CardInfo.Emoji),
	})
	player.PushClientRes(CardMod.NotifyCard())
	return nil
}

// 请求玩家榜单
func ReqRank(player *Player, buf []byte) error {
	req := &msg.ReqRank{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	m := &MsqMod.Msg{
		From:  int(player.M_DwUin),
		Type:  MsqMod.HANDLE_TYPE_RANK_INFO,
		Extra: RankMsg{RankType: int(req.Type)},
		SendT: GoUtil.Now(),
	}
	RankInfo := G_GameLogicPtr.RankMgrCall(m).(*RankInfo)
	res := make(map[int32]*msg.ResPlayerSimple)
	for k, rank := range RankInfo.List {
		res[int32(k+1)] = G_GameLogicPtr.GetResSimplePlayerByUid(rank.Uid)
	}

	player.PushClientRes(&msg.ResRank{
		Type:     req.Type,
		RankList: res,
		MyRank:   int32(RankInfo.MyRank),
		MyScore:  float32(RankInfo.MyScore),
	})
	return nil
}

// 请求竞标赛榜单
func ReqChampshipRank(player *Player, buf []byte) error {
	req := &msg.ReqRank{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	res, err := SendMsgToCenterSync(&MsqMod.Msg{
		From:       int(player.M_DwUin),
		HandleType: MsqMod.HANDLE_MOD_CHAMPSHIP_RANK_LIST,
	})
	if err != nil {
		return err
	}
	if res == nil {
		player.PushClientRes(&msg.ResChampshipRank{})
		return nil
	}
	m := res.Extra.(*msg.ResChampshipRank)
	player.PushClientRes(m)
	return nil
}

func ReqChampshipPreRank(player *Player, buf []byte) error {
	req := &msg.ReqRank{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	res, err := SendMsgToCenterSync(&MsqMod.Msg{
		From:       int(player.M_DwUin),
		HandleType: MsqMod.HANDLE_MOD_CHAMPSHIP_PRE_RANK,
	})
	if err != nil {
		return err
	}
	if res == nil {
		player.PushClientRes(&msg.ResChampshipPreRank{})
		return nil
	}
	m := res.Extra.(*msg.ResChampshipPreRank)
	player.PushClientRes(m)
	return nil
}

// 请求邮件列表
func ReqMailList(player *Player, buf []byte) error {
	MailMod := player.PlayMod.getMailMod()
	player.PushClientRes(MailMod.BackData())
	return nil
}

// 读邮件
func ReqReadMail(player *Player, buf []byte) error {
	req := &msg.ReqReadMail{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	MailMod := player.PlayMod.getMailMod()
	err = MailMod.Read(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResReadMail{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(MailMod.NotifyMail(int(req.Id)))
	player.PushClientRes(&msg.ResReadMail{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

// 领取邮件奖励
func ReqGetMailReward(player *Player, buf []byte) error {
	req := &msg.ReqGetMailReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	MailMod := player.PlayMod.getMailMod()
	itemList, err := MailMod.Reward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResGetMailReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_MailReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetMailReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("mail_reward", map[string]interface{}{
		"mail_id":   req.Id,
		"item_list": itemList,
	})

	player.PlayMod.save()
	player.PushClientRes(MailMod.NotifyMail(int(req.Id)))
	player.PushClientRes(&msg.ResGetMailReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

// 删除邮件
func ReqDeleteMail(player *Player, buf []byte) error {
	req := &msg.ReqDeleteMail{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	MailMod := player.PlayMod.getMailMod()
	err = MailMod.Del(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResDeleteMail{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(MailMod.NotifyMail(int(req.Id)))
	player.PushClientRes(&msg.ResDeleteMail{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

// 领取商店免费奖励
func ReqFreeShop(player *Player, buf []byte) error {
	ChargeMod := player.PlayMod.getChargeMod()
	Item, err := ChargeMod.FireFreeShop()
	if err != nil {
		player.SendErrClienRes(&msg.ResFreeShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Item, msg.ITEM_POP_LABEL_FreeShop.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResFreeShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.ChargeBackData()
	player.PushClientRes(&msg.ResFreeShop{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("gift_free", map[string]interface{}{
		"gift_type": "store",
		"item_id":   Item[0].Id,
		"num":       Item[0].Num,
	})
	return nil
}

// 购买棋子商店物品
func ReqBuyChessShop(player *Player, buf []byte) error {
	req := &msg.ReqBuyChessShop{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChargeMod := player.PlayMod.getChargeMod()
	LostItem, Item, _, err := ChargeMod.BuyChess(int(req.Id), player.IsWeeklyDiscount())
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleLoseItem(LostItem, "ChessShop")
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop2{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Item, msg.ITEM_POP_LABEL_ChessShop.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("store_buy", map[string]interface{}{
		"item_id":    int(req.Id),
		"change_num": 1,
		"cost_type":  "diamond",
		"cost_num":   -LostItem[0].Num,
	})
	player.PlayMod.save()
	player.ChargeBackData()
	player.PushClientRes(&msg.ResBuyChessShop{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("buy_product_diamond", map[string]interface{}{
		"diamond_cost": -LostItem[0].Num,
		"product_id":   int(req.Id),
		"product_name": mergeDataCfg.GetNameById(int(req.Id)),
	})
	return nil
}

// 购买棋子商店物品
func ReqBuyChessShop2(player *Player, buf []byte) error {
	req := &msg.ReqBuyChessShop2{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChargeMod := player.PlayMod.getChargeMod()
	LostItem, _, ChessId, err := ChargeMod.BuyChess(int(req.Id), player.IsWeeklyDiscount())
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop2{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleLoseItem(LostItem, "ChessShop")
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop2{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	err = ChessMod.AddChess(ChessId)
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop2{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResBuyChessShop2{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("store_buy", map[string]interface{}{
		"item_id":    int(req.Id),
		"change_num": 1,
		"cost_type":  "diamond",
		"cost_num":   LostItem[0].Num,
	})
	player.PlayMod.save()
	player.ChargeBackData()
	player.PushClientRes(&msg.ResBuyChessShop2{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("buy_product_diamond", map[string]interface{}{
		"diamond_cost": LostItem[0].Num,
		"product_id":   int(req.Id),
		"product_name": mergeDataCfg.GetNameById(int(req.Id)),
	})
	return nil
}

// 刷新棋子商店
func ReqRefreshChessShop(player *Player, buf []byte) error {
	ChargeMod := player.PlayMod.getChargeMod()
	ChessMod := player.PlayMod.getChessMod()
	ChargeMod.InitChessShop(ChessMod.GetEmitList())
	Cost := []*item.Item{item.NewItem(item.ITEM_DIAMOND_ID, -20)}
	err := player.HandleItem(Cost, msg.ITEM_POP_LABEL_RefreshChessShop.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResRefreshChessShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.ChargeBackData()
	player.TeLog("refresh_store_diamond", map[string]interface{}{
		"diamond_cost": 20,
	})
	return nil
}

// 领取无尽礼包免费奖励
func ReqEndlessReward(player *Player, buf []byte) error {
	req := &msg.ReqEndlessReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	EndlessMod := player.PlayMod.getEndlessMod()
	BaseMod := player.PlayMod.getBaseMod()
	itemList, err := EndlessMod.GetReward(BaseMod.GetLevel())
	if err != nil {
		player.SendErrClienRes(&msg.ResEndlessReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_EndlessReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResEndlessReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(EndlessMod.BackData())
	player.PushClientRes(&msg.ResEndlessReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	Ids := make([]int, 0)
	Nums := make([]int, 0)
	for _, v := range itemList {
		Ids = append(Ids, v.Id)
		Nums = append(Nums, v.Num)
	}
	player.TeLog("gift_ad", map[string]interface{}{
		"gift_type":   "endless",
		"item_id":     Ids,
		"item_number": Nums,
	})
	return nil
}

// 请求无尽礼包数据
func ReqEndless(player *Player, buf []byte) error {
	EndlessMod := player.PlayMod.getEndlessMod()
	player.PushClientRes(EndlessMod.BackData())
	return nil
}

// 请求存钱罐奖励
func ReqPiggyBankReward(player *Player, buf []byte) error {
	PiggyBankMod := player.PlayMod.getPiggyBankMod()
	itemList := PiggyBankMod.Fire(0)
	err := player.HandleItem(itemList, msg.ITEM_POP_LABEL_PiggyBankReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPiggyBankReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(PiggyBankMod.BackData())
	player.PushClientRes(&msg.ResPiggyBankReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("piggy_bank_open", map[string]interface{}{
		"piggy_bank_type": piggyBank.PIGGY_BANK_TYPE_AD,
		"item_list":       itemList,
	})
	return nil
}

// 生成订单号
func ReqCreateOrderSn(player *Player, buf []byte) error {
	req := &msg.ReqCreateOrderSn{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	OrderSn, err := player.CreateOrderSn(req)
	if err != nil {
		return err
	}
	player.PushClientRes(&msg.ResCreateOrderSn{
		OrderSn: OrderSn,
	})
	return nil
}

// 订单发货
func ReqShippingOrder(player *Player, buf []byte) error {
	return nil
}

// 锦标赛
func ReqChampshipReward(player *Player, buf []byte) error {
	ChampshipMod := player.PlayMod.getChampshipMod()
	itemList := ChampshipMod.GetReward()
	err := player.HandleItem(itemList, msg.ITEM_POP_LABEL_ChampshipReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResChampshipReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.BackChampship()
	player.TriggerSeed()
	player.PushClientRes(&msg.ResChampshipReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	MaxId := champshipCfg.GetMaxRewardId()
	if MaxId == ChampshipMod.Reward {
		FriendMod := player.PlayMod.getFriendMod()
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_GET_CHAMPIONSHIP_PRIZE, "")
		player.UpdateUserInfo()
	}
	player.TeLog("championship_reward", map[string]interface{}{
		"season_id":         GoUtil.ZeroTimestamp(),
		"champship_step_id": ChampshipMod.Reward,
		"reward_type":       "step",
		"item_list":         itemList,
		"champship_score":   ChampshipMod.Score,
	})
	return nil
}

func ReqLimitEvent(player *Player, buf []byte) error {
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	player.PushClientRes(LimitedTimeEventMod.BackData())
	return nil
}

func ReqKv(player *Player, buf []byte) error {
	req := &msg.ReqKv{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	KvMod := player.PlayMod.getKvMod()
	KvMod.SetVar(int(req.Key), req.Value)
	log.Debug("player %d set kv key %d value %s", player.M_DwUin, req.Key, req.Value)
	return nil
}

func ReqFriendRecommend(player *Player, buf []byte) error {
	FriendMod := player.PlayMod.getFriendMod()
	RecommendList := make([]*msg.ResPlayerSimple, 0)
	FriendNum := FriendMod.GetFriendNum()
	var n int
	if FriendNum < 10 {
		n = 3
	} else {
		Active := 0
		for k := range FriendMod.GetFriendList() {
			PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(k)
			if PlayerSimpleData == nil {
				continue
			}
			if PlayerSimpleData.Login > GoUtil.Now()-86400 {
				Active++
			}
		}
		n = max(0, 3-(Active/10))
	}
	PlayerList := GetRecommendPlayer(player, n)

	for _, v := range PlayerList {
		PlayerSimpleData := G_GameLogicPtr.GetResSimplePlayerByUid(v)
		RecommendList = append(RecommendList, PlayerSimpleData)
	}
	player.PushClientRes(&msg.ResFriendRecommend{
		List: RecommendList,
	})
	player.PlayMod.save()
	return nil
}

func ReqFriendList(player *Player, buf []byte) error {
	player.FriendListBackData()
	return nil
}

func ReqFriendApply(player *Player, buf []byte) error {
	player.FriendApplyBackData()
	return nil
}

func ReqFriendCardMsg(player *Player, buf []byte) error {
	player.FriendCardBackData()
	return nil
}

func ReqFriendTimeLine(player *Player, buf []byte) error {
	player.FriendLogBackData()
	return nil
}

func ReqFriendTLUpvote(player *Player, buf []byte) error {
	req := &msg.ReqFriendTLUpvote{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	Items, FUid, err := FriendMod.Upvote(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResFriendTLUpvote{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_TLUpvote.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResFriendTLUpvote{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	now := GoUtil.Now()
	// 添加时间线
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_HANDBOOK_UPVOTE,
		From:  int(player.M_DwUin),
		To:    int(FUid),
		SendT: now,
		End:   now + sevendays,
	}
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_VISIT_UPVOTE, "")
	player.UpdateUserInfo()
	FriendMgrSend(m)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResFriendTLUpvote{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	player.TeLog("friend_upvote", map[string]interface{}{
		"player_id": int(req.Id),
	})
	return nil
}

func ReqFriendTReward(player *Player, buf []byte) error {
	req := &msg.ReqFriendTReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	Items, err := FriendMod.GetReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResFriendTReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_FriendTReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResFriendTReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResFriendTReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

func ReqChampshipRankReward(player *Player, buf []byte) error {
	_, myPreRank := player.GetChampshipRank()
	ChampshipMod := player.PlayMod.getChampshipMod()
	itemList, err := ChampshipMod.GetRankReward(myPreRank)
	if err != nil {
		player.SendErrClienRes(&msg.ResChampshipRankReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_ChampshipRankReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResChampshipRankReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if myPreRank <= 5 {
		FriendMod := player.PlayMod.getFriendMod()
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_GET_CHAMPIONSHIP_RANK, GoUtil.String(myPreRank))
		player.UpdateUserInfo()
	}
	player.PlayMod.save()
	player.BackChampship()
	player.PushClientRes(&msg.ResChampshipRankReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("championship_reward", map[string]interface{}{
		"season_id":         GoUtil.ZeroTimestamp() - 86400,
		"champship_step_id": myPreRank,
		"reward_type":       "rank",
		"item_list":         itemList,
		"champship_score":   ChampshipMod.Score,
	})
	return nil
}

func ReqSetName(player *Player, buf []byte) error {
	req := &msg.ReqSetName{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetNickName(req.Name)
	player.PushClientRes(&msg.ResSetName{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	player.PlayMod.save()
	player.BackUserInfo()
	player.UpdateUserInfo()
	player.TeLog("nickname_set", map[string]interface{}{
		"nickname_name": req.Name,
	})
	return nil
}

func ReqSetPetName(player *Player, buf []byte) error {
	req := &msg.ReqSetPetName{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetPetName(req.Name)
	player.PushClientRes(&msg.ResSetPetName{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	player.PlayMod.save()
	player.BackUserInfo()
	PlayroomMod := player.PlayMod.getPlayroomMod()
	PlayroomMod.UnLock(BaseMod.GetLevel())
	player.LimitedTimePlayroomTrigger()
	player.UpdateUserInfo()
	player.TeLog("petname_set", map[string]interface{}{
		"petname": req.Name,
	})
	return nil
}

func ReqUserInfo(player *Player, buf []byte) error {
	player.BackUserInfo()
	return nil
}

func ReqMasterCard(player *Player, buf []byte) error {
	req := &msg.ReqMasterCard{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	err = CardMod.MasterCardEx(int(req.Id), int(req.CardId))
	if err != nil {
		player.SendErrClienRes(&msg.ResMasterCard{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResMasterCard{
		Code:     msg.RES_CODE_SUCCESS,
		MasterId: req.Id,
		CardId:   req.CardId,
	})
	player.PushClientRes(CardMod.NotifyCard())
	player.TeLog("master_card", map[string]interface{}{
		"wild_card_type": req.Id,
		"card_id":        req.CardId,
	})
	return nil
}

func ReqSetFacebookUrl(player *Player, buf []byte) error {
	req := &msg.ReqSetFacebookUrl{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetFacebookUrl(req.Url)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResSetFacebookUrl{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqInviteFriendData(player *Player, buf []byte) error {
	InviteMod := player.PlayMod.getInviteMod()
	player.PushClientRes(InviteMod.BackData())
	return nil
}

func ReqSelfInvited(player *Player, buf []byte) error {
	req := &msg.ReqSelfInvited{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	now := GoUtil.Now()
	FriendMgrSend(&MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_INVITE_FRIEND,
		From:  int(player.M_DwUin),
		To:    int(req.InviterId),
		SendT: now,
		End:   now + 86400,
	})
	player.PushClientRes(&msg.ResSelfInvited{
		ResultCode: 1,
	})
	return nil
}

func ReqGetInviteReward(player *Player, buf []byte) error {
	req := &msg.ReqGetInviteReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	InviteMod := player.PlayMod.getInviteMod()
	Items, err := InviteMod.GetReward(int(req.GetIndex))
	if err != nil {
		log.Debug("ReqGetInviteReward err :%v", err)
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_invite.String())
	if err != nil {
		log.Debug("ReqGetInviteReward err :%v", err)
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGetInviteReward{
		ResultCode: 1,
	})
	player.PushClientRes(InviteMod.BackData())

	Str := ""
	for k := range InviteMod.InviteList {
		Str += strconv.Itoa(k) + ","
	}
	player.TeLog("register_invite_reward", map[string]interface{}{
		"register_type":  "facebook",
		"item_id":        Items,
		"register_score": InviteMod.GetIndex,
		"player_id":      Str,
	})
	return nil
}

func ReqAutoAddInviteFriend(player *Player, buf []byte) error {
	req := &msg.ReqAutoAddInviteFriend{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	Uid := int(req.Id)
	if Uid == 0 && Uid == int(player.M_DwUin) {
		player.PushClientRes(&msg.ResAutoAddInviteFriend{
			ResultCode: int32(msg.RES_CODE_FAIL),
		})
		return fmt.Errorf("uid wrong %d", Uid)
	}
	FriendMod.AddFriend(Uid)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResAutoAddInviteFriend{
		ResultCode: 1,
	})
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Uid,
		Type:  MsqMod.HANDLE_TYPE_INVITE_ADD_FRIEND,
		SendT: GoUtil.Now(),
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": req.Id,
		"add_type":  "邀请注册",
	})
	return nil
}

func ReqAutoAddInviteFriend2(player *Player, buf []byte) error {
	req := &msg.ReqAutoAddInviteFriend2{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	Uid, err := GetUidByFaceBook(req.Id)
	if Uid == 0 || Uid == int(player.M_DwUin) {
		player.PushClientRes(&msg.ResAutoAddInviteFriend2{
			ResultCode: 0,
		})
		log.Debug("ReqAutoAddInviteFriend2 err :%v", err)
		return err
	}
	if err != nil {
		player.SendErrClienRes(&msg.ResAutoAddInviteFriend2{
			ResultCode: 0,
		})
		log.Debug("ReqAutoAddInviteFriend2 err :%v", err)
		return err
	}
	FriendMod.AddFriend(Uid)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResAutoAddInviteFriend2{
		ResultCode: 1,
	})
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Uid,
		Type:  MsqMod.HANDLE_TYPE_INVITE_ADD_FRIEND,
		SendT: GoUtil.Now(),
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": req.Id,
		"add_type":  "邀请注册",
	})
	return nil
}

func ReqSelectLimitEvent(player *Player, buf []byte) error {
	req := &msg.ReqSelectLimitEvent{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	BaseMod := player.PlayMod.getBaseMod()
	Items, EventType, OrderNum, RewardList, err := LimitedTimeEventMod.SelectProgressReward(int(req.Id), BaseMod.GetLevel(), BaseMod.GetEnergy())
	if err != nil {
		player.SendErrClienRes(&msg.ResSelectLimitEvent{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_SelectLimitEvent.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResSelectLimitEvent{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("time_limited_slot", map[string]interface{}{
		"slot_order_number": OrderNum,
		"slot_reward_list":  RewardList,
		"slot_reward":       limitedTimeEventCfg.GetEventName(EventType),
	})
	player.PlayMod.save()
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(LimitedTimeEventMod.ProgressBackData())
	player.PushClientRes(&msg.ResSelectLimitEvent{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 请求挖矿基础数据
func ReqMining(player *Player, buf []byte) error {
	player.MiningBackData()
	return nil
}

// 请求挖矿
func ReqMiningTake(player *Player, buf []byte) error {
	req := &msg.ReqMiningTake{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	MiningMod := player.PlayMod.getMiningMod()
	ActivityInfo := player.GetActivityInfo(activity.ACT_TYPE_MINING)
	Status := player.GetActivityStatus(activity.ACT_TYPE_MINING)
	if Status != ACT_STATUS_START {
		player.SendErrClienRes(&msg.ResMiningTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not start",
		})
		return fmt.Errorf("activity not start")
	}
	LoseItem := miningCfg.GetLoseItem(ActivityInfo.Id)
	err = player.HandleLoseItem(LoseItem, "MiningLose")
	if err != nil {
		player.SendErrClienRes(&msg.ResMiningTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	itemList, err := MiningMod.Take(req.Map, int(req.Gem))
	if err != nil {
		player.SendErrClienRes(&msg.ResMiningTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_MiningTake.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResMiningTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("ReqMiningTake", map[string]interface{}{})
	player.PlayMod.save()
	player.NotifyRed(activity.ACT_TYPE_MINING)
	player.MiningBackData()
	return nil
}

// 领取关卡奖励
func ReqMiningReward(player *Player, buf []byte) error {
	MiningMod := player.PlayMod.getMiningMod()
	Status := player.GetActivityStatus(activity.ACT_TYPE_MINING)
	if Status != ACT_STATUS_START {
		player.SendErrClienRes(&msg.ResMiningReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not start",
		})
		return fmt.Errorf("activity not start")
	}
	itemList, Mining, err := MiningMod.GetReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResMiningReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_MiningReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResMiningReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("mining_finish", map[string]interface{}{
		"expend_token_num": Mining,
		"level_ID":         MiningMod.Pass - 1,
	})
	player.PushClientRes(&msg.ResMiningReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.PlayMod.save()
	player.MiningBackData()
	return nil
}

// 请求猜颜色基础数据
func ReqGuessColor(player *Player, buf []byte) error {
	req := &msg.ReqGuessColor{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	GuessColorMod := player.PlayMod.getGuessColorMod()
	Items := GuessColorMod.FirstIn()
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_GuessColor.String())
	if err != nil {
		return err
	}
	player.PlayMod.save()
	player.GuessColorBackData()
	return nil
}

// 请求猜颜色
func ReqGuessColorTake(player *Player, buf []byte) error {
	req := &msg.ReqGuessColorTake{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Status := player.GetActivityStatus(activity.ACT_TYPE_GUESS_COLOR)
	if Status != ACT_STATUS_START {
		player.SendErrClienRes(&msg.ResGuessColorTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not start",
		})
		return fmt.Errorf("activity not start")
	}
	GuessColorMod := player.PlayMod.getGuessColorMod()
	LoseItem := GuessColorMod.GetLoseItem()
	err = player.HandleLoseItem(LoseItem, "GuessColorLose")
	if err != nil {
		player.SendErrClienRes(&msg.ResGuessColorTake{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	var M map[int32]int32
	if req.Map != nil {
		M = req.Map.Map
	}
	GuessColorMod.Take(M, req.OMap)
	player.TeLog("ReqGuessColorTake", map[string]interface{}{
		"map":   req.Map.Map,
		"o_map": req.OMap,
	})
	player.PlayMod.save()
	player.GuessColorBackData()
	player.NotifyRed(activity.ACT_TYPE_GUESS_COLOR)
	return nil
}

// 请求猜颜色奖励
func ReqGuessColorReward(player *Player, buf []byte) error {
	req := &msg.ReqGuessColorReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Status := player.GetActivityStatus(activity.ACT_TYPE_GUESS_COLOR)
	if Status != ACT_STATUS_START {
		player.SendErrClienRes(&msg.ResGuessColorReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not start",
		})
		return fmt.Errorf("activity not start")
	}
	GuessColorMod := player.PlayMod.getGuessColorMod()
	itemList, err := GuessColorMod.GetReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResGuessColorReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(itemList, msg.ITEM_POP_LABEL_GuessColorReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGuessColorReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("guess_color_finish", map[string]interface{}{
		"item_list": itemList,
	})
	player.PlayMod.save()
	player.GuessColorBackData()
	return nil
}

// 请求竞赛基础数据
func ReqRace(player *Player, buf []byte) error {
	player.RaceBackData()
	return nil
}

// 请求竞赛开始
func ReqRaceStart(player *Player, buf []byte) error {
	req := &msg.ReqRaceStart{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	RaceMod := player.PlayMod.getRaceMod()
	err = RaceMod.StartGame()
	if err != nil {
		player.SendErrClienRes(&msg.ResRaceStart{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.RaceBackData()
	player.PushClientRes(&msg.ResRaceStart{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 领取竞赛奖励
func ReqRaceReward(player *Player, buf []byte) error {
	req := &msg.ResRaceReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	RaceMod := player.PlayMod.getRaceMod()
	Items, err := RaceMod.GetReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResRaceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_RaceReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResRaceReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.RaceBackData()
	player.TeLog("ReqRaceReward", map[string]interface{}{
		"item_list": Items,
	})
	player.PushClientRes(&msg.ResRaceReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 请求playroom基础数据
func ReqPlayroom(player *Player, buf []byte) error {
	player.PlayroomBackData()
	return nil
}

// 请求playroom拜访信息
func ReqPlayroomInfo(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomInfo{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Targer := int(req.Uid)
	if req.Uid == 0 {
		Targer = PlayroomMod.GetTarget()
	}
	if req.Uid == -1 {
		Targer = player.GetVisitorPlayer()
	}
	if Targer == 0 {
		player.PlayroomVisit(Targer)
		return nil
	}
	PlayerData := G_GameLogicPtr.GetSimplePlayerByUid(Targer)
	if PlayerData == nil {
		return fmt.Errorf("ReqPlayroomInfo player %d not found", Targer)
	}
	PlayroomMod.SetTarget(Targer)
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_VISITROOM})
	if PlayerData.Loginout > 0 && PlayerData.Loginout < GoUtil.Now()-int64(playroomCfg.GetGameOutline()) {
		GameType := PlayroomMod.RandGameType()
		PlayroomMod.SetGameId(GameType)
		Star := min(500, max(PlayerData.Star/10, 10))
		ChessMod := player.PlayMod.getChessMod()
		EmitList := ChessMod.GetOrderEmit()
		ColorList := make([]string, 0)
		for _, v := range EmitList {
			Color := mergeDataCfg.GetEmitProduceChessType(v)
			ColorList = append(ColorList, Color...)
		}
		RandList := make([]int, 0)
		for _, v := range PlayerData.Chess {
			Color := mergeDataCfg.GetColorById(v)
			Lv := mergeDataCfg.GetLvById(v)
			if GoUtil.InStringArray(Color, ColorList) && Lv <= 8 && Lv != 0 {
				RandList = append(RandList, v)
			}
		}
		RandList = GoUtil.UniqueInts(RandList)
		if len(RandList) == 0 {
			PlayroomMod.SetGameReward3(Star)
		} else if len(RandList) == 1 {
			PlayroomMod.SetGameReward2(RandList[0], Star)
		} else {
			ChessList := GoUtil.RandSliceNum(RandList, 2)
			PlayroomMod.SetGameReward(ChessList[0], ChessList[1], Star)
		}
	} else {
		PlayroomMod.SetGameId(playroom.GAME_TYPE_FILP)
		PlayroomMod.SetGameRewardFlip(int(float64(PlayerData.Star)*0.03), int(float64(PlayerData.Star)*0.05), int(float64(PlayerData.Star)*0.1))
	}
	player.PlayMod.save()
	player.PlayroomVisit(Targer)
	return nil
}

// 请求playroom交互
func ReqPlayroomInteract(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomInteract{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, PType, err := PlayroomMod.Interact(int(req.Id), int(req.Type))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomInteract{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleLoseItem(Items, "PlayroomInteract")
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomInteract{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if GoUtil.InArray(int(req.Id), playroomCfg.GetInteractIdBath()) {
		player.SetPlayroomKiss(0, int(player.M_DwUin))
	}
	PExp := playroomCfg.GetInteractPExp(int(req.Id))
	_, err = player.GetPlayerBaseMod().AddExp(player, 0, PExp)
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomInteract{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
	}
	BaseMod := player.PlayMod.getBaseMod()
	PlayroomMod.AddInteractNum(BaseMod.GetLevel())
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_INTERACT, A: []interface{}{int(req.Id)}})
	player.PlayMod.save()
	player.LimitedTimePlayroomTrigger_(PType)
	player.NotifyPlayroomKiss()
	player.TeLog("playroom_interact", map[string]interface{}{
		"interact_id":   int(req.Id),
		"interact_type": int(req.Type),
	})
	player.PushClientRes(&msg.ResPlayroomInteract{
		Code:        msg.RES_CODE_SUCCESS,
		InteractNum: int32(PlayroomMod.InteractNum),
	})
	return nil
}

// 请求playroom设置房间
func ReqPlayroomSetRoom(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomSetRoom{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	_, err = PlayroomMod.SetRoom(GoUtil.MapInt32ToInt(req.Playroom))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomSetRoom{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_ROOMDEC})
	player.PlayMod.save()
	//player.RoomDecoSetLog(Diff)
	player.PushClientRes(&msg.ResPlayroomSetRoom{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 玩游戏
func ReqPlayroomGame(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomGame{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Target := PlayroomMod.GetTarget()
	if Target == 0 {
		player.SendErrClienRes(&msg.ResPlayroomGame{
			Code: msg.RES_CODE_FAIL,
			Msg:  "no target",
		})
		return fmt.Errorf("no target")
	}
	Items := player.GetPlayroomGameReward(int(req.Type), 0)
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomGame.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomGame{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	now := GoUtil.Now()
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Target,
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_GAME,
		SendT: now,
		End:   now + sevendays,
		Extra: GameResult{
			T:     PlayroomMod.GetGameId(),
			Emoji: int(req.EmojiId),
		},
	})
	ItemsMsg := make(map[int32]*msg.ItemInfo, 0)
	for k, v := range PlayroomMod.GameReward {
		ItemsMsg[int32(k)] = &msg.ItemInfo{
			Id:  int32(v.Id),
			Num: int32(v.Num),
		}
	}
	player.TeLog("finish_mini_game", map[string]interface{}{
		"mini_game_type": PlayroomMod.GetGameId(),
		"is_chip":        false,
		"item_list":      Items,
	})
	PlayroomMod.ResetGame()
	player.PlayroomBackData()
	player.PlayerDecoSetLog("emoji", int(req.EmojiId), "playroom_game")
	player.PushClientRes(&msg.ResPlayroomGame{
		Code:  msg.RES_CODE_SUCCESS,
		Type:  req.Type,
		Items: ItemsMsg,
	})
	player.PlayMod.save()
	return nil
}

// 选择奖励
func ReqPlayroomSelectReward(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomSelectReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items := player.GetPlayroomGameReward(playroom.GAME_RESULT_HIGH, int(req.Id))
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomGame.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomSelectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	GameId := PlayroomMod.GetGameId()
	Target := PlayroomMod.GetTarget()
	if GameId == 0 || Target == 0 {
		player.SendErrClienRes(&msg.ResPlayroomSelectReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "no game or target",
		})
		return fmt.Errorf("no game or target")
	}
	now := GoUtil.Now()
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Target,
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_LOSE,
		SendT: now,
		End:   now + sevendays,
		Item:  Items,
		Extra: GameResult{
			T:     PlayroomMod.GetGameId(),
			Emoji: int(req.EmojiId),
		},
	})
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_PET_THIEF) {
		player.GetPetThiefReward(Target)
	}
	player.AddPlayroomChip(int(req.Id))
	player.TeLog("finish_mini_game", map[string]interface{}{
		"mini_game_type": PlayroomMod.GetGameId(),
		"is_chip":        true,
		"item_list":      Items,
	})
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_PLAYROOM_GAME})
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_GET_VISIT_GAME_PRIZE_1, "")
	player.UpdateUserInfo()
	PlayroomMod.ResetGame()
	player.PlayerDecoSetLog("emoji", int(req.EmojiId), "playroom_select_reward")
	player.PlayroomBackData()
	go NotifyPetroomGame(Target)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomSelectReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 处理偷取物品
func ReqPlayroomLose(player *Player, buf []byte) error {
	PlayroomMod := player.PlayMod.getPlayroomMod()
	PlayroomMod.ResetLose()
	player.PushClientRes(PlayroomMod.NotifyLose())
	return nil
}

// 宠物开始工作
func ReqPlayroomWork(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomWork{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, err := PlayroomMod.Work()
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomWork{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	// 开始工作 设置定时器
	if Items != nil {
		LimitedTimePlayroomWorkTrigger(player)
		player.UpdateUserInfo()
	}
	err = player.HandleLoseItem(Items, "playroomWork")
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomWork{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_PETWORK})
	player.TeLog("pet_assist_active", map[string]interface{}{
		"assist_time_left": PlayroomMod.Endtime - GoUtil.Now(),
	})
	player.PlayMod.save()
	player.PushClientRes(PlayroomMod.NotifyWork())
	// player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomWork{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 宠物休息
func ReqPlayroomRest(player *Player, buf []byte) error {
	PlayroomMod := player.PlayMod.getPlayroomMod()
	PlayroomMod.Rest()
	player.PlayMod.save()
	player.TeLog("playroom_rest", map[string]interface{}{})
	player.PushClientRes(PlayroomMod.NotifyWork())
	player.TeLog("pet_assist_callback", map[string]interface{}{})
	return nil
}

// 抽取转盘
func ReqPlayroomDraw(player *Player, buf []byte) error {
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Id, Items, err := PlayroomMod.Draw()
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomDraw{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = Id
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomDraw.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomDraw{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	// player.PlayroomBackData()
	player.PushClientRes(PlayroomMod.NotifyMood())
	player.TeLog("playroom_draw", map[string]interface{}{
		"draw_id":   Id,
		"item_list": Items,
	})
	player.PushClientRes(&msg.ResPlayroomDraw{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(Id),
	})
	return nil
}

// 清理碎片
func ReqPlayroomChip(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomChip{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, ChipNum, err := PlayroomMod.RemoveChip(req.Uid)
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomChip{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleLoseItem(Items, msg.ITEM_POP_LABEL_PlayroomChip.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomChip{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("remove_chip", map[string]interface{}{
		"remove_chip_number": ChipNum,
	})
	for range ChipNum {
		player.SubPlayroomChip(int(player.M_DwUin))
	}
	player.PlayMod.save()
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomChip{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 翻牌游戏
func ReqPlayroomFlip(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomFlip{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()

	CardId, err := PlayroomMod.FlipCard(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomFlip{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playroom_flip", map[string]interface{}{
		"CardID": CardId,
		"Id":     req.Id,
	})
	player.PlayMod.save()
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomFlip{
		Code:   msg.RES_CODE_SUCCESS,
		Id:     req.Id,
		CardId: int32(CardId),
	})
	return nil
}

func ReqPlayroomFlipReward(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomFlipReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, Target, Result, err := PlayroomMod.GetFlipReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomFlipReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	Items1 := Items
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	if LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_PET_THIEF) && Result == playroom.FLIP_TYPE_GOLD {
		player.GetPetThiefReward(Target)
	}

	err = player.HandleItem(Items1, msg.ITEM_POP_LABEL_PlayroomFlip.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomFlip{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if Result == playroom.FLIP_TYPE_GOLD {
		FriendMod := player.PlayMod.getFriendMod()
		FriendMod.AddActLog(friend.ACT_LOG_TYPE_GET_VISIT_GAME_PRIZE, "")
		player.UpdateUserInfo()
	}
	now := GoUtil.Now()
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Target,
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_LOSE,
		SendT: now,
		End:   now + sevendays,
		Item:  Items,
		Extra: GameResult{
			T:     playroom.GAME_TYPE_FILP,
			Emoji: int(req.EmojiId),
		},
	})
	player.TeLog("finish_mini_game", map[string]interface{}{
		"mini_game_type": playroom.GAME_TYPE_FILP,
		"is_chip":        false,
		"item_list":      Items,
	})
	player.PlayerDecoSetLog("emoji", int(req.EmojiId), "playroom_flip_reward")
	player.PlayMod.save()
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomFlipReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomBuyItem(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomBuyItem{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	AddItems, CostItem := PlayroomMod.BuyItem(int(req.Id))
	if CostItem == nil {
		err := fmt.Errorf("can not buy item")
		player.SendErrClienRes(&msg.ResPlayroomBuyItem{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	err = player.HandleLoseItem(CostItem, msg.ITEM_POP_LABEL_PlayroomBuyItem.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomBuyItem{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(AddItems, msg.ITEM_POP_LABEL_PlayroomBuyItem.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomBuyItem{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playroom_buy_item", map[string]interface{}{
		"item_id": req.Id,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomBuyItem{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomUpvote(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomUpvote{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, err := PlayroomMod.GiveUpvote(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomUpvote{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomUpvote.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomUpvote{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
	}
	now := GoUtil.Now()
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    int(req.Id),
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_UPVOTE,
		SendT: now,
		End:   now + sevendays,
	})
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    int(req.Id),
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_KISS,
		SendT: now,
		End:   now + sevendays,
	})
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_PLAYROOM_UPVOTE})
	player.AddPlayroomUpvote(int(req.Id))
	player.SetPlayroomKiss(1, int(req.Id))
	player.TeLog("visit_like", map[string]interface{}{
		"user_id": req.Id,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomUpvote{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomUnlock(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomUnlock{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	err = PlayroomMod.Unlock(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomUnlock{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("room_unlcok", map[string]interface{}{
		"room_type": playroomCfg.GetUnlockName(int(req.Id)),
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomUnlock{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(req.Id),
	})
	player.PlayroomBackData()
	return nil
}

func ReqPlayroomTask(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomTask{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, err := PlayroomMod.GetDailyTaskReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomTask{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomTask.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomTask{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	Type := playroomCfg.GetDailyTaskType(int(req.Id))
	player.TeLog("room_daily_task", map[string]interface{}{
		"task_id":   req.Id,
		"is_reward": PlayroomMod.DailyTaskCanReward(Type),
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomTask{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(req.Id),
	})
	player.PlayroomBackData()
	return nil
}

func ReqPlayroomTaskReward(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomTaskReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Id, Items, err := PlayroomMod.GetTaskReward(int(req.Type))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.args["ResItemPopId"] = Id
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomTaskReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomTaskReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playroom_task_reward", map[string]interface{}{
		"task_type": req.Type,
		"reward_id": Id,
		"reward":    Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResPlayroomTaskReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(Id),
		Type: req.Type,
	})
	player.NotifyPlayroomTask()
	return nil
}

func ReqOfflineReconnectFunc(a gate.Agent, buf []byte) error {
	req := &msg.ReqOfflineReconnect{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	res := &msg.ResOfflineReconnect{}
	res.DwUin = req.DwUin
	if v, ok := G_GameLogicPtr.M_Players.Load(req.DwUin); ok {
		player := v.(*Player)
		agent := player.GetAgentByPlayer()
		if agent != a {
			player.SetAgent(a)
			internal.AsignPlayerToAgents(a, player)
			internal.Agents.Delete(agent)
		}
		player.TeLog("reconnect", map[string]interface{}{})
		res.Result = 3
	} else {
		res.Result = 2
	}
	data, _ := proto.Marshal(res)
	G_GameLogicPtr.PackResInfo(a, "ResOfflineReconnect", data)
	return nil
}

// playroom 打工离线
func ReqPlayroomOutline(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomOutline{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	if req.CostDia < 0 {
		var itemList []*item.Item
		item := item.NewItem(item.ITEM_DIAMOND_ID, int(req.CostDia))
		itemList = append(itemList, item)
		err := player.HandleItem(itemList, msg.ITEM_POP_LABEL_ChessEx.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResPlayroomOutline{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}

	err = ChessMod.ExChess(int(req.OldChessId), int(req.NewChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomOutline{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	HandbookMod := player.PlayMod.getHandbookMod()
	HandbookMod.SetHandbook(int(req.NewChessId))
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomOutline{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResPlayroomOutline{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomWrokOutline(player *Player, buf []byte) error {
	PlayroomMod := player.PlayMod.getPlayroomMod()
	PlayroomMod.SetWorkOutline(0)
	player.PlayMod.save()
	player.PushClientRes(PlayroomMod.NotifyStatus())
	player.PushClientRes(&msg.ResPlayroomWrokOutline{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomShop(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomShop{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	AddItems, LoseItem, err := PlayroomMod.ShopBuy(int(req.Id), int(req.Num), player.IsWeeklyDiscount())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	// 消耗道具为0则为广告获取
	if LoseItem[0].Num == 0 {
		err := PlayroomMod.AdWatch(AddItems[0].Id)
		if err != nil {
			player.SendErrClienRes(&msg.ResPlayroomBuyItem{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}
	err = player.HandleLoseItem(LoseItem, msg.ITEM_POP_LABEL_PlayroomShop.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(AddItems, msg.ITEM_POP_LABEL_PlayroomShop.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomShop{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playroom_shop", map[string]interface{}{
		"shop_id": req.Id,
		"num":     req.Num,
		"reward":  AddItems,
	})
	player.PetItemGetLog(AddItems, LoseItem, "Shop")
	player.PlayMod.save()
	player.PushClientRes(PlayroomMod.NotifyMood())
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomShop{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqFriendTreasure(player *Player, buf []byte) error {
	player.PushClientRes(player.PlayMod.getFriendTreasureMod().BackData())
	return nil
}

func ReqFriendTreasureStart(player *Player, buf []byte) error {
	req := &msg.ReqFriendTreasureStart{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	// FriendMod := player.PlayMod.getFriendMod()
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	if FriendTreasureMod.Status == 1 {
		player.SendErrClienRes(&msg.ResFriendTreasureStart{
			Code: msg.RES_CODE_FAIL,
			Msg:  "game not over",
		})
		return fmt.Errorf("game not over")
	}

	if FriendTreasureMod.Star < 100 {
		player.SendErrClienRes(&msg.ResFriendTreasureStart{
			Code: msg.RES_CODE_FAIL,
			Msg:  "not enough star",
		})
		return fmt.Errorf("not enough star")
	}
	player.TeLog("friend_treasure_start", map[string]interface{}{})
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_PETTREASURE})
	FriendTreasureMod.InitGame(req.List, req.List2)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResFriendTreasureStart{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqFriendTreasureFilp(player *Player, buf []byte) error {
	req := &msg.ReqFriendTreasureFilp{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	Items, Uid, err := FriendTreasureMod.Flip(int(req.Pos))
	if err != nil {
		player.SendErrClienRes(
			&msg.ResFriendTreasureFilp{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			},
		)
		return err
	}
	now := GoUtil.Now()
	if Uid != 0 && Items != nil {
		FriendMgrSend(&MsqMod.Msg{
			From:  int(player.M_DwUin),
			To:    int(Uid),
			Type:  MsqMod.HANDLE_TYPE_TREASURE_RESULT,
			Extra: Items[0].Num,
			SendT: now,
			End:   now + sevendays,
		})
	}

	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_FriendtreasureFilp.String())
	if err != nil {
		player.SendErrClienRes(
			&msg.ResFriendTreasureFilp{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			},
		)
		return err
	}
	player.TeLog("friend_treasure_filp", map[string]interface{}{
		"pos":   req.Pos,
		"items": Items,
		"uid":   Uid,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResFriendTreasureFilp{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqFriendTreasureEnd(player *Player, buf []byte) error {
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	Items, FriendItemNum, err := FriendTreasureMod.EndGame()
	if err != nil {
		player.SendErrClienRes(
			&msg.ResFriendTreasureEnd{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			},
		)
		return err
	}
	now := GoUtil.Now()
	for _, v := range FriendTreasureMod.List {
		ItemNum := 0
		if v.Status == 1 {
			ItemNum = FriendItemNum
		}
		FriendMgrSend(&MsqMod.Msg{
			From:  int(player.M_DwUin),
			To:    int(v.Uid),
			Type:  MsqMod.FRIEND_TREASURE_HANDLE,
			SendT: now,
			End:   now + sevendays,
			Extra: []*item.Item{item.NewItem(item.ITEM_STAR_ID, ItemNum)},
		})
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_FriendtreasureEnd.String())
	if err != nil {
		player.SendErrClienRes(
			&msg.ResFriendTreasureEnd{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			},
		)
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	FriendMod.AddActLog(friend.ACT_LOG_TYPE_OPEN_PET_TREASURE, "")
	player.UpdateUserInfo()
	player.TeLog("pet_treasure_open", map[string]interface{}{
		"pet_treasure_step":   FriendTreasureMod.Shift,
		"pet_treasure_box":    FriendTreasureMod.BoxItems,
		"pet_treasure_reward": append(Items, FriendTreasureMod.BoxItems...),
	})
	FriendTreasureMod.ResetGame()
	player.PlayMod.save()
	player.PushClientRes(FriendTreasureMod.BackData())
	player.PushClientRes(&msg.ResFriendTreasureEnd{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqKafkaLog(player *Player, buf []byte) error {
	req := &msg.ReqKafkaLog{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	player.TeLog(req.Event, map[string]interface{}{
		"From": "Client",
		"data": req.Data,
	})
	return nil
}

func ReqCardHandbookReward(player *Player, buf []byte) error {
	req := &msg.ReqCardHandbookReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CardMod := player.PlayMod.getCardMod()
	Items, err := CardMod.GetHandbookReward(int(req.CardId))
	if err != nil {
		player.SendErrClienRes(&msg.ResCardHandbookReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_CardHandbookReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCardHandbookReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("card_handbook_reward", map[string]interface{}{
		"CardId": int(req.CardId),
		"Items":  Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCardHandbookReward{
		Code:   msg.RES_CODE_SUCCESS,
		CardId: req.CardId,
	})
	return nil
}

// 设置表情
func ReqSetEmoji(player *Player, buf []byte) error {
	req := &msg.ReqSetEmoji{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	EmojiMod := player.PlayMod.getEmojiMod()
	err = EmojiMod.SetEmoji(int(req.Id), int(req.Type))
	if err != nil {
		player.SendErrClienRes(&msg.ResSetEmoji{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playerdeco_emoji_set", map[string]interface{}{
		"emoji_name":     emojiCfg.GetEmojiName(int(req.Id)),
		"emoji_id":       int(req.Id),
		"emoji_setplace": int(req.Type),
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResSetEmoji{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.BackUserInfo()
	return nil
}

// 请求收藏室内
func ReqCollectInfo(player *Player, buf []byte) error {
	CollectMod := player.PlayMod.getCollectMod()
	player.PushClientRes(CollectMod.BackData())
	return nil
}

// 领取收集奖励
func ReqCollect(player *Player, buf []byte) error {
	req := &msg.ReqCollect{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CollectMod := player.PlayMod.getCollectMod()
	Type := collectCfg.GetRewardType(int(req.Id))
	Num := 0
	AreaType := ""
	switch Type {
	case collect.COLLECT_TYPE_EMOJI:
		AreaType = "emoji"
		Num = player.PlayMod.getEmojiMod().GetEmojiNum()
	case collect.COLLECT_TYPE_FACE: // 增加默认头像1个
		AreaType = "face"
		Num = player.PlayMod.getFaceMod().GetFaceNum() + 1
	case collect.COLLECT_TYPE_AVATAR:
		AreaType = "avatar"
		Num = player.PlayMod.getAvatarMod().GetAvatarNum()
	}
	Items, err := CollectMod.GetReward(int(req.Id), Num)
	if err != nil {
		player.SendErrClienRes(&msg.ResCollect{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_Collect.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCollect{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(CollectMod.BackData())
	player.PushClientRes(&msg.ResCollect{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("playerdeco_warehouse_unlock", map[string]interface{}{
		"area_type": AreaType,
		"Id":        int(req.Id),
		"Items":     Items,
	})
	return nil
}

// 获取出售棋子获得的星星数量
func ReqSellChessNum(player *Player, buf []byte) error {
	req := &msg.ReqSellChessNum{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	data, _ := mergeDataCfg.GetOne(int(req.ChessId))
	Num := data.SellNum
	limitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	if limitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_PAYBACK_DAY) {
		Num = data.Star
	}
	player.PushClientRes(&msg.ResSellChessNum{
		Num: int32(Num),
	})
	return nil
}

func ReqPlayroomDressSet(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomDressSet{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Parts, _, error := PlayroomMod.PlayroomDressSet(GoUtil.MapInt32ToInt(req.DressSet))
	if error != nil {
		player.SendErrClienRes(&msg.ResPlayroomDressSet{
			Code: msg.RES_CODE_FAIL,
			Msg:  error.Error(),
		})
		return error
	}
	player.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_PETDRESS, A: []interface{}{Parts}})
	player.PlayMod.save()
	//player.PetDecoSetLog(Diff)
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomDressSet{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomPetAirSet(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomPetAirSet{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	error := PlayroomMod.PlayroomPetAirSet(int(req.PetAirSet))
	if error != nil {
		player.SendErrClienRes(&msg.ResPlayroomPetAirSet{
			Code: msg.RES_CODE_FAIL,
			Msg:  error.Error(),
		})
		return error
	}
	player.PlayMod.save()
	player.PlayroomBackData()
	player.PushClientRes(&msg.ResPlayroomPetAirSet{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqLimitEventLuckyCat(player *Player, buf []byte) error {
	req := &msg.ReqLimitEventLuckyCat{}
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	err := LimitedTimeEventMod.LuckyCat(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResLimitEventLuckyCat{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	err = ChessMod.AddChess(int(req.ChessId))
	if err != nil {
		player.SendErrClienRes(&msg.ResLimitEventLuckyCat{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
	}
	data := &PlayerChessData{}
	err = data.UpdateChessData(player, req.MChessData)
	if err != nil {
		player.SendErrClienRes(&msg.ResLimitEventLuckyCat{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqLimitEventLuckyCat",
			"req":           req,
		})
		return err
	}
	player.TeLog("limit_event_luckycat", map[string]interface{}{
		"ChessId": int(req.ChessId),
	})
	player.PlayMod.save()
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(&msg.ResLimitEventLuckyCat{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqActivityReward(player *Player, buf []byte) error {
	req := &msg.ReqActivityReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ActivityInfo := player.GetActivityInfo(int(req.Id))
	if ActivityInfo == nil {
		player.SendErrClienRes(&msg.ResActivityReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not exist",
		})
		return fmt.Errorf("activity not exist")
	}
	ActivityMod := player.PlayMod.getActivityMod()
	Items, err := ActivityMod.GetReward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResActivityReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_ActivityReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResActivityReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("activity_reward", map[string]interface{}{
		"Id":    int(req.Id),
		"Items": Items,
	})
	player.PlayMod.save()
	player.initAcitivity()
	player.BackDataActivity()
	player.PushClientRes(&msg.ResActivityReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqLang(player *Player, buf []byte) error {
	req := &msg.ReqLang{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetLang(req.Lang)
	player.PushClientRes(&msg.ResLang{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqCatTrickReward(player *Player, buf []byte) error {
	req := &msg.ReqCatTrickReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	if !LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_CAT_TRICK) {
		player.SendErrClienRes(&msg.ResCatTrickReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  "cat trick event not exist",
		})
		return fmt.Errorf("cat trick event not exist")
	}
	Items, err := LimitedTimeEventMod.GetCatTrickReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResCatTrickReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_CatTrickReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCatTrickReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cat_trick_reward", map[string]interface{}{
		"Items": Items,
	})
	player.PlayMod.save()
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(&msg.ResCatTrickReward{
		Code:    msg.RES_CODE_SUCCESS,
		IsClose: !LimitedTimeEventMod.CheckExist(limitedTimeEvent.EVENT_TYPE_CAT_TRICK),
	})
	return nil
}

func ReqAddNpc(player *Player, buf []byte) error {
	req := &msg.ReqAddNpc{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	err = FriendMod.SetNpc(int(req.NpcId))
	if err != nil {
		player.SendErrClienRes(&msg.ResAddNpc{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if len(FriendMod.Npc) == 1 { // 首次添加NPC 视为邀请好友成功
		InviteMod := player.PlayMod.getInviteMod()
		InviteMod.AddInvite(int(req.NpcId))
	}
	player.PlayMod.save()
	player.FriendListBackData()
	player.TeLog("add_npc", map[string]interface{}{
		"NpcId": int(req.NpcId),
	})
	FriendMod.AddReplyInfo(int(req.NpcId), friend.REPLY_TYPE_GREETING, "", GoUtil.Now()+oneday, nil)
	FriendMod.AddReplyInfo(int(req.NpcId), friend.REPLY_TYPE_GREETING_Get, "", GoUtil.Now()+oneday, nil)
	player.AddLog(int(req.NpcId), friend.LOG_TYPE_FRIEND_BECOME_NPC, GoUtil.String(req.NpcId), GoUtil.Now())
	player.PushClientRes(&msg.ResAddNpc{
		Code:  msg.RES_CODE_SUCCESS,
		NpcId: req.NpcId,
	})
	return nil
}

func ReqChargeReceive(player *Player, buf []byte) error {
	req := &msg.ReqChargeReceive{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    int(req.Uid),
		SendT: GoUtil.Now(),
		Type:  MsqMod.HANDLE_TYPE_CHARGE_RECEIVE,
		Extra: req.Content,
	})
	player.PushClientRes(&msg.ResChargeReceive{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqAddWish(player *Player, buf []byte) error {
	req := &msg.ReqAddWish{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChargeMod := player.PlayMod.getChargeMod()
	Items, err := ChargeMod.AddWish(int(req.Id), int(req.Type))
	if err != nil {
		player.SendErrClienRes(&msg.ResAddWish{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if Items != nil {
		err = player.HandleItem(Items, msg.ITEM_POP_LABEL_AddWish.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResAddWish{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}
	player.TeLog("wish_add", map[string]interface{}{
		"Id":    int(req.Id),
		"Type":  int(req.Type),
		"Items": Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResAddWish{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqGetWish(player *Player, buf []byte) error {
	req := &msg.ReqGetWish{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChargeMod := player.PlayMod.getChargeMod()
	Items, err := ChargeMod.GetWish()
	if err != nil {
		player.SendErrClienRes(&msg.ResGetWish{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	if Items != nil {
		err = player.HandleItem(Items, msg.ITEM_POP_LABEL_GetWish.String())
		if err != nil {
			player.SendErrClienRes(&msg.ResGetWish{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			})
			return err
		}
	}
	player.TeLog("wish_get", map[string]interface{}{
		"wish": Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGetWish{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqSendWishBeg(player *Player, buf []byte) error {
	req := &msg.ReqSendWishBeg{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChargeMod := player.PlayMod.getChargeMod()
	err = ChargeMod.SendWishBeg(req.Uid)
	if err != nil {
		player.SendErrClienRes(&msg.ResSendWishBeg{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	for _, v := range req.Uid {
		FriendMgrSend(&MsqMod.Msg{
			From:  int(player.M_DwUin),
			To:    int(v),
			Type:  MsqMod.HANDLE_TYPE_WISHLIST_SEND,
			SendT: GoUtil.Now(),
		})
	}
	player.TeLog("wish_send", map[string]interface{}{
		"Uid": req.Uid,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResSendWishBeg{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqWishApplyList(player *Player, buf []byte) error {
	req := &msg.ReqWishApplyList{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	List := FriendMod.GetWishApply()
	rs := make([]*msg.ResFriendApplyInfo, 0)
	for _, v := range List {
		PD := G_GameLogicPtr.GetResSimplePlayerByUid(int(v.Uid))
		if PD == nil {
			continue
		}
		rs = append(rs, &msg.ResFriendApplyInfo{
			Player: PD,
			Time:   int32(v.Time),
		})
	}

	player.PlayMod.save()
	player.PushClientRes(&msg.ResWishApplyList{
		ApplyList: rs,
	})
	return nil
}

func ReqWishApply(player *Player, buf []byte) error {
	req := &msg.ReqWishApply{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	err = FriendMod.ApplyWish(req.Uid)
	if err != nil {
		player.SendErrClienRes(&msg.ResWishApply{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    int(req.Uid),
		Type:  MsqMod.HANDLE_TYPE_WISHLIST_AGREE,
		SendT: GoUtil.Now(),
	})
	player.TeLog("wish_apply", map[string]interface{}{
		"Uid": int(req.Uid),
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResWishApply{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	return nil
}

func ReqGuidePlayroom(player *Player, buf []byte) error {
	req := &msg.ReqGuidePlayroom{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	GuideMod := player.PlayMod.getGuideMod()
	B := GuideMod.GetPlayroomGuide()
	if B {
		player.SendErrClienRes(&msg.ResGuidePlayroom{
			Code: msg.RES_CODE_FAIL,
			Msg:  "已领取",
		})
		return fmt.Errorf("已领取")
	}
	Items := []*item.Item{item.NewItem(int(req.Id), 1)}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_PlayroomGame.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResWishApply{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	GuideMod.SetPlayroomGuide(true)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGuidePlayroom{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqId2Verify(player *Player, buf []byte) error {
	req := &msg.ReqId2Verify{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	if req.Id == "" || req.Name == "" {
		player.SendErrClienRes(&msg.ResId2Verify{
			ResultCode: msg.RES_CODE_FAIL,
			Msg:        "id is zero",
		})
		return fmt.Errorf("id is zero")
	}
	err = GoUtil.IdVerify(req.Name, req.Id)
	if err != nil {
		player.SendErrClienRes(&msg.ResId2Verify{
			ResultCode: msg.RES_CODE_FAIL,
			Msg:        err.Error(),
		})
		return err
	}
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetId2Verify(req.Name, req.Id)
	player.TeLog("id2verify", map[string]interface{}{
		"Id":   req.Id,
		"Name": req.Name,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResId2Verify{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqFriendByCode(player *Player, buf []byte) error {
	req := &msg.ReqFriendByCode{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	if req.Code == "" {
		player.SendErrClienRes(&msg.ResFriendByCode{
			Code: msg.RES_CODE_FAIL,
			Msg:  "code is empty",
		})
		return fmt.Errorf("code is empty")
	}
	CodeNum := GoUtil.ParseUniqueStringToInt(req.Code)
	if CodeNum <= 0 {
		player.SendErrClienRes(&msg.ResFriendByCode{
			Code: msg.RES_CODE_FAIL,
			Msg:  "code is invalid",
		})
		return fmt.Errorf("code is invalid")
	}
	Uid := int64(CodeNum) + int64(100000) + int64(conf.Server.AppID*100000000)
	if Uid == player.M_DwUin {
		player.SendErrClienRes(&msg.ResFriendByCode{
			Code: msg.RES_CODE_FAIL,
			Msg:  "can not add yourself",
		})
		return fmt.Errorf("can not add yourself")
	}

	PlayerSimpleData := G_GameLogicPtr.GetResSimplePlayerByUid(int(Uid))
	if PlayerSimpleData == nil {
		player.SendErrClienRes(&msg.ResFriendByCode{
			Code: msg.RES_CODE_FAIL,
			Msg:  "player not exist",
		})
		return fmt.Errorf("player not exist")
	}
	player.PushClientRes(&msg.ResFriendByCode{
		Code:   msg.RES_CODE_SUCCESS,
		Player: PlayerSimpleData,
	})
	return nil
}

func ReqPlayroomGameShowReward(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomGameShowReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	Items := player.GetPlayroomGameReward(int(req.Type), int(req.SelectId))
	player.PushClientRes(&msg.ResPlayroomGameShowReward{
		Items: item.ItemToMsg(Items),
	})
	return nil
}

func ReqPlayroomGuide(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomGuide{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	PlayroomMod := player.PlayMod.getPlayroomMod()
	err = PlayroomMod.Guide(int(req.Type))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomGuide{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("playroom_guide", map[string]interface{}{
		"Physiology": req.Type,
	})
	player.PlayMod.save()
	player.PushClientRes(PlayroomMod.NotifyMood())
	player.PushClientRes(&msg.ResPlayroomGuide{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqCatnip(player *Player, buf []byte) error {
	player.CatnipBackData()
	return nil
}

// TODO : 猫草大作战邀请好友
func ReqCatnipInvite(player *Player, buf []byte) error {
	req := &msg.ReqCatnipInvite{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipInvite{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	ActivityInfo := player.GetActivityInfo(ActivityId)
	CatnipMod := player.PlayMod.getCatnipMod()
	err = CatnipMod.Invite(int(req.Uid), int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipInvite{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cats_garden_invite", map[string]interface{}{
		"Id": int(req.Id),
	})
	FriendMgrSend(&MsqMod.Msg{
		From: int(player.M_DwUin),
		To:   int(req.Uid),
		Type: MsqMod.HANDLE_TYPE_CATNIP_INVITE,
		End:  ActivityInfo.EndT,
		Extra: CatnipMsg{
			ActivityId: ActivityId,
			GameId:     int(req.Id),
		},
		SendT: GoUtil.Now(),
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipInvite{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	return nil
}

func ReqCatnipAgree(player *Player, buf []byte) error {
	req := &msg.ReqCatnipAgree{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	ActivityInfo := player.GetActivityInfo(ActivityId)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipAgree{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	_, err = CatnipMod.Agree(int(req.Id), int(req.Uid))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipAgree{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.SetCatnipPartner(int(req.Id), int(req.Uid), ActivityInfo.EndT)
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipAgree{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cats_garden_accept_invite", map[string]interface{}{
		"Id": int(req.Id),
	})
	FriendMgrSend(&MsqMod.Msg{
		From: int(player.M_DwUin),
		To:   int(req.Uid),
		Type: MsqMod.HANDLE_TYPE_CATNIP_AGREE,
		Extra: CatnipMsg{
			ActivityId: ActivityId,
			GameId:     int(req.Id),
		},
		SendT: GoUtil.Now(),
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipAgree{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	return nil
}

// TODO : 猫草大作战设置倍数
func ReqCatnipMultiply(player *Player, buf []byte) error {
	req := &msg.ReqCatnipMultiply{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipMultiply{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	err = CatnipMod.Multiply(int(req.Multiply))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipMultiply{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("catnip_multiply", map[string]interface{}{
		"Mul": int(req.Multiply),
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipMultiply{
		Code:     msg.RES_CODE_SUCCESS,
		Multiply: req.Multiply,
	})
	return nil
}

// TODO : 猫草大作战开始游戏
func ReqCatnipPlay(player *Player, buf []byte) error {
	req := &msg.ReqCatnipPlay{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	Id, Growth, PartnerId, Items, ItemCost, FriendItems, err := CatnipMod.Play(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleLoseItem(ItemCost, msg.ITEM_POP_LABEL_CatnipPlay.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_CatnipPlay.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cats_garden_expend_token", map[string]interface{}{
		"friends_ID": PartnerId,
		"multiple":   CatnipMod.GetMultiple(),
		"expend_num": ItemCost[0].Num,
		"get_award":  Items,
	})
	if Growth > 0 {
		player.CatnipGrowthMsg(PartnerId, int(req.Id), Growth, FriendItems*CatnipMod.Mul)
	}
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipPlay{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(Id),
	})
	return nil
}

// TODO : 猫草大作战领取奖励
func ReqCatnipReward(player *Player, buf []byte) error {
	req := &msg.ReqCatnipReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	Items, GameInfo, err := CatnipMod.Reward(int(req.Id))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_CatnipReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cats_garden_stage_reward", map[string]interface{}{
		"Id":              int(req.Id),
		"friends_ID":      GameInfo.Partner,
		"friends_get_num": GameInfo.PartnerAdd,
		"user_get_num":    GameInfo.Progress - GameInfo.PartnerAdd,
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 猫草大作战领取大奖
func ReqCatnipGrandReward(player *Player, buf []byte) error {
	req := &msg.ReqCatnipGrandReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipPlay{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	Items, err := CatnipMod.GrandReward()
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipGrandReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_CatnipGrandReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipGrandReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("cats_garden_final_reward", map[string]interface{}{
		"get_reward": Items,
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipGrandReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqCatnipRefuse(player *Player, buf []byte) error {
	req := &msg.ReqCatnipRefuse{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipRefuse{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	err = CatnipMod.Refuse(int(req.Id), int(req.Uid))
	if err != nil {
		player.SendErrClienRes(&msg.ResCatnipRefuse{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("catnip_refuse", map[string]interface{}{
		"Id": int(req.Id),
	})
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    int(req.Uid),
		Type:  MsqMod.HANDLE_TYPE_CATNIP_REFUSE,
		SendT: GoUtil.Now(),
		Extra: CatnipMsg{
			ActivityId: player.GetActivityId(activity.ACT_TYPE_CATNIP),
			GameId:     int(req.Id),
		},
	})
	//player.CatnipBackData()
	player.PlayMod.save()
	player.PushClientRes(&msg.ResCatnipRefuse{
		Code: msg.RES_CODE_SUCCESS,
		Uid:  req.Uid,
	})
	return nil
}

func ReqActPass(player *Player, buf []byte) error {
	player.ActPassBackData()
	return nil
}

func ReqActPassReward(player *Player, buf []byte) error {
	PassMod := player.PlayMod.getPassMod()
	Items, NewLevel := PassMod.GetRewardItems()
	err := player.HandleItem(Items, msg.ITEM_POP_LABEL_ActPassReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResActPassReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("act_pass_reward", map[string]interface{}{
		"NewLevel": NewLevel,
		"Items":    Items,
	})
	player.PlayMod.save()
	player.PushClientRes(&msg.ResActPassReward{
		Code:        msg.RES_CODE_SUCCESS,
		RewardLevel: GoUtil.IntToInt32(NewLevel),
	})
	return nil
}

func ReqFriendReplyHandle(player *Player, buf []byte) error {
	req := &msg.ReqFriendReplyHandle{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	FriendMod := player.PlayMod.getFriendMod()
	ReplyInfo := FriendMod.ReplyFriend(int(req.LogId))
	if ReplyInfo == nil {
		player.SendErrClienRes(&msg.ResFriendReplyHandle{
			Code: msg.RES_CODE_FAIL,
			Msg:  "reply info not exist",
		})
		return fmt.Errorf("reply info not exist")
	}
	ErrType := msg.FRIEND_REPLY_HANDLE_ERR_TYPE_NONE
	now := GoUtil.Now()
	if req.Type == 1 && ReplyInfo.Uid > 10000 {
		switch ReplyInfo.Type {
		case friend.REPLY_TYPE_GREETING:
			ReplyData := friend.ReplyInfo{
				Uid:   int(player.M_DwUin),
				Type:  friend.REPLY_TYPE_GREETING_Get,
				Param: req.Param,
			}
			FriendMgrSend(&MsqMod.Msg{
				From:  int(player.M_DwUin),
				To:    int(ReplyInfo.Uid),
				Type:  MsqMod.HANDLE_TYPE_FRIEND_GREETING_REPLY,
				SendT: now,
				End:   now + sevendays,
				Extra: ReplyData,
			})
		case friend.REPLY_TYPE_CATNIP: // 猫草大作战同意邀请
			GameId := GoUtil.Int(ReplyInfo.Param)
			activityInfo := player.GetActivityInfo(player.GetActivityId(activity.ACT_TYPE_CATNIP))
			if activityInfo == nil {
				break
			}
			err := player.SetCatnipPartner(GameId, ReplyInfo.Uid, activityInfo.EndT)
			if err == nil {
				CatnipMod := player.PlayMod.getCatnipMod()
				ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
				UserList, _ := CatnipMod.Agree(GameId, ReplyInfo.Uid)
				player.TeLog("catnip_agree", map[string]interface{}{
					"Id": int(GameId),
				})
				FriendMgrSend(&MsqMod.Msg{
					From: int(player.M_DwUin),
					To:   int(ReplyInfo.Uid),
					Type: MsqMod.HANDLE_TYPE_CATNIP_AGREE,
					Extra: CatnipMsg{
						ActivityId: ActivityId,
						GameId:     int(GameId),
					},
					SendT: now,
					End:   now + sevendays,
				})

				for _, v := range UserList {
					FriendMgrSend(&MsqMod.Msg{
						From: int(player.M_DwUin),
						To:   int(v),
						Type: MsqMod.HANDLE_TYPE_CATNIP_AGREE,
						Extra: CatnipMsg{
							ActivityId: ActivityId,
							GameId:     int(GameId),
						},
						SendT: now,
						End:   now + sevendays,
					})
				}
				player.CatnipBackData()
			} else {
				ErrType = msg.FRIEND_REPLY_HANDLE_ERR_TYPE_CATNIP
			}

		}
	}
	Items := baseCfg.GetFriendReplyReward()
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_FriendReplyHandle.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResFriendReplyHandle{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.FriendLogBackData()
	player.PushClientRes(&msg.ResFriendReplyHandle{
		Code:    msg.RES_CODE_SUCCESS,
		LogId:   req.LogId,
		Type:    req.Type,
		ErrType: ErrType,
	})
	return nil
}

func ReqGetChessRetireReward(player *Player, buf []byte) error {
	req := &msg.ReqGetChessRetireReward{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	ChessMod := player.PlayMod.getChessMod()
	Items, err := ChessMod.GetRetireReward(req.Id)
	if err != nil {
		player.SendErrClienRes(&msg.ResGetChessRetireReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	err = player.HandleItem(Items, msg.ITEM_POP_LABEL_GetChessRetireReward.String())
	if err != nil {
		player.SendErrClienRes(&msg.ResGetChessRetireReward{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	player.TeLog("get_chess_retire_reward", map[string]interface{}{
		"EmitSerise": req.Id,
		"Items":      Items,
	})
	player.PushClientRes(ChessMod.BackData())
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGetChessRetireReward{
		Code: msg.RES_CODE_SUCCESS,
		Id:   req.Id,
	})
	return nil
}

func ReqCatnipEmoji(player *Player, buf []byte) error {
	req := &msg.ReqCatnipEmoji{}
	err := proto.Unmarshal(buf, req)
	if err != nil {
		return err
	}
	CatnipMod := player.PlayMod.getCatnipMod()
	ActivityId := player.GetActivityId(activity.ACT_TYPE_CATNIP)
	if ActivityId == 0 {
		player.SendErrClienRes(&msg.ResCatnipEmoji{
			Code: msg.RES_CODE_FAIL,
			Msg:  "activity not active",
		})
		return fmt.Errorf("activity not active")
	}
	GameInfo := CatnipMod.GetGameInfo(int(req.Id))
	CatnipMod.SetSendEmoji(int(req.Id), int(req.EmojiId))
	if GameInfo.Partner == 0 {
		player.SendErrClienRes(&msg.ResCatnipEmoji{
			Code: msg.RES_CODE_FAIL,
			Msg:  "no partner",
		})
		return fmt.Errorf("no partner")
	}
	FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    GameInfo.Partner,
		Type:  MsqMod.HANDLE_TYPE_CATNIP_SEND_EMOJI,
		Extra: int(req.EmojiId),
	})
	player.PushClientRes(&msg.ResCatnipEmoji{
		Code:    msg.RES_CODE_SUCCESS,
		EmojiId: req.EmojiId,
		Id:      req.Id,
	})

	player.CatnipBackData()
	return nil
}
