123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package orchard
- import (
- "active/constant"
- "active/tools"
- "encoding/json"
- "fmt"
- "git.jiaxianghudong.com/go/logs"
- "git.jiaxianghudong.com/webs/pkg/rds"
- "math/rand"
- "strconv"
- "time"
- )
- var OrchardConf OrchardConfig
- var OrchardCopy OrchardConfig
- type OrchardConfig struct {
- DurationWash int `json:"duration_wash" yaml:"duration_wash"`
- DurationTime time.Duration `json:"duration_time" yaml:"duration_time"`
- FreeWashLimit int64 `json:"free_wash_limit" yaml:"free_wash_limit"`
- StealFruit int64 `json:"steal_fruit" yaml:"steal_fruit"`
- DeclineRate int64 `json:"decline_rate" yaml:"decline_rate"`
- DeclineMaxExp int64 `json:"decline_max_exp" yaml:"decline_max_exp"`
- StealPer int64 `json:"steal_per" yaml:"steal_per"`
- OpenDuration time.Duration `json:"open_duration" yaml:"open_duration"`
- Limit int `json:"limit" yaml:"limit"`
- TreeVersion string `json:"tree_version" yaml:"tree_version"`
- StoreVersion string `json:"store_version" yaml:"store_version"`
- TreeList []OrchardTrees `json:"tree_list" yaml:"tree_list"` //树的概率配置
- WashList []WashItem `json:"wash_list" yaml:"wash_list"` //施肥 的配置
- Multiple []int64 `json:"multiple" yaml:"multiple"` // 倍数配置
- BlessingList []Blessing `json:"blessing_list" yaml:"blessing_list"` //祈福道具列表
- StoreList []OrchardStore `json:"store_list" yaml:"store_list"`
- WaterLimit int `json:"water_limit" yaml:"water_limit"`
- }
- type OrchardTrees struct {
- Level int64 `json:"level" yaml:"level"`
- Exp int64 `json:"exp" yaml:"exp"`
- MaxFruit int64 `json:"max_fruit" yaml:"max_fruit"`
- BasicProduct int64 `json:"basic_product" yaml:"basic_product"`
- Common int64 `json:"common" yaml:"common"`
- Middle int64 `json:"middle" yaml:"middle"`
- High int64 `json:"high" yaml:"high"`
- Super int64 `json:"super" yaml:"super"`
- ShowOutput int64 `json:"show_output" yaml:"show_output"`
- }
- type WashItem struct {
- Type int64 `json:"type" yaml:"type"` // 1:井水,2:圣水,3:泉水,4:清水
- CostBean int64 `json:"cost_bean" yaml:"cost_bean"`
- Exp int64 `json:"exp" yaml:"exp"`
- RemainTimes int64 `json:"remain_times" yaml:"remain_times"` //剩余次数
- TimeStamp int64 `json:"time_stamp"` //时间戳
- Countdown int64 `json:"countdown"` //剩余时间
- WaterTimes int64 `json:"water_times" yaml:"water_times"` //施肥 剩余次数
- }
- type Blessing struct {
- Type int64 `json:"type" yaml:"type"`
- CostDiamond int64 `json:"cost_diamond" yaml:"cost_diamond"`
- UpdatedRate int64 `json:"updated_rate" yaml:"updated_rate"`
- Duration int64 `json:"duration" yaml:"duration"`
- }
- type OrchardFruit struct {
- Id int64 `json:"id" sql:"id"`
- UserId int64 `json:"user_id" sql:"user_id"` //用户id
- Number int64 `json:"number" sql:"number"` //果实数量
- CreateTime int64 `json:"create_time" sql:"create_time"` //创建时间
- OpenTime int64 `json:"open_time" sql:"open_time"` //结果时间
- Level int64 `json:"level" sql:"level"` //果实等级
- IsSteal int64 `json:"number_total" sql:"is_steal" gorm:"column:is_steal"` //果实总数
- IsAccelerate int64 `json:"is_accelerate" sql:"is_accelerate"` //是否加速
- IsIncrease int64 `json:"is_increase" sql:"is_increase"` //是否加产
- StealUserId int64 `json:"-" gorm:"steal_user_id"` //偷东西的id
- StealNumber int64 `json:"steal_number" gorm:"steal_number"` //偷东西数量
- RemainTime int64 `json:"remain_time" gorm:"-"`
- CanSteal bool `json:"can_steal" gorm:"-"`
- StealMsg []struct {
- UserId int64 `json:"user_id"`
- Number int64 `json:"number"`
- } `json:"steal_msg" gorm:"-"` //偷数量
- StealJson string `json:"-" gorm:"steal_json"`
- }
- type OrchardTree struct {
- Id int64 `json:"id" sql:"id" gorm:"id"`
- UserId int64 `json:"user_id" sql:"user_id" gorm:"user_id"` //用户id
- Login string `json:"login" sql:"login" `
- //LoginTime time.Time `json:"login_time" gorm:"login_time"` //登录时间
- Level int64 `json:"level" sql:"level" gorm:"level"` //树等级
- Exp int64 `json:"exp" sql:"exp" gorm:"exp"`
- Number int64 `json:"number" sql:"number" gorm:"number"` //果实数量
- OpenTime int64 `json:"open_time" sql:"open_time" gorm:"open_time"` //结果时间
- CreateTime int64 `json:"create_time" sql:"create_time" gorm:"create_time"` //结果时间
- MaxFruit int64 `json:"max_fruit" sql:"max_fruit" gorm:"max_fruit"` //最大果数
- NextLevelExp int64 `json:"next_level_exp" sql:"next_level_exp" gorm:"-"` //下一级的经验
- WealthGod int64 `json:"wealth_god"` //财神
- Laojun int64 `json:"laojun"` // 太上老君
- LuckyCat int64 `json:"lucky_cat"` //幸运神
- }
- type TreeMini struct {
- UserId int64 `json:"user_id" sql:"user_id" gorm:"user_id"` //用户id
- Level int64 `json:"level" sql:"level" gorm:"level"` //树等级
- }
- func (this TreeMini) TableName() string {
- return "orchard_tree_tmp"
- }
- func (t *TreeMini) List(limit int, offset int) (list []*TreeMini) {
- db := tools.NewConn(MysqlExtDb)
- db.Where("user_id >=10000").Order("user_id").Limit(limit).Offset(offset).Find(&list)
- return
- }
- func (this OrchardTree) TableName() string {
- return "orchard_tree"
- }
- func (this OrchardFruit) TableName() string {
- return "orchard_fruit"
- }
- func (this *OrchardConfig) GetConf() {
- tools.ReloadYaml("orchard.yaml", this)
- tools.ReloadYaml("orchard.yaml", &OrchardCopy)
- //var exp int64 = 0
- for i := range this.TreeList {
- this.TreeList[i].Middle += this.TreeList[i].Common
- this.TreeList[i].High += this.TreeList[i].Middle
- this.TreeList[i].Super += this.TreeList[i].High
- //exp += this.TreeList[i].Exp
- //this.TreeList[i].Exp = exp
- }
- //fmt.Printf("处理后数据 %+v \n", this)
- }
- type OrchardUserInfo struct {
- OrchardTree OrchardTree `json:"orchard_tree" yaml:"OrchardTree"`
- FruitList []OrchardFruit `json:"fruit_list"`
- CanSteal bool `json:"can_steal"`
- }
- type OrchardUserDay struct {
- StealNumber int64 `json:"steal_number" yaml:"steal_number"` //偷取果实数量
- DurationTimeStamp []int64 `json:"duration_time_stamp" yaml:"duration_time_stamp"` //持续时间
- Countdowns []int64 `json:"countdowns"` //倒计时
- WaterRemainTimes []int64 `json:"water_remain_times" yaml:"water_remain_times"` //施肥 剩余次数
- WaterUserList []string `json:"water_user_list"` //用户施肥 列表
- StealUserList []string `json:"steal_user_list"` //偷取列表
- Login bool `json:"login" yaml:"login"`
- }
- //获得树的缓存
- func (this *OrchardUserInfo) GetData(userID string) error {
- db := tools.NewConn(MysqlExtDb)
- //data := rds.Redis.Get(fmt.Sprintf(constant.ORCHARDATA, userID)).Val()
- data := ""
- if data != "" {
- err := json.Unmarshal([]byte(data), this)
- if this.OrchardTree.Level != 0 {
- this.OrchardTree.Number = OrchardConf.TreeList[this.OrchardTree.Level-1].BasicProduct
- } else {
- this.OrchardTree.Number = OrchardConf.TreeList[0].BasicProduct
- }
- if err != nil {
- logs.Errorf("redpacket[%s] GetInviteBeanData json err:%v", userID, err)
- return err
- }
- } else {
- if this.OrchardTree.FindOne(userID) != nil {
- timeNow := time.Now()
- openTime := timeNow.Add(OrchardConf.OpenDuration).Unix()
- this.OrchardTree.CreateTime = timeNow.Unix()
- this.OrchardTree.OpenTime = openTime
- this.OrchardTree.UserId, _ = strconv.ParseInt(userID, 10, 64)
- //this.OrchardTree.CurrentFruit = 1
- this.OrchardTree.Login = timeNow.Format("2006-01-02")
- this.OrchardTree.Level = 1
- this.OrchardTree.MaxFruit = OrchardConf.TreeList[this.OrchardTree.Level-1].MaxFruit
- this.OrchardTree.NextLevelExp = OrchardConf.TreeList[this.OrchardTree.Level-1].Exp
- if err := this.OrchardTree.Insert(db); err != nil {
- return err
- }
- orchardFruit := OrchardFruit{
- CreateTime: time.Now().Unix(),
- OpenTime: time.Now().Unix(),
- }
- orchardFruit.UserId, _ = strconv.ParseInt(userID, 10, 64)
- //生成个果实
- fruitLevel := randFruitLevel(1)
- orchardFruit.OpenTime = time.Now().Unix()
- orchardFruit.CreateTime = time.Now().Unix()
- orchardFruit.Level = int64(fruitLevel)
- orchardFruit.Number = OrchardConf.TreeList[0].BasicProduct
- orchardFruit.RemainTime = openTime - timeNow.Unix()
- orchardFruit.IsSteal = orchardFruit.Number
- //插入果实
- if err := orchardFruit.Insert(db); err != nil {
- return err
- }
- this.FruitList = append(this.FruitList, orchardFruit)
- //创建一个果实
- }
- this.OrchardTree.Number = OrchardConf.TreeList[this.OrchardTree.Level-1].BasicProduct
- this.FruitList, _ = FindOrchardFruitAll(userID)
- for i := range this.FruitList {
- if this.FruitList[i].StealMsg == nil {
- this.FruitList[i].StealMsg = make([]struct {
- UserId int64 `json:"user_id"`
- Number int64 `json:"number"`
- }, 0)
- }
- }
- }
- this.CanSteal = false
- for i := range this.FruitList {
- if this.FruitList[i].OpenTime == 0 {
- this.FruitList[i].RemainTime = -1
- } else if time.Now().Unix() > this.FruitList[i].OpenTime {
- this.FruitList[i].RemainTime = 0
- } else {
- this.FruitList[i].RemainTime = this.FruitList[i].OpenTime - time.Now().Unix()
- }
- if time.Now().Unix() >= this.FruitList[i].OpenTime && this.FruitList[i].IsSteal == 0 {
- this.CanSteal = true
- }
- }
- return nil
- }
- func ReloadDataOrchardUserInfo(userId string) error {
- rds.Redis.Del(fmt.Sprintf(constant.ORCHARDATA, userId))
- var orchardUserInfo OrchardUserInfo
- orchardUserInfo.GetData(userId)
- return nil
- }
- //设置树的缓存
- func (this *OrchardUserInfo) SetData(userID string) bool {
- data, _ := json.Marshal(this)
- rds.Redis.Set(fmt.Sprintf(constant.ORCHARDATA, userID), string(data), 30*60*time.Second)
- return true
- }
- //
- func randFruitLevel(treeLevel int) int {
- tree := &OrchardConf.TreeList[treeLevel-1]
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- number := int64(r.Uint64() % 100)
- if number < tree.Common {
- return 1
- } else if number < tree.Middle {
- return 2
- } else if number < tree.High {
- return 3
- } else {
- return 4
- }
- }
- func RandFruit(treeLevel int, userId int64, Laojun int64, wealthGod int64) *OrchardFruit {
- var accelerate time.Duration = 100
- var isAccelerate int64 = 0
- var isIncrease int64 = 0
- openTime := time.Now().Add(OrchardConf.OpenDuration / 100 * accelerate).Unix()
- orchardFruit := &OrchardFruit{
- CreateTime: time.Now().Unix(),
- OpenTime: openTime,
- IsAccelerate: isAccelerate,
- IsIncrease: isIncrease,
- }
- orchardFruit.UserId = userId
- //生成个果实
- fruitLevel := 1
- orchardFruit.Level = int64(treeLevel)
- orchardFruit.OpenTime = openTime
- orchardFruit.CreateTime = time.Now().Unix()
- orchardFruit.Level = int64(fruitLevel)
- orchardFruit.Number = OrchardConf.TreeList[treeLevel-1].BasicProduct
- orchardFruit.RemainTime = openTime - time.Now().Unix()
- orchardFruit.IsSteal = orchardFruit.Number
- return orchardFruit
- }
|