package game

import (
	"errors"
	"fmt"
	"math"
	"server/GoUtil"
	cardCfg "server/conf/card"
	decorateCfg "server/conf/decorate"
	mergeDataCfg "server/conf/mergeData"
	miningCfg "server/conf/mining"
	playroomCfg "server/conf/playroom"
	"server/db"
	"server/game/internal"
	"server/game/mod/card"
	"server/game/mod/friend"
	"server/game/mod/item"
	"server/game/mod/limitedTimeEvent"
	MsqMod "server/game/mod/msg"
	"server/game/mod/order"
	"server/game/mod/piggyBank"
	"server/game/mod/playroom"
	"server/game/mod/quest"
	"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{}
	proto.Unmarshal(buf, detail)
	player.PlayerBaseMod.ResPlayerBaseInfo(player)
	return nil
}

func ReqPlayerBriefProfileDataFunc(player *Player, buf []byte) error {
	detail := &msg.ReqPlayerBriefProfileData{}
	proto.Unmarshal(buf, detail)
	Uid := int(detail.DwUin)
	PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(Uid)
	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)),
	})
	return nil
}

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

// 更新玩家物品
func UpdateBaseItemInfofunction(player *Player, buf []byte) error {
	detail := &msg.UpdateBaseItemInfo{}
	proto.Unmarshal(buf, detail)
	player.PlayerBaseMod.UpdateBaseItemInfo(detail)
	return nil
}

// 请求玩家棋盘信息
func ReqPlayerChessDataFunc(player *Player, buf []byte) error {
	detail := &msg.ReqPlayerChessData{}
	proto.Unmarshal(buf, detail)
	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{}
	proto.Unmarshal(buf, req)
	MaxEnergyMul := player.GetPlayerBaseMod().GetMaxEnergyMul(player)
	if 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(MaxEnergyMul)),
		"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.PushClientRes(OrderMod.BackData())
	player.PushClientRes(player.PlayMod.getBaseMod().BackData())
	return nil
}

// 领取图鉴奖励
func ReqGetHandbookReward(player *Player, buf []byte) error {
	req := &msg.ReqGetHandbookReward{}
	proto.Unmarshal(buf, req)
	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
	}
	var itemList []*item.Item
	itemList = append(itemList, &item.Item{Id: item.ITEM_ENERGY_ID, Num: 5})
	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 ReqRewardOrder(player *Player, buf []byte) error {
	req := &msg.ReqRewardOrder{}
	proto.Unmarshal(buf, req)
	OrderMod := player.PlayMod.getOrderMod()
	ChessMod := player.PlayMod.getChessMod()
	PlayroomMod := player.PlayMod.getPlayroomMod()
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	Item, mergeList, OrderType, Star, err := OrderMod.RewardOrder(int(req.OrderId))
	if OrderType == order.Trigger_type { // playroom触发式订单获取额外道具
		Item1 := PlayroomMod.GetReward()
		Item = item.Merge(Item, Item1)
		P1, P2 := playroomCfg.GetPremiumItem()
		if P1 == Item1[0].Id || P2 == Item1[0].Id {
			Star = int(math.Ceil(float64(Star) * 0.8))
		} else {
			Star = int(math.Ceil(float64(Star) * 0.6))
		}
		Target := GetVisitorPlayer(player)
		PlayroomMod.Target = Target
		PlayroomMod.Status = playroom.STATUS_VISIT
		PlayroomBackData(player)
	}

	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)
		if len(AddItem) > 0 {
			player.TeLog("time_limited_event_action", map[string]interface{}{
				"event_type": "meteor_show",
				"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)
		player.args["ResItemPopId"] = 0
		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": "chest_rain",
				"item_list":  ChestRainItems,
			})
		}
	}
	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.QuestTrigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_FINISHORDER})
	player.TeLog("order_finish", map[string]interface{}{
		"order_id":          int(req.OrderId),
		"order_item_id":     mergeList,
		"order_type":        OrderType,
		"order_star_reward": Star,
	})
	EmitRetireTrigger2(player)
	Lv := player.GetPlayerBaseMod().GetLevel()
	EnergyMul := player.PlayMod.getBaseMod().GetEnergyMul()
	Emit := ChessMod.GetOrderEmit()
	OrderMod.CreateOrder(Lv, Emit, EnergyMul)

	// 存钱罐增加钻石
	PiggyBankMod := player.PlayMod.getPiggyBankMod()
	PiggyBankMod.Trigger()

	// 锦标赛增加积分
	if player.GetPlayerBaseMod().GetLevel() >= 6 {
		ChampshipMod := player.PlayMod.getChampshipMod()
		ChampshipMod.AddScore(mergeList)
		player.HandleInChampshipRank()
	}
	// playroom 触发式订单
	err = PlayroomOrderTrigger(player)
	if err != nil {
		player.SendErrClienRes(&msg.ResRewardOrder{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	FriendTreasureMod.AddStar(Star / 10)
	player.PushClientRes(FriendTreasureMod.NotifyStar())
	// 限时事件增加进度
	LimitedTimeEventMod.AddProgress(player.GetPlayerBaseMod().GetLevel())
	player.PlayMod.save()
	player.PushClientRes(PiggyBankMod.BackData())
	player.PushClientRes(OrderMod.BackData())
	BackChampship(player)
	player.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(LimitedTimeEventMod.ProgressBackData())
	player.PushClientRes(&msg.ResRewardOrder{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqDelOrder(player *Player, buf []byte) error {
	req := &msg.ReqDelOrder{}
	proto.Unmarshal(buf, req)
	OrderMod := player.PlayMod.getOrderMod()
	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.PushClientRes(OrderMod.BackData())
	return nil
}

// 场景装饰
func ReqDecorate(player *Player, buf []byte) error {
	req := &msg.ReqDecorate{}
	proto.Unmarshal(buf, req)
	DecorateMod := player.PlayMod.getDecorateMod()
	DecorateId := int(req.DecorateId)
	AreaId := int(req.AreaId)
	CostItem := DecorateMod.GetDecorateCostItem(AreaId, DecorateId)
	err := player.HandleItem(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
	}
	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)
	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": AreaId,
		})
	}
	player.TeLog("finish_deco", map[string]interface{}{
		"deco_step_id": DecorateId,
	})

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

// 场景装饰
func ReqDecorateAll(player *Player, buf []byte) error {
	req := &msg.ReqDecorateAll{}
	proto.Unmarshal(buf, req)
	DecorateMod := player.PlayMod.getDecorateMod()
	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()
	CostItem, AddItem, DecorateNum, DecorateList, Log := DecorateMod.DecorateAll(PlayerBaseMod.GetStar())
	err := player.HandleItem(CostItem, msg.ITEM_POP_LABEL_DecorateCost.String()) // 扣除道具
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	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)
	if err != nil {
		player.SendErrClienRes(&msg.ResDecorateAll{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
		return err
	}

	player.TeLog("one_click_decoration", map[string]interface{}{
		"deco_step_id": DecorateList,
	})

	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()
	BackUserInfo(player)
	player.UpdateUserInfo()
	return nil
}

// -----------------------ChessApi----------------------
// 更新棋盘信息
func UpdatePlayerChessDataFunc(player *Player, buf []byte) error {
	data := &PlayerChessData{}
	err := data.UpdatePlayerChessData(player, buf)
	if err != nil {
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "UpdatePlayerChessDataFunc",
		})
	}
	RedBackData(player)
	player.UpdateUserInfo()
	return err
}

// 从缓存中获取棋子
func ReqGetChessFromBuff(player *Player, buf []byte) error {
	req := &msg.ReqGetChessFromBuff{}
	proto.Unmarshal(buf, req)
	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqGetChessFromBuff",
		})
		return err
	}
	Update = OrderMod.CreateExtraOrder([]int{ChessId}, AddNewEmit, ChessMod.GetUnlockChessList(), BaseMod.GetEnergyMul())
	if Update {
		player.PushClientRes(OrderMod.BackData())
	}
	EmitRetireTrigger1(player)
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResGetChessFromBuff{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.UpdateUserInfo()
	return nil
}

// 放置棋子进背包
func ReqPutChessInBag(player *Player, buf []byte) error {
	req := &msg.ReqPutChessInBag{}
	proto.Unmarshal(buf, req)
	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqPutChessInBag",
		})
		return err
	}
	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{}
	proto.Unmarshal(buf, req)
	ChessMod := player.PlayMod.getChessMod()
	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqTakeChessOutBag",
		})
		return err
	}
	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{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	ChessMod := player.PlayMod.getChessMod()
	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqSeparateChess",
		})
		return err
	}
	player.PlayMod.save()
	player.PushClientRes(ChessMod.BackData())
	player.PushClientRes(&msg.ResSeparateChess{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("scissors_use", map[string]interface{}{
		"scissors_item_id": int(req.ChessId),
	})
	player.UpdateUserInfo()
	return nil
}

// 棋子转换
func ReqChessEx(player *Player, buf []byte) error {
	req := &msg.ReqChessEx{}
	proto.Unmarshal(buf, req)
	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
		}
	}

	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqChessEx",
		})
		return err
	}
	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{}
	proto.Unmarshal(buf, req)
	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(),
		})
		player.TeLog("outsync_event", map[string]interface{}{
			"outsync_event": "ReqSourceChest",
		})
		return err
	}
	player.PlayMod.save()
	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{}
	proto.Unmarshal(buf, req)
	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.PlayMod.save()
	player.PushClientRes(&msg.ResCardSeasonFirstReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 领取卡牌系列收集奖励
func ReqCardCollectReward(player *Player, buf []byte) error {
	req := &msg.ReqCardCollectReward{}
	proto.Unmarshal(buf, req)
	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
	}

	if chess != 0 {
		player.PlayMod.getChessMod().AddChessBuff(chess)
		player.PushClientRes(player.PlayMod.getOrderMod().BackData())
	}
	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{}
	proto.Unmarshal(buf, req)
	CardMod := player.PlayMod.getCardMod()
	itemList, 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.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
	}
	player.PlayMod.save()
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(&msg.ResAllCollectReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// ---------------------GuildApi----------------------
func ReqGuideReward(player *Player, buf []byte) error {
	req := &msg.ReqGuideReward{}
	proto.Unmarshal(buf, req)
	GuildMod := player.PlayMod.getGuildMod()
	itemList, err := GuildMod.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.PlayMod.save()
	player.PushClientRes(GuildMod.BackData())
	player.PushClientRes(&msg.ResGuideReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// -----------------dailyTaskApi---------------------
func ReqGetDailyTaskReward(player *Player, buf []byte) error {
	req := &msg.ReqGetDailyTaskReward{}
	proto.Unmarshal(buf, req)
	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": 0,
	})
	return nil
}

func ReqGetDailyWeekReward(player *Player, buf []byte) error {
	req := &msg.ReqGetDailyWeekReward{}
	proto.Unmarshal(buf, req)
	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.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{}
	proto.Unmarshal(buf, req)
	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 ReqSetAvatar(player *Player, buf []byte) error {
	req := &msg.ReqSetAvatar{}
	proto.Unmarshal(buf, req)
	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,
	})
	BackUserInfo(player)
	player.UpdateUserInfo()
	b := false
	if req.Avatar == 0 {
		b = true
	}
	player.TeLog("avatar_set", map[string]interface{}{
		"avatar_id":   req.Avatar,
		"is_platform": b,
	})
	return nil
}

// 头像
func ReqSetFace(player *Player, buf []byte) error {
	req := &msg.ReqSetFace{}
	proto.Unmarshal(buf, req)
	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,
	})
	BackUserInfo(player)
	player.UpdateUserInfo()
	return nil
}

// 购买能量
func ReqBuyEnergy(player *Player, buf []byte) error {
	req := &msg.ReqBuyEnergy{}
	proto.Unmarshal(buf, req)
	BaseMod := player.PlayMod.getBaseMod()
	Item, Energy, Diamond := BaseMod.BuyEnergy(int(req.Energy))
	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.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.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{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	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.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: int32(EndTime),
		Num:     int32(Times),
	})
	return nil
}

// 连击快手奖励
func ReqFastProduceReward(player *Player, buf []byte) error {
	req := &msg.ReqFastProduceReward{}
	proto.Unmarshal(buf, req)
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	itemList, EndTime, Times, 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.PushClientRes(LimitedTimeEventMod.BackData())
	player.PushClientRes(&msg.ResFastProduceReward{
		Code:    msg.RES_CODE_SUCCESS,
		EndTime: EndTime,
		Num:     int32(Times),
	})
	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.PlayMod.save()
	player.PushClientRes(&msg.ResLimitSenceReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

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

// 搜索用户
func ReqSearchPlayer(player *Player, buf []byte) error {
	req := &msg.ReqSearchPlayer{}
	proto.Unmarshal(buf, req)
	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
		}
		if player.PlayMod.getFriendMod().CheckFriend(int(v.DwUin)) {
			continue
		}
		if player.PlayMod.getFriendMod().CheckApply(int(v.DwUin)) {
			continue
		}
		SearchPlayer := G_GameLogicPtr.GetResSimplePlayerByUid(int(v.DwUin))
		list = append(list, SearchPlayer)

	}
	player.PushClientRes(&msg.ResSearchPlayer{
		List: list,
	})

	return nil
}

// 申请好友
func ReqApplyFriend(player *Player, buf []byte) error {
	req := &msg.ReqApplyFriend{}
	proto.Unmarshal(buf, req)
	Uid := int(req.Uid)
	FriendMod := player.PlayMod.getFriendMod()
	if FriendMod.CheckFriend(Uid) {
		player.SendErrClienRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "already friend",
		})
		return fmt.Errorf("already friend")
	}
	if FriendMod.AddSendApply(Uid) {
		player.SendErrClienRes(&msg.ResApplyFriend{
			Code: msg.RES_CODE_FAIL,
			Msg:  "already apply",
		})
		return fmt.Errorf("already apply")
	}
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_APPLY,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	player.PushClientRes(&msg.ResApplyFriend{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": Uid,
		"add_type":  "apply",
	})
	return nil
}

// 同意申请
func ReqAgreeFriend(player *Player, buf []byte) error {
	req := &msg.ReqAgreeFriend{}
	proto.Unmarshal(buf, req)
	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")
	}
	m := &MsqMod.Msg{
		Type:  MsqMod.HADNLE_TYPE_AGREE,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	FriendMod.AddFriend(Uid)
	player.PlayMod.save()
	player.PushClientRes(&msg.ResAgreeFriend{
		Code:   msg.RES_CODE_SUCCESS,
		Uid:    req.Uid,
		Player: G_GameLogicPtr.GetResSimplePlayerByUid(int(req.Uid)),
	})
	player.AddLog(Uid, friend.LOG_TYPE_FRIEND_BECOME, "")
	FriendApplyBackData(player)
	return nil
}

// 删除好友
func ReqDelFriend(player *Player, buf []byte) error {
	req := &msg.ReqDelFriend{}
	proto.Unmarshal(buf, req)
	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, "")
	FriendListBackData(player)
	player.TeLog("friend_delete", map[string]interface{}{
		"player_id": Uid,
	})
	return nil
}

// 拒绝申请
func ReqRefuseFriend(player *Player, buf []byte) error {
	req := &msg.ReqRefuseFriend{}
	proto.Unmarshal(buf, req)
	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,
	})
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_DEL,
		From:  int(player.M_DwUin),
		To:    Uid,
		SendT: GoUtil.Now(),
	}
	FriendMgrSend(m)
	FriendApplyBackData(player)
	return nil
}

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

	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")
	}
	err = player.SubCard(CardId)
	if err != nil {
		player.SendErrClienRes(&msg.ResCardGive{
			Code: msg.RES_CODE_FAIL,
			Msg:  err.Error(),
		})
	}
	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))
		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{}
	proto.Unmarshal(buf, req)
	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.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{}
	proto.Unmarshal(buf, req)
	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,
	})
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_REG_CARD_REFUSE,
		From:  int(player.M_DwUin),
		To:    CardInfo.AUid,
		SendT: GoUtil.Now(),
		Extra: *CardInfo,
	}
	FriendMgrSend(m)
	return nil
}

// 赠送卡牌
func ReqCardSend(player *Player, buf []byte) error {
	req := &msg.ReqCardSend{}
	proto.Unmarshal(buf, req)
	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
	}
	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))
	player.PushClientRes(&msg.ResCardSend{
		Code: msg.RES_CODE_SUCCESS,
	})
	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{}
	proto.Unmarshal(buf, req)
	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
	}
	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, "")

	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{}
	proto.Unmarshal(buf, req)
	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))
	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,
		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{}
	proto.Unmarshal(buf, req)
	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))
	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,
	})
	m := &MsqMod.Msg{
		Type:  MsqMod.HANDLE_TYPE_ARGREE_EX_CARD,
		From:  int(player.M_DwUin),
		To:    CardInfo.BUid,
		Extra: *CardInfo,
	}
	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{}
	proto.Unmarshal(buf, req)
	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,
	}
	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{}
	proto.Unmarshal(buf, req)
	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,
	}
	player.PushClientRes(CardMod.NotifyCard())
	player.PushClientRes(CardMod.NotifyTimes())
	FriendMgrSend(m)
	return nil
}

// 领取好友卡牌
func ReqGetFriendCard(player *Player, buf []byte) error {
	req := &msg.ReqRefuseCardExchange{}
	proto.Unmarshal(buf, req)
	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)
	// if CardInfo.ExId != 0 {
	// 	player.AddLog(CardInfo.BUid, friend.LOG_TYPE_CARD_EX_SUCCESS_2, fmt.Sprintf("%d_%d", CardInfo.ExId, CardInfo.CardId))
	// }
	player.PlayMod.save()
	player.PushClientRes(&msg.ResGetFriendCard{
		Code:   msg.RES_CODE_SUCCESS,
		Id:     req.Id,
		CardId: int32(CardInfo.CardId),
	})
	player.PushClientRes(CardMod.NotifyCard())
	return nil
}

// 请求玩家榜单
func ReqRank(player *Player, buf []byte) error {
	req := &msg.ReqRank{}
	proto.Unmarshal(buf, req)
	m := &MsqMod.Msg{
		From:  int(player.M_DwUin),
		Type:  MsqMod.HANDLE_TYPE_RANK_INFO,
		Extra: RankMsg{RankType: int(req.Type)},
	}
	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{}
	proto.Unmarshal(buf, req)
	m := G_GameLogicPtr.ChampshipMgr.GetRankMsg(int(player.M_DwUin))
	player.PushClientRes(m)
	return nil
}

func ReqChampshipPreRank(player *Player, buf []byte) error {
	req := &msg.ReqRank{}
	proto.Unmarshal(buf, req)
	m := G_GameLogicPtr.ChampshipMgr.GetPreRankMsg(int(player.M_DwUin))
	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{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	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.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{}
	proto.Unmarshal(buf, req)
	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.PushClientRes(ChargeMod.BackData())
	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{}
	proto.Unmarshal(buf, req)
	ChargeMod := player.PlayMod.getChargeMod()
	LostItem, Item, _, err := ChargeMod.BuyChess(int(req.Id))
	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":   Item[0].Num,
	})
	player.PlayMod.save()
	player.PushClientRes(ChargeMod.BackData())
	player.PushClientRes(&msg.ResBuyChessShop{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("buy_product_diamond", map[string]interface{}{
		"diamond_cost": Item[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{}
	proto.Unmarshal(buf, req)
	ChargeMod := player.PlayMod.getChargeMod()
	LostItem, _, ChessId, err := ChargeMod.BuyChess(int(req.Id))
	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.PushClientRes(ChargeMod.BackData())
	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.PushClientRes(ChargeMod.BackData())
	player.TeLog("refresh_store_diamond", map[string]interface{}{
		"diamond_cost": 20,
	})
	return nil
}

// 领取无尽礼包免费奖励
func ReqEndlessReward(player *Player, buf []byte) error {
	req := &msg.ReqEndlessReward{}
	proto.Unmarshal(buf, req)
	EndlessMod := player.PlayMod.getEndlessMod()
	itemList, err := EndlessMod.GetReward()
	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{}
	proto.Unmarshal(buf, req)
	OrderSn, err := CreateOrderSn(player, int(req.ChargeId), req.PlatForm, req.Channel)
	if err != nil {
		return err
	}
	player.PushClientRes(&msg.ResCreateOrderSn{
		OrderSn: OrderSn,
	})
	return nil
}

// 订单发货
func ReqShippingOrder(player *Player, buf []byte) error {
	req := &msg.ReqShippingOrder{}
	proto.Unmarshal(buf, req)

	go TriggerShippingOrder(player, req)
	player.PushClientRes(&msg.ResShippingOrder{
		Code: msg.RES_CODE_SUCCESS,
	})
	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()
	BackChampship(player)
	player.PushClientRes(&msg.ResChampshipReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	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{}
	proto.Unmarshal(buf, req)
	KvMod := player.PlayMod.getKvMod()
	KvMod.SetVar(int(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.FriendList {
			PlayerSimpleData := G_GameLogicPtr.GetSimplePlayerByUid(k)
			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 {
	FriendListBackData(player)
	return nil
}

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

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

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

func ReqChampshipRankReward(player *Player, buf []byte) error {
	MyLastRank := G_GameLogicPtr.ChampshipMgr.getLastMyRank(int(player.M_DwUin))
	ChampshipMod := player.PlayMod.getChampshipMod()
	itemList, err := ChampshipMod.GetRankReward(MyLastRank)
	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
	}
	player.PlayMod.save()
	BackChampship(player)
	player.PushClientRes(&msg.ResChampshipRankReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	player.TeLog("championship_reward", map[string]interface{}{
		"season_id":         GoUtil.ZeroTimestamp() - 86400,
		"champship_step_id": MyLastRank,
		"reward_type":       "rank",
		"item_list":         itemList,
		"champship_score":   ChampshipMod.Score,
	})
	return nil
}

func ReqSetName(player *Player, buf []byte) error {
	req := &msg.ReqSetName{}
	proto.Unmarshal(buf, req)
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetNickName(req.Name)
	player.PushClientRes(&msg.ResSetName{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	player.PlayMod.save()
	BackUserInfo(player)
	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{}
	proto.Unmarshal(buf, req)
	BaseMod := player.PlayMod.getBaseMod()
	BaseMod.SetNickName(req.Name)
	player.PushClientRes(&msg.ResSetPetName{
		ResultCode: msg.RES_CODE_SUCCESS,
	})
	player.PlayMod.save()
	BackUserInfo(player)
	player.UpdateUserInfo()
	player.TeLog("petname_set", map[string]interface{}{
		"petname": req.Name,
	})
	player.UpdateUserInfo()
	return nil
}

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

func ReqMasterCard(player *Player, buf []byte) error {
	req := &msg.ReqMasterCard{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
		Type: MsqMod.HANDLE_TYPE_INVITE_FRIEND,
		From: int(player.M_DwUin),
		To:   int(req.InviterId),
	})
	player.PushClientRes(&msg.ResSelfInvited{
		ResultCode: 1,
	})
	return nil
}

func ReqGetInviteReward(player *Player, buf []byte) error {
	req := &msg.ReqGetInviteReward{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	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,
	})
	G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
		From: int(player.M_DwUin),
		To:   Uid,
		Type: MsqMod.HANDLE_TYPE_INVITE_ADD_FRIEND,
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": req.Id,
		"add_type":  "invite add friend",
	})
	return nil
}

func ReqAutoAddInviteFriend2(player *Player, buf []byte) error {
	req := &msg.ReqAutoAddInviteFriend2{}
	proto.Unmarshal(buf, req)
	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,
	})
	G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
		From: int(player.M_DwUin),
		To:   Uid,
		Type: MsqMod.HANDLE_TYPE_INVITE_ADD_FRIEND,
	})
	player.TeLog("friend_add", map[string]interface{}{
		"player_id": req.Id,
		"add_type":  "invite add friend",
	})
	return nil
}

func ReqSelectLimitEvent(player *Player, buf []byte) error {
	req := &msg.ReqSelectLimitEvent{}
	proto.Unmarshal(buf, req)
	LimitedTimeEventMod := player.PlayMod.getLimitedTimeEventMod()
	Items, slot_reward, slot_order_number, err := LimitedTimeEventMod.SelectProgressReward(int(req.Id))
	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": slot_order_number,
		"slot_reward_list":  slot_reward,
		"slot_reward":       slot_reward[int(req.Id)],
	})
	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 {
	MiningBackData(player)
	return nil
}

// 请求挖矿
func ReqMiningTake(player *Player, buf []byte) error {
	req := &msg.ReqMiningTake{}
	proto.Unmarshal(buf, req)
	MiningMod := player.PlayMod.getMiningMod()
	ActivityInfo := GetActivityInfo(player, ACT_TYPE_MINING)
	Status := GetActivityStatus(player, 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.PlayMod.save()
	player.NotifyRed(ACT_TYPE_MINING)
	MiningBackData(player)
	return nil
}

// 领取关卡奖励
func ReqMiningReward(player *Player, buf []byte) error {
	MiningMod := player.PlayMod.getMiningMod()
	Status := GetActivityStatus(player, 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, 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.PlayMod.save()
	MiningBackData(player)
	return nil
}

// 请求猜颜色基础数据
func ReqGuessColor(player *Player, buf []byte) error {
	req := &msg.ReqGuessColor{}
	proto.Unmarshal(buf, req)
	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()
	GuessColorBackData(player)
	return nil
}

// 请求猜颜色
func ReqGuessColorTake(player *Player, buf []byte) error {
	req := &msg.ReqGuessColorTake{}
	proto.Unmarshal(buf, req)
	Status := GetActivityStatus(player, 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
	}
	GuessColorMod.Take(req.Map)
	player.PlayMod.save()
	GuessColorBackData(player)
	player.NotifyRed(ACT_TYPE_GUESS_COLOR)
	return nil
}

// 请求猜颜色奖励
func ReqGuessColorReward(player *Player, buf []byte) error {
	req := &msg.ReqGuessColorReward{}
	proto.Unmarshal(buf, req)
	Status := GetActivityStatus(player, 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.PlayMod.save()
	GuessColorBackData(player)
	return nil
}

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

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

// 领取竞赛奖励
func ReqRaceReward(player *Player, buf []byte) error {
	req := &msg.ResRaceReward{}
	proto.Unmarshal(buf, req)
	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
	}
	RaceBackData(player)
	player.PushClientRes(&msg.ResRaceReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

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

// 请求playroom拜访信息
func ReqPlayroomInfo(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomInfo{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Targer := int(req.Uid)
	if req.Uid == 0 {
		Targer = PlayroomMod.GetTarget()
	}
	if req.Uid == -1 {
		Targer = GetVisitorPlayer(player)
	}
	if Targer == 0 {
		return fmt.Errorf("ReqPlayroomInfo no target")
	}
	PlayerData := G_GameLogicPtr.GetSimplePlayerByUid(Targer)
	PlayroomMod.SetTarget(Targer)
	if PlayerData.Loginout > 0 && PlayerData.Loginout < GoUtil.Now()-int64(playroomCfg.GetGameOutline()) { // todo DEBUG
		log.Debug("ReqPlayroomInfo PlayerData.Loginout:%v, %v", PlayerData.Loginout, playroomCfg.GetGameOutline())
		PlayroomMod.SetGameId(1)
		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 {
				RandList = append(RandList, v)
			}
		}
		if len(RandList) == 0 {
			PlayroomMod.SetGameReward(0, 0, Star)
		} else if len(RandList) == 1 {
			PlayroomMod.SetGameReward(0, RandList[0], Star)
		} else {
			ChessList := GoUtil.RandSliceNum(RandList, 2)
			PlayroomMod.SetGameReward(ChessList[0], ChessList[1], Star)
		}
	} else {
		PlayroomMod.SetGameId(2)
		PlayroomMod.SetGameRewardFlip(int(float64(PlayerData.Star)*0.03), int(float64(PlayerData.Star)*0.05), int(float64(PlayerData.Star)*0.1))
	}
	player.PlayMod.save()
	PlayroomVisit(player, Targer)
	return nil
}

// 请求playroom交互
func ReqPlayroomInteract(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomInteract{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, _, 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
	}
	player.PlayMod.save()
	LimitedTimePlayroomTrigger(player)
	player.PushClientRes(PlayroomMod.NotifyMood())
	player.PushClientRes(&msg.ResPlayroomInteract{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 请求playroom设置房间
func ReqPlayroomSetRoom(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomSetRoom{}
	proto.Unmarshal(buf, req)
	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.PlayMod.save()
	PlayroomBackData(player)
	player.PushClientRes(&msg.ResPlayroomSetRoom{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 玩游戏
func ReqPlayroomGame(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomGame{}
	proto.Unmarshal(buf, req)
	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")
	}
	PlayerData := G_GameLogicPtr.GetSimplePlayerByUid(Target)
	Items := make([]*item.Item, 0)
	if req.Type == playroom.GAME_RESULT_LOW {
		Items = append(Items, item.NewItem(item.ITEM_STAR_ID, PlayerData.Level*2))
		PlayroomMod.ResetGame()
	}
	if req.Type == playroom.GAME_RESULT_MIDDLE {
		Items = append(Items, item.NewItem(item.ITEM_STAR_ID, PlayerData.Level*3))
		PlayroomMod.ResetGame()
	}

	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
	}
	player.PlayMod.save()
	// PlayroomBackData(player)
	// PlayroomVisit(player, PlayroomMod.GetTarget())
	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.PushClientRes(&msg.ResPlayroomGame{
		Code:  msg.RES_CODE_SUCCESS,
		Type:  req.Type,
		Items: ItemsMsg,
	})
	return nil
}

// 选择奖励
func ReqPlayroomSelectReward(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomSelectReward{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items := PlayroomMod.SelectReward(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
	}
	Target := PlayroomMod.GetTarget()
	G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Target,
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_LOSE,
		SendT: GoUtil.Now(),
		Extra: Items,
	})
	PlayroomMod.ResetGame()
	player.PlayMod.save()
	PlayroomBackData(player)
	PlayroomVisit(player, PlayroomMod.GetTarget())
	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{}
	proto.Unmarshal(buf, req)
	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.PlayMod.save()
	player.PushClientRes(PlayroomMod.NotifyWork())
	// PlayroomBackData(player)
	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.PushClientRes(PlayroomMod.NotifyWork())
	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()
	// PlayroomBackData(player)
	player.PushClientRes(PlayroomMod.NotifyMood())
	player.PushClientRes(&msg.ResPlayroomDraw{
		Code: msg.RES_CODE_SUCCESS,
		Id:   int32(Id),
	})
	return nil
}

// 清理碎片
func ReqPlayroomChip(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomChip{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items := PlayroomMod.RemoveChip(int(req.Num))
	err := player.HandleItem(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.PlayMod.save()
	PlayroomBackData(player)
	player.PushClientRes(&msg.ResPlayroomChip{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

// 翻牌游戏
func ReqPlayroomFlip(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomFlip{}
	proto.Unmarshal(buf, req)
	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.Kafka("playroom_flip", map[string]interface{}{
		"CardID": CardId,
		"Id":     req.Id,
	})
	player.PlayMod.save()
	PlayroomBackData(player)
	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{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	Items, Target, 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) {
		Items1 = item.Merge(Items1, Items1)
	}
	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
	}
	G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
		From:  int(player.M_DwUin),
		To:    Target,
		Type:  MsqMod.HANDLE_TYPE_PLAYROOM_LOSE,
		SendT: GoUtil.Now(),
		Extra: Items,
	})
	player.PlayMod.save()
	PlayroomBackData(player)
	player.PushClientRes(&msg.ResPlayroomFlipReward{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqPlayroomBuyItem(player *Player, buf []byte) error {
	req := &msg.ReqPlayroomBuyItem{}
	proto.Unmarshal(buf, req)
	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.PlayMod.save()
	// PlayroomBackData(player)
	player.PushClientRes(&msg.ResPlayroomBuyItem{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

func ReqOfflineReconnectFunc(a gate.Agent, buf []byte) error {
	req := &msg.ReqOfflineReconnect{}
	proto.Unmarshal(buf, req)
	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)
		}
		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{}
	proto.Unmarshal(buf, req)
	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{}
	proto.Unmarshal(buf, req)
	PlayroomMod := player.PlayMod.getPlayroomMod()
	AddItems, LoseItem, err := PlayroomMod.ShopBuy(int(req.Id), int(req.Num))
	if err != nil {
		player.SendErrClienRes(&msg.ResPlayroomShop{
			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.PlayMod.save()
	PlayroomBackData(player)
	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{}
	proto.Unmarshal(buf, req)
	// 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")
	}

	// FriendList := FriendMod.GetFriendList()
	// FriendList2 := GoUtil.SubSlices(FriendList, FriendTreasureMod.GetFriendList())
	// if len(FriendList2) < 5 {
	// 	player.SendErrClienRes(&msg.ResFriendTreasureStart{
	// 		Code: msg.RES_CODE_FAIL,
	// 		Msg:  "not enough friends",
	// 	})
	// 	return fmt.Errorf("not enough friends")
	// }

	// Alive := make([]int, 0)
	// NotAlive := make([]int, 0)
	// Now := GoUtil.Now()
	// for _, v := range FriendList2 {
	// 	PD := G_GameLogicPtr.GetSimplePlayerByUid(v)
	// 	if PD.Login < Now-86400 {
	// 		NotAlive = append(NotAlive, v)
	// 	} else {
	// 		Alive = append(Alive, v)
	// 	}
	// }

	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{}
	proto.Unmarshal(buf, req)
	FriendTreasureMod := player.PlayMod.getFriendTreasureMod()
	Items, err := FriendTreasureMod.Flip(int(req.Pos))
	if err != nil {
		player.SendErrClienRes(
			&msg.ResFriendTreasureFilp{
				Code: msg.RES_CODE_FAIL,
				Msg:  err.Error(),
			},
		)
		return err
	}

	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.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
	}
	for _, v := range FriendTreasureMod.List {
		if v.Status != 1 {
			continue
		}
		G_GameLogicPtr.FriendMgrSend(&MsqMod.Msg{
			From:  int(player.M_DwUin),
			To:    int(v.Uid),
			Type:  MsqMod.FRIEND_TREASURE_HANDLE,
			SendT: GoUtil.Now(),
			Extra: []*item.Item{item.NewItem(item.ITEM_STAR_ID, FriendItemNum)},
		})
	}
	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
	}
	player.PlayMod.save()
	player.PushClientRes(&msg.ResFriendTreasureEnd{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}

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

func ReqCardHandbookReward(player *Player, buf []byte) error {
	req := &msg.ReqCardHandbookReward{}
	proto.Unmarshal(buf, req)
	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.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{}
	proto.Unmarshal(buf, req)
	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.PlayMod.save()
	player.PushClientRes(&msg.ResSetEmoji{
		Code: msg.RES_CODE_SUCCESS,
	})
	return nil
}
