package models

import (
	"context"
	"fmt"
	mhayaString "github.com/mhaya/extend/string"
	mhayaTime "github.com/mhaya/extend/time"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/internal/param"
	"github.com/mhaya/game/game_cluster/internal/third"
	"github.com/mhaya/game/game_cluster/internal/token"
	mhayaLogger "github.com/mhaya/logger"
	"math/rand"
	"strings"
	"time"
)

type Player struct {
	UserName          string `json:"userName" bson:"userName"`                   // 用户名
	OpenId            string `json:"openId" bson:"openId"`                       // 对应tgid
	UserNameMaybe     string `json:"userNameMaybe" bson:"userNameMaybe"`         // 昵称
	NickName          string `json:"nickName" bson:"nickName"`                   // 昵称
	Pid               string `json:"pid" bson:"pid"`                             // 上级ID
	XID               string `json:"xID" bson:"xID"`                             // 绑定推特ID
	TonWall           string `json:"tonWall" bson:"tonWall"`                     // 绑定钱包地址
	Email             string `json:"email" bson:"email"`                         // 邮箱
	Mobile            string `json:"mobile" bson:"mobile"`                       // 手机号
	Avatar            string `json:"avatar" bson:"avatar"`                       // 头像
	Password          string `json:"-" bson:"password"`                          //账户密码
	Salt              string `json:"-" bson:"salt"`                              //密码盐
	IsSetPwd          bool   `json:"isSetPwd" bson:"-"`                          //是否设置密码
	PwdErrTimes       int    `json:"pwdErrTimes" bson:"pwdErrTimes"`             //密码错误次数
	PwdErrRefreshTime int64  `json:"pwdErrRefreshTime" bson:"pwdErrRefreshTime"` //密码到期时间

	IsCashOut         int                       `json:"-" bson:"IsCashOut"`                         //服务器判断是否有提现的数据 0 否 1是
	IsRobot           int                       `json:"isRobot" bson:"isRobot"`                     // 是否为机器人
	IsLeader          int                       `json:"isLeader" bson:"isLeader"`                   //  是否是社区长
	IsVip             int                       `json:"isVip" bson:"isVip"`                         // 是否为VIP
	IsFirstBindingXID int                       `json:"isFirstBindingXID" bson:"isFirstBindingXID"` // 是否是第一次绑定推特
	Level             int                       `json:"level" bson:"level"`                         // 等级
	Exp               int                       `json:"exp" bson:"exp"`                             // 经验
	Gender            int                       `json:"gender" bson:"gender"`                       // 性别
	Birthday          string                    `json:"birthday" bson:"birthday"`                   // 生日
	Successions       int                       `json:"successions" bson:"successions"`             // 连续登陆天数
	MaxSuccessions    int                       `json:"maxSuccessions" bson:"maxSuccessions"`       // 最大连续等级天数
	PrevTime          int64                     `json:"prevTime" bson:"prevTime"`                   // 上次登陆时间
	LoginTime         int64                     `json:"loginTime" bson:"loginTime"`                 // 登陆时间
	LoginIP           string                    `json:"loginIP" bson:"loginIP"`                     // 登录IP
	LoginFailure      int                       `json:"loginFailure" bson:"loginFailure"`           // 登陆失败次数
	JoinIP            string                    `json:"joinIP" bson:"joinIP"`                       // 注册IP
	Rank              int                       `json:"rank" bson:"rank"`                           // 排名
	JoinTime          int64                     `json:"joinTime" bson:"joinTime"`                   // 注册时间
	CreateTime        int64                     `json:"createTime" bson:"createTime"`               // 创建时间
	UpdateTime        int64                     `json:"updateTime" bson:"updateTime"`               // 更新时间
	DailyRefreshTime  int64                     `json:"dailyRefreshTime" bson:"dailyRefreshTime"`   // 每日刷新时间
	HourRefreshTime   int64                     `json:"hourRefreshTime" bson:"hourRefreshTime"`     // 每小时更新
	WeeklyRefreshTime int64                     `json:"weeklyRefreshTime" bson:"weeklyRefreshTime"` // 每周刷新时间
	RollDay           int64                     `json:"rollDay" bson:"rollDay"`                     // 抽奖日期
	Status            int                       `json:"status" bson:"status"`                       // 状态
	Roll              *Roll                     `json:"roll" bson:"roll"`                           // 抽奖
	Guide             int                       `json:"guide" bson:"guide"`                         //
	Share             *Share                    `json:"share" bson:"share"`                         // 分享
	FirstReward       int                       `json:"firstReward" bson:"firstReward"`             // 首次登陆奖励
	Index             int                       `json:"-" bson:"index"`                             // 索引
	Item              ItemBasePack              `json:"item" bson:"item"`                           // 道具
	SinIn             SignIn                    `json:"sinIn" bson:"sinIn"`                         // 签到
	DailyTask         DailyTask                 `json:"dailyTask" bson:"dailyTask"`                 // 日常任务
	AchieveTask       AchieveTask               `json:"achieveTask" bson:"achieveTask"`             // 成就任务
	RankReward        RankReward                `json:"rankReward" bson:"rankReward"`               // 排行奖励
	Invite            Invite                    `json:"invite" bson:"invite"`                       // 邀请
	InviteReward      *InviteReward             `json:"inviteReward" bson:"inviteReward"`           // 邀请奖励
	PlayerReward      map[int]*PlayerRewardBase `json:"-" bson:"-"`
}

func NewPlayer(token *token.Token) *Player {
	return &Player{
		UserName:       token.PlayerID,
		OpenId:         token.OpenID,
		NickName:       token.Nickname,
		IsRobot:        0,
		Successions:    1,
		MaxSuccessions: 1,
		LoginTime:      mhayaTime.Now().Unix(),
		CreateTime:     mhayaTime.Now().Unix(),
		UpdateTime:     mhayaTime.Now().Unix(),
		RollDay:        mhayaTime.Now().Unix(),
		Exp:            0,
		Status:         1,
		Rank:           0,
		Item:           NewItem(),
		Guide:          1,
		Level:          1,
		Index:          1,
		FirstReward:    1,
		Roll:           &Roll{RollNums: 0, RollDays: 0},
		DailyTask:      NewDailyTask(),
		Share:          &Share{IsDrawShare: 0},
		SinIn:          NewSignIn(),
		AchieveTask:    NewAchieveTask(),
		RankReward:     NewRankReward(),
		Invite:         NewInvite(),
	}
}

func NewBotPlayer(token *token.Token) *Player {
	return &Player{
		UserName:       token.PlayerID,
		OpenId:         token.OpenID,
		NickName:       token.Nickname,
		IsRobot:        1,
		Successions:    1,
		MaxSuccessions: 1,
		LoginTime:      mhayaTime.Now().Unix(),
		CreateTime:     mhayaTime.Now().Unix(),
		UpdateTime:     mhayaTime.Now().Unix(),
		RollDay:        mhayaTime.Now().Unix(),
		Exp:            0,
		Status:         1,
		Rank:           0,
		Item:           NewItem(),
		Guide:          1,
		Level:          1,
		Index:          1,
		FirstReward:    1,
		Roll:           &Roll{RollNums: 0, RollDays: 0},
		DailyTask:      NewDailyTask(),
		Share:          &Share{IsDrawShare: 0},
		SinIn:          NewSignIn(),
		AchieveTask:    NewAchieveTask(),
		RankReward:     NewRankReward(),
		Invite:         NewInvite(),
	}
}
func (p *Player) Init() {
	if p.InviteReward == nil {
		p.InviteReward = &InviteReward{IsClaim: 0}
	}
}

// InitDaily 每日刷新
func (p *Player) InitDaily() {

	if !mhayaTime.CreateFromTimestamp(p.DailyRefreshTime).IsToday() {
		//刷新每日任务
		p.DailyTask.RefreshDailyTask()
		//刷新抽奖每日分享
		p.Share.RefreshShare()
		//刷新是否有排行榜奖励
		p.RankReward.InitRank(p.UserName, 1)
		//更新时间到当天
		p.DailyRefreshTime = mhayaTime.Now().Unix()
	}
}

func (p *Player) InitWeekly() {
	if !mhayaTime.CreateFromTimestamp(p.WeeklyRefreshTime).IsThisWeek() {

		p.SinIn.RefreshSignIn()
		// 刷新是否有排行榜奖励
		p.RankReward.InitRank(p.UserName, 2)

		p.WeeklyRefreshTime = mhayaTime.Now().Unix()
	}
}

func (p *Player) InitAchieveTask() {
	p.AchieveTask.RefreshAchieveTask()
}

func (p *Player) LevelUp() bool {
	ret, ok := data.LevelConfig.Get(p.Level + 1)
	if !ok {
		return false
	}
	curLevel := p.Level
	if p.Item[ItemRoll].CurNum >= ret.DiceNum && p.Item[ItemInvite].CurNum >= ret.Invite {
		p.Level += 1
		if p.Level > curLevel {
			p.Item[ItemRoll].AddMaxNum(ret.DiceLimit)
			p.Item[ItemRoll].CurNum -= ret.DiceNum
			p.Item[ItemInvite].CurNum -= ret.Invite

			go p.SetPassiveInviteReward()

			//添加日志
			mhayaLogger.Infof("player level up, playerName=%v,level=%v", p.UserName, p.Level)
			p.SetPlayerRewardLog(SourceLevelUp, p.Level, []data.ItemReward{{ItemID: ItemAllRoll, ItemBaseType: 2, Amount: ret.DiceLimit}}, []data.ItemReward{}, p.Level)
			return true
		}
	}

	return false
}

func (p *Player) RefreshRoll() bool {
	ret, ok := data.DiscreteRuleConfig.Get(RefreshRoll)
	if !ok {
		return false
	}
	arr := strings.Split(ret.Condition, ",")
	if len(arr) != 2 {
		return false
	}
	h, _ := mhayaString.ToInt64(arr[0])
	m, _ := mhayaString.ToInt64(arr[1])
	s := mhayaTime.CreateFromTimestamp(p.RollDay).DiffInSeconds(mhayaTime.Now())
	if s >= h {
		p.RollDay = mhayaTime.Now().Unix()
		num := p.Item[ItemRoll].AddAll()
		if num > 0 {
			p.SetPlayerRewardLog(SourceRefreshRoll, 1, []data.ItemReward{{ItemID: ItemRoll, ItemBaseType: 1, Amount: num}}, []data.ItemReward{}, 0)
		}
		mhayaLogger.Infof("RefreshRoll by condition 1 rollDay:%d,num:%v", s, num)
		return true
	} else if s >= m {
		p.RollDay = mhayaTime.Now().Unix()
		num := p.Item[ItemRoll].AddSame()
		if num > 0 {
			p.SetPlayerRewardLog(SourceRefreshRoll, 2, []data.ItemReward{{ItemID: ItemRoll, ItemBaseType: 1, Amount: num}}, []data.ItemReward{}, 0)
		}
		mhayaLogger.Infof("RefreshRoll by condition 2 rollDay:%d,num:%v", s, num)
		return true
	}
	return false
}

func (p *Player) CheckTaskByType1(openid int64, id int) bool {

	ret := data.AchieveTaskConfig.GetAll()
	for _, v := range ret {
		if v.Type == 2 {
			continue
		}
		if v.ID == id {
			if v.ID == 2 {
				if third.GetIfChannel(openid) == 1 {
					return true
				} else {
					return false
				}
			} else {
				return true
			}
		}
	}
	return false
}

func (p *Player) FinishAchieveTaskByType1(id int) bool {
	if a, ok := p.AchieveTask[id]; ok {
		if a.Num == 0 && a.Validate == 1 { //待验证
			openid, ok1 := mhayaString.ToInt64(p.OpenId)
			if !ok1 {
				mhayaLogger.Warnf("FinishAchieveTaskByType1 验证失败 username :%v,  id : %v", p.UserName, id)
				return false
			}
			if p.CheckTaskByType1(openid, id) {
				p.AchieveTask[id].Validate = 2 //验证成功
				p.AchieveTask[id].Num = 1
				p.AchieveTask[id].Status = 1
				p.AchieveTask[id].RewardNum = 1

				//处理邀请订阅顺序问题
				if id == 2 {
					go p.SetPassiveInviteReward()
				}

			} else {
				p.AchieveTask[id].Validate = 0 //验证成功
				p.AchieveTask[id].Status = 0
				mhayaLogger.Warnf("FinishAchieveTaskByType1 验证失败 username :%v,  id : %v", p.UserName, id)
				return false
			}

		} else {
			if a.Validate == 0 {
				p.AchieveTask[id].Validate = 1
			}
		}
		return true
	}
	return false
}

// FinishAchieveTaskByType2 完成成就类型2
func (p *Player) FinishAchieveTaskByType2() bool {
	//var range data.RangeInt32{}
	for _, v := range p.AchieveTask {
		if v.Type == 2 {
			ret, ok := data.AchieveTaskConfig.Get(v.ID)
			if !ok {
				continue
			}
			if p.Item[ItemInvite].Num <= ret.Condition.Max && p.AchieveTask[v.ID].Num != ret.Condition.Max {
				p.AchieveTask[v.ID].Num += 1
				if p.AchieveTask[v.ID].Num >= ret.Condition.Min && p.AchieveTask[v.ID].Num <= ret.Condition.Max {
					p.AchieveTask[v.ID].Status = 1
					p.AchieveTask[v.ID].RewardNum += 1
				}
			}
		}
	}
	return false
}

func (p *Player) FinishDailyTask(tp, num int) bool {
	var f bool
	for i := 0; i < num; i++ {
		for _, v := range p.DailyTask {
			if v.Type == tp && v.Status == 0 {
				p.DailyTask[v.ID].Num += 1
				if p.DailyTask[v.ID].Num == v.Condition {
					p.DailyTask[v.ID].Status = 1
					f = true
					continue
				}
			}
		}
	}

	return f
}

func (p *Player) RollOnce(num int) *param.RollOnce {
	var roll param.RollOnce
	levelConfig, ok := data.LevelConfig.Get(p.Level)
	if !ok {
		return nil
	}

	if levelConfig == nil {
		return nil
	}
	r1 := rand.Int31n(6) + 1
	r2 := rand.Int31n(6) + 1
	r3 := rand.Int31n(6) + 1

	//rule 4
	if (r1 == 1 && r2 == 1 && r3 == 1) || (r1 == 6 && r2 == 6 && r3 == 6) {
		roll.Score = levelConfig.Ratio * levelConfig.Rule4
		roll.RollType = constant.RollSpecialTripleBet
	} else if r1 == r2 && r2 == r3 {
		roll.Score = levelConfig.Ratio * levelConfig.Rule3
		roll.RollType = constant.RollTripleBet
	} else if r1 == r2 || r1 == r3 || r2 == r3 {
		roll.Score = levelConfig.Ratio * levelConfig.Rule2
		roll.RollType = constant.RollDoubleBet
	} else {
		roll.Score = levelConfig.Ratio * levelConfig.Rule1
		if r1+r2+r3 > 9 {
			roll.RollType = constant.RollBigBet
		} else {
			roll.RollType = constant.RollSmallBet
		}
	}

	roll.Rand = make(map[int]int32)
	roll.Rand[1] = r1
	roll.Rand[2] = r2
	roll.Rand[3] = r3

	p.Item[ItemScore].Add(roll.Score * num)
	p.Item[ItemRoll].Sub(1 * num)

	p.SetPlayerRewardLog(SourceRoll, 0, []data.ItemReward{{ItemID: ItemScore, ItemBaseType: 1, Amount: roll.Score * num}}, []data.ItemReward{{ItemID: ItemRoll, ItemBaseType: 1, Amount: 1 * num}}, roll)

	p.Roll.RollNums += 1 * num
	roll.IsDailyTask = p.FinishDailyTask(1, num)
	roll.IsLevelUp = p.LevelUp()

	return &roll
}

// Draw 最多尝试重复执行50次
func (p *Player) Draw() *param.Draw {
	for i := 0; i < 50; i++ {
		draw, ok := p.DrawOnce()
		if ok {
			return draw
		}
	}
	return nil
}

func (p *Player) NewPlayer10Draw() {
	if p.Roll.DrawTotal == 0 {
		ret, ok := data.DiscreteRuleConfig.Get(NewPlayer10Draw)
		if !ok {
			return
		}
		arr := strings.Split(ret.Condition, ",")
		if len(arr) != 2 {
			return
		}

		minC := mhayaString.ToIntD(arr[0], 4)
		MaxC := mhayaString.ToIntD(arr[1], 6)

		p.Roll.NewDraw = make(map[int]int)
		wei := []int{1, 2, 4, 5, 11, 12}
		wei2 := []int{1, 2, 4, 12}

		var m []int
		for i := 0; i < minC; i++ {
			r := rand.Intn(2)
			if r == 0 {
				m = append(m, 5)
			} else {
				m = append(m, 11)
			}
		}
		var num = len(m)
		var num2 = 0
		for i := 0; i < 10-minC; i++ {
			if num2 == 0 {
				if num >= MaxC {
					k2 := rand.Intn(len(wei2))
					cur := wei2[k2]
					if cur == 11 || cur == 5 {
						num++
					}
					if cur == 4 || cur == 12 {
						num2++
					}
					m = append(m, cur)
				} else {
					k := rand.Intn(len(wei))
					cur := wei[k]
					if cur == 11 || cur == 5 {
						num++
					}
					if cur == 4 || cur == 12 {
						num2++
					}
					m = append(m, cur)
				}
			} else {
				if num >= MaxC-1 {
					k2 := rand.Intn(len(wei2))
					cur := wei2[k2]
					if cur == 11 || cur == 5 {
						num++
					}
					if cur == 4 || cur == 12 {
						num2++
					}
					m = append(m, cur)
				} else {
					k := rand.Intn(len(wei))
					cur := wei[k]
					if cur == 11 || cur == 5 {
						num++
					}
					if cur == 4 || cur == 12 {
						num2++
					}
					m = append(m, cur)
				}
			}
		}
		m = Shuffle(m)
		for key, v := range m {
			p.Roll.NewDraw[key+1] = v
		}
	}
}

func Shuffle(slice []int) []int {
	rand.Seed(time.Now().UnixNano())
	for i := len(slice) - 1; i > 0; i-- {
		j := rand.Intn(i + 1)
		slice[i], slice[j] = slice[j], slice[i]
	}
	return slice
}

func (p *Player) DrawOnce() (*param.Draw, bool) {
	var ret map[int]*data.DrawConfigRow
	var draw param.Draw
	var wg = 0

	p.NewPlayer10Draw()

	if p.Roll.DrawTotal < 10 {
		ret = data.DrawConfig.GetByType(1)
		for _, v := range ret {
			wg += v.Weight
		}
	} else {
		ret = data.DrawConfig.GetByType(2)
		for _, v := range ret {
			wg += v.Weight
		}
	}

	if wg == 0 {
		return nil, false
	}
	var cur int
	var curID int

	//出去新手10连后规则
	if p.Roll.DrawTotal >= 10 {
		weight := int(rand.Int31n(int32(wg))) + 1
		for _, v := range ret {
			if v.Weight == 0 {
				continue
			}

			if weight > cur && weight <= cur+v.Weight {
				curID = v.ID
				break
			}
			cur += v.Weight
		}
	} else {
		curID = p.Roll.NewDraw[p.Roll.DrawTotal+1]
	}

	if d, ok := ret[curID]; ok {
		if d.HourTotalCondition > 0 {
			i, err := GetDrawHour(curID)
			if err != nil {
				return nil, false
			}
			if int(i) > d.HourTotalCondition {
				return nil, false
			}
		}

		if d.DailyTotalCondition > 0 {
			i, err := GetDrawDailyKey(curID)
			if err != nil {
				return nil, false
			}
			if int(i) > d.DailyTotalCondition {
				return nil, false
			}
		}

		if d.WeeklyTotalCondition > 0 {
			i, err := GetDrawWeeklyKey(curID)
			if err != nil {
				return nil, false
			}
			if int(i) > d.WeeklyTotalCondition {
				return nil, false
			}
		}
		p.Item.AddItem(d.Reward)
		p.Item[ItemDrawsNumber].Sub(1)
		draw.Reward = d.Reward
		draw.ID = d.Order
		draw.CurID = curID
		if len(p.Roll.Draw) == 0 {
			p.Roll.Draw = make(map[int]int)
		}
		if len(p.Roll.DrawRatio) == 0 {
			p.Roll.DrawRatio = make(map[int]float64)
		}

		p.Roll.Draw[curID] += 1
		p.Roll.DrawTotal += 1

		p.Roll.DrawRatio[curID] = float64(p.Roll.Draw[curID]) / float64(p.Roll.DrawTotal)

		p.SetPlayerRewardLog(SourceDraw, 0, draw.Reward, []data.ItemReward{{1, ItemDrawsNumber, 1}}, draw)
	} else {
		return nil, false
	}
	return &draw, true
}

func GetDrawHour(id int) (int64, error) {
	s := mhayaTime.CreateFromTime(mhayaTime.Now().Hour(), 0, 0).ToDateTimeFormat()
	key := fmt.Sprintf("%v:%v:%v", constant.DrawHourKey, id, s)
	return mdb.RDB.IncrBy(context.Background(), key, 1).Result()
}

func GetDrawDailyKey(id int) (int64, error) {
	key := fmt.Sprintf("%v:%v:%v", constant.DrawDailyKey, id, mhayaTime.Now().Format(mhayaTime.DateFormat))
	return mdb.RDB.IncrBy(context.Background(), key, 1).Result()
}

func GetDrawWeeklyKey(id int) (int64, error) {
	key := fmt.Sprintf("%v:%v:%v", constant.DrawWeeklyKey, id, mhayaTime.Now().Week())
	return mdb.RDB.IncrBy(context.Background(), key, 1).Result()
}

func (p *Player) SignIn() *param.ChangeData {
	var sign param.ChangeData
	p.SinIn.SignDailyTime = mhayaTime.Now().Unix()
	p.SinIn.Sign[p.SinIn.Num].IsSign = 1
	p.SinIn.Sign[p.SinIn.Num].SignTime = mhayaTime.Now().Unix()
	var addItem AddItem
	at := addItem.Add(p.SinIn.Sign[p.SinIn.Num].Reward)
	sign.AddItem = at
	p.Item.AddItem(p.SinIn.Sign[p.SinIn.Num].Reward)
	p.SinIn.Num += 1

	p.SetPlayerRewardLog(SourceSignIn, p.SinIn.Num, p.SinIn.Sign[p.SinIn.Num].Reward, []data.ItemReward{}, nil)

	sign.IsLevelUp = p.LevelUp()
	return &sign
}

func (p *Player) SetPlayerRewardLog(tp, tid int, addReward, subReward []data.ItemReward, desc interface{}) {
	p.Index += 1
	if len(p.PlayerReward) == 0 {
		p.PlayerReward = make(map[int]*PlayerRewardBase)
	}
	p.PlayerReward[p.Index] = &PlayerRewardBase{
		UserName:   p.UserName,
		AddReward:  addReward,
		SubReward:  subReward,
		Desc:       desc,
		Source:     tp,
		SourceId:   tid,
		CreateTime: mhayaTime.Now().Unix(),
	}

}

func (p *Player) SetPassiveInviteReward() {

	ret2, ok := data.DiscreteRuleConfig.Get(InvitePlayer)
	c, tok := mhayaString.ToInt(ret2.Condition)

	if !ok || !tok || c != p.Level {
		return
	}

	ret3, ok := data.DiscreteRuleConfig.Get(InviteVipPlayer)
	c, tok = mhayaString.ToInt(ret3.Condition)

	if !ok || !tok || c != p.Level {
		return
	}
	var isCheck bool
	if ts, ok := p.AchieveTask[2]; ok {
		if ts.Num == 1 {
			isCheck = true
		}
	}

	if !isCheck {
		// 升级邀请奖励
		openid, _ := mhayaString.ToInt64(p.OpenId)
		if third.GetIfChannel(openid) == 0 {
			mhayaLogger.Infof("player SetPassiveInviteReward,Not following the channel  playerName=%v,level=%v,pid=%v", p.UserName, p.Level, p.Pid)
			return
		}
	}

	//被邀请人
	key := fmt.Sprintf("%v:1:%v", constant.InviteKey, p.UserName)

	//获取邀请人
	pUid := mdb.RDB.Get(context.Background(), key).Val()

	/*	pbase := mdb.RDB.HGet(context.Background(), constant.PlayerBaseKey, pUid).Val()
		mdb.RDB.Del(context.Background(), key)
		var base param.PlayerBase
		err := jsoniter.Unmarshal([]byte(pbase), &base)
		if err != nil {
			return
		}*/

	var addItem []data.ItemReward
	if p.IsVip == 0 {
		//主动邀请记录, 设置邀请人数量+1
		key = fmt.Sprintf("%v:%v:%v", constant.InviteKey, InvitePlayer, pUid)
		err := mdb.RDB.IncrBy(context.Background(), key, 1).Err()
		if err != nil {
			mhayaLogger.Infof("player SetPassiveInviteReward,not vip invite playerName=%v,level=%v,param=%v,err=%v", p.UserName, p.Level, key, err)
		}

		addItem = append(addItem, ret2.Reward...)
	} else {
		//主动邀请记录, 设置邀请人数量+1
		key = fmt.Sprintf("%v:%v:%v", constant.InviteKey, InviteVipPlayer, pUid)
		err := mdb.RDB.IncrBy(context.Background(), key, 1).Err()
		if err != nil {
			mhayaLogger.Infof("player SetPassiveInviteReward, vip invite playerName=%v,level=%v,param=%v,err=%v", p.UserName, p.Level, key, err)
		}
		addItem = append(addItem, ret3.Reward...)
	}

	/*	//社区长额外奖励
		if base.IsLeader == 1 {
			ret, ok := data.DiscreteRuleConfig.Get(InvitePlayerByLeader)
			if !ok {
				return
			}
			addItem = append(addItem, ret.Reward...)
		}*/
	if p.InviteReward.IsClaim == 2 {
		p.InviteReward.Reward = []data.ItemReward{}
		p.InviteReward.IsClaim = 0
		p.InviteReward.RewardTime = 0
	}
	p.InviteReward.Reward = append(p.InviteReward.Reward, addItem...)
	p.InviteReward.Merge()

}

func (p *Player) SetInviteReward() {
	ret, ok := data.DiscreteRuleConfig.Get(InvitePlayerByLeader)

	if !ok {
		return
	}
	ret2, ok := data.DiscreteRuleConfig.Get(InvitePlayer)

	if !ok {
		return
	}

	ret3, ok := data.DiscreteRuleConfig.Get(InviteVipPlayer)

	if !ok {
		return
	}
	var addItem []data.ItemReward

	key1 := fmt.Sprintf("%v:%v:%v", constant.InviteKey, InvitePlayer, p.UserName)
	key2 := fmt.Sprintf("%v:%v:%v", constant.InviteKey, InviteVipPlayer, p.UserName)

	//自己作为邀请人判断奖励
	freeNum, _ := mdb.RDB.Get(context.Background(), key1).Int()

	VipNum, _ := mdb.RDB.Get(context.Background(), key2).Int()

	for i := 0; i < freeNum; i++ {
		addItem = append(addItem, ret2.Reward...)
		p.SetPlayerRewardLog(SourceInvite, InvitePlayer, []data.ItemReward{}, []data.ItemReward{}, 2)
		if p.IsLeader == 1 {
			addItem = append(addItem, ret.Reward...)
			p.SetPlayerRewardLog(SourceInvite, InvitePlayerByLeader, []data.ItemReward{}, []data.ItemReward{}, 2)
		}

		p.Item[ItemInvite].Num += 1
		p.Item[ItemInvite].CurNum += 1

		p.Invite.Set(InvitePlayer, ret.Reward)

		p.FinishAchieveTaskByType2()
		mdb.RDB.DecrBy(context.Background(), key1, 1)
	}

	for i := 0; i < VipNum; i++ {
		addItem = append(addItem, ret3.Reward...)
		p.SetPlayerRewardLog(SourceInvite, InviteVipPlayer, []data.ItemReward{}, []data.ItemReward{}, 2)
		if p.IsLeader == 1 {
			addItem = append(addItem, ret.Reward...)
			p.SetPlayerRewardLog(SourceInvite, InvitePlayerByLeader, []data.ItemReward{}, []data.ItemReward{}, 2)
		}

		p.Item[ItemInvite].Num += 1
		p.Item[ItemInvite].CurNum += 1

		p.Invite.Set(InviteVipPlayer, ret.Reward)
		p.FinishAchieveTaskByType2()
		mdb.RDB.DecrBy(context.Background(), key2, 1)
	}

	if (freeNum + VipNum) > 0 {
		SetRank(constant.RankSourceInvite, p.IsRobot, p.UserName, freeNum+VipNum)
	}

	if p.InviteReward.IsClaim == 2 {
		p.InviteReward.Reward = []data.ItemReward{}
		p.InviteReward.IsClaim = 0
		p.InviteReward.RewardTime = 0
	}
	p.InviteReward.Reward = append(p.InviteReward.Reward, addItem...)
	p.InviteReward.Merge()
}

func (p *Player) SetInvite(uid string) bool {

	key := fmt.Sprintf("%v:1:%v", constant.InviteKey, p.UserName)
	ret, ok := data.DiscreteRuleConfig.Get(InviteMaxExpireTime)
	if !ok {
		return false
	}
	t, _ := mhayaString.ToInt64(ret.Condition)
	err := mdb.RDB.Set(context.Background(), key, uid, time.Duration(t)*24*time.Hour).Err()
	if err != nil {
		return false
	}
	return true

}

func (p *Player) ClaimRankReward(id int) param.ChangeData {
	var resp param.ChangeData
	if rank, ok := p.RankReward[id]; ok {
		if rank.Status == 1 && len(rank.Reward) > 0 {
			var addItem AddItem
			resp.AddItem = addItem.Add(rank.Reward)
			p.Item.AddItem(rank.Reward)
			p.RankReward[id].Status = 2

			p.RankReward[id].ClaimTime = mhayaTime.Now().Unix()
			p.SetPlayerRewardLog(SourceRank, id, rank.Reward, []data.ItemReward{}, nil)
		}
	}
	p.LevelUp()

	return resp
}

func (p *Player) ClaimInviteReward(id int) param.ChangeData {
	var resp param.ChangeData
	if len(p.InviteReward.Reward) > 0 {
		var addItem AddItem
		resp.AddItem = addItem.Add(p.InviteReward.Reward)
		p.Item.AddItem(p.InviteReward.Reward)
		p.InviteReward.IsClaim = 2
		p.InviteReward.RewardTime = mhayaTime.Now().Unix()
		p.InviteReward.Reward = []data.ItemReward{}
		p.SetPlayerRewardLog(SourceInvite, id, p.InviteReward.Reward, []data.ItemReward{}, nil)
	}
	p.LevelUp()

	return resp
}

func (p *Player) ClaimAchieveTaskReward(id int) param.ChangeData {
	var resp param.ChangeData
	if ach, ok := p.AchieveTask[id]; ok {
		if ach.Status == 1 && ach.RewardNum > 0 {
			var addItem AddItem
			resp.AddItem = addItem.Add(ach.Reward)
			p.Item.AddItem(ach.Reward)
			ach.RewardNum -= 1
			p.SetPlayerRewardLog(SourceAchieveTask, id, ach.Reward, []data.ItemReward{}, nil)
		}
		if ach.RewardNum == 0 {
			ret, _ := data.AchieveTaskConfig.Get(ach.ID)
			if ret.Condition.Max == ach.Num {
				p.AchieveTask[id].Status = 2
			} else {
				p.AchieveTask[id].Status = 0
			}
		}
	}
	p.LevelUp()

	return resp
}

func (p *Player) ClaimDailyTaskReward(id int) param.ChangeData {
	var resp param.ChangeData
	if task, ok := p.DailyTask[id]; ok {
		if task.Status == 1 {
			var addItem AddItem
			resp.AddItem = addItem.Add(task.Reward)
			p.Item.AddItem(task.Reward)
			task.Status = 2
			p.SetPlayerRewardLog(SourceDailyTask, id, task.Reward, []data.ItemReward{}, nil)
		}
	}
	p.LevelUp()

	return resp
}
