orchard.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package orchard
  2. import (
  3. "active/constant"
  4. "active/tools"
  5. "encoding/json"
  6. "fmt"
  7. "git.jiaxianghudong.com/go/logs"
  8. "git.jiaxianghudong.com/webs/pkg/rds"
  9. "math/rand"
  10. "strconv"
  11. "time"
  12. )
  13. var OrchardConf OrchardConfig
  14. var OrchardCopy OrchardConfig
  15. type OrchardConfig struct {
  16. DurationWash int `json:"duration_wash" yaml:"duration_wash"`
  17. DurationTime time.Duration `json:"duration_time" yaml:"duration_time"`
  18. FreeWashLimit int64 `json:"free_wash_limit" yaml:"free_wash_limit"`
  19. StealFruit int64 `json:"steal_fruit" yaml:"steal_fruit"`
  20. DeclineRate int64 `json:"decline_rate" yaml:"decline_rate"`
  21. DeclineMaxExp int64 `json:"decline_max_exp" yaml:"decline_max_exp"`
  22. StealPer int64 `json:"steal_per" yaml:"steal_per"`
  23. OpenDuration time.Duration `json:"open_duration" yaml:"open_duration"`
  24. Limit int `json:"limit" yaml:"limit"`
  25. TreeVersion string `json:"tree_version" yaml:"tree_version"`
  26. StoreVersion string `json:"store_version" yaml:"store_version"`
  27. TreeList []OrchardTrees `json:"tree_list" yaml:"tree_list"` //树的概率配置
  28. WashList []WashItem `json:"wash_list" yaml:"wash_list"` //施肥 的配置
  29. Multiple []int64 `json:"multiple" yaml:"multiple"` // 倍数配置
  30. BlessingList []Blessing `json:"blessing_list" yaml:"blessing_list"` //祈福道具列表
  31. StoreList []OrchardStore `json:"store_list" yaml:"store_list"`
  32. WaterLimit int `json:"water_limit" yaml:"water_limit"`
  33. }
  34. type OrchardTrees struct {
  35. Level int64 `json:"level" yaml:"level"`
  36. Exp int64 `json:"exp" yaml:"exp"`
  37. MaxFruit int64 `json:"max_fruit" yaml:"max_fruit"`
  38. BasicProduct int64 `json:"basic_product" yaml:"basic_product"`
  39. Common int64 `json:"common" yaml:"common"`
  40. Middle int64 `json:"middle" yaml:"middle"`
  41. High int64 `json:"high" yaml:"high"`
  42. Super int64 `json:"super" yaml:"super"`
  43. ShowOutput int64 `json:"show_output" yaml:"show_output"`
  44. }
  45. type WashItem struct {
  46. Type int64 `json:"type" yaml:"type"` // 1:井水,2:圣水,3:泉水,4:清水
  47. CostBean int64 `json:"cost_bean" yaml:"cost_bean"`
  48. Exp int64 `json:"exp" yaml:"exp"`
  49. RemainTimes int64 `json:"remain_times" yaml:"remain_times"` //剩余次数
  50. TimeStamp int64 `json:"time_stamp"` //时间戳
  51. Countdown int64 `json:"countdown"` //剩余时间
  52. WaterTimes int64 `json:"water_times" yaml:"water_times"` //施肥 剩余次数
  53. }
  54. type Blessing struct {
  55. Type int64 `json:"type" yaml:"type"`
  56. CostDiamond int64 `json:"cost_diamond" yaml:"cost_diamond"`
  57. UpdatedRate int64 `json:"updated_rate" yaml:"updated_rate"`
  58. Duration int64 `json:"duration" yaml:"duration"`
  59. }
  60. type OrchardFruit struct {
  61. Id int64 `json:"id" sql:"id"`
  62. UserId int64 `json:"user_id" sql:"user_id"` //用户id
  63. Number int64 `json:"number" sql:"number"` //果实数量
  64. CreateTime int64 `json:"create_time" sql:"create_time"` //创建时间
  65. OpenTime int64 `json:"open_time" sql:"open_time"` //结果时间
  66. Level int64 `json:"level" sql:"level"` //果实等级
  67. IsSteal int64 `json:"number_total" sql:"is_steal" gorm:"column:is_steal"` //果实总数
  68. IsAccelerate int64 `json:"is_accelerate" sql:"is_accelerate"` //是否加速
  69. IsIncrease int64 `json:"is_increase" sql:"is_increase"` //是否加产
  70. StealUserId int64 `json:"-" gorm:"steal_user_id"` //偷东西的id
  71. StealNumber int64 `json:"steal_number" gorm:"steal_number"` //偷东西数量
  72. RemainTime int64 `json:"remain_time" gorm:"-"`
  73. CanSteal bool `json:"can_steal" gorm:"-"`
  74. StealMsg []struct {
  75. UserId int64 `json:"user_id"`
  76. Number int64 `json:"number"`
  77. } `json:"steal_msg" gorm:"-"` //偷数量
  78. StealJson string `json:"-" gorm:"steal_json"`
  79. }
  80. type OrchardTree struct {
  81. Id int64 `json:"id" sql:"id" gorm:"id"`
  82. UserId int64 `json:"user_id" sql:"user_id" gorm:"user_id"` //用户id
  83. Login string `json:"login" sql:"login" `
  84. //LoginTime time.Time `json:"login_time" gorm:"login_time"` //登录时间
  85. Level int64 `json:"level" sql:"level" gorm:"level"` //树等级
  86. Exp int64 `json:"exp" sql:"exp" gorm:"exp"`
  87. Number int64 `json:"number" sql:"number" gorm:"number"` //果实数量
  88. OpenTime int64 `json:"open_time" sql:"open_time" gorm:"open_time"` //结果时间
  89. CreateTime int64 `json:"create_time" sql:"create_time" gorm:"create_time"` //结果时间
  90. MaxFruit int64 `json:"max_fruit" sql:"max_fruit" gorm:"max_fruit"` //最大果数
  91. NextLevelExp int64 `json:"next_level_exp" sql:"next_level_exp" gorm:"-"` //下一级的经验
  92. WealthGod int64 `json:"wealth_god"` //财神
  93. Laojun int64 `json:"laojun"` // 太上老君
  94. LuckyCat int64 `json:"lucky_cat"` //幸运神
  95. }
  96. type TreeMini struct {
  97. UserId int64 `json:"user_id" sql:"user_id" gorm:"user_id"` //用户id
  98. Level int64 `json:"level" sql:"level" gorm:"level"` //树等级
  99. }
  100. func (this TreeMini) TableName() string {
  101. return "orchard_tree_tmp"
  102. }
  103. func (t *TreeMini) List(limit int, offset int) (list []*TreeMini) {
  104. db := tools.NewConn(MysqlExtDb)
  105. db.Where("user_id >=10000").Order("user_id").Limit(limit).Offset(offset).Find(&list)
  106. return
  107. }
  108. func (this OrchardTree) TableName() string {
  109. return "orchard_tree"
  110. }
  111. func (this OrchardFruit) TableName() string {
  112. return "orchard_fruit"
  113. }
  114. func (this *OrchardConfig) GetConf() {
  115. tools.ReloadYaml("orchard.yaml", this)
  116. tools.ReloadYaml("orchard.yaml", &OrchardCopy)
  117. //var exp int64 = 0
  118. for i := range this.TreeList {
  119. this.TreeList[i].Middle += this.TreeList[i].Common
  120. this.TreeList[i].High += this.TreeList[i].Middle
  121. this.TreeList[i].Super += this.TreeList[i].High
  122. //exp += this.TreeList[i].Exp
  123. //this.TreeList[i].Exp = exp
  124. }
  125. //fmt.Printf("处理后数据 %+v \n", this)
  126. }
  127. type OrchardUserInfo struct {
  128. OrchardTree OrchardTree `json:"orchard_tree" yaml:"OrchardTree"`
  129. FruitList []OrchardFruit `json:"fruit_list"`
  130. CanSteal bool `json:"can_steal"`
  131. }
  132. type OrchardUserDay struct {
  133. StealNumber int64 `json:"steal_number" yaml:"steal_number"` //偷取果实数量
  134. DurationTimeStamp []int64 `json:"duration_time_stamp" yaml:"duration_time_stamp"` //持续时间
  135. Countdowns []int64 `json:"countdowns"` //倒计时
  136. WaterRemainTimes []int64 `json:"water_remain_times" yaml:"water_remain_times"` //施肥 剩余次数
  137. WaterUserList []string `json:"water_user_list"` //用户施肥 列表
  138. StealUserList []string `json:"steal_user_list"` //偷取列表
  139. Login bool `json:"login" yaml:"login"`
  140. }
  141. //获得树的缓存
  142. func (this *OrchardUserInfo) GetData(userID string) error {
  143. db := tools.NewConn(MysqlExtDb)
  144. //data := rds.Redis.Get(fmt.Sprintf(constant.ORCHARDATA, userID)).Val()
  145. data := ""
  146. if data != "" {
  147. err := json.Unmarshal([]byte(data), this)
  148. if this.OrchardTree.Level != 0 {
  149. this.OrchardTree.Number = OrchardConf.TreeList[this.OrchardTree.Level-1].BasicProduct
  150. } else {
  151. this.OrchardTree.Number = OrchardConf.TreeList[0].BasicProduct
  152. }
  153. if err != nil {
  154. logs.Errorf("redpacket[%s] GetInviteBeanData json err:%v", userID, err)
  155. return err
  156. }
  157. } else {
  158. if this.OrchardTree.FindOne(userID) != nil {
  159. timeNow := time.Now()
  160. openTime := timeNow.Add(OrchardConf.OpenDuration).Unix()
  161. this.OrchardTree.CreateTime = timeNow.Unix()
  162. this.OrchardTree.OpenTime = openTime
  163. this.OrchardTree.UserId, _ = strconv.ParseInt(userID, 10, 64)
  164. //this.OrchardTree.CurrentFruit = 1
  165. this.OrchardTree.Login = timeNow.Format("2006-01-02")
  166. this.OrchardTree.Level = 1
  167. this.OrchardTree.MaxFruit = OrchardConf.TreeList[this.OrchardTree.Level-1].MaxFruit
  168. this.OrchardTree.NextLevelExp = OrchardConf.TreeList[this.OrchardTree.Level-1].Exp
  169. if err := this.OrchardTree.Insert(db); err != nil {
  170. return err
  171. }
  172. orchardFruit := OrchardFruit{
  173. CreateTime: time.Now().Unix(),
  174. OpenTime: time.Now().Unix(),
  175. }
  176. orchardFruit.UserId, _ = strconv.ParseInt(userID, 10, 64)
  177. //生成个果实
  178. fruitLevel := randFruitLevel(1)
  179. orchardFruit.OpenTime = time.Now().Unix()
  180. orchardFruit.CreateTime = time.Now().Unix()
  181. orchardFruit.Level = int64(fruitLevel)
  182. orchardFruit.Number = OrchardConf.TreeList[0].BasicProduct
  183. orchardFruit.RemainTime = openTime - timeNow.Unix()
  184. orchardFruit.IsSteal = orchardFruit.Number
  185. //插入果实
  186. if err := orchardFruit.Insert(db); err != nil {
  187. return err
  188. }
  189. this.FruitList = append(this.FruitList, orchardFruit)
  190. //创建一个果实
  191. }
  192. this.OrchardTree.Number = OrchardConf.TreeList[this.OrchardTree.Level-1].BasicProduct
  193. this.FruitList, _ = FindOrchardFruitAll(userID)
  194. for i := range this.FruitList {
  195. if this.FruitList[i].StealMsg == nil {
  196. this.FruitList[i].StealMsg = make([]struct {
  197. UserId int64 `json:"user_id"`
  198. Number int64 `json:"number"`
  199. }, 0)
  200. }
  201. }
  202. }
  203. this.CanSteal = false
  204. for i := range this.FruitList {
  205. if this.FruitList[i].OpenTime == 0 {
  206. this.FruitList[i].RemainTime = -1
  207. } else if time.Now().Unix() > this.FruitList[i].OpenTime {
  208. this.FruitList[i].RemainTime = 0
  209. } else {
  210. this.FruitList[i].RemainTime = this.FruitList[i].OpenTime - time.Now().Unix()
  211. }
  212. if time.Now().Unix() >= this.FruitList[i].OpenTime && this.FruitList[i].IsSteal == 0 {
  213. this.CanSteal = true
  214. }
  215. }
  216. return nil
  217. }
  218. func ReloadDataOrchardUserInfo(userId string) error {
  219. rds.Redis.Del(fmt.Sprintf(constant.ORCHARDATA, userId))
  220. var orchardUserInfo OrchardUserInfo
  221. orchardUserInfo.GetData(userId)
  222. return nil
  223. }
  224. //设置树的缓存
  225. func (this *OrchardUserInfo) SetData(userID string) bool {
  226. data, _ := json.Marshal(this)
  227. rds.Redis.Set(fmt.Sprintf(constant.ORCHARDATA, userID), string(data), 30*60*time.Second)
  228. return true
  229. }
  230. //
  231. func randFruitLevel(treeLevel int) int {
  232. tree := &OrchardConf.TreeList[treeLevel-1]
  233. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  234. number := int64(r.Uint64() % 100)
  235. if number < tree.Common {
  236. return 1
  237. } else if number < tree.Middle {
  238. return 2
  239. } else if number < tree.High {
  240. return 3
  241. } else {
  242. return 4
  243. }
  244. }
  245. func RandFruit(treeLevel int, userId int64, Laojun int64, wealthGod int64) *OrchardFruit {
  246. var accelerate time.Duration = 100
  247. var isAccelerate int64 = 0
  248. var isIncrease int64 = 0
  249. openTime := time.Now().Add(OrchardConf.OpenDuration / 100 * accelerate).Unix()
  250. orchardFruit := &OrchardFruit{
  251. CreateTime: time.Now().Unix(),
  252. OpenTime: openTime,
  253. IsAccelerate: isAccelerate,
  254. IsIncrease: isIncrease,
  255. }
  256. orchardFruit.UserId = userId
  257. //生成个果实
  258. fruitLevel := 1
  259. orchardFruit.Level = int64(treeLevel)
  260. orchardFruit.OpenTime = openTime
  261. orchardFruit.CreateTime = time.Now().Unix()
  262. orchardFruit.Level = int64(fruitLevel)
  263. orchardFruit.Number = OrchardConf.TreeList[treeLevel-1].BasicProduct
  264. orchardFruit.RemainTime = openTime - time.Now().Unix()
  265. orchardFruit.IsSteal = orchardFruit.Number
  266. return orchardFruit
  267. }